首先给出实例:
再给出代码:
import acm.graphics.*;
import acm.program.*;
import acm.util.RandomGenerator;
public class Beeperline extends GraphicsProgram { //图片重叠
public void run(){
GImage g1 = new GImage("C:\\Users\\Administrator\\Pictures\\greenBackground.jpg");
GImage g2 = new GImage("C:\\Users\\Administrator\\Pictures\\greenForeground.jpg");
int[][] p1 = g1.getPixelArray();
int[][] p2 = g2.getPixelArray();
for(int i=0; i<p2.length; i++) {
for(int j=0;j<p2[i].length;j++) {
int red = (p2[i][j] & 0xff0000) >> 16;
int green = (p2[i][j]& 0xff00) >> 8;
int blue = (p2[i][j]& 0xff);
if(green-red>30 && green-blue>30) p2[i][j]=p1[i][j];
}
}
g2 = new GImage(p2);
add(g2,0,0);
RandomGenerator a=new RandomGenerator();
println(a.nextInt(0,p2.length-1));
println(a.nextInt(0,p2[0].length-1));
}
}