Bonjour,
J'ai voulu gérer chaque pixel dans une fenêtre. J'ai donc crée un tableau de pixels qui est égale à la résolution de ma fenêtre. Et dans chaque pixel, il y a 2 entiers pour la position et 4 entiers pour la gestion de la couleur.
Lorsque j'éxécute, seulement quelques pixels au hasard s'allument dans la fenêtre.
Ce qui est bizarre, c'est que quand je fais un threads sleep apres le repaint, à ce moment là tous les pixels sont coloriés.
Y a t il un moyens de gérer tous les pixels de façon fluide et pourquoi cette erreur.
La classe Fenêtre:
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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 import javax.swing.JFrame; public class Fenetre extends JFrame{ int n; int m; Panneau pan; Pixel []tab; public Fenetre(int n, int m){ this.n =n; this.m =m; int k =0; this.tab = new Pixel[n*m]; for(int i =0; i<n;i++){ for(int j =0; j<m; j++){ tab[k] = new Pixel(i,j , 255, 0, 0, 100); k++; } } pan = new Panneau(n,m, tab[0]); this.setSize(n, m); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setContentPane(pan); this.setVisible(true); go(); } private void go(){ Pixel tmp; for(int i =1; i<n*m;i++){ tmp = tab[i]; pan.setP(tmp); pan.repaint(); try{ Thread.sleep(1);//Si on enlève cette partie, on a un nuage de point } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(i); } } }
La classe Panneau:
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.awt.*; import javax.swing.*; public class Panneau extends JPanel { int n,m; Pixel p; Panneau(int n, int m, Pixel p){ this.n =n; this.m =m; this.p = p; } public void paintComponent(Graphics g){ Color c = new Color(p.getR(),p.getG(),p.getB(),p.getA()); g.setColor(c); g.drawRect(p.getX(),p.getY(), 1, 1 ); System.out.println(">>>>>"+p.getX()+" " +p.getY()); } void setP(Pixel p){ this.p =p; } }
La classe Pixel:
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class Pixel { int x,y,r,g,b,a; Pixel(int x, int y, int r,int g, int b, int a){ this.x =x; this.y =y; this.r =r; this.g =g; this.b =b; this.a =a; } 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 int getR() { return r; } public void setR(int r) { this.r = r; } public int getG() { return g; } public void setG(int g) { this.g = g; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getA() { return a; } public void setA(int a) { this.a = a; } }
Merci.
Partager