1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
public class MonInterface extends JDialog implements Observer
{
private static final long serialVersionUID = 1L;
private static Voiture maVoiture;
private static Regulateur monRegulateur;
// element interface
private JLabel lblVitesseEnCours;
private JButton btnAcc;
private final JButton btnContact;
private JButton btnFrein;
private JButton btnEmbrayage;
private JButton btnDesactReg;
private JButton btnActReg;
private static int i;
static javax.swing.Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args)
{
try
{
MonInterface dialog = new MonInterface();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
maVoiture = new Voiture();
monRegulateur = new Regulateur();
MonInterface RegVit = new MonInterface();
maVoiture.addObserver(RegVit);
monRegulateur.addObserver(RegVit);
i = 0;
int timerDelay = 100;
timer = new javax.swing.Timer(timerDelay, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
maVoiture.setVitesseEnCours();
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public MonInterface()
{
setBounds(100, 100, 356, 470);
getContentPane().setLayout(null);
lblVitesseEnCours = new JLabel("0");
lblVitesseEnCours.setBounds(10, 11, 210, 100);
getContentPane().add(lblVitesseEnCours);
btnAcc = new JButton(new ImageIcon("accelerateur.jpg"));
btnAcc.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent arg0)
{
// TODO Auto-generated method stub
if(btnAcc.getModel().isPressed() && !timer.isRunning())
{
timer.start();
}
else if (!btnAcc.getModel().isPressed() && timer.isRunning())
{
timer.stop();
}
}
});
btnAcc.setBounds(230, 278, 100, 150);
getContentPane().add(btnAcc);
btnContact = new JButton(new ImageIcon("contactOFF.jpg"));
btnContact.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
maVoiture.setDemarrage();
if (!maVoiture.getDemarrage())
{
btnContact.setIcon(new ImageIcon("contactOFF.jpg"));
}
else
{
btnContact.setIcon(new ImageIcon("contactON.jpg"));
}
}
});
btnContact.setBounds(230, 11, 100, 100);
getContentPane().add(btnContact);
btnFrein = new JButton(new ImageIcon("frein.jpg"));
btnFrein.setBounds(120, 278, 100, 100);
getContentPane().add(btnFrein);
btnEmbrayage = new JButton(new ImageIcon("embrayage.jpg"));
btnEmbrayage.setBounds(10, 278, 100, 100);
getContentPane().add(btnEmbrayage);
btnDesactReg = new JButton(new ImageIcon("desactreg.jpg"));
btnDesactReg.setBounds(29, 191, 50, 50);
getContentPane().add(btnDesactReg);
btnActReg = new JButton(new ImageIcon("actreg.jpg"));
btnActReg.setBounds(29, 136, 50, 50);
getContentPane().add(btnActReg);
}
@Override
public void update(Observable o, Object arg)
{
//System.out.println(maVoiture.getDemarrage());
System.out.println(""+maVoiture.getVitesseEncours());
lblVitesseEnCours.setText(""+maVoiture.getVitesseEncours());
}
} |
Partager