我需要制作一个“清除”按钮来删除屏幕和阵列中的所有图像。我目前正在使用EZ.removeAllEZElements();
,但这清除了一切。甚至贴纸调色板和背景(画布)。如何从阵列和画布中删除所有放置的图片?
这是我的代码
sticker [] arraysticker = new sticker [80];
int image = 0;
int flag = 0;
else if (backgroundPicture.isPointInElement(clickX, clickY) && !savePicture.isPointInElement(clickX, clickY)
&& !loadPicture.isPointInElement(clickX, clickY) && !clearPicture.isPointInElement(clickX, clickY)
&& !rectanglePicture.isPointInElement(clickX, clickY)){
if (flag == 1){ //if hatSoundPlay is true
sticker sticker1 = new sticker();//creates a new sticker1
sticker1.sticker11("hat.png", clickX, clickY);//places sticker
arraysticker[image] = sticker1;//puts sticker 1 into the array
image ++;//increments image by 1
hatsound.play(); //and a hatsound will play
然后用if (flag == 2){
然后3然后再重复3次。每个标志都是一个新贴纸。
这是我清晰的代码。
else if (clearPicture.isPointInElement(clickX, clickY)){
EZ.removeAllEZElements();
//make a clear array
EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2);
EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7);
EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6);
EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6);
EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6);
EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6);
EZ.addImage("save.png", 19* EZ.getWindowWidth() / 20, 3* EZ.getWindowHeight () / 9);
EZ.addImage("load.png", 19* EZ.getWindowWidth() / 20, 4* EZ.getWindowHeight () / 9);
EZ.addImage("clear.png", 19* EZ.getWindowWidth() / 20, 5* EZ.getWindowHeight () / 9);
}
我正在清除所有图像并替换原始图像。问题是,带坐标的图像仍然存储在数组中。明确需要清除画布和数组。
这是我的整个代码。只删除EZ.addImage和EZ.addSound代码。
public class main {
public static void main(String[] args) throws IOException {
EZ.initialize(1644,1022);//sets page size
sticker [] arraysticker = new sticker [80];//creates an array to hold 20 sticker
int image = 0;//declares an integer variable named image
int flag = 0;//declares in integer variable named flag and set it to 0
while (true){//while it is true
if (EZInteraction.wasMouseLeftButtonReleased()){//if left button is released
int clickX = EZInteraction.getXMouse(); //get x coordinates
int clickY = EZInteraction.getYMouse();//get y coordinates
if (hatPicture.isPointInElement(clickX, clickY) && rectanglePicture.isPointInElement(clickX, clickY)){//if hat is in the x and y coordinates
hatsound.play(); //hatsound will play
flag = 1; //set flag to 1
}else if(bluntPicture.isPointInElement(clickX, clickY) && rectanglePicture.isPointInElement(clickX, clickY)){//if blunt is in the x and y coordinates
bluntsound.play(); //then bluntsound will play
flag = 2;//set flag to 2
}else if(dealwithitPicture.isPointInElement(clickX, clickY) && rectanglePicture.isPointInElement(clickX, clickY)){//if dealwithit is in the x and y coordinates
dealwithitsound.play(); //then dealwithitsound will pay
flag = 3;//set flag to 3
}else if(weedPicture.isPointInElement(clickX, clickY) && rectanglePicture.isPointInElement(clickX, clickY)){//if weed is in the x and y coordinates
weedsound.play(); //then weedsound will play
flag = 4;//set flag to 4
}
else if (backgroundPicture.isPointInElement(clickX, clickY) && !savePicture.isPointInElement(clickX, clickY)
&& !loadPicture.isPointInElement(clickX, clickY) && !clearPicture.isPointInElement(clickX, clickY)
&& !rectanglePicture.isPointInElement(clickX, clickY)){
if (flag == 1){ //if hatSoundPlay is true
sticker sticker1 = new sticker();//creates a new sticker1
sticker1.sticker11("hat.png", clickX, clickY);//places sticker
arraysticker[image] = sticker1;//puts sticker 1 into the array
image ++;//increments image by 1
hatsound.play(); //and a hatsound will play
}else if (flag == 2){ //else if bluntSound is true
sticker sticker2 = new sticker();//creates new sticker2
sticker2.sticker11("blunt.png", clickX, clickY);//places sticker
arraysticker[image] = sticker2;//puts sticker 2 into the array
image ++;//increments image by 1
bluntsound.play(); //and blunt sound will play
}else if (flag == 3){ //if dealwithitsound is true
sticker sticker3 = new sticker();//creates new sticker3
sticker3.sticker11("dealwithit.png", clickX, clickY);//places sticker
arraysticker[image] = sticker3;//puts sticker 3 into the array
image ++;//increments image by 1
dealwithitsound.play(); //dealwithitsound will play
}else if (flag == 4){ //if weedsound is true
sticker sticker4 = new sticker();//create new sticker4
sticker4.sticker11("weed.png", clickX, clickY);//places sticker
arraysticker[image] = sticker4;//puts sticker 4 into the array
image ++;//increments image by 1
weedsound.play(); //weedsound will also play
}
}
else if (savePicture.isPointInElement(clickX, clickY)){
//clear array
FileWriter fw = new FileWriter("save.txt");//creates new file writer that writes to save.txt
for (int i = 0; i < image; i++){//requirements
fw.write(arraysticker[i].imagename + " " + arraysticker[i].x + " " + arraysticker[i].y + "\n"); //if requirements are met write
}
fw.close();//close writer
System.out.println("saved");//print out saved to check to see if saved
}
else if (loadPicture.isPointInElement(clickX, clickY)){
Scanner sc = new Scanner (new File ("save.txt"));//creates a new scanner that reads from save.txt
EZ.removeAllEZElements();//removes all elements
//clear array
EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2);
EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7);
EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6);
EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6);
EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6);
EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6);
EZ.addImage("save.png", 19* EZ.getWindowWidth() / 20, 3* EZ.getWindowHeight () / 9);
EZ.addImage("load.png", 19* EZ.getWindowWidth() / 20, 4* EZ.getWindowHeight () / 9);
EZ.addImage("clear.png", 19* EZ.getWindowWidth() / 20, 5* EZ.getWindowHeight () / 9);
while (sc.hasNext()){
String word = sc.next();//reads next word
int x = sc.nextInt();//reads next integer
int y = sc.nextInt();//reads next integer
switch (word){//switch commang
case "hat.png"://if starting word is hat.png
EZ.addImage("hat.png", x, y);
break;//breaks out of switch statement
case "blunt.png"://if starting word is blunt.png
EZ.addImage("blunt.png", x, y);
break;//breaks out of switch statement
case "dealwithit.png"://if starting word is dealwithit.png
EZ.addImage("dealwithit.png", x, y);
break;//breaks out of switch statement
case "weed.png"://if starting word is weed.png
EZ.addImage("weed.png", x, y);
break;//breaks out of switch statement
}
}
System.out.println("loaded");//for checking
}
else if (clearPicture.isPointInElement(clickX, clickY)){
EZ.removeAllEZElements();
//make a clear array
EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2);
EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7);
EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6);
EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6);
EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6);
EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6);
EZ.addImage("save.png", 19* EZ.getWindowWidth() / 20, 3* EZ.getWindowHeight () / 9);
EZ.addImage("load.png", 19* EZ.getWindowWidth() / 20, 4* EZ.getWindowHeight () / 9);
EZ.addImage("clear.png", 19* EZ.getWindowWidth() / 20, 5* EZ.getWindowHeight () / 9);
}
/*
else if (hatPicture.isPointInElement(clickX, clickY) && !rectanglePicture.isPointInElement(clickX, clickY)){
//EZImage
}
else if (bluntPicture.isPointInElement(clickX, clickY) && !rectanglePicture.isPointInElement(clickX, clickY)){
//translate
}
else if (dealwithitPicture.isPointInElement(clickX, clickY) && !rectanglePicture.isPointInElement(clickX, clickY)){
//translate
}
else if (weedPicture.isPointInElement(clickX, clickY) && !rectanglePicture.isPointInElement(clickX, clickY)){
//translate
}
}
=============================RIGHT CLICK DELETE=============================
if (EZInteraction.wasMouseRightButtonReleased()){
int clickX = EZInteraction.getXMouse();
int clickY = EZInteraction.getYMouse();
if (hatPicture.isPointInElement(clickX, clickY)){
}
}*/
}
EZ.refreshScreen();
}
}
}
这是我的贴纸类
public class sticker {
public String name;
public int x, y;
public EZImage image;
public String imagename;
public sticker(){
x = 0;
y = 0;
name = null;
image = null;
}
void sticker11(String name, int X, int Y){
image = EZ.addImage(name, X, Y);
x = X;
y = Y;
imagename = name;
}
}