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

    Default Rotating objects in Java Applets

    I am trying to create an Archer in Java.

    I want to create an Archer that fires an arrow onto a trajectory path. The trajectory can be changed by pressing the "UP" or "DOWN" arrow key. This will move the arm up and down.

    I created the Archer graphic. Its a simple network of lines. To correctly move the Arm to the right cordinate, I solved for the x cordinante of a circle (x-h)^2 + (y-k)^2. That was simple enough. When I change the value for the Y (what you control with the arrow keys) the Arm maintains its size and moves the proper place.

    THE PROBLEM

    I want to mount a bow on the arm. The endpoint of the line for my arm is in the correct place, but if I place a bow on the arm it remains in the same spot. Is there a way to rotate objects in Java? I already move the arm in place, but to calculate the positions on the bow lines requires calculus... I have to find the tangent line to a circle and thats just to attach a single line to the bow... not to mention making a more complex bow. How do you rotate objects? Thats my main question. Is there a method I can call that I can rotate the rest of the object with? Is there any simpler method of undertaking this task? Its impossible that there isn't... theres so many simple flash games and I think I remember that my guns never skewed when I waved them up and down.

    Help would bee appreciated


    Archer Applet

    PHP Code:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    	
    public class Archer
    {
    
    		private double x;
    		private double y;
    		private double armY;
    		private double armX;
    		private double armxcord;
    		private double armycord;
    		private double bowslopex;
    		private double bowslopey;
    		
    		public Archer(double aX, double aY, double yarm)
    		{
    			x = aX;
    			y = aY;
    			armY = yarm;
    			if(armY >= 10)
    			{
    				armY = 10;
    			}
    			
    		}
    		public void ArmX()
    		{
    			armX = Math.sqrt(-Math.pow(armY, 2)+100);
    			armxcord = x + 18 + armX;
    			armycord = y + 10 - armY;
    		}
    		public void Bowslant()
    		{
    			bowslopex = (armxcord);
    		}
    
    		public void draw(Graphics2D g2)
    		{
    			Ellipse2D.Double head = new Ellipse2D.Double(x,y,10,10);
    			Line2D.Double body = new Line2D.Double(x+4,y+10,x+4,y+10);
    			Line2D.Double arm = new Line2D.Double(x+4,y+10,armxcord , armycord );
    			Line2D.Double bow1 = new Line2D.Double(armxcord, armycord,armxcord-armY, armycord-armX);
    			Line2D.Double bow2 = new Line2D.Double(armxcord, armycord,armxcord+armY, armycord+armX);
    			g2.setStroke(new BasicStroke(1));
    			g2.setColor(Color.black);
    			g2.draw(arm);
    			g2.setColor(Color.white);
    			g2.fill(head);
    			g2.setColor(Color.black);
    			g2.draw(head);
    			g2.draw(body);
    			g2.draw(bow1);
    			g2.draw(bow2);
    			
    			g2.setStroke(new BasicStroke(1));
    		}
    
    }

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

    Default

    I found that g2.rotate(theta, x, y) will rotate any object. I solved my own problem. You can close this if you want

Similar Threads

  1. Rotating and flipping an image in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:01 PM
  2. reading dir in java applets
    By willemjav in forum Java Applets
    Replies: 3
    Last Post: 02-07-2008, 12:36 AM
  3. Two Problems Rotating and collision detection help
    By jaferris in forum Java Applets
    Replies: 2
    Last Post: 01-07-2008, 11:19 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
  •