I have created this code that is supposed to draw certain things when I selected a radio button on a JForm, I have used NetBeans to create the GUI. When I select a radio button nothing happens. I have been trying to figure out what's wrong for a while but I still cannot find a solution that's why I came here. If anyone could spot a mistake I would be thankful.
public class DrawShapesGUI extends javax.swing.JFrame {
private int figureID;
public DrawShapesGUI() {
initComponents();
repaint();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code"></editor-fold>
private void lineButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 1;
repaint();
}
private void rectButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 2;
repaint();
}
private void ovalButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 3;
repaint();
}
private void arcButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 4;
repaint();
}
private void polygonButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 5;
repaint();
}
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(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.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 DrawShapesGUI().setVisible(true);
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
if (figureID == 1) {
g.drawLine(50, 50, 100, 100);
} else if (figureID == 2) {
g.fillRect(50, 50, 100, 100);
} else if (figureID == 3) {
g.fillOval(100, 100, 100, 60);
} else if (figureID == 4) {
g.drawArc(50, 50, 200, 200, 90, 30);
} else if (figureID == 5) {
Polygon poly = new Polygon();
poly.addPoint(100, 50);
poly.addPoint(150, 50);
poly.addPoint(200, 100);
poly.addPoint(150, 150);
poly.addPoint(100, 150);
poly.addPoint(50, 100);
g.fillPolygon(poly);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…