Results 1 to 4 of 4
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default 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();
    	}
    	
    }

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

    Default

    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.
    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();
                }
            });
        }
        
    }
    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)

    db

  3. #3
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default 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();
    	}
    }

  4. #4
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    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

  1. is this circle okeh ?
    By SHE in forum New To Java
    Replies: 0
    Last Post: 04-14-2011, 05:34 AM
  2. Josephu's Circle
    By Pyrexkidd in forum New To Java
    Replies: 8
    Last Post: 08-01-2010, 08:44 AM
  3. Circle arrays
    By n00b in forum New To Java
    Replies: 15
    Last Post: 05-05-2010, 05:04 PM
  4. Circle and line
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:56 AM
  5. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •