Results 1 to 10 of 10
Thread: Java Rectangle
- 10-31-2010, 03:27 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Java Rectangle
Hello,
I have another question.
I can make a rectangle in Java but the output is this:

This is my code:
Thanks!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(); } }
- 10-31-2010, 04:15 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
What is your question? b.t.w. you should override the paintComponent( ... ) method, not the paint( ... ) method.
kind regards,
Jos
-
- 10-31-2010, 04:47 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 10-31-2010, 04:54 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Ok ik have changed
but now i just get a white windowJava Code:public void paintComponent(Graphics g) { g.drawRect (10, 10, 200, 200); }
-
- 10-31-2010, 04:58 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
huh, what do you mean?
- 10-31-2010, 05:24 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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(); } }
-
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-31-2010, 07:15 PM #10
Similar Threads
-
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
Construct a rectangle
By cstokes91 in forum New To JavaReplies: 1Last Post: 01-27-2010, 06:14 AM -
Need Help with Java Rectangle Applet
By dmollica in forum Java AppletsReplies: 4Last Post: 12-10-2009, 08:45 PM -
How to Fill a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:10 PM -
How to Draw a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks