In my program I need to create a circle that moves around in its panel that bounces off the sides in a realistic manner based off of physics. My program seems to work fine until the circle begins to move in a direction it has already moved in before. When this happens the circle starts moving in an erratic way and exits the panel. Also this only happens when it hits the X plane sides, not the top or bottom. Here's my code so far:
//panel import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MovingCirclePanel extends JPanel { private Circle circle, circle2; private javax.swing.Timer timer; private boolean move = true; public MovingCirclePanel(Color backColor, int width, int height) { setBackground(backColor); setPreferredSize(new Dimension(width, height)); circle = new Circle(width / 2, height / 2, 25, Color.red); circle.setDirection(300); circle.setVelocity(5); circle2 = new Circle(width / 2, height / 2, 25, Color.blue); circle2.setDirection(0); circle2.setVelocity(5); timer = new javax.swing.Timer(5, new MoveListener()); addMouseListener(new MouseListener()); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); circle.fill(g); circle2.fill(g); } private class MoveListener implements ActionListener { public void actionPerformed(ActionEvent e) { int x = circle.getX(); int radius = circle.getRadius(); int width = getWidth(); int y = circle.getY(); int height = getHeight(); int x2 = circle2.getX(); int radius2 = circle2.getRadius(); int y2 = circle2.getY(); if(x - radius <= 0 || x + radius >= width) { circle.turn(circle.getDirection() + 180); } if(y - radius <= 0 || y + radius >= height) { circle.turn(circle.getDirection() + 180); } circle.move(); repaint(); } } private class MouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(move) { timer.stop(); move = false; } else { timer.start(); move = true; } } } } //Implementation of panel import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MovingCircle { public static void main(String[] args) { JFrame theGUI = new JFrame(); theGUI.setTitle("Moving Circle"); theGUI.setSize(800, 800); theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MovingCirclePanel panel = new MovingCirclePanel(Color.white, 200, 300); Container pane = theGUI.getContentPane(); pane.add(panel); theGUI.setVisible(true); } }
Any help is appreciated.//circle class import java.awt.*; public class Circle { private int centerX, centerY, radius, velocity, direction; private Color color; public Circle(int x, int y, int r, Color c) { centerX = x; centerY = y; radius = r; color = c; } public int getX() { return centerX; } public int getY() { return centerY; } public int getRadius() { return radius; } public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public void fill(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2); } public boolean containsPoint(int x, int y) { int xSquared = (x - centerX) * (x - centerX); int ySquared = (y - centerY) * (y - centerY); int radiusSquared = radius * radius; return xSquared + ySquared - radiusSquared <= 0; } public void move(int xAmount, int yAmount) { centerX = centerX + xAmount; centerY = centerY + yAmount; } public void setVelocity(int v) { velocity = v; } public void setDirection(int degrees) { direction = degrees % 360; } public void turn(int degrees) { direction = (direction + degrees) % 360; } public void move() { move((int)(velocity * Math.cos(Math.toRadians(direction))), (int)(velocity * Math.sin(Math.toRadians(direction)))); } public int getDirection() { return direction; } }