Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2008, 09:36 PM
Member
 
Join Date: Oct 2008
Posts: 35
Unome is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-14-2008, 10:11 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-15-2008, 01:23 AM
Member
 
Join Date: Oct 2008
Posts: 35
Unome is on a distinguished road
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(50movebullet);                //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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-15-2008, 01:53 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-15-2008, 03:20 AM
Member
 
Join Date: Mar 2008
Posts: 2
JavaJenius is on a distinguished road
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-15-2008, 05:26 AM
Member
 
Join Date: Oct 2008
Posts: 35
Unome is on a distinguished road
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 xstartint 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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-16-2008, 08:10 AM
Member
 
Join Date: Oct 2008
Posts: 35
Unome is on a distinguished road
I'm still really needing help on this? Does anyone know how to do this?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-17-2008, 09:34 AM
Member
 
Join Date: Oct 2008
Posts: 35
Unome is on a distinguished road
I've got a similar question in a new thread. If a moderator could close this... that would be nice. If not Thanks anyways
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 07:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org