Note that there are some explanatory texts on larger screens.

plurals
  1. POObserver Pattern
    primarykey
    data
    text
    <p>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...</p> <p>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.</p> <p>By now I have the following code:</p> <p>----------------------Observable class--------------------------------------------</p> <pre><code>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(); } </code></pre> <p>---------------------------Observer class----------------------------------------</p> <pre><code>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) { } </code></pre> <p>How would you do that??</p> <p>Thanks in advance!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload