0

I'm having a little problem with my Java code. For a school project, we have to write a simple Java program. I have some programming experience, but not with Java.

We're using Netbeans, and we have to make a JFrame, containing a panel containing an applet. The applet should be a simple traffic light (red/orange/green), and the JFrame should contain three buttons "Red", "Orange" and "Green", which should update the traffic light.

I got most of it working: I have an applet that draws the traffic light based on the booleans red, orange and green, and the updating using the buttons works too.

The problem is that the screen only redraws when I hide the window behind others, and then make it appear again. I have very limited understanding of how painting works in Java, and I can't really find any solutions for this problem on the internet. Can anyone help me?

Here's my code:

DeFrame.java

package my.AppletInPanel;

public class DeFrame extends javax.swing.JFrame {

private DeApplet applet;


/** Creates new form DeFrame */
public DeFrame() {
    initComponents();
    applet = new DeApplet();
    jPanel1.add(applet);
}

/** 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")
// [Generated code]

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    applet.SetDaColor(true, false, false);
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    applet.SetDaColor(false, true, false);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    applet.SetDaColor(false, false, true);
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new DeFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

And DeApplet.java:

package my.AppletInPanel;

import java.applet.Applet;
import java.awt.*;

public class DeApplet extends Applet {

public boolean rood = true, oranje = false, groen = false;

public DeApplet(){
    setLayout(null);
    setSize(50, 150);
}

public void SetDaColor(boolean r, boolean o, boolean g){
    rood = r;
    oranje = o;
    groen = g;
}

public void paint(Graphics g){
    super.paint(g);

    g.setColor(Color.white);
    g.drawRect(0, 0, 50, 150);
    g.fillRect(0, 0, 50, 150);

    if(rood){
        g.setColor(Color.red);
        g.fillOval(0 , 0, 50 ,50);
    }
    if (oranje){
        g.setColor(Color.orange);
        g.fillOval(0 , 50, 50 ,50);
    }
    if (groen){
        g.setColor(Color.green);
        g.fillOval(0 , 100, 50 ,50);
    }

    String s1 = (new Boolean(rood).toString());
    String s2 = (new Boolean(oranje).toString());
    String s3 = (new Boolean(groen).toString());

    g.setColor(Color.black);
    g.drawString(s1, 25, 25);
    g.drawString(s2, 25, 75);
    g.drawString(s3, 25, 125);
}
}
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Timon Knigge
  • 294
  • 3
  • 16

2 Answers2

1

try repainting the panel and/or the frame

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    applet.SetDaColor(false, true, false);
    applet.repaint();
}
schippi
  • 1,034
  • 5
  • 15
0

You should have a look at Java Swing revalidate() vs repaint(), which discusses methods of importance to you.

Since this is for school, I would also like to point out that your "which color to show" logic would be greatly improved by use of Java Enums, where you could not wind up in the weird state that e.g. groen and rood are both true at the same time.

Community
  • 1
  • 1
larsson
  • 11
  • 1