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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| public class Sousfenetre extends JFrame{
/****/
private static final long serialVersionUID = 1L;
// Composant
public JCheckBox C = new JCheckBox();
public JButton Start = new JButton("Start"); // Creation de bouton Go
public JButton Stop = new JButton("Stop"); // Creation de bouton Stop
public JButton ReStart = new JButton("ReStart"); // Creation de bouton Stop
public JLabel AngleL = new JLabel("Angle : 40");
public JLabel PuissanceL = new JLabel("Puissance : 100");
public JSlider Vitesse = new JSlider();
public JSlider Angle = new JSlider();
//
public Panneau pan = new Panneau();
public Panneau2 pan2 = new Panneau2();
public ImageIcon ic;
public ImageIcon iframe;
public Image im;
public Timer timer;
int ballx=90,bally=630;
public static final double x0=90, y0=630,g=9.8;
public double v0,vx0,vy0,vx,vy;
public double dy,dyt,dx,dxt,theta;
public double time,ftime;
public double vtime=0; //changement de temps
public double delay=50;
public int netposx;
public int netposy;
public double w1,h1;
public Sousfenetre(){
this.setLayout(new BorderLayout());
iframe = new ImageIcon("Cannon.PNG");
this.setIconImage(iframe.getImage());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pan = new Panneau();
pan2 = new Panneau2();
this.setResizable(false);
this.setTitle("Animation"); // Titre de l'application
this.setSize(650, 500); // la taille de fenetre d'application
this.setAlwaysOnTop(true);
this.setFocusable(true);
this.setLocationRelativeTo(null);
ic = new ImageIcon("kkk.gif");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // par default
event e = new event();
Start.addActionListener(e);
ReStart.addActionListener(e);
timer = new Timer(50,e);
//timer.start();
Stop.addActionListener(e);
Vitesse = new JSlider(JSlider.HORIZONTAL,10,80,40);
// Vitesse.addChangeListener( C );
Angle = new JSlider(JSlider.HORIZONTAL, 0,300,100);
// Angle.addChangeListener(C);
Vitesse.setBounds(10, 20, 100, 10);
Angle.setBounds(30, 30, 300, 20);
pan2.add(Start);
pan2.add(Stop);
pan2.add(Vitesse);
pan2.add(Angle);
this.add(pan, BorderLayout.CENTER);
this.add(pan2,BorderLayout.NORTH);
netposx = pan.netx + pan.neth/2;
netposy = pan.nety + pan.neth/2;
this.setVisible(true);
info();
}
public void info(){
theta = Vitesse.getValue(); //angle
theta = Math.toRadians(theta);
v0 = Angle.getValue(); //vitesse initial
vx0 = v0*Math.cos(theta); //vitesse vertical
vy0 = v0*Math.sin(theta); //vitesse horizontale
time = -vy0/-g; //time to altitude
dy = (vy0*time) + (0.5*g*time*time); //altitude
ftime = time*2; //total time
dx = vx0*ftime; //distance
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == Start){
vtime +=delay/1000;
timer.start();
Vitesse.setEnabled(false);
Angle.setEnabled(false);
}else if(arg0.getSource() == Stop){
timer.stop();
Vitesse.setEnabled(true);
Angle.setEnabled(true);
}else if(arg0.getSource() == timer){
v0 = Angle.getValue();
vx0 = v0*Math.cos(theta);
vy0 = v0*Math.sin(theta);
dxt = vx0*vtime;
dyt = (vy0*vtime) + (0.5*(-g)*vtime*vtime);
w1= dxt+x0;
h1 = -dyt+y0;
Graphics g = pan.getGraphics();
/* try {
if(C.isSelected()){
Image ball = ImageIO.read(new File("koora.gif"));
g.drawImage(ball, (int)w1, (int)h1, pan);
}
} catch (Exception e) {
e.printStackTrace();
}*/
if(h1 >= 650 || w1>= 1250){
timer.stop();
Vitesse.setEnabled(false);
Angle.setEnabled(false);
}
if(w1 >= pan.netx && w1<= pan.netx+pan.netw){
if(h1 >= pan.nety && h1 <=pan.nety+pan.neth){
timer.stop();
Vitesse.setEnabled(true);
Angle.setEnabled(true);
JOptionPane.showMessageDialog(pan, "BRAVOO!!! Vous Avez Gangé");
}
}
System.out.println(h1+" "+pan.nety);
vtime += 0.05;
}else if(arg0.getSource()== ReStart){
timer.stop();
Vitesse.setEnabled(true);
Angle.setEnabled(true);
dxt = 0;
dyt = 0;
vtime = 0;
Graphics g = pan.getGraphics();
try {
int h = pan.getHeight()-1;
int w = pan.getWidth()-1;
Image ar = ImageIO.read(new File("ar.PNG"));
Image cannon = ImageIO.read(new File("can.gif"));
Image ball = ImageIO.read(new File("koora.gif"));
Image net = ImageIO.read(new File("net.png"));
Image wall = ImageIO.read(new File("wall.gif"));
g.drawImage(ar, 0, 0,w,h, pan);
g.drawImage(ball, ballx, bally, pan);
g.drawImage(wall, pan.wallx, pan.wally,pan.wallh,pan.wallw, pan);
g.drawImage(net, pan.netx, pan.nety,pan.netw,pan.neth, pan);
g.drawImage(cannon, 20, 600, pan);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class change implements ChangeListener{
public void stateChanged(ChangeEvent arg0) {
if(arg0.getSource() == Vitesse){
int v = Vitesse.getValue();
String s = "Angle : "+ Integer.toString(v);
AngleL.setText(s);
info();
}else if(arg0.getSource()==Angle){
int v = Angle.getValue();
String s = "Puissance : "+ Integer.toString(v);
PuissanceL.setText(s);
info();
}
}
}
} |
Partager