Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Simple shape program will not draw

  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Simple shape program will not draw

    I have a very basic program, it creates a JFrame, then i have a separate class which creates a circle component i then add this to my JFrame. For some reason it won't draw the circle, it complies with no errors. I have added a fill colour to make sure i couldn't miss it.

    EDIT: Ok, so i have got it to appear by placing frame.setVisible(true); below the frame.add() line. Could someone explain why it works this way but not how it is below?

    JFrame, with component added
    Java Code:
    import javax.swing.*;
    public class EmptyFrameViewer 
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame();
            
            final int FRAME_HEIGHT = 800;
            final int FRAME_WIDTH = 800;
            
            frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);
            frame.setTitle("Circle");
            frame.setVisible(true); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            CircleComponent circle = new CircleComponent();
            frame.add(circle);
                    
        }
        
    }
    Circle component
    Java Code:
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.*;
    public class CircleComponent extends JComponent 
    {
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            
            Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 400, 400);
            
            g2.setColor(Color.RED);
            
            g2.fill(circle);
        }
    }
    Last edited by wdh321; 10-02-2012 at 05:31 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Simple shape program will not draw

    You can't just add components to a visible window. You have to tell it to revalidate the layout if you do that, otherwise stuff won't show up properly. You should usually add all your components before making the window visible, but if you really need to add something after showing it, call revalidate afterwards.
    wdh321 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Simple Shape Recognition Java Program
    By TheShadow in forum New To Java
    Replies: 6
    Last Post: 07-31-2013, 09:25 PM
  2. Simple draw line program not working
    By forms in forum New To Java
    Replies: 7
    Last Post: 01-24-2012, 04:51 PM
  3. How to draw a shape with listener ??
    By matrixcool in forum AWT / Swing
    Replies: 13
    Last Post: 12-15-2010, 06:38 PM
  4. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 03:35 PM
  5. Replies: 0
    Last Post: 05-20-2010, 08:18 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
  •