Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Moving circle problem help

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving circle problem help

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

    //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;
    	}
    }
    Any help is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Moving circle problem help

    Sounds like your not setting the display positions correctly.
    Have you tried debugging your code by displaying values using println()?
    If you println() the variables as they change you should be able to see where your logic is bad.

Similar Threads

  1. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM
  2. Moving files in a Directory
    By Sai in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 23rd, 2010, 04:49 AM
  3. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM
  4. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM