Dav
Dav

Reputation: 3

can't make geometric shapes to move in Java

I'm all new to this site and to Java, so please be lenient.

I'm writing a program that allows to draw different type of shapes with a button click and after hitting another button move/stop/reset them.

I've made already the most part I think (the shapes are correctly creating and storing in an arraylist, the same with the reset, which clear the screen), but I can't figure out how to make them move.I got a function for movement but can't find a way to make the shapes form the arraylist to move. Can anyone give me a little advise.

Thanks

P.S. If there is something wrong/bad coding and needs to be fixed I'll be grateful if you will point at them.

Here is my code:

MyShape class is for creating different shapes.

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 - creating circles.

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 - rectangles

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());
    }
}

and the DrawShape class which handles pretty much everything

    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);


    }
}

Upvotes: 0

Views: 735

Answers (1)

Danail Alexiev
Danail Alexiev

Reputation: 7772

From the code you posted I can see that you are not calling your moveIt() method anywhere.

You have the right idea of how to move things around. The basic algorithm is:

  1. Calculate new positions
  2. Repaint the view

I can recommend you do the following:

  • You are currently calling your init method in a thread. I am not sure this is needed. Remove the thread logic and just call the method on the main thread.
  • Introduce another button tho start the actual animation. When clicking, create a thread that will call your moveIt() method.

Upvotes: 1

Related Questions