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 136 137 138 139
|
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.event.*;
class Niveau2 extends JFrame implements Runnable,KeyListener,MouseListener,MouseMotionListener
{
ZoneJeux monLaby1;
ZoneJeux monLaby2;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
boolean attend;
Niveau2(){
JPanel haut = new JPanel();
JPanel milieu = new JPanel();
JPanel bas = new JPanel();
b1=new JButton("Depart");
b2=new JButton("Pause");
b3=new JButton("Reprise");
b4=new JButton("Quitter");
monLaby1= new ZoneJeux();
monLaby2= new ZoneJeux();
milieu.setLayout(new GridLayout(1,2));
haut.add(b1);
haut.add(b2);
haut.add(b3);
haut.add(b4);
milieu.add(monLaby1);
milieu.add(monLaby2);
haut.setBackground(Color.gray);
milieu.setBackground(Color.white);
bas.setBackground(Color.gray);
Container c=this.getContentPane();
c.add("North",haut);
c.add("Center",milieu);
c.add("South",bas);
b1.addActionListener(new ActionB1());
b2.addActionListener(new ActionB2());
b3.addActionListener(new ActionB3());
b4.addActionListener(new ActionB4());
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
pack();
setVisible(true);
monLaby1.addSouris(Color.RED);
monLaby2.addSouris(Color.BLUE);
}
public void run(){
while(1){
if(attend){
wait();
}
}
}
public void keyPressed(KeyEvent evt) {
int code = e.getKeyCode();
switch(code)
{
case VK_LEFT :
{
monLaby1.bougeSouris(monLaby1.getPosSourisX()-1,monLaby1.getPosSourisX());
break;
}
case VK_RIGTH :
{
monLaby1.bougeSouris(monLaby1.getPosSourisX()+1,monLaby1.getPosSourisX());
break;
}
case VK_UP :
{
monLaby1.bougeSouris(monLaby1.getPosSourisX(),monLaby1.getPosSourisX()-1);
break;
}
case VK_DOWN :
{
monLaby1.bougeSouris(monLaby1.getPosSourisX(),monLaby1.getPosSourisX()+1);
break;
}
}
}
public void mousePressed(MouseEvent e) {
monLaby2.bougeSouris((int)(getX()/10),(int)(getY()/10));
repaint();
}
public void mouseDragged(MouseEvent e) {
//vérifier que la souris est dans la zone
if (contains(e.getX(),e.getY())) {
monLaby2.bougeSouris((int)(getX()/10),(int)(getY()/10));;
repaint();
}
}
class ActionB1 implements ActionListener{
public void actionPerformed(ActionEvent e){
start();
}
}
class ActionB2 implements ActionListener {
public void actionPerformed(ActionEvent e){
attend = true;
}
}
class ActionB3 implements ActionListener {
public void actionPerformed(ActionEvent e){
attend = false
notify();
}
}
class ActionB4 implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
} |
Partager