gpt4 book ai didi

java - 如何删除刚刚通过鼠标单击创建的对象? (创建另一个时)

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:49 24 4
gpt4 key购买 nike

我一直在寻找答案和所有内容,但找不到解决我的问题的方法。我希望在创建新圆圈时可以删除上一个圆圈(通过鼠标单击创建)?

提前非常感谢您。 :)

package javaapplication1;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class CercuriRandom extends Applet implements MouseListener{

int x,y,z,r,v,a;
Thread t;
Color culoare;
Random rand;

@Override
public void init(){
t=new Thread();
rand= new Random();
culoare=new Color(r, v, a);
addMouseListener(this);
}

@Override
public void update(Graphics g){
g.setColor(culoare);
g.fillRoundRect(x, y, z, z, z, z);
try {
Thread.sleep(25);
} catch (InterruptedException e) { }
//repaint();
}

@Override
public void mouseClicked(MouseEvent me) {
if (me.getButton()==MouseEvent.BUTTON1) {
r=rand.nextInt(256);
v=rand.nextInt(256);
a=rand.nextInt(256);
culoare=new Color(r,v,a);
x=rand.nextInt(getWidth());
y=rand.nextInt(getWidth());
z=rand.nextInt(100);
repaint();
}
}

@Override
public void mousePressed(MouseEvent me) {

}

@Override
public void mouseReleased(MouseEvent me) {

}

@Override
public void mouseEntered(MouseEvent me) {

}

@Override
public void mouseExited(MouseEvent me) {

}

}

我将在这里留下创建圆圈所需的代码。我一直在寻找答案和所有内容,但找不到解决我的问题的方法。我希望在创建新圆圈时可以删除上一个圆圈(通过鼠标单击创建)?

提前非常感谢您。 :)

最佳答案

您可以通过将最后绘制的圆的值存储为类变量来引用圆:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class CercuriRandom extends Applet implements MouseListener {

int x, y, z, r, v, a;
// class attributes that store the last circle that was drawn
int lastX, lastY, lastZ;
Thread t;
Color culoare;
Random rand;

@Override
public void init() {
t = new Thread();
rand = new Random();
culoare = new Color(r, v, a);
addMouseListener(this);
}

@Override
public void update(Graphics g){
// clear the last drawn rectangle (before painting the new one)
g.clearRect(lastX, lastY, lastZ, lastZ);

g.setColor(culoare);
g.fillRoundRect(x, y, z, z, z, z);

// store the last values in order to delete them on next draw
lastX = x;
lastY = y;
lastZ = z;
try {
Thread.sleep(25);
} catch (InterruptedException e) { }
//repaint();
}

@Override
public void mouseClicked(MouseEvent me) {
if (me.getButton() == MouseEvent.BUTTON1) {
r = rand.nextInt(256);
v = rand.nextInt(256);
a = rand.nextInt(256);
culoare = new Color(r, v, a);
x = rand.nextInt(getWidth());
y = rand.nextInt(getWidth());
z = rand.nextInt(100);
repaint();
}
}

@Override
public void mousePressed(MouseEvent me) {

}

@Override
public void mouseReleased(MouseEvent me) {

}

@Override
public void mouseEntered(MouseEvent me) {

}

@Override
public void mouseExited(MouseEvent me) {

}

}

关于java - 如何删除刚刚通过鼠标单击创建的对象? (创建另一个时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51193785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com