I would like to implement an application in which I hace two classes (a graphic one, and a controller one). Mi aim is to reflect the changes that happen in the controller class in the graphic one exactly when they occur. I need to do it by using the observer pattern, but I don't know how to get it...
The pretended working is the following one: - In the graphic class, I have three JPanels that I want to colour with a two-second interval between each Panle is coloured.
By now I have the following code:
----------------------Observable class--------------------------------------------
public class ImagenControl extends Observable{
private String panel1,panel2,panel3;
private int counter;
private Timer colorTimer;
private ActionListener colorListener=new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (counter==0){
panel1="yellow";
setChanged();
}
else if (counter==1){
panel2="blue";
setChanged();
}
else if (counter==2){
panel3="green";
setChanged();
}
if (counter==2)
colorTimer.stop();
else counter++;
}
};
public void giveColor(){
counter=0;
colorTimer=new Timer(2000,colorListener);
colorTimer.start();
}
---------------------------Observer class----------------------------------------
public class Imagen extends javax.swing.JFrame implements Observer{
public Imagen() {
initComponents();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
ImagenControl ctr=new ImagenControl();
ctr.giveColor();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Imagen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
// End of variables declaration
@Override
public void update(Observable o, Object o1) {
}
How would you do that??
Thanks in advance!!