Animation in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Animation in Java

  1. #1
    Join Date
    Sep 2005
    Posts
    74

    Animation in Java

    I was asked to make a hockey game by my Java teacher. I can get the puck to draw, but I'm not sure how to move it. Should I use a thread, or a timer?

    Also, how can I check if the puck collides with the outside of the application window? I would like to keep it in the window, and when it hits a window boundary to send it off in another direction.

  2. #2
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    You should store the (X,Y) position of the puck in variables, and use them for drawing, as in g.drawOval(x,y, height, width) then increment them inside the main loop, to check for collision simply write an if statement checking if it is smaller than 0 or bigger than the height of the window, then compare the y position to 0 and the width. if they collide simply multiply the x, y value by -1 this will reverse the puck

    I hope this helps

  3. #3
    Join Date
    Sep 2005
    Posts
    74
    I have these two classes:

    Code:
    Hockey.class
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Hockey extends JApplet implements ActionListener
    {
    	private final int WIDTH = 400, HEIGHT = 200;
    	
    	private JPanel surface = new JPanel();
    	private HockeyThread thread;
    	
    	public void init()
    	{	
    		getContentPane().setLayout(new BorderLayout());
    		getContentPane().add("Center",surface);
    		getContentPane().setSize(WIDTH,HEIGHT);
    
    	}// init()
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		thread = new HockeyThread(surface);
    		Thread hockeyThread = new Thread(thread);
    		hockeyThread.start();
    	}// actionPerformed()
    }//Hockey
    Code:
    HockeyThread.class
    
    import java.awt.*;
    import javax.swing.*;
    
    public class HockeyThread implements Runnable 
    {
    	private int xCoord = 0;
    	private int yCoord = 0;
    	
    	private JPanel surface;
    	
    	public void run()
    	{
    		go();
    	}
    	
    	public HockeyThread(JPanel draw)
    	{
    		surface = draw;
    	}
    	
    	public void go()
    	{
    		Graphics g = surface.getGraphics();
    		
    		Dimension size = surface.getSize();
    		g.translate(size.width / 2, size.height / 2);
    		
    		g.fillOval(xCoord,yCoord,20,15);
    		g.setColor(Color.black);
    	}// go()
    }// HockeyThread
    There are no syntatical errors, but for some reason the puck won't draw in the center of the screen like it is supposed to. Anyone know what I could be doing wrong here?
    Last edited by Dark Rain; 11-23-2005 at 03:08 PM.

  4. #4
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I think that your problem is in the run() method, the thread runs but go() is called only once.

    I usually do all the drawing inside the paint() method

    Try this

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Hockey extends JApplet implements ActionListener
    {
    	private final int WIDTH = 400, HEIGHT = 200;
    	
    	private JPanel surface = new JPanel();
    	private HockeyThread thread;
    	
    	public void init()
    	{	
    		getContentPane().setLayout(new BorderLayout());
    		getContentPane().add("Center",surface);
    		getContentPane().setSize(WIDTH,HEIGHT);
    
    	}// init()
    	
    	public void actionPerformed(ActionEvent e)
    	{	
    	}// actionPerformed()
    	
    	public void start()
    	{
    		thread = new HockeyThread(surface);
    		Thread hockeyThread = new Thread(thread);
    		hockeyThread.start();
    	}
    }//Hockey
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class HockeyThread implements Runnable 
    {
    	private int xCoord = 0;
    	private int yCoord = 0;
    	
    	private JPanel surface;
    	
    	public void run()
    	{
    		while(true)
    		{
    			go();
    		}	
    	}
    	
    	public HockeyThread(JPanel draw)
    	{
    		surface = draw;
    	}
    	
    	public void go()
    	{
    		Graphics g = surface.getGraphics();
    		Dimension size = surface.getSize();
    		g.translate(size.width / 2, size.height / 2);
    		
    		g.fillOval(xCoord,yCoord,20,15);
    		g.setColor(Color.black);
    	}// go()
    }// HockeyThread

  5. #5
    Join Date
    Sep 2005
    Posts
    74
    I changed some things:

    Code:
    HockeyThread.class
    
    import java.awt.*;
    import javax.swing.*;
    
    public class HockeyThread extends HockeyOther implements Runnable 
    {	
    	private static final int XMIN = 0;
    	private static final int XMAX = 420;
    	private static final int XSTART = 0;
    	private static final int YMIN = 0;
    	private static final int YMAX = 345;
    	private static final int YSTART = 0;
    	private static final int SIDE = 5;
    	private static final int DELTA = 5;
    	private static final int RANGE = 5;
    	
    	private Hockey applet;
    	private Point location;
    	
    	public HockeyThread(Hockey app)
    	{
    		applet = app;
    		location = new Point(XSTART,YSTART);
    		state = MOVING;
    	}
    	
    	public Point getLocation()
    	{
    		return location;
    	}
    	
    	public void go()
    	{	
    		Graphics g = applet.getGraphics();
    		
    		g.setColor(Color.white);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    		
    		int dx = (int)(DELTA);
    		int dy = (int)(RANGE);
    		
    		if (location.x + dx >= XMIN && location.x + dx <= XMAX)
    		{
    			location.x = location.x + dx;
    		} else {
    			dx *= -1;
    		}
    		if (location.y + dy >= YMIN && location.y + dy <= YMAX)
    		{
    			location.y = location.y + dy;
    		} else {
    			dy *= -1;
    		}
    		if (location.x + dx <= XMAX && location.x + dx >= XMIN)
    		{
    			location.x = location.x  + dx;
    		} else {
    			dx *= -1;
    		}
    		if (location.y + dy <= YMAX && location.y + dy >= YMIN)
    		{
    			location.y = location.y + dy;
    		} else {
    			dy *= -1;
    		}
    		
    		g.setColor(Color.red);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    	}// go()
    Although, I can't seem to get the boundries right. Either the puck flies out of bounds into nowhere, or starts warping around.
    Last edited by Dark Rain; 11-23-2005 at 07:32 PM.

  6. #6
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I took some time with MS Paint and explained it a little
    Attached Images Attached Images

  7. #7
    Join Date
    Sep 2005
    Posts
    74
    Thanks for the tutorial. The only problem is the puck stays at 0,0 now. Do I need a loop somewhere to move it?

  8. #8
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    You need to do the checking in the main loop, and start with the puck anywhere inbounds

    Post your full code again, if ithat doesen't help
    Last edited by Wizard1988; 11-24-2005 at 01:31 AM.

  9. #9
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    DarkRain look at your if blocks.
    2 of them are same other 2 . you only need 2...
    last one reverse the effect of first.
    if (location.x + dx >= XMIN && location.x + dx <= XMAX)
    if (location.x + dx <= XMAX && location.x + dx >= XMIN)

    if (location.y + dy >= YMIN && location.y + dy <= YMAX)
    if (location.y + dy <= YMAX && location.y + dy >= YMIN)

  10. #10
    Join Date
    Sep 2005
    Posts
    74
    Here is my code:
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class HockeyThread extends HockeyOther implements Runnable 
    {	
    	private static final int XMIN = 0;
    	private static final int XMAX = 420;
    	private static final int YMIN = 0;
    	private static final int YMAX = 345;
    	private static final int SIDE = 5;
    	private static final int DELTA = 5;
    	private static final int RANGE = 5;
    	
    	private static final int XSTART = (int)Math.random() * SIDE;
    	private static final int YSTART = (int)Math.random() * SIDE;
    	
    	private Hockey applet;
    	private Point location;
    	
    	public HockeyThread(Hockey app)
    	{
    		applet = app;
    		location = new Point(XSTART,YSTART);
    		state = MOVING;
    	}
    	
    	public Point getLocation()
    	{
    		return location;
    	}
    	
    	public void go()
    	{	
    		Graphics g = applet.getGraphics();
    		
    		g.setColor(Color.white);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    		
    		int dx = (int)(DELTA);
    		int dy = (int)(RANGE);
    		
    		//	Check x-axis movement
    		if ((location.x <= XMIN) | (location.x + SIDE >= XMAX))
    		{
    			dx *= -1;
    		} else {
    			dx += dx;
    		}
    		
    		// Check y-axis movement
    		if ((location.y <= YMIN) | (location.y + SIDE >= YMAX))
    		{
    			dy *= -1;
    		} else {
    			dy += dy;
    		}
    		
    		g.setColor(Color.red);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    	}// go()
    	
    	public synchronized void die()
    	{
    		state = DEAD;
    	}
    	
    	public void run()
    	{
    		while (state != DEAD)
    		{
    			go();
    			delay(85);
    		}
    	}
    }// HockeyThread
    I call the go() method in the main loop, but it doesn't seem to do any good. Also, I tried setting a random start location with the XSTART,YSTART call but, that also doesn't work.

  11. #11
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    Can you post the HockeyOther class?

  12. #12
    Join Date
    Sep 2005
    Posts
    74
    Here you go:

    Code:
    public class HockeyOther 
    {
    	protected int state;
    	public static final int DEAD = -1;
    	public static final int MOVING = 0;
    	
    	public HockeyOther()
    	{
    		state = MOVING;
    	}
    	
    	protected void delay(int N)
    	{
    		try {
    			Thread.sleep(N);
    		} catch (InterruptedException e) {
    			System.out.println(e.toString());
    		}
    	}
    
    }

  13. #13
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    Is there any other code that you didn't post ? because I can't get this to compile...

  14. #14
    Join Date
    Sep 2005
    Posts
    74
    Sorry =p. I forgot the GUI part of it:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Hockey extends JApplet
    {
    	private HockeyThread thread = new HockeyThread(this);
    	
    	public void init()
    	{	
    		this.setSize(425,350);
    		
    		new Thread(thread).start();
    	}// init()
    	
    	public void PaintComponent(Graphics g)
    	{
    	}
    }//Hockey
    That's the one you want to compile.

  15. #15
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Thumbs up

    I feel like an idiot... My if statements for the collision detection were wrong, so sorry for misleading you there, but I did get this to work.

    I think that your problem might have been that you were declaring dx and dy inside the go() method.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class HockeyThread extends HockeyOther implements Runnable 
    {	
    	private static final int XMIN = 0;
    	private static final int XMAX = 400;
    	private static final int YMIN = 0;
    	private static final int YMAX = 200;
    	private static final int SIDE = 10;
    	private static final int DELTA = 4;
    	private static final int RANGE = 3;
    	
    	private static final int XSTART = 15;
    	private static final int YSTART = 15;
    	
    	private Hockey applet;
    	private Point location;
    	
    	private int dx;
    	private int dy;
    	
    	public HockeyThread(Hockey app)
    	{
    		applet = app;
    		location = new Point(XSTART,YSTART);
    		state = MOVING;
    		
    		dx = (int)(DELTA);
    		dy = (int)(RANGE);
    	}
    	
    	public Point getLocation()
    	{
    		return location;
    	}
    	
    	public void move()
    	{
    		location.x += dx;
    		location.y += dy;
    	}
    	
    	public void go()
    	{	
    		Graphics g = applet.getGraphics();
    		
    		g.setColor(Color.white);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    	
    		move();
    		
    		if(location.x + dx >=XMIN && location.x + dx <= XMAX)
    		{
    			move();
    		}
    		else{
    			dx*=-1;
    		}
    		if(location.y + dy >= YMIN && location.y +dy <= YMAX)
    		{
    			move();
    		}
    		else{
    			dy*=-1;
    		}
    		
    		g.setColor(Color.blue);
    		g.fillRect(location.x,location.y,SIDE,SIDE);
    		
    	}// go()
    	
    	public synchronized void die()
    	{
    		state = DEAD;
    	}
    	
    	public void run()
    	{
    		while (state != DEAD)
    		{
    			go();
    			delay(50);
    		}
    	}
    }// HockeyThread

    Again sorry for misleading you with the if statements.
    Last edited by Wizard1988; 11-24-2005 at 09:59 PM.

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 04:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  4. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center