Results 1 to 8 of 8
  1. #1
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default Animation Applet

    Hi I'm Unome... I've been taking java for a year in school and I'm starting to do some independent study.

    I ran across Timer's in my Java textbook and figured out how to use the swing timer and with that was able to with ActionListener make some simple animations. I came across a problem. I want to be able to take persay... a bullet object and every time I call the bullet object it will create a new bullet that animates. (For instance when I press space bar a bullet is created and fly's x amount to the right) I am able to make the bullet... I'm able to make the bullet fly to the right animated... How do I call a bullet object in another class? I know the question might be stupid or simple to you guys, but every time I call the bullet object in a testclass the applet won't open anymore? Am I using something wrong? Is there a better way to do it? I am open to any opinions and answers... I'm an eager learner and have open ears.

    Help me please :)

    Thankyou

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Are there error messages in the java console when you run the program? If so, copy them here. Also copy your code around the line numbers referenced in the error message.

  3. #3
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default

    mmK... This first class is my Bullet Timer. It displays a bullet that shoots across the screen for a certain amount of time.

    It works fine.

    PHP Code:
    import javax.swing.Timer;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Line2D;
    import javax.swing.JApplet;
    import java.awt.Rectangle;
    import java.awt.Color;
    
    
    
    public class Animate extends JApplet
    {
    	int count = 0;
    	int count2 = 0;
    	int drawdecider = 1;
    	int bulletcount = 0;
    	int stickcounter = 0;
    	int pcount = 0;
    	int bool = 1;
    	final int xSIZE = 400;
    	final int ySIZE = 80;
    
    	public Animate()
    	{
    		bulletMover movebullet = new bulletMover();					//calls the innerclass
    		Timer supertimer = new Timer(50, movebullet);				//sets up the timer for animation
    		supertimer.getDelay();
    		supertimer.start();	
    	}
    	public void animatetest()
    	{
    		
    	}
    	
    
    	
    	
    	public void paint(final Graphics g)
    	{
    		setSize(xSIZE,ySIZE);
    		count++;
    		count2 = count2 + 12;
    		
    		Graphics2D g2 = (Graphics2D)g;
    		
    		Rectangle clear = new Rectangle(xSIZE,ySIZE);									//rectangle used to clear board every refresh
    		bullet projectile1 = new bullet(count2,5);										//calls a bullet Graphic
    		g2.setColor(Color.white);														//sets the rectangle refresh to white
    		g2.fill(clear);
    		g2.draw(clear);
    		projectile1.shoot(g2);															//draws bullet at cordinate
    
    	}
    	
    	
    			class bulletMover implements ActionListener
    			{
    				int count;							//keeps track of how many times the paint has been called.
    		 
    				public void actionPerformed(ActionEvent e)
    				{	
    					if(count < 10)
    					{
    						repaint();							//paints
    						count++;
    					}
    				}
    			}
    
    	
    	
    		
    }

    This second class is my Control. I intend to upgrade it later with a listener taht reacts to a keystroke such as a spacebar, thus creating a new bullet object every time I hit the key.... I haven't been able to get the bullet to display at all yet so I haven't done this yet.

    PHP Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Line2D;
    import javax.swing.JApplet;
    import java.awt.Rectangle;
    import java.awt.Color;
    
    
    
    public class MasterAnimate extends JApplet
    {
    	public MasterAnimate()
    	{
    		
    		stickinstructions stick1 = new stickinstructions();
    		Timer masterTimer = new Timer(50,stick1);
    	}
    	
    	public void paint(Graphics g)
    	{
    		Animate bulletanimation = new Animate();
    		bulletanimation.animatetest();
    	}
    	
    	
    	class stickinstructions implements ActionListener
    	{
    		int count;							//keeps track of how many times the paint has been called.
     
    		public void actionPerformed(ActionEvent e)
    		{	
    			if(count < 10)
    			{
    				repaint();
    				count++;
    			}
    		}
    	}
    	
    	
    
    }
    No errors display. A java applet opens at standard size blank. I know my code is probobalyl riddled with errors. Can you help me figure out what I'm supposed to do?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You have two applet classes. Do you have the HTML that you use to load them into a browser and execute them?

    Have you read about how applets and browser's communicate? There are some standard methods in the applet that the browser calls. I do NOT see them in your code. Go read about applets and browsers for info on how to code applets.

    To debug your program add some println() statements to the code to show where the execution flow is going and what the values of the variables are.

  5. #5
    JavaJenius is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    Judging by your code, you seem to have little experience in programming java applets. I suggest you go over some basic java applet skills, then ease your way into developing more complicated applets.

    I want to be able to take persay... a bullet object and every time I call the bullet object it will create a new bullet that animates. (For instance when I press space bar a bullet is created and fly's x amount to the right) I am able to make the bullet... I'm able to make the bullet fly to the right animated...
    What is the bullet? Is this an image that you are trying to animate? If you describe it a little more, i can give you some basic code to work with.

  6. #6
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default

    Sorry, I should have clarified.

    I'm using eclipse to run all my work right now so implementing it into a web browser isn't priority at the moment.

    Let me rephrase everything I said, I must have came off confusing.

    I created a bullet object.

    BULLET OBJECT
    PHP Code:
    import java.awt.BasicStroke;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.geom.*;
    import java.awt.Rectangle;
    
    
    public class bullet 
    {
    	private int startx;
    	private int starty;
    	
    	public bullet(int xstart, int ystart)
    	{
    		startx = xstart;
    		starty = ystart;
    	}
    	
    	public void shoot(Graphics2D g2)
    	{
    		
    		Rectangle bulletbase = new Rectangle(startx,starty,3,2);									//creates the body of the bullet
    		Ellipse2D.Double bulletnose = new Ellipse2D.Double(startx+2,starty,2,2);					//creates the rounded tip of the bullet
    		
    		g2.setStroke(new BasicStroke(1));
    		g2.setColor(Color.black);
    		g2.draw(bulletbase);
    		g2.draw(bulletnose);
    		g2.fill(bulletbase);
    		g2.fill(bulletnose);
    		g2.setStroke(new BasicStroke(1));
    		g2.setColor(Color.black);
    	}
    
    }
    This bullet object is the object you see called in the animate class. The bullet displays fine in a test class.

    Then in the Animate class when I test it (alone) It displays a bullet shooting a certain distance to the right.

    I did this using the Swing Timer and having the ActionListner repaint the bullet at cordinate to the right every 50 ms.

    Now here is what I want to do. I plan to make a simple game for my AP Programming class. I created a Stick figure that walked to the right and fired his gun, little bullets would go to the right every few seconds. I noticed however, that once I learned how to use Actions (hit the spacebar) I'd run into a problem. I need to isolate the bullets as new objects so they can occur as frequent or unfrequent as the user wants them too.. So I logically thought... Hey I'll call my Animate class in a test class. When I did nothing happened. I know that there has to be a way, I simply don't know exactly what that way is. I'm probobally ignorant and don't know the format you use when you call an object with a timer already in it. I think its essential to be able to create a bullet object that continues to animate beyond a single frame and can be called often, if I'm gonna implement a health system I'm gonna need seperate objects to do that. Anyways, I hope that clarifies. I do know a little bit about Applets. I created a Monopoly program using only applets, so I know how to draw, color, fill, all the basics to that along with Rectangle, Ellipse, Line, Point and the other objects.... I know that java is object oriented so now I'm trying to learn how to make Objects with Timers and thus animation in them. If there is another way that animates objects, I'll be happy to learn it too. Bear with my ignorance, I'm just trying to learn. Any other suggestions?

  7. #7
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default

    I'm still really needing help on this? Does anyone know how to do this?

  8. #8
    Unome is offline Member
    Join Date
    Oct 2008
    Posts
    44
    Rep Power
    0

    Default

    I've got a similar question in a new thread. If a moderator could close this... that would be nice. If not :) Thanks anyways

Similar Threads

  1. Two sleeps in animation
    By George1935 in forum AWT / Swing
    Replies: 4
    Last Post: 10-13-2008, 01:47 AM
  2. Applet Vs JApplet ?? which is better for animation?
    By Mba7eth in forum New To Java
    Replies: 3
    Last Post: 09-09-2008, 05:55 PM
  3. Text animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:44 PM
  4. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04:37 AM
  5. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 11:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •