Results 1 to 10 of 10

Thread: Java Rectangle

  1. #1
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Java Rectangle

    Hello,
    I have another question.
    I can make a rectangle in Java but the output is this:



    This is my code:

    PHP Code:
    /*
    
    Name: Window
    Filename: Window.java
    
    */
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Window extends JFrame {
    
    	public Window() {
    	
    		setTitle("Window");
    		setSize(600, 400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setBackground(Color.WHITE);
    		setVisible(true);
    		
    		
    	
    	}
    	
    	public void paint(Graphics g) {
    		
    		g.drawRect (10, 10, 200, 200);  
    	
    	}
    	
    	
    	public static void main(String[] args) {
    	
    		Window w = new Window();
    	
    	}
    
    }
    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    What is your question? b.t.w. you should override the paintComponent( ... ) method, not the paint( ... ) method.

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by JosAH View Post
    What is your question? b.t.w. you should override the paintComponent( ... ) method, not the paint( ... ) method.
    But he'll need to override paintComponent for a JPanel not for the JFrame since it doesn't have this method.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    But he'll need to override paintComponent for a JPanel not for the JFrame since it doesn't have this method.
    Yep, I missed that one completely; thanks for pointing that out (it's Sunday so I'm forgiven ;-)

    kind regards,

    Jos

  5. #5
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok ik have changed

    Java Code:
    	public void paintComponent(Graphics g) {
    		
    		g.drawRect (10, 10, 200, 200);  
    	
    	}
    but now i just get a white window

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

    Default

    Quote Originally Posted by java_beginner_ View Post
    Ok ik have changed

    Java Code:
    	public void paintComponent(Graphics g) {
    		
    		g.drawRect (10, 10, 200, 200);  
    	
    	}
    but now i just get a white window
    Let's see a compilable/runnable program.

  7. #7
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    huh, what do you mean?

  8. #8
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    here is my code

    Java Code:
    /*
    
    Name: Window
    Filename: Window.java
    
    */
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Window extends JFrame {
    
    	public Window() {
    	
    		setTitle("Window");
    		setSize(600, 400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setBackground(Color.WHITE);
    		setVisible(true);
    		
    		
    	
    	}
    	
    	public void paintComponent(Graphics g) {
    		
    		g.drawRect (10, 10, 200, 200);  
    	
    	}
    	
    	
    	public static void main(String[] args) {
    	
    		Window w = new Window();
    	
    	}
    
    }

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

    Default

    Again, you should not do this in a JFrame and again JFrame has no paintComponent method. Again, do this in a JPanel and then place that JPanel into a JFrame. Please read a tutorial, any tutorial, on Swing graphics, starting with the standard oracle tutorial as it will explain a lot to you.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    In general, avoid using class names that clash with the standard JDK classes. In this case, Window is an exceptionally inappropriate choice, since javax.swing.JFrame extends java.awt.Frame, which in turn extends java.awt.Window.

Similar Threads

  1. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  2. Construct a rectangle
    By cstokes91 in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 06:14 AM
  3. Need Help with Java Rectangle Applet
    By dmollica in forum Java Applets
    Replies: 4
    Last Post: 12-10-2009, 08:45 PM
  4. How to Fill a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 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
  •