Bonjour,
J’essaie maintenant depuis quelques jours de faire déplacer un rond 2D grâce a un thread pour un jeux en 2D ou le but est donc d'exploser des cibles. Jai des cibles mouvantes, mais hélas cela ne marche pas vraiment. mon rond se déplacer sur la moitié de l'écran puis ne bouge plus :) Je suppose que cela vient d'un trop grand nombre de thread lancé mais je ne sait pas comment optimiser cela :) bref après avoir cherché, je m'en remet aux experts pour me donner quelques astuces :)
Voici le code :
Class cible :
Ensuite la class des cibles circulaires :Code:
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 import java.awt.*; import javax.swing.*; import java.util.Random; public abstract class Cible { private int taille; private int x; private int y; private Color couleur; private Container conteneur; Random rand = new Random(); public Cible(Container conteneur) { super(); this.conteneur = conteneur; float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); this.setTaille((int) (100*Math.random())); this.setX((int) (Math.random()*(this.getConteneur().getWidth()-this.getTaille()))); this.setY((int) (Math.random()*(this.getConteneur().getHeight()-this.getTaille()))); this.couleur= new Color(r, g, b); conteneur.setBackground(Color.BLACK); } public abstract void dessiner(Graphics g); public abstract boolean recevoirTir(int tirX, int tirY); public int getTaille() { return taille; } public void setTaille(int taille) { this.taille = taille; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Color getCouleur() { return couleur; } public void setCouleur(Color couleur) { this.couleur = couleur; } public Container getConteneur() { return conteneur; } public void setConteneur(Container conteneur) { this.conteneur = conteneur; } }
enfin la classe des cibles circulaires mouvantes :Code:
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 import java.awt.Color; import java.awt.Container; import java.awt.Graphics; public class CibleCirculaire extends Cible { public CibleCirculaire(Container conteneur) { super(conteneur); // TODO Auto-generated constructor stub } @Override public void dessiner(Graphics g) { // TODO Auto-generated method stub g.setColor(this.getCouleur()); g.fillOval(this.getX(), this.getY(), this.getTaille(), this.getTaille()); } @Override public boolean recevoirTir(int tirX, int tirY) { // TODO Auto-generated method stub if(Math.pow(tirX-this.getX(), 2)+Math.pow(tirY-this.getY(), 2)<=Math.pow(this.getTaille(), 2)) { //System.out.println("Touché"+tirX); this.setTaille(this.getTaille()-5); } return false; } }
et enfin ma class main :Code:
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 import java.awt.Color; import java.awt.Container; import java.awt.Graphics; public class CibleCirculaireMobile extends CibleCirculaire { private int dx; private Thread t=new Thread(); private String sens="droite"; private Object verrou= new Object(); //private int x=this.getX(); public String getSens() { return sens; } public void setSens(String sens) { this.sens = sens; } public int getDx() { return dx; } public void setDx(int dx) { this.dx = dx; } public Thread getT() { return t; } public void setT(Thread t) { this.t = t; } public CibleCirculaireMobile(Container conteneur, int d) { super(conteneur); this.dx=d; // TODO Auto-generated constructor stub } public boolean recevoirTir(int tirX, int tirY) { // TODO Auto-generated method stub if(Math.pow(tirX-this.getX(), 2)+Math.pow(tirY-this.getY(), 2)<=Math.pow(this.getTaille(), 2)) { //System.out.println("Touché"+tirX); this.setTaille(this.getTaille()-5); } return false; } public void deplacer() { if(t.isAlive()){ return; } else { Thread t = new Thread() { public void run() { while(!interrupted()) { if(getSens()=="gauche") { setX(getX()-20); setSens("gauche"); } else { setX(getX()+40); setSens("droite"); int calcTaille=getX()+getTaille(); //System.out.println(calcTaille); if(calcTaille>=799) { setSens("gauche"); } } System.out.println(getName()); try { sleep(100); //System.out.println("sleep"); } catch(InterruptedException e){ destroy(); return; } //}//while //System.out.println("boug ton boule"); getConteneur().repaint(); } } }; t.start(); t = null; getConteneur().repaint(); } } }
Code:
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 import java.awt.BorderLayout; import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.*; public class Main extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Main frame = new Main(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 799, 600); final List <Cible>list = new ArrayList<Cible>(); contentPane = new JPanel() { protected void paintComponent(Graphics g) { int limit1=(int) (5*Math.random()); for(int i=0; i<5; i++) { list.add(new CibleCarree(contentPane)); list.get(i).dessiner(g); } int limit2=(int) (5*Math.random()); for(int i=5; i<10; i++) { list.add(new CibleCirculaire(contentPane)); list.get(i).dessiner(g); } for(int i=10; i<11; i++) { CibleCirculaireMobile laCible= new CibleCirculaireMobile(contentPane, 5); list.add(laCible); list.get(i).dessiner(g); laCible.deplacer(); } /* */ } }; contentPane.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { for(int b=0; b<list.size(); b++) { list.get(b).recevoirTir(arg0.getX(), arg0.getY()); if(list.get(b).getTaille()<14) { list.remove(b); } } repaint(); } }); contentPane.setBackground(Color.pink); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); contentPane.setBackground(Color.PINK); setContentPane(contentPane); repaint(); } }