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: Rectangle won't display in JFrame

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rectangle won't display in JFrame

    So I've been getting back into Java and downloaded eclipse back onto my laptop. However when I go to make a simple rectangle into a JFrame, the frame will pop up but no rectangle will be shown. Could any of you please provide assistance?

    Here is my main class, which sets up the JFrame...

    i
    mport javax.swing.JFrame;
    public class Frame{
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		frame.setTitle("I hope this fucking works");
    		frame.setSize(400,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		rectangle component = new rectangle();
    		frame.add(component);
    		frame.setVisible(true);
    	}
     
    }

    Here is the class that sets up the rectangle....
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    import java.awt.Rectangle;
     
     
    public class rectangle extends JComponent {
    	public void draw(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		Rectangle box = new Rectangle(10,10,50,50);
    		g2.draw(box);
    	}
     
    }

    Thanks for your time and consideration


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Rectangle won't display in JFrame

    You're using a draw() method, which doesn't actually tie into the Swing hierarchy. You're probably looking for the paint() method.

    Better yet, you should probably be using the paintComponent() method. Don't forget to call super.paintComponent() though.

    Recommended reading: Custom Painting - Tutorials - Static Void Games
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Rectangle won't display in JFrame

    What color is being used to draw the rectangle?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Rectangle won't display in JFrame

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers, including a few "house rules" that will help you feel more confident as a new user of this forum.

Similar Threads

  1. Wont display the rectangle.
    By Baboop in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 5th, 2012, 05:09 PM
  2. Why won't this image display?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2012, 10:05 PM
  3. How to display a Rectangle object in JFrame (Problem).
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 08:22 PM
  4. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 04:23 PM
  5. Replies: 2
    Last Post: October 31st, 2011, 10:19 AM