Solved

Oval shape does not display

Posted on 2012-03-21
8
403 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
  • 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 500 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 41
What's wrong with this code? 4 26
JAVA API design with micro service cloud in mind 1 46
Bot application - advice 3 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

856 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