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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
public class ListenerVertC 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;
static List<Point> tabcp = new ArrayList<>();
private List<Point> tabcq;
int cppd = -16711936;
public ListenerVertC(BufferedImage image, List<Point> tabcq) {
this.image=image;
this.tabcq=tabcq;
Graphics g = (Graphics)image.getGraphics();
Graphics2D g2 = image.createGraphics();
System.out.println("HelloV");
tabcp.clear();
eerc(g2);
for(int i=0; i<tabcp.size(); i++) {
System.out.print(tabcp.get(i).x+" "+tabcp.get(i).y+" ");
}
for(int i=0; i<tabcq.size(); i++) {
System.out.print(tabcq.get(i).x+" "+tabcq.get(i).y+" ");
}
System.out.println();
}
public void paintComponent(Graphics g) {
g.drawImage(image,0,0,null);
}
public void eerc(Graphics2D g2) {
Graphics g = (Graphics)image.getGraphics();
tabcp.add(new Point(xp1,xp2));
tabcq.add(new Point(xp1,xp2));
Shape rectangledepart = new Rectangle2D.Float(xp1,xp2,25,25);
Shape rectangleEntier = new Rectangle2D.Float(0,0,706,600);
Area outside = new Area(rectangleEntier);
outside.subtract(new Area(rectangledepart));
Color cpp= new Color(image.getRGB(xp1, xp2));
int pr=cpp.getRed();
int pg=cpp.getGreen();
int pb=cpp.getBlue();
@SuppressWarnings("unused")
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));
tabcq.add(new Point(xg1, xg2));
Shape rectangleg = new Rectangle2D.Float(xg1,xg2,24,24);
outside = OutsideRect((Rectangle2D) rectangleg, outside);
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));
tabcq.add(new Point(xh1, xh2));
Shape rectangleh = new Rectangle2D.Float(xh1,xh2,24,24);
outside = OutsideRect((Rectangle2D) rectangleh, outside);
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));
tabcq.add(new Point(xb1, xb2));
Shape rectangleb = new Rectangle2D.Float(xb1,xb2,24,24);
outside = OutsideRect((Rectangle2D) rectangleb, outside);
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));
tabcq.add(new Point(xd1, xd2));
Shape rectangled = new Rectangle2D.Float(xd1,xd2,24,24);
outside = OutsideRect((Rectangle2D) rectangled, outside);
xpd1+=25;
xd1+=25;
cpd1=cpd11;
}
g2.setClip(outside);
BufferedImage image2 = createImage(g2, tabcq);
}
private Area OutsideRect(Rectangle2D r, Area c) {
c.subtract(new Area(r));
return c;
}
public static void paintAleat(Graphics2D g2, 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) {
g2.setColor(Color.blue);
}
if (nr==2) {
g2.setColor(Color.red);
}
if (nr==3) {
g2.setColor(Color.yellow);
}
if (nr==4) {
g2.setColor(Color.green);
}
if (nr==5) {
g2.setColor(new Color( 213, 128, 0));
}
g2.drawRect(x2, y2, x3, y3);
g2.fillRect(x2, y2, x3, y3);
y2+=25;
}
}
private static BufferedImage createImage(Graphics2D g2, List<Point> tabcq) {
BufferedImage image = new BufferedImage(706, 750, BufferedImage.TYPE_INT_RGB);
try {
int x4=1,y4=1,x5=1;
Point position = new Point(x4,y4);
while (x5<=706) {
if (position != tabcp && position != tabcq)
paintAleat(g2, x4, y4, 23, 23);
x4+=25;
x5+=25;
}
for(int i=0; i<tabcp.size(); i++) {
g2.setColor(Color.GREEN);
g2.drawRect(tabcp.get(i).x, tabcp.get(i).y, 23, 23);
}
} finally {
g2.dispose();
}
return image;
}
} |
Partager