Results 1 to 6 of 6
  1. #1
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Why only one of the two rectangles is painted?

    Below is a snippet of code that is supposed to paint two rectangles. However, only one is painted. What am I missing here?

    Thanks

    ****************

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.geom.Area;

    public class Test {

    public static void main(String[] args) {
    Frame f = new Frame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setAlwaysOnTop(true);
    f.setVisible(true);

    }
    }

    class Frame extends JFrame {

    Frame(){
    setTitle("Test");
    setSize(300,300);

    add(new DrawFigures(new Rectangle2D.Double(60,60,80,80))); //Only one of these rectangles
    add(new DrawFigures(new Rectangle2D.Double(20,20,120,120))); // is painted

    }


    }

    class DrawFigures extends JComponent {


    Rectangle2D Rect;


    DrawFigures(){

    }

    DrawFigures(Rectangle2D Rect_){
    Rect=Rect_;
    }

    public void paintComponent (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g);


    g2.draw(Rect);

    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    When you a component to a JFrame, you're actually adding the component to the JFrame's contentPane. If you read the Swing tutorial section on the layout managers, you'll find out that the content panes of top-level containers use BorderLayout as their default layout, and you'd see that by adding a component to a BorderLayout-using container without specifying which place to place it defaults to placing the component BorderLayout.CENTER. The long and short of it is, you're adding one component right on top of the other. A solution is to give the contentPane a different layout such as a GridLayout(1, 0).

    Good luck and welcome!

  3. #3
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Thanks for your answer. But the two rectangles must overlap. Is there a workaround?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you use only one JComponent or JPanel that does the actual drawing, and give it an ArrayList of Rectangle2D. In the paintComponent method use a for loop to iterate through the ArrayList<Rectangle2D> drawing each one. Give the class an addRectangle(Rectangle2D rect) method so outside classes can add rectangles to your drawing component.

  5. #5
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Re: Why only one of the two rectangles is painted?

    Sorry for the delay. Thanks for your valuable help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why only one of the two rectangles is painted?

    You're welcome!

Similar Threads

  1. how to know when jframe swing is painted
    By petmoreno in forum AWT / Swing
    Replies: 1
    Last Post: 11-02-2010, 07:06 PM
  2. Drawing Rectangles - NullPointerExceptions
    By cselic in forum Java 2D
    Replies: 4
    Last Post: 05-20-2010, 02:37 PM
  3. Rectangles method
    By bdario1 in forum New To Java
    Replies: 31
    Last Post: 03-31-2010, 09:32 PM
  4. Replies: 16
    Last Post: 01-04-2010, 06:34 PM
  5. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:25 PM

Posting Permissions

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