Results 1 to 4 of 4
  1. #1
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default Oval does not appear in the panel

    I must be doing something wrong and oval is not appearing at all.
    Here is the code :

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package learn;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    /**
     *
     * @author skiabox
     */
    public class Oval extends javax.swing.JFrame {
        
        
        
        /**
         * Creates new form Oval
         */
        public Oval() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 300, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Oval().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        // End of variables declaration                   
    }
    
    
    class NewPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.RED);
            g.fillOval(0, 0, getWidth(), getHeight());
        }
    }
    The ide that I am using is netbeans.

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

    Default Re: Oval does not appear in the panel

    Quote Originally Posted by skiabox View Post
    I must be doing something wrong and oval is not appearing at all.
    Here is the code :

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Oval extends javax.swing.JFrame {
        
        public Oval() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 300, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>                        
    
        public static void main(String args[]) {
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Oval.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Oval().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        // End of variables declaration                   
    }
    
    
    class NewPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.RED);
            g.fillOval(0, 0, getWidth(), getHeight());
        }
    }
    The ide that I am using is netbeans.

    Ask yourself this:
    • First: Which class draws the oval?
    • Then: Where do you add an object of that class to a GUI -- to a container that is ultimately held by a top-level window?

  3. #3
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default Re: Oval does not appear in the panel

    I tried to add

    add(new NewPanel());


    in the Oval method but nothing happens.

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

    Default Re: Oval does not appear in the panel

    Quote Originally Posted by skiabox View Post
    I tried to add

    add(new NewPanel());


    in the Oval method but nothing happens.

    I did not see this attempt in the original code you've posted. For this to work, you need to,
    1. Make sure that the layout of the container accepting your JPanel will display the JPanel.
    2. this means you should not be using GroupLayout, but rather one of the more user-friendly layout managers. Check the tutorials for information on this.
    3. Either add your component to the GUI before packing and calling setVisible(true) on the GUI or if done after the GUI has been rendered, calling revalidate() and repaint() on any container that has had its components changed.
    4. Avoid using NetBeans code generation when you are learning Swing. Instead read the tutorials and code by hand.

Similar Threads

  1. OVal 1.80
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-06-2011, 06:54 PM
  2. JPanel and Oval/Ellipse Problems
    By PorgrammingNoob117 in forum AWT / Swing
    Replies: 1
    Last Post: 05-04-2011, 09:59 PM
  3. how to draw oval using Graphics object
    By usha in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 12:15 PM
  4. Clicking on an oval in JPanel
    By chielt in forum Java 2D
    Replies: 6
    Last Post: 03-17-2010, 12:08 AM
  5. OVal 1.10
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-03-2007, 06:00 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
  •