views:

49

answers:

1

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);
 ...
  }
}
+2  A: 

Don't set the size in paintComponent.

You did not provide that code, but you have some position in your code where you know the size of that array, and the size of your rectangles, so set dimensions of your JPanel there.

Here is an example (using JFrame, not Applet, but the ideas is the same) that looks like this:

alt text

public class ScrollPanelFrame extends JFrame{

    public ScrollPanelFrame() {
        ArrayPanel panel = new ArrayPanel(20, 20);
        JScrollPane container = new JScrollPane(
                panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
        getContentPane().add(container);
    }

    class ArrayPanel extends JPanel {
        final int RECTANGLE_WIDTH = 100;
        final int RECTANGLE_HEIGHT = 100;

        int rectangleCountX;
        int rectangleCountY;

        public ArrayPanel(int rectangleCountX, int rectangleCountY) {
            this.rectangleCountX = rectangleCountX;
            this.rectangleCountY = rectangleCountY;
            this.setPreferredSize(new Dimension(RECTANGLE_WIDTH * rectangleCountX,
                                                RECTANGLE_HEIGHT * rectangleCountY));
        }

        @Override
        public void paintComponent(Graphics g) {
            for(int x = 0 ; x < rectangleCountX ; x++) {
                for(int y = 0 ; y < rectangleCountY ; y++) {
                    g.setColor(new Color(0, 0, (x+y)*64 % 256));
                    g.fillRect(x*RECTANGLE_WIDTH, y*RECTANGLE_HEIGHT,
                               RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
                }
            }
        }
    }

    public static void main(String[] args) {
        ScrollPanelFrame frame = new ScrollPanelFrame();
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}
Peter Lang
Little typo: "y <= rectangleCountX" should be "y <= rectangleCountY". Doesn't change the output of the program.
Russ Hayward
@Russ Hayward: Thanks, I corrected that.
Peter Lang
@Peter, thanks, it works!! :)
ohana