JScrollPane doesn't work with Graphics
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Paul_Keeling
Posted On:   Tuesday, August 13, 2002 05:02 AM

Does JScrollPane work easily with Graphics? The GUI I am trying to create will have an indeterminate number of coloured blocks, so I need scroll bars to allow the full number of blocks to be seen. My test code, with just a few of blocks, is below. I have a class which contains the paintComponent(Graphics) method, and an object of that is put inside a JScrollPane and the whole lot is added to a JFrame . I have tried playing around with various sizes, and with various combinations of JFrame and JPanel , but the scrollbars don't allow me to scroll to see the full blocks. In the JScrollPane constructor I have declared them to be always visible so that I can see them, but th   More>>


Does JScrollPane work easily with Graphics?


The GUI I am trying to create will have an indeterminate number of coloured blocks, so I need scroll bars to allow the full number of blocks to be seen. My test code, with just a few of blocks, is below.


I have a class which contains the paintComponent(Graphics) method, and an object of that is put inside a JScrollPane and the whole lot is added to a JFrame . I have tried playing around with various sizes, and with various combinations of JFrame and JPanel , but the scrollbars don't allow me to scroll to see the full blocks. In the JScrollPane constructor I have declared them to be always visible so that I can see them, but they don't do anything.



Should my ColorPanel class implement Scrollable ? I'd like to avoid that, as it seems unnecessarily complicated. Has anybody got any ideas? Many thanks.



			
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import javax.swing.*;

class ScrollTest extends JFrame
{ private Toolkit tk = Toolkit.getDefaultToolkit();
private Dimension d = tk.getScreenSize();
int screenHeight = d.height;
int screenWidth = d.width;

public ScrollTest()
{ super("Test");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
setSize(screenWidth, screenHeight);
Container contentPane = getContentPane();

ColorPanel cp = new ColorPanel();
cp.setPreferredSize(new Dimension(400, 400));
JScrollPane scroller = new JScrollPane(cp,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JViewport vp = scroller.getViewport();

vp.setViewSize(new Dimension(350, 350));
vp.add(cp);
contentPane.add(scroller, "Center");
}

public static void main(String[] args)
{ ScrollTest st = new ScrollTest();
st.pack();
st.show();
}
}

class ColorPanel extends JPanel
{
public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(50, 250, 50, 500);
g.fillRect(250, 50, 500, 50);
g.setColor(Color.green);
g.fillRect(50, 750, 50, 50);
g.fillRect(750, 50, 50, 50);
}
}



   <<Less

Re: JScrollPane doesn't work with Graphics

Posted By:   Mike_Friedrich  
Posted On:   Tuesday, August 13, 2002 05:53 AM

I played a little bit with your code, and got this working:


...
// this is your real internal size - painting is done in these limits
cp.setPreferredSize(new Dimension(850, 850));
...
//vp.setViewSize(new Dimension(350, 350));
vp.add(cp);
vp.setPreferredSize(new Dimension(350, 350));
contentPane.add(scroller, "Center");
pack();
...

Regards, Mike
About | Sitemap | Contact