我一直在寻找答案和所有答案,但找不到解决我问题的方法。我希望我可以在创建新圆圈时删除上一个圆圈(通过鼠标单击创建)吗?
非常感谢您。 :)
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) {
}
}
我将在此处保留创建圆所需的代码。 我一直在寻找答案和所有答案,但找不到解决我问题的方法。我希望我可以在创建新圆圈时删除上一个圆圈(通过鼠标单击创建)吗?
非常感谢您。 :)
答案 0 :(得分:0)
您可以通过将最后绘制的圆的值存储为类变量来引用圆:
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) {
}
}