Le dessin d'un cercle dans un Jframe

Je suis en train de dessiner un petit cercle dans un carré gris JFrame comme un indicateur de bâton de postes pour un RC à distance.

J'ai deux classes:

public class GUI2 extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI2 frame = new GUI2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public GUI2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    StickWidget Left = new StickWidget();
    Left.setName("Left Stick");
    Left.drawCircle(10, 10);
    contentPane.add(Left.draw(), BorderLayout.EAST);
    StickWidget Right = new StickWidget();
    Right.setName("Right Stick");
    contentPane.add(Right.draw(), BorderLayout.WEST);
    }
}

Et j'ai une deuxième classe:

public class StickWidget extends JPanel {
private javax.swing.JPanel panel_1;
private String NameOfStick;
private int xpos;
private int ypos;
public void initcomponents()
{
panel_1 = new javax.swing.JPanel();
}
public StickWidget() {
//do nothing?
}
public Component draw()
{
initcomponents();
JPanel main = new JPanel();
main.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
Dimension D1 = new Dimension(120, 150);
main.setMinimumSize(D1);
main.setMaximumSize(D1);
main.setPreferredSize(D1);
main.setSize(D1);
main.setLayout(new MigLayout("", "[grow]", "[grow][grow]"));
JPanel panel = new JPanel();
main.add(panel, "cell 0 0,grow");
JLabel StickName = new JLabel(NameOfStick);
panel.add(StickName);
panel_1.setBackground(Color.LIGHT_GRAY);
Dimension d = new Dimension(100, 100);
panel_1.setMinimumSize(d);
panel_1.setMaximumSize(d);
main.add(panel_1, "cell 0 1,grow");
return main;
}
public void drawCircle(int x, int y)
{
Graphics2D g2d = (Graphics2D)panel_1.getGraphics();
g2d.fillOval(x, y, 10, 10);
}
public void setName(String s)
{
NameOfStick = s;
}

}

Le programme actuel attire l'interface graphique un peu fine mais quand je tente d'exécuter les drawCircle() il tombe en morceaux et me donne des erreurs. Je ne suis pas sûr de ce que j'ai fait de mal, je n'ai pas beaucoup d'expérience avec le dessin 2d.

Donc mes questions:

-Est-ce la bonne démarche?
-Comment puis-je obtenir le drawCircle à dessiner des cercles dans le gris Jframes?

Message D'Erreur:

java.lang.NullPointerException
at experiments.StickWidget.drawCircle(StickWidget.java:59)
at experiments.GUI2.<init>(GUI2.java:44)
at experiments.GUI2$1.run(GUI2.java:23)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  • Pouvez-vous inclure l'erreur?
  • Ive a ajouté
InformationsquelleAutor Dragongeek | 2015-07-10