[英]Can't figure out how to get rid of a graphic when I figure hit a button
我试图做到这一点,所以当您按下按钮时,您会破坏某些东西,但是我无法让敌人消失(红色方块)。这是图形的代码:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D P1 = (new Ellipse2D.Double(x, y, 40, 40));
Rectangle2D P2 = (new Rectangle2D.Double(x2, y2, 40, 40));
g2.fill(P1);
g2.setPaint(Color.RED);
g2.fill(P2);
}
使用这种键绑定方法,如何使Rectangle2D消失?
public void actionPerformed(ActionEvent e) {
repaint();
x += velx;
y += vely;
x2 += velx2;
y2 += vely2;
}
public void up() {
vely = -2;
velx = 0;
}
public void down() {
vely = 2;
velx = 0;
}
public void left() {
vely = 0;
velx = -2;
}
public void right() {
vely = 0;
velx = 2;
}
public void stop() {
vely = 0;
velx = 0;
}
public void stop2() {
vely2 = 0;
velx2 = 0;
}
public void up2() {
vely2 = -2;
velx2 = 0;
}
public void down2() {
vely2 = 2;
velx2 = 0;
}
public void left2() {
vely2 = 0;
velx2 = -2;
}
public void right2() {
vely2 = 0;
velx2 = 2;
}
public void attack() {
if (x <= x2+100 && y <= y2+100){
}
}
//public void attack() {
//
//}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
up();
}
if(code == KeyEvent.VK_DOWN){
down();
}
if(code == KeyEvent.VK_LEFT){
left();
}
if(code == KeyEvent.VK_RIGHT){
right();
}
if(code == KeyEvent.VK_W){
up2();
}
if(code == KeyEvent.VK_S){
down2();
}
if(code == KeyEvent.VK_A){
left2();
}
if(code == KeyEvent.VK_D){
right2();
}
if(code == KeyEvent.VK_P){
stop();
}
if(code == KeyEvent.VK_O){
stop2();
}
if(code == KeyEvent.VK_Z){
attack();
}
}
与其创建要在paintComponent
方法本身内绘制的对象,不如维护要渲染的对象的集合并在每个paintComponent
调用中遍历该集合。 然后只需从集合中删除即可“销毁” P2对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.