1

我需要绘制一个矩形、圆形和线条,然后为它们设置动画(例如按左->它(所有对象)向左移动。)

我画这样的物体

class MyCanvas extends JComponent {

int x = 10;
int y = 10;
public MyCanvas()
{
    Action someaction = new AbstractAction(){
        @Override
        public void actionPerformed(ActionEvent e) {
            x+=30;
            //revalidate();
            repaint();
        }
    };
    this.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), someaction);
}        
@Override
public void paintComponent(Graphics g) {
g.drawRect(x,y, 200, 200); 
g.drawOval(x, y, 50, 50);
g.drawLine(x, y, 50, 30);
  }
}

但它不动。

4

1 回答 1

2
  1. 用于paintComponent(Graphics g)SwingJComponents而不是paint(Graphics g)

  2. 用于 Swing KeyBindings_KeyEventsJComponents

  3. 将对象(之前准备paintComponent(Graphics g))放入数组,从数组中绘制元素paintComponent(Graphics g)

于 2012-06-11T20:13:28.600 回答