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
|
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Game implements Runnable {
private boolean stop;
private boolean pause;
private Watch watch;
private JLabel label;
public Game() {
//Watch
watch =new Watch();
//Fenetre
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Pause");
label = new JLabel();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pause == true) {
pause = false;
} else {
pause = true;
}
}
});
JPanel p = new JPanel(new FlowLayout());
p.add(b);
p.add(label);
frame.setContentPane(p);
frame.setSize(new Dimension(200, 100));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//Lance le thread
new Thread(this).start();
}
public void run() {
int delta = 2;
while (!stop) {
try {
Thread.sleep(delta);
} catch (InterruptedException e) {
}
if (!pause) {
watch.update(delta);
label.setText((int)watch.getTime()/1000+"");
}
}
}
public void setPause(boolean pause) {
this.pause = pause;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public static void main(String[] args) {
Game g = new Game();
}
private class Watch {
private int time;
public Watch(){
time = 0;
}
public void update(float delta) {
time+=delta;
}
public int getTime() {
return time;
}
}
} |
Partager