Groupe : Membres
|
Je le peux, je précise que c'est mon code source  Donc il peut paraître un peu bizarre parfois vu que je n'ai presque rien copier.
Citation : Mon codeCitation : FenetreCode : Java 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202 | package gui;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author emmanuel
*/
public class Fenetre extends JFrame {
public static void main(String[] args) {
// TODO code application logic here
Fenetre fen = new Fenetre();
}
private Panneau pan = new Panneau();
private int bougerX = 2;
private int bougerY = 2;
private int tempBougerX = 0;
private int tempBougerY = 0;
private JPanel container = new JPanel();
private boolean pause = true;
private JButton bouton1 = new JButton("Stop");
private JButton bouton2 = new JButton("Go");
private JLabel label = new JLabel("La forme");
private JComboBox boite = new JComboBox();
private Thread t;
public Fenetre() {
setVisible(true);
this.setTitle("Ma premier fenetre");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(pan, BorderLayout.CENTER);
container.add(bouton1, BorderLayout.SOUTH);
JPanel south = new JPanel();
south.add(bouton1);
south.add(bouton2);
container.add(south, BorderLayout.SOUTH);
//ajout d'un container en haut
JPanel container_NORTH = new JPanel();
container_NORTH.setForeground(Color.GRAY);
Dimension preferredSize = new Dimension(800,30);
container_NORTH.setPreferredSize(preferredSize);
container_NORTH.add(label);
container_NORTH.add(boite);
//on ajoute les type de forme a la boite
boite.addItem("Oval");
boite.addItem("Carré");
boite.addItem("Triangle");
boite.addItem("Etoile");
boite.addActionListener(new FormesListener());
boite.setSelectedIndex(0);
container.add(container_NORTH, BorderLayout.NORTH);
this.setContentPane(container);
this.setVisible(true);
bouton1.addActionListener(new Bouton1Listener());
bouton2.addActionListener(new Bouton2Listener());
bouton1.setEnabled(false); // Votre bouton n'est plus cliquable !
bouton2.setEnabled(true);
}
private void go() {
for(;;)
{
int x = pan.GetPosX(), y = pan.GetPosY();
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x == pan.getWidth()-50 && pause != true){
bougerX = 1;
}
else if(y == pan.getHeight()-50 && pause != true){
bougerY = 1;
}
else if(x == 0 && pause != true){
bougerX = 0;
}
else if(y == 0 && pause != true){
bougerY = 0;
}
switch(bougerX){
case 0 :
x++;
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
case 1 :
x--;
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
case 2 :
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
default : System.out.println("Error !");
}
switch(bougerY){
case 0 :
y++;
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
case 1 :
y--;
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
case 2 :
pan.SetPosX(x);
pan.SetPosY(y);
pan.repaint();
break;
default : System.out.println("Error !");
}
}
}
class Bouton1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(pause == false){
pause = true;
tempBougerX = bougerX;
tempBougerY = bougerY;
bougerX = 2;
bougerY = 2;
}
bouton1.setEnabled(false);
bouton2.setEnabled(true);
}
}
class Bouton2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(pause == true){
pause = false;
bougerX = tempBougerX;
bougerY = tempBougerY;
t = new Thread(new PlayAnimation());
t.start();
}
bouton2.setEnabled(false); // Votre bouton n'est plus cliquable !
bouton1.setEnabled(true);
}
}
class PlayAnimation implements Runnable{
public void run() {
go();
}
}
class FormesListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
pan.setForme(boite.getSelectedItem().toString());
}
}
}
|
Citation : Mon code JAVACitation : PanneauCode : Java 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 | package gui;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Panneau extends JPanel{
private int posX = 0;
private int posY = 0;
private String forme = null;
private int[] TrianglePointsX = {posX,posX+50,posX+25};
private int[] TrianglePointsY = {posY+50,posY+50,posY};
private int[] EtoilePointsX = {posX,posX+15,posX+2,posX+25,posX+48,posX+35,posX+50,posX+32,posX+25,posX+18};
private int[] EtoilePointsY = {posY+20,posY+25,posY+50,posY+32,posY+50,posY+25,posY+20,posY+20,posY,posY+20};
@Override
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.fillRect(0,0, this.getWidth(), this.getHeight());
g2d.setColor(Color.orange);
fill(g2d);
}
public void fill(Graphics2D g2d){
if(forme.equals("Oval")){
g2d.fillOval(posX, posY, 50, 50);
}
else if(forme.equals("Carré")){
g2d.fillRect(posX, posY, 50, 50);
}
else if(forme.equals("Triangle")){
g2d.fillPolygon(TrianglePointsX, TrianglePointsY, 3);
}
else if(forme.equals("Etoile")){
g2d.fillPolygon(EtoilePointsX, EtoilePointsY, 10);
}
else{
}
}
public int GetPosX(){
return posX;
}
public void SetPosX(int posX){
this.posX = posX;
//pour le triangle
TrianglePointsX[0] = this.posX;
TrianglePointsX[1] = this.posX+50;
TrianglePointsX[2] = this.posX+25;
//pour l'etoile
EtoilePointsX[0] = this.posX;
EtoilePointsX[1] = this.posX+15;
EtoilePointsX[2] = this.posX+2;
EtoilePointsX[3] = this.posX+25;
EtoilePointsX[4] = this.posX+48;
EtoilePointsX[5] = this.posX+35;
EtoilePointsX[6] = this.posX+50;
EtoilePointsX[7] = this.posX+32;
EtoilePointsX[8] = this.posX+25;
EtoilePointsX[9] = this.posX+18;
}
public int GetPosY(){
return posY;
}
public void SetPosY(int posY){
this.posY = posY;
//pour le triangle
TrianglePointsY[0] = this.posY+50;
TrianglePointsY[1] = this.posY+50;
TrianglePointsY[2] = this.posY;
//pour l'etoile
EtoilePointsY[0] = this.posY+20;
EtoilePointsY[1] = this.posY+25;
EtoilePointsY[2] = this.posY+50;
EtoilePointsY[3] = this.posY+32;
EtoilePointsY[4] = this.posY+50;
EtoilePointsY[5] = this.posY+25;
EtoilePointsY[6] = this.posY+20;
EtoilePointsY[7] = this.posY+20;
EtoilePointsY[8] = this.posY;
EtoilePointsY[9] = posY+20;
}
public void setForme(String forme) {
this.forme = forme;
}
}
|
Édité
le 09/07/2008 à 11:33:12
par rXp<!>
|