java中的drawLine方法怎么重新画线的时候保留刚才画的线?帮我看看下面的程序该怎么改才能实现?
staticclassgetPanelextendsJPanel{privateintx=200;privateinty=200;privateintx2=x;priva...
static class getPanel extends JPanel {
private int x = 200;
private int y = 200;
private int x2 = x ;
private int y2 = y ;
public getPanel() {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
int xtemp;
int ytemp;
switch (arg0.getKeyCode()) {
case KeyEvent.VK_UP:
xtemp=x2;
ytemp=y2;
x=xtemp;
y=ytemp;
x2=x;
y2-=50;
break;
case KeyEvent.VK_DOWN:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 = x;
y2 += 50;
break;
case KeyEvent.VK_LEFT:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 -= 50;
y2 = y;
break;
case KeyEvent.VK_RIGHT:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 += 50;
y2 = y;
break;
default:
break;
}
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(x, y, x2, y2);
}
}
} 展开
private int x = 200;
private int y = 200;
private int x2 = x ;
private int y2 = y ;
public getPanel() {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
int xtemp;
int ytemp;
switch (arg0.getKeyCode()) {
case KeyEvent.VK_UP:
xtemp=x2;
ytemp=y2;
x=xtemp;
y=ytemp;
x2=x;
y2-=50;
break;
case KeyEvent.VK_DOWN:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 = x;
y2 += 50;
break;
case KeyEvent.VK_LEFT:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 -= 50;
y2 = y;
break;
case KeyEvent.VK_RIGHT:
xtemp = x2;
ytemp = y2;
x = xtemp;
y = ytemp;
x2 += 50;
y2 = y;
break;
default:
break;
}
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(x, y, x2, y2);
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询