Solved

Oval shape does not display

Posted on 2012-03-21
8
396 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now