Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why doesn't the frame viewer print the rectangles?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Why doesn't the frame viewer print the rectangles?

    I am trying to draw two rectangles in a new frame, and I am making some mistake which I can't figure out. It should be easy but I just don't see it.

    Here are the two classes:


    Drawn rectangles:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.JComponent;
     
    //Draws two rectangles
     
     
    public class Four
    {
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
     
    		Rectangle box = new Rectangle(5,10,10,10);
    		g2.draw(box);
     
    		box.translate(15,25);
    		g2.draw(box);
    	}
    }


    And the frame:

    import java.awt.Component;
    import javax.swing.JFrame;
     
    public class BoxViewer 
    {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
     
    		frame.setSize(300,400);
    		frame.setTitle("Two Rectangles");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		Four component = new Four(); //Mixes up both classes
     
    		frame.add(component); //Error is here
     
    		frame.setVisible(true);
    	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why doesn't the frame viewer print the rectangles?

    You should check the error message in detail, and post it here as it will tell you what you're doing wrong. You need to add a Component or one of its children to a JFrame, and your Four class doesn't extend anything. Perhaps you want to have it extend an existing JComponent type of class.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Why doesn't the frame viewer print the rectangles?

    Quote Originally Posted by curmudgeon View Post
    You should check the error message in detail, and post it here as it will tell you what you're doing wrong. You need to add a Component or one of its children to a JFrame, and your Four class doesn't extend anything. Perhaps you want to have it extend an existing JComponent type of class.
    That is correct! Thank you very much.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why doesn't the frame viewer print the rectangles?

    You're quite welcome! So, out of curiosity, what class did you have Four extend?

Similar Threads

  1. Help With Moving A 2D Array Of Rectangles
    By billg118 in forum Collections and Generics
    Replies: 1
    Last Post: April 22nd, 2012, 02:23 PM
  2. Question on Rectangles
    By parkBENch in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 11:13 PM
  3. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  4. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 06:27 AM
  5. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM