Results 1 to 4 of 4
Thread: Why is there no circle?
- 05-23-2011, 07:03 AM #1
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
Why is there no circle?
Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; public class Circle extends JFrame { public Circle(){ super("Circle"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,250); JPanel panel = new JPanel(); add(panel); setVisible(true); } public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.BLUE); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawOval(25, 25, 200, 200); g2.dispose(); } public static void main(String[] args){ new Circle(); } }
- 05-23-2011, 07:23 AM #2
Because custom painting is done in an extended JComponent or JPanel, not in a top level window.
Adding a paintComponent method to a JFrame subclass doesn't do anything. The method is never invoked. Placing the method in an anonymous subclass of JPanel will show your circle.Also, all Swing components should be constructed and manipulated on the EDT: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)Java Code:public class Circle extends JFrame { public Circle(){ super("Circle"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,250); JPanel panel = new JPanel() { public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.BLUE); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawOval(25, 25, 200, 200); g2.dispose(); } }; add(panel); setVisible(true); } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { public void run() { new Circle(); } }); } }
db
- 05-23-2011, 07:59 AM #3
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
I did solve this one, here is the code:
Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Circle extends JFrame { public Circle(){ super("Circle"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,250); CirclePane circle = new CirclePane(); add(circle); setVisible(true); } public static void main(String[] args){ new Circle(); } } class CirclePane extends JPanel { public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.BLUE); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Ellipse2D.Float circle = new Ellipse2D.Float(25, 10, 200, 200); g2.draw(circle); g2.dispose(); } }
- 05-23-2011, 08:07 AM #4
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
There are some aspects of your code that I don't understand just yet and there's only one more chapter left on swing in my textbook and it's about Java web start so I'm not sure if what you've done will be covered. It's the main method that is confusing me. I don't know anything about the SwingUtilities class.
Thanks for the link, I'll give it a read and see if that helps.
Similar Threads
-
is this circle okeh ?
By SHE in forum New To JavaReplies: 0Last Post: 04-14-2011, 05:34 AM -
Josephu's Circle
By Pyrexkidd in forum New To JavaReplies: 8Last Post: 08-01-2010, 08:44 AM -
Circle arrays
By n00b in forum New To JavaReplies: 15Last Post: 05-05-2010, 05:04 PM -
Circle and line
By c_walker in forum New To JavaReplies: 1Last Post: 01-27-2010, 03:56 AM -
How to write numbers around a circle
By pheonix in forum New To JavaReplies: 8Last Post: 06-11-2009, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks