Results 1 to 8 of 8

Thread: Archer Applet

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

    Default Archer Applet

    Unome again. I'm still pursuing this goal. I was able to get the rotations to all sync correctly but now I'm pursing a new goal. An Archer applet.

    GOAL: Archer moves bow up and down according to arrow key strokes.
    Archer launches an arrow at every press of the space bar.

    So far I've got the archer, and I've been able to manipulate the arm up and down, so I'm confident once I apply the key listener I can get that to work.

    My problem is launching a new arrow. I got the trajectory calculated... thats not the problem. The problem is displaying the animation over and over at any given time. This requires an independent animation which I've decided to test with something simple.

    Here is a bullet animation.

    PHP Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    
    import javax.swing.JApplet;
    import java.awt.*;
    
    
    
    public class bulletanimator extends JApplet implements Runnable
    {
    
    	double posx = 10;
    	final double POSY = 10;
    	Thread shooter = null;
    	double count;
    
    	
    	public void paint(Graphics g)
    	{
    		count++;
    		
    		Graphics2D g2 = (Graphics2D)g;
    
    		bullet eraser = new bullet(posx-12,POSY,2);
    		bullet projectile = new bullet(posx,POSY,1);
    
    		eraser.shoot(g2);
    		projectile.shoot(g2);
    		
    	}
    	
    	public void start() 
    	{
    		shooter = new Thread(this);
    		shooter.start();
    	}
    
    	public void run() {
    
    	    while (count<10) {
    	    	if(count < 10)
    	    	{
    	    		posx = posx + 12;
    	    	}
    	    	repaint();
    
    	        try {
    
    	            Thread.sleep(20);
    
    	        } catch ( InterruptedException e ) {
    
    	            // do nothing
    	        }
    	    }
    	}
    	
    }
    When I run this Applet. A bullet object will rapidly fire across the screen ONCE.

    I decided to take this and try to run it within my Working Archer applet.

    PHP Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JApplet;
    import java.awt.*;
    
    
    public class Animatetester extends JApplet implements Runnable
    {
    	final static int WIDTH = 400;
    	final static int HEIGHT = 200;
    	double stickx = 50;
    	double sticky = 50;
    	double angle = -Math.PI/3;
    	double shoot = 0;
    	Thread runner = null;
    	
    	
    	public void paint(Graphics g)
    	{
    
    		Graphics2D g2 = (Graphics2D)g;
    		setSize(WIDTH,HEIGHT);
    		Rectangle clear = new Rectangle(0,0,WIDTH,HEIGHT);
    		Archer stickbob = new Archer(stickx,sticky,angle);
    		Arrow arrow1 = new Arrow(stickx+4,sticky+16);
    
    		
    		g2.setColor(Color.white);
    		g2.fill(clear);
    		g2.setColor(Color.black);
    	    stickbob.draw(g2);
    	    g2.rotate(-angle,stickx+4,sticky+16);
    	    arrow1.draw(g2);
    	    g2.rotate(angle,stickx+4,sticky+16);
    	    
    	    if(angle == Math.PI/3)                                          //here is where I'm attempting to display the bullet animation. 
    	    {
    		bulletanimator bullet = new bulletanimator();
    		bullet.start();
    	    bullet.run();
    	    }
    
    	}
    	
    	public void start() 
    	{
    		runner = new Thread(this);
    		runner.start();
    	}
    
    	public void run() {
    
    	    while ( runner != null) {
    	    	if(angle < Math.PI/3)
    	    	{
    	    		angle  = angle + Math.PI/80;
    	    	}
    	    	repaint();
    
    	        try {
    
    	            Thread.sleep(20);
    
    	        } catch ( InterruptedException e ) {
    
    	            // do nothing
    	        }
    	    }
    	}
    	
    	
    
    }
    When I run this The archer lifts his bow like always, but the bullet never fires when the required bow angle occurs. What is my issue?

    I really hope I can get some help. I've had very little response to my questions (with an exception of Norm. Thankyou very much for responding)
    Please help me.

    Unome :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Why are the two code segements both JApplets?
    Are there two <APPLET tags in the HTML, one for each?
    BTW do you have HTML to run your code?

    To help you debug your code add some println() statements to show execution flow and variable value changes.

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

    Default

    I'm still running everything in eclipse... so I got no HTML integrated yet. I'm gonna only do that after I've debugged and gotten it to work. The println statements are a good idea... I read something about Multiple Independent Animations that required I turn Bulletanimator into a subclass within my main animator. My question is.. I know that will work. But can I run an if statement like this

    if(//some sort or paramater)
    {
    BulletAnimator tester == new BulletAnimator(x,y);
    projectile1.start();
    Bullet projectile1 == new Bullet(tester.getX(),tester.getY());
    projectile1.draw(g2);

    }

    Would that work? I'm not sure I want to be able to create multiple of these bullets at the same time... you know whenever I hit the space key? But if I did this would it call new bullet objects? Or would it create the same old one over and over and over

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Using new will create a new instance of the class.

    You should try it occasionally in a browser to make sure the IDE is NOT doing something that a browser doesn't.

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

    Default

    Mmk... I better try that then. How do you load it in a browser? Can you give me a basic rundown here or link me to somewhere where it does?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    To load an applet in a browser, you write an HTML page with the <APPLET tag. Use Search to find sample code here on this forum.

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

    Default

    Thanks norm. Using those println's was real helpful, it helped me isolate what code wasn't running and I was able through trial and error to develop a method to work this thing. I'm sure I could perfect it, but for now. Its good enough.

    I'm gonna start working on sending it to a browser, and then beging the trajectory and Actionlisteners.

    Thanks :D

  8. #8
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

Similar Threads

  1. Need with an applet
    By maggie_2 in forum Java Applets
    Replies: 6
    Last Post: 09-21-2008, 08:07 PM
  2. Applet in a GUI
    By serfster in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 11:09 PM
  3. applet
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 03:24 AM
  4. Applet
    By kapoorje in forum Java Applets
    Replies: 0
    Last Post: 07-24-2007, 04:06 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 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
  •