Dans le cadre d'un projet pour la spécialité ISN en terminale, je me suis lancé dans la programmation d'un jeu avec une interface graphique:
Nom : Colonisation interface finale.jpg
Affichages : 301
Taille : 169,5 Ko

J'ai crée des actionListener pour chaque boutons dans lesquels j'ai des actions de dessins sur la bufferedImage. J'aimerais maintenant faire en sorte de re-générer la bufferedImage que j'ai généré (les cases colorées donc) de manière aléatoire plus tôt (via la class Cases), mais seulement sur certaines parties de l'image. Existe t-il une méthode permettant de générer une BufferedImage en excluant certaines parties de l'image ?

Code : 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
68
69
70
71
import javax.swing.*;
 
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
 
 
 
@SuppressWarnings("serial")
 
public class Cases extends JPanel {
 
	final static BufferedImage image = 
            new BufferedImage(706, 750, BufferedImage.TYPE_INT_RGB);
			Graphics g = (Graphics)image.createGraphics();
 
		  int x1=0,y1=0,x4=1,y4=1,x5=1;
 
	public Cases() {
			  g.setColor(Color.BLACK);
			  while (x1<=706) {
			  g.drawLine(x1, 0, x1, 600);
			  x1+=25;
			  }
			  while (y1<=600) {
			  g.drawLine(0, y1, 707, y1);
			  y1+=25;
			  }
			  while (x5<=706) {
			  paintAleat(g, x4, y4, 23, 23);
			  x4+=25;
			  x5+=25;
			  }
			  g.dispose();
}
	public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
}
 
	   public static BufferedImage getImage() {
	       return image;
	   }
 
 
	public static void paintAleat(Graphics g, int x2, int y2, int x3, int y3) {
		while (y2<600) {
			Random r = new Random();
			int valeurMin = 1, valeurMax = 6;
			int nr = valeurMin + r.nextInt(valeurMax - valeurMin);
			if (nr==1) {
				g.setColor(Color.blue);
			}
			if (nr==2) {
				g.setColor(Color.red);
			}
			if (nr==3) {
				g.setColor(Color.yellow);
			}
			if (nr==4) {
				g.setColor(Color.green);
			}
			if (nr==5) {
				g.setColor(new Color( 213, 128, 0));
			}
	    	g.drawRect(x2, y2, x3, y3);
	    	g.fillRect(x2, y2, x3, y3);
	    	y2+=25;
		}
    }	
}
Code : 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
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
import javax.swing.*;
import java.awt.Point;
import java.io.*;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import java.awt.image.BufferedImage;
 
public class ListenerBleuC extends JPanel{
 
    private final BufferedImage image;
	  int xp1=1, xp2=1;
	  int xpg1=1, xph1=1, xpb1=1, xpd1=1, xpg2=1, xpd2=1, xph2=1, xpb2=1;
	  int xg1=xpg1-25, xg2=xpg2;
	  int xd1=xpd1+25, xd2=xpd2;
	  int xh1=xph1, xh2=xph2-25;
	  int xb1=xpb1, xb2=xpb2+25;
      List<Point> tabcp = new ArrayList<>();
	  int cppd = -16776961;
 
    public ListenerBleuC(BufferedImage image) {
        this.image=image;
        System.out.println("HelloB");
        tabcp.clear();
        eerc();
        for(int i=0; i<tabcp.size(); i++) {
            System.out.print(tabcp.get(i).x+" "+tabcp.get(i).y+" ");       
        }
        System.out.println();
	}
 
	public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
	}
 
	public static int detectB; {
 
	}
 
	public void eerc() {
        Graphics g = (Graphics)image.getGraphics();
        tabcp.add(new Point(xp1,xp2));
 
        Color cpp= new Color(image.getRGB(xp1, xp2));
		 int pr=cpp.getRed();
	     int pg=cpp.getGreen();
	     int pb=cpp.getBlue();
		 int cpp1 = new Color(pr,pg,pb).getRGB();
 
		 if (xg1 >=0) {
			 Color cpg = new Color(image.getRGB(xg1, xg2));
			 int pr2=cpg.getRed();
			 int pg2=cpg.getGreen();
			 int pb2=cpg.getBlue();
			 int cpg1 = new Color(pr2,pg2,pb2).getRGB();
 
		 while (cpg1 == cppd) {
		 Color cpgg = new Color(image.getRGB(xg1, xg2));
		 int pr22=cpgg.getRed();
		 int pg22=cpgg.getGreen();
		 int pb22=cpgg.getBlue();
		 int cpg11 = new Color(pr22,pg22,pb22).getRGB();
		 if (cpg11 != cppd) {
			 break;
		 }
	 	 g.setColor(new Color(pr2,pg2,pb2));
	 	 g.fillRect(xpg1, xpg2, 24, 24);
	 	 g.fillRect(xg1, xg2, 24, 24);
		 tabcp.add(new Point(xg1, xg2));
	 	 xpg1-=25;
	 	 xg1-=25;
	 	 cpg1=cpg11;
		 }
		 }
 
		 if (xh2 >=0) {
			 Color cph = new Color(image.getRGB(xh1, xh2));
			 int pr3=cph.getRed();
			 int pg3=cph.getGreen();
			 int pb3=cph.getBlue();
			 int cph1 = new Color(pr3,pg3,pb3).getRGB();
 
			 while (cph1 ==cppd) {
				 Color cphh = new Color(image.getRGB(xh1,xh2));
				 int pr33=cphh.getRed();
				 int pg33=cphh.getGreen();
				 int pb33=cphh.getBlue();
				 int cph11 = new Color(pr33,pg33,pb33).getRGB();
 
				 if (cph11 != cppd) {
					 break;
				 }
			 		g.setColor(new Color(pr3,pg3,pb3));
			 		g.fillRect(xph1, xph2, 24, 24);
			 		g.fillRect(xh1, xh2, 24, 24);
					tabcp.add(new Point(xh1, xh2));
			 		xph2-=25;
			 		xh2-=25;
			 		cph1=cph11;
			 }
		 }
 
		 if (xb2 >=0) {
			 Color cpb = new Color(image.getRGB(xb1, xb2));
			 int pr4=cpb.getRed();
			 int pg4=cpb.getGreen();
			 int pb4=cpb.getBlue();
			 int cpb1 = new Color(pr4,pg4,pb4).getRGB();
 
			 while (cpb1== cppd) {
				 Color cpbb = new Color(image.getRGB(xb1, xb2));
				 int pr44=cpbb.getRed();
				 int pg44=cpbb.getGreen();
				 int pb44=cpbb.getBlue();
				 int cpb11 = new Color(pr44,pg44,pb44).getRGB();
 
				 if (cpb11 != cppd) {
					 break;
				 }
					 g.setColor(new Color(pr4,pg4,pb4));
					 g.fillRect(xpb1, xpb2, 24, 24);
					 g.fillRect(xb1, xb2, 24, 24);
					 tabcp.add(new Point(xb1, xb2));
					 xpb2+=25;
					 xb2+=25;
					 cpb1=cpb11;
			 }
		 }
 
		 Color cpd = new Color(image.getRGB(xd1, xd2));
		 int pr1=cpd.getRed();
	     int pg1=cpd.getGreen();
	     int pb1=cpd.getBlue();
		 int cpd1 = new Color(pr1,pg1,pb1).getRGB();
 
		 while (cpd1==cppd) {
			 Color cpdd = new Color(image.getRGB(xd1, xd2));
			 int pr11=cpdd.getRed();
		     int pg11=cpdd.getGreen();
		     int pb11=cpdd.getBlue();
			 int cpd11 = new Color(pr11,pg11,pb11).getRGB();
 
			 if (cpd11 != cppd) {
				 break;
			 }
				 g.setColor(new Color(pr1,pg1,pb1));
				 g.fillRect(xp1, xpd2, 24, 24);
				 g.fillRect(xd1, xd2, 24, 24);
				 tabcp.add(new Point(xd1, xd2));
				 xpd1+=25;
				 xd1+=25;
				 cpd1=cpd11;
		 }
	}
 
}
Merci d'avance.