如何在Java中按键一次使形状移动?

时间:2016-10-28 18:14:19

标签: java swing shapes

我在大学里有一个任务是画一个椭圆并让它在屏幕上一步一步地按下按钮一次。这是我的代码:

public class Window extends JPanel {

private static Ellipse2D.Double Ellipse;
private JFrame frame;

public Window() {
    super();
    int width = 20;
    int height = 30;
    Ellipse = new Ellipse2D.Double(width, height, 100, 50);        
}

public Dimension getPreferredSize()
{
    return (new Dimension(frame.getWidth(), frame.getHeight()));
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponents(g);
    Graphics2D brush = (Graphics2D) g;
    int width = getWidth();
    int height = getHeight();
    g.clearRect(0, 0, width, height);
    brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    brush.draw(Ellipse);
}
public class MoveCircle implements KeyListener, ActionListener {

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("Working on top!");
        double newX = 0; double newY = 0;
        if (e.getKeyCode() == Event.ENTER) {                
            for (int i = 0; i < 26; i ++)
            {
                System.out.println("Working on bottom!");
                newX = Ellipse.x + 10;
                Ellipse.x = newX;
                newY = Ellipse.y + 10;
                Ellipse.y = newY;   
                repaint();                  
            }                
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}
private void createAndDisplayGUI(Window window)
{
    frame = new JFrame();
    Container container = frame.getContentPane();       
    container.add(window);
    window.addKeyListener(new MoveCircle());        
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    window.requestFocusInWindow();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            Window window = new Window();
            window.createAndDisplayGUI(window);
        }
    });
}

}

虽然我在周期中调用函数repaint(),但每次仍然椭圆从原始点到最后一次迭代中定义的点从根本上移动而不在每次迭代中绘制椭圆的位置。 是否可以使用Swing Timer在每次迭代中重绘椭圆?我很乐意在这方面得到帮助。

1 个答案:

答案 0 :(得分:3)

您有两个问题:第一个是您的循环正在UI线程上运行。第二个是你对重绘工作的误解。所有重绘都会向UI线程添加一个请求进行重新绘制的请求。它本身并没有进行重新绘制。因此,如果您在UI线程上运行操作,多次调用它将不起作用。正如你的建议,你可以使用Swing Timer解决这个问题,因为我已经把它放在了

之下
ActionListener al = new ActionListener() {
  int iterations = 0;
  public void actionPerformed(ActionEvent ae) {
           if (iterations == 25) {
             timer.stop();
           }
           interations++;
           System.out.println("Working on bottom!");
            newX = Ellipse.x + 10;
            Ellipse.x = newX;
            newY = Ellipse.y + 10;
            Ellipse.y = newY;   
            repaint();                  
  }
};

final timer = new javax.swing.Timer(delay, al);
timer.start();