1

I know I am being an idiot and that's why I can't figure it out but I am trying to paint a bunch of rectangles with randoms size and position using paintComponent. I am trying to make sure that all of them are painted within the frame. I am able to do it with the following code (snippet) but I am wondering if there is a better way to do it than me hardcoding numbers into the program. Is there a method that I should take a look at that might be what I'm looking for?

Here's the inner class that overrides the paintComponent() method:

class DrawPanel extends JPanel {
    public void paintComponent(Graphics g) {
        int red = (int)(Math.random()*256);
        int blue = (int)(Math.random()*256);
        int green = (int)(Math.random()*256);

        g.setColor(new Color(red, blue, green));
        //The following 4 lines keep the rects within the frame
        //The frame is 500,500
        int ht = (int)(Math.random()*400);
        int wd = (int)(Math.random()*400);

        int x = (int)(Math.random()*100);
        int y = (int)(Math.random()*100);

        g.fillRect(x,y,ht,wd);
    }
}
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Nico
  • 3,471
  • 2
  • 29
  • 40
  • What problems are you having? Are you sure that you want to randomize within `paintComponent(...)`, such that any time the gui is repainted for any reason, the rectangles will change. And are you sure that you don't want to call the `super.paintComponent(...)` method? – Hovercraft Full Of Eels Dec 08 '12 at 16:48
  • 1
    Also, shouldn't you be using the `getSize()` of the DrawPanel and not use the JFrame's size? Else you are ignoring its title bar etc... – Hovercraft Full Of Eels Dec 08 '12 at 16:50
  • See related examples [here](http://stackoverflow.com/q/9849950/230513). – trashgod Dec 08 '12 at 16:53
  • Thanks Reimeus, Hovercraft and Lucas. The program was working as it was supposed to even before. The coding was just ugly and as you guys pointed out, a little wrong too. – Nico Dec 08 '12 at 17:31

3 Answers3

4

I am wondering if there is a better way to do it than me hardcoding numbers into the program.

Yes there is

  • call getSize() on the enclosing JPanel, the DrawPanel, so you can see the actual boundaries of the component that is being drawn upon. (edit: or getWidth() and getHeight() as recommended by Lucas -- 1+ to his answer!).
  • Also, you will usually want to call the super's paintComponent(...) method within the child's override.
  • Also you will usually want to do your randomization elsewhere, such as in the DrawPanel's constructor so as not to change your rectangles each time you re-size the GUI.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

You should base your co-ordinates & sizes on the DrawPanel component size. Also using Random.nextInt instead of Math.random() will make it easier to keep within range based on the current size of the panel:

Random random = new Random();
int ht = random.nextInt(getHeight());
int wd = random.nextInt(getWidth());

int x = random.nextInt(getWidth() - wd);
int y = random.nextInt(getHeight() - ht);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • This is what I was looking for. Thanks a lot. I've upvoted all the other answers too since they were all helpful but this fixed the "problem". – Nico Dec 08 '12 at 17:29
2

There is a method to get the length and width of the panel you are working in.

getHeight();
getWidth();

These will return the current size of the JPanel you are working in, meaning if you resize the window it'll actually still draw them inside.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getWidth()

Lucas
  • 5,071
  • 1
  • 18
  • 17