I have Java applet to draw an array (just some rectangle one after another).
When user select to create array of size n
, it will draw n
rectangles connected together. When n
gets bigger, the graphics get bigger, but since i use JPanel
to draw the array, and JPanel
won't scroll, i have to add that JPanel
into a JScrollPane
, but still it won't scroll. The user can see only part of the whole array.
Anyone can give me some help?
Here is my code:
public class ArrayPanel extends JPanel {
....
public void paintComponent(Graphics g) {
...draw array here..
// I wish to get the updated size of the graphis here,
// then i can reset the preferredSize()....?
System.out.println("width=" + getWidth() + " height=" + getHeight());
}
}
public class ArrayDemo extends JPanel {
public ArrayDemo() {
super(new BorderLayout());
arrayPanel = new ArrayPanel();
arrayPanel.setPreferredSize(new Dimension(400, 300));
JScrollPane container = new JScrollPane(arrayPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
container.setPreferredSize(arrayPanel.getPreferredSize());
add(container, BorderLayout.CENTER);
...
}
}