如何使形状移动以在JPanel中单击?

时间:2018-01-10 15:50:40

标签: java swing timer jpanel shape

我正试图让形状移动到我点击鼠标的任何地方。我得到了移动的形状,它朝着我点击的方向移动,但它从未完全移动到我点击的位置,它错过了很好的距离。由于它没有完全移动到我点击的位置,因此形状继续移动并且不会停止。

以下是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

public class GraphicsTester extends JFrame implements MouseListener {
    private int startX;
    private int startY;
    private int endX;
    private int endY;
    private int stepX;
    private int stepY;
    private Timer sliderTimer = new Timer(40, new SlideListener());

    public GraphicsTester() throws IOException {
        setTitle("Graphics Tester 1");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel basePanel = new JPanel(new BorderLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(Color.red);
                g2d.fillOval(215 + stepX, 215 + stepY, 30, 30);
                startX = stepX;
                startY = stepY;
                if (215 + stepX == endX && 215 + stepY == endY) {
                    sliderTimer.stop();
                }
            }
        };
        basePanel.setPreferredSize(new Dimension(500, 500));
        basePanel.addMouseListener(this);

        getContentPane().add(basePanel);
        pack();
        setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new GraphicsTester();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        endX = e.getX();
        endY = e.getY();
        System.out.println("endX: " + endX + ", endY: " + endY);
        sliderTimer.start();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    private class SlideListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            stepX += (endX - startX) / 5; // the 5's are just arbitrary values, I chose 5 because it makes the shape slide at the speed I want
            stepY += (endY - startY) / 5;
            System.out.println("stepX: " + (215 + stepX) + ", stepY: " + (215 + stepY));
            repaint();
        }
    }
}

如何让形状移动到我点击的位置?问题在于stepX += (endX - startX) / 5;stepY += (endY - startY) / 5;。这两行导致形状不能完全移动到我点击的位置,因为它们不会将stepXstepY精确地增加到点击,而是“#34; round"在每个增量。我不知道如何修复这两行,以便将stepXstepY精确地增加到我点击的位置,因此一旦到达endX和{{{{}} 1}}。

1 个答案:

答案 0 :(得分:0)

您的主要问题是,您在215方法中将stepX添加到stepYpaintComponent(),这就是为什么它似乎是“离开”的原因。这是因为当您真正希望它以stepX + 215stepY + 215(基于点击时的鼠标指针)绘制时,它会在stepXstepY处绘制)。据推测,+ 215只是初始位置的偏移量?

简单的解决方法是将您的paintComponent()方法更改为:

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.RED);
    g2d.fillOval(stepX, stepY, 30, 30);
    startX = stepX;
    startY = stepY;
    if (stepX == endX && stepY == endY) {
        sliderTimer.stop();
    }
}

或者只是与所有坐标的偏移量一致。

其次,你的slideTimer.stop()极不可能被召唤。如果以下情况属实,您应设置stepX = endXstepY = endYend - step < 5,即

stepX = endX - startX < 5 ? endX : stepX + (endX - startX) / 5;
stepY = endY - startY < 5 ? endY : stepY + (endY - startY) / 5;

这可能无法让它达到最佳效果,所以请随意尝试。