Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help randomly putting rectangles into a JPanel

    Just need some assistance with drawing random rectangles inside of a JPanel when the form loads. I have a seperate rectangle class created that has a constructor for the following :

    Code:
    public Rectangle(int newX, int newY, int newWidth, 
    int newHeight, Color newColor)
     { 
            x = newX; 
            y = newY; 
            width = newWidth; 
            height = newHeight; 
            color = newColor;
     }
    It also has a paint method. I also have another JFrame class for my GUI created but I am new to GUIs and not sure how to do this. My panel is named panelRectangles and when the form loads it is supposed to create a whole bunch of random rectangles in the panel to be used for sorting. I am supposed to use the following code to make a seperate class that extends JPanel and will be used to draw my rectangles but not sure where it goes.. in my GUI Frame Class? Thanks for any assistance.

    Code:
    staticclass RectanglePanel extends JPanel 
    {
       public RectanglePanel() 
       {
         super();
         setOpaque(false); // don't paint all the bits
         setBorder(BorderFactory.createLineBorder(Color.black));
       }
       protectedvoid paintComponent(Graphics g) 
       {
    // code to paint the rectangles goes here
       }
    }

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should implement:

    Code:
    public void paint(Graphics g);
    as part of RectanglePanel. Then use the rendering context to draw whatever it is you need, in this case being the variable g.

    You can then do one of two things. The first thing would be to have the Rectangle objects have their own draw(Graphics g); method, or the second is to make a call from the Graphics object passed through the paint() method defined in RectanglePanel.

    The first method would require you keeping a reference to all Rectangle objects and iterating through a list of them calling each of their draw(Graphics g) methods, and the second option means using the Graphics object and explicitly making calls.

    So in RectanglePanel you would have the following perhaps:

    Code:
    public void paint(Graphics g) {
       Graphics2D g2D = (Graphics2D)g; // personally I prefer using Graphics2D because of the extended functionality
    
       g2D.drawRectangle( /* params in here */ );
    }
    alternatively you could do the following

    Code:
    public void paint(Graphics g) {
       Graphics2D g2D = (Graphics2D)g;
    
       for(Rectangle r : rectangleList)  {
          r.draw(g2D);
       }
    }
    So as you can see in that example, you'd store a list of all the objects and iterate through them drawing each one to screen by passing a reference to the Graphics2D object.

    Pretty straight forward really.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •