Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oval shape does not display

Posted on 2012-03-21
8
Medium Priority
?
412 Views
Last Modified: 2012-03-21
The oval shape I am trying to draw , does not display in the window.

Here is the 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();
        add(new NewPanel());
    }

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

Open in new window



Any ideas guys?
Thank you.
0
Comment
Question by:skiabox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37748344
This will work and shows Oval


/*
 * 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();
         getContentPane().add(new NewPanel());
        setSize(200,200);
    }

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

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37748359
this is how it looks like
(changed
 setSize(200,200);
to
 setSize(200,300);
otherwise it is a circle)
oval
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37748392
In the above code I removed all NetBeans stuf and just added your NewPanel to container
(in Swing you needd to add to getContentPane(). not to JFrame itself):
   getContentPane().add(new NewPanel());


If you  want
to do it with NetBeans GUI manger you need to add your custom elenment (class NewPanel)
to the pallette, see these links:

http://stackoverflow.com/questions/691890/reusing-a-jpanel-in-netbeans-gui-designer

http://wiki.netbeans.org/FaqFormUsingCustomComponent

http://stackoverflow.com/questions/816286/how-to-include-custom-panel-with-netbeans-gui-builder
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:skiabox
ID: 37748523
Is there any way I can use both NetBeans' generated code and

 getContentPane().add(new NewPanel());

Open in new window


?
I just tried this but I see no oval.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37748529
look at my post ID: 37748392 and recommendations how to do it in the links there
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37748537
Better go away from this Netbeans GUI designer and create the components yourself - then you'll not depend on them and will be more flexible
0
 

Author Closing Comment

by:skiabox
ID: 37748634
Great links.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37748640
I think this part froom this link should help you to add this your siubclass of JPanle:
http://wiki.netbeans.org/FaqFormUsingCustomComponent

To use a Swing component subclass without instantiating it in the GUI builder

    Use the Swing component ancestor in the GUI builder (e.g. JTable).
    Select the component, go to Properties window, select Code tab, Custom Creation Code property.
    Invoke the editor and enter java code you'd like to have to instantiate the subclass. E.g.: new MyTable();
    Note you can also define a full anonymous innerclass here.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question