不能使几何形状在Java中移动

时间:2015-10-09 11:09:21

标签: java swing paintcomponent shapes

我是这个网站和Java的新手,所以请宽容。

我正在编写一个程序,允许通过单击按钮绘制不同类型的形状,然后按下另一个按钮移动/停止/重置它们。

我已经完成了我认为最重要的部分(形状正确地创建并存储在一个arraylist中,与重置相同,它清除了屏幕),但我无法弄清楚如何让它们移动。我有一个移动功能,但无法找到一种方法使形状形成arraylist移动​​。任何人都可以给我一点建议。

由于

P.S。如果有错误/错误的编码并需要修复,如果你指出它们,我将不胜感激。

这是我的代码:

MyShape类用于创建不同的形状。

import java.awt.*;
import java.util.Random;

public abstract class MyShape extends Component {

    protected Color color;
    private int x, y, dimX, dimY;
    public Random random = new Random();

    public MyShape(int x, int y, int dimX, int dimY){
        this.x = x;
        this.y = y;
        this.dimX = dimX;
        this.dimY = dimY;
        color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }

    public abstract void draw(Graphics g);

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDimX() {
        return dimX;
    }

    public void setDimX(int dimX) {
        this.dimX = dimX;
    }

    public int getDimY() {
        return dimY;
    }

    public void setDimY(int dimY) {
        this.dimY = dimY;
    }
}

CircleShape - 创建圈子。

import java.awt.*;

public class CircleShape extends MyShape {

    public CircleShape(int x, int y, int dimX, int dimY) {
        super(x, y, dimX, dimY);
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.fillOval(getX(), getY(), getDimX(), getDimY());
    }
}

RectangleShape - 矩形

import java.awt.*;

public class RectangleShape extends MyShape {

    public RectangleShape(int x, int y, int dimX, int dimY) {
        super(x, y, dimX, dimY);
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(getX(), getY(), getDimX(), getDimY());
    }
}

以及处理几乎所有内容的DrawShape类

    public class DrawShapes extends JPanel {

    private JButton addButton, resumeAllButton, stopAllButton, resetButton;
    private final int FRAME_WIDTH = 800;
    private final int FRAME_HEIGHT = 530;
    private int x, y, dimX, dimY;
    private Random random = new Random();
    public List<MyShape> myShapeList = new CopyOnWriteArrayList<MyShape>();
    private Timer timer = null;
    public boolean move = false;

    public DrawShapes() {

        this.setLayout(null);
        addButton = new JButton("Add Shape");
        resumeAllButton = new JButton("Resume Shapes");
        stopAllButton = new JButton("Stop All Shapes");
        resetButton = new JButton("Reset");

        addButton.setBounds(40, 20, 150, 30);
        resumeAllButton.setBounds(230, 20, 150, 30);
        stopAllButton.setBounds(420, 20, 150, 30);
        resetButton.setBounds(610, 20, 150, 30);
        this.add(addButton);
        this.add(resumeAllButton);
        this.add(stopAllButton);
        this.add(resetButton);

        stopAllButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                move = false;
            }
        });

        resumeAllButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                move = true;

            }
        });

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Thread thread = new Thread() {
                    public void run() {
                        init();
                    }
                };
                thread.start();

            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < myShapeList.size(); i++) {
                    myShapeList.clear();
                    repaint();
                }
            }
        });


    }

    public void moveIt() {

        boolean directionUp = random.nextBoolean();
        boolean directionLeft = random.nextBoolean();
        boolean directionDown = !directionUp;
        boolean directionRight = !directionLeft;

        while (move) {
            if (x <= 0) {
                directionRight = true;
                directionLeft = false;
            }
            if (x >= FRAME_WIDTH - dimX) {
                directionRight = false;
                directionLeft = true;
            }
            if (y <= 70) {
                directionUp = false;
                directionDown = true;
            }
            if (y >= FRAME_HEIGHT + 50 - dimY) {
                directionUp = true;
                directionDown = false;
            }
            if (directionUp)
                y--;
            if (directionDown)
                y++;
            if (directionLeft)
                x--;
            if (directionRight)
                x++;
        }
    }

    public void init() {
        x = 0;
        y = 0;
        dimX = (random.nextInt(FRAME_WIDTH) + 100) / 2;
        dimY = (random.nextInt(FRAME_HEIGHT) + 100) / 2;

        while (x <= 0)
            x = (random.nextInt(FRAME_WIDTH) - dimX);
        while (y <= 70)
            y = (random.nextInt(FRAME_HEIGHT) - dimY);
        int choice = 0;
        choice = random.nextInt(2) + 1;
        switch (choice) {
            case 1:
                RectangleShape rectangleShape = new RectangleShape(x, y, dimX, dimY);
                myShapeList.add(rectangleShape);
                timer.start();
                repaint();
                break;
            case 2:
                CircleShape circleShape = new CircleShape(x, y, dimX, dimY);
                myShapeList.add(circleShape);
                repaint();
                break;
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 70, 800, 530);
        for (MyShape aMyShapeList : myShapeList) {
            aMyShapeList.draw(g);

        }

    }

    public static void main(String args[]) {

        JFrame jFrame = new JFrame();
        jFrame.add(new DrawShapes());
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setSize(800, 600);
        jFrame.setResizable(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        jFrame.setLocation(dim.width / 2 - jFrame.getSize().width / 2, dim.height / 2 - jFrame.getSize().height / 2);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);


    }
}

1 个答案:

答案 0 :(得分:1)

从您发布的代码中我可以看到您没有在任何地方调用moveIt()方法。

你对如何移动事物有正确的想法。基本算法是:

  1. 计算新职位
  2. 重绘视图
  3. 我建议您执行以下操作:

    • 您当前正在线程中调用init方法。我不确定这是否需要。删除线程逻辑,只需在主线程上调用该方法。
    • 引入另一个按钮开始实际动画。单击时,创建一个将调用moveIt()方法的线程。