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
| import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
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;
List<Point> tabcp = new ArrayList<>();
Shape rectangleEntier = new Rectangle2D.Float(0,0,706,600);
int cppd = -16711936;
public ListenerVertC(BufferedImage image) {
this.image=image;
System.out.println("HelloV");
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 void eerc() {
Graphics g = (Graphics)image.getGraphics();
final Graphics2D g2 = image.createGraphics();
tabcp.add(new Point(xp1,xp2));
Shape rectangle = new Rectangle2D.Float(xp1,xp2,25,25);
Area outsidep = OutsideRect((Rectangle2D) rectangle);
g2.setClip(outsidep);
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));
Shape rectangleg = new Rectangle2D.Float(xg1,xg2,24,24);
Area outsideg = OutsideRect((Rectangle2D) rectangleg);
//g2.setClip(outsideg);
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));
Shape rectangleh = new Rectangle2D.Float(xh1,xh2,24,24);
Area outsideh = OutsideRect((Rectangle2D) rectangleh);
//g2.setClip(outsideh);
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));
Shape rectangleb = new Rectangle2D.Float(xb1,xb2,24,24);
Area outsideb = OutsideRect((Rectangle2D) rectangleb);
//g2.setClip(outsideb);
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));
Shape rectangled = new Rectangle2D.Float(xd1,xd2,24,24);
Area outsided = OutsideRect((Rectangle2D) rectangled);
//g2.setClip(outsided);
xpd1+=25;
xd1+=25;
cpd1=cpd11;
}
}
private Area OutsideRect(Rectangle2D r) {
Area outside = new Area(rectangleEntier);
outside.subtract(new Area(r));
return outside;
}
} |
Partager