Results 1 to 4 of 4
  1. #1
    avsfan151933 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Help with Applet and Animation

    Hello, I am trying to animate a rocket vertically by drawing it then erasing it then redrawing it in an applet.
    The problem I am running into is my for loop which should be updating the coordinates and repainting it, I am at a loss and was hoping I could get some help. Thank you

    Java Code:
    package javaapplication15;
    
    import java.applet.Applet;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import javax.swing.JApplet;
    
    /**
     *
     * @author Administrator
     */
    public class RocketLaunch extends JApplet
    {
    
        private int offset=50;
        
        @SuppressWarnings("empty-statement")
       
      private static final int PANEL_WIDTH = 1000;
      private static final int PANEL_HEIGHT = 1000;
      private int imageHeight , imageWidth ;
      private int currX , currY ;
      private Color rocketColor;
      private int appletHeight, appletWidth;
    
    
    
    
    
        @Override
        public void init()
            {
            appletWidth = 800;
            appletHeight = 800;
            setBackground(Color.white);
             setSize(appletWidth, appletHeight);
    
    
            }
    
    
    
    
    
    
       
           
        @Override
            public void paint( Graphics g )
        {
            imageHeight = 200 ;
            imageWidth = 100;
            currX = 100 ;
            currY = 200 ;
            rocketColor = Color.BLUE;
    
            g.setColor(rocketColor);
            g.drawRect( currX , currY , imageWidth , imageHeight);
            g.fillRect( currX , currY , imageWidth , imageHeight);
    
            Polygon po=new Polygon();
            po.addPoint(currX, currY);
            po.addPoint(currX + (imageWidth / 2), currY - (imageHeight / 4));
            po.addPoint(currX + imageWidth , imageHeight);
            g.fillPolygon(po);
    
            Polygon rocketRightFin=new Polygon();
            rocketRightFin.addPoint(currX + imageWidth, currY + ( 3 * (imageHeight / 4)));
            rocketRightFin.addPoint(currX + imageWidth + (2*(imageWidth / 4)), currY + (7*(imageHeight / 8)));
            rocketRightFin.addPoint(currX + imageWidth + (2*(imageWidth / 4)), currY + imageHeight + (imageHeight/ 8));
            rocketRightFin.addPoint(currX + imageWidth, currY + imageHeight);
            g.fillPolygon(rocketRightFin);
    
            Polygon rocketLeftFin=new Polygon();
            rocketLeftFin.addPoint(currX, currY + ( 3 * (imageHeight / 4)));
            rocketLeftFin.addPoint(currX - (2*(imageWidth / 4)), currY + (7*(imageHeight / 8)));
            rocketLeftFin.addPoint( currX - (2*(imageWidth / 4)), currY + imageHeight + (imageHeight/ 8));
            rocketLeftFin.addPoint(currX, currY + imageHeight);
            g.fillPolygon(rocketLeftFin);
         // close pain
    
    
    
            
    
            for ( int i = 0 ; i <= 100 ; i++ )
            {
                try
                {
                    Thread.sleep( 30 );
                }
                catch ( InterruptedException e )
                {
                }
    
                currX = currX + 0 ;
                currY = currY - 5 ;
                // now call repaint which will ultimately lead to paint being called
                repaint();
            } //close for loop
        } }//close run

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Not only do you call Thread.sleep on the event dispatch thread, but you're doing it in paint, the method responsible for drawing. This will effectively freeze your application.

    Suggestions.
    1. Don't call Thread.sleep on the EDT. Ever. Please google "concurrency in Swing" for more on this.
    2. Don't have program logic in a painting method.
    3. Don't draw in paint but rather in a JPanel's paintComponent method.
    4. Use a Swing Timer for your delay and for your "loop". check out the tutorial on this.

    and most importantly: read the tutorials on Swing graphics as they will get you started in the right direction.

  3. #3
    avsfan151933 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you, I guess my question needs more refinement.

    First I have a Rocket Class, but I cannot draw it in the Applet, I have googled and searched and perhaps I am wording it incorrectly but I cannot find it.

    The second problem I am having with this Rocket Class is displaying it in an applet, I have called it and everything but I still come up with a blank applet.

    My Rocket Class
    Java Code:
    package javaapplication15;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    
    /**
     *
     * @author Administrator
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    
    /**
     *
     * @author Administrator
     */
    public class Rocket
    {
    
    
    //Declaring the variables for the rocket body by which all other of the rocket parts
        //will be based off iof or in reference to
        private final int rocketBodyX, rocketBodyY;
        private final int rocketBodyWidth, rocketBodyHeight;
    private final Color rocketColor;
    
        //This is defining the variables needed for the nose cone or tip of the rocket
        private final int rocketNoseX1, rocketNoseY1, rocketNoseX2, rocketNoseY2, rocketNoseX3, rocketNoseY3;
    
        //The left fin
        private final int rocketFinLeftX1, rocketFinLeftX2, rocketFinLeftX3, rocketFinLeftX4, rocketFinLeftY1, rocketFinLeftY2, rocketFinLeftY3, rocketFinLeftY4;
    
        //The right fin
        private final int rocketFinRightX1, rocketFinRightX2, rocketFinRightX3, rocketFinRightX4, rocketFinRightY1, rocketFinRightY2, rocketFinRightY3, rocketFinRightY4;
    
    
        private static final Color NOSE_CONE = Color.RED;
        private static final Color LEFT_FIN = Color.ORANGE;
        private static final Color RIGHT_FIN = Color.ORANGE;
    
    
        //Sets the default construcor if the user were to simply call upon this as default these are the specifications
        public Rocket()
        {
            this(200, 100, 25, 50, Color.PINK);
    }
    
        //defining the paramaterized constructor and designing the graphic rocket itself
        public Rocket(int left, int top, int width, int height, Color color)
    
        {
    //The main part of the rocket body by which all other parts of the rocket will be based off of
            rocketBodyX = left;
            rocketBodyY = top;
            rocketBodyWidth = width;
            rocketBodyHeight = height;
            rocketColor = color;
    
    
    
            //designing the nose cone by which we will call to it as a polygon for simplicity sakes
            rocketNoseX1 = rocketBodyX;
            rocketNoseY1 = rocketBodyY;
            rocketNoseX2 = rocketBodyX + (rocketBodyWidth / 2);
            rocketNoseY2 = rocketBodyY - (rocketBodyHeight / 4);
            rocketNoseX3 = rocketBodyX + rocketBodyWidth;
            rocketNoseY3 = rocketBodyHeight;
    
            //designing the left fin of the rocket again we will be using a polygon
            rocketFinLeftX1 = rocketBodyX;
            rocketFinLeftY1 = rocketBodyY + ( 3 * (rocketBodyHeight / 4));
            rocketFinLeftX2 = rocketBodyX - (2*(rocketBodyWidth / 4));
            rocketFinLeftY2 = rocketBodyY + (7*(rocketBodyHeight / 8));
            rocketFinLeftX3 = rocketFinLeftX2;
            rocketFinLeftY3 = rocketBodyY + rocketBodyHeight + (rocketBodyHeight/ 8);
            rocketFinLeftX4 = rocketBodyX;
            rocketFinLeftY4 = rocketBodyY + rocketBodyHeight;
    
            //designing the right fin of the rocket using polygon
            rocketFinRightX1 = rocketBodyX + rocketBodyWidth;
            rocketFinRightY1 = rocketBodyY + ( 3 * (rocketBodyHeight / 4));
            rocketFinRightX2 = rocketBodyX + rocketBodyWidth + (2*(rocketBodyWidth / 4));
            rocketFinRightY2 = rocketBodyY + (7*(rocketBodyHeight / 8));
            rocketFinRightX3 = rocketFinRightX2;
            rocketFinRightY3 = rocketBodyY + rocketBodyHeight + (rocketBodyHeight/ 8);
            rocketFinRightX4 = rocketBodyX + rocketBodyWidth;
            rocketFinRightY4 = rocketBodyY + rocketBodyHeight;
    
    
        }
    
    
    
    
    
    
    //dsiplays the face
        public void paintRocket(Graphics pen)
        {
    
    
    
            pen.setColor(rocketColor);
            pen.fillRect(rocketBodyX, rocketBodyX, rocketBodyWidth, rocketBodyHeight);
    
            pen.setColor(LEFT_FIN);
            Polygon rocketLeftFin=new Polygon();
    
            rocketLeftFin.addPoint(rocketFinLeftX1, rocketFinLeftY1);
            rocketLeftFin.addPoint(rocketFinLeftX2, rocketFinLeftY2);
            rocketLeftFin.addPoint(rocketFinLeftX3, rocketFinLeftY3);
            rocketLeftFin.addPoint(rocketFinLeftX4, rocketFinLeftY4);
            pen.fillPolygon(rocketLeftFin);
    
            pen.setColor(RIGHT_FIN);
            Polygon rocketRightFin=new Polygon();
            rocketRightFin.addPoint(rocketFinRightX1, rocketFinRightY1);
            rocketRightFin.addPoint(rocketFinRightX2, rocketFinRightY2);
            rocketRightFin.addPoint(rocketFinRightX3, rocketFinRightY3);
            rocketRightFin.addPoint(rocketFinRightX4, rocketFinRightY4);
            pen.fillPolygon(rocketRightFin);
    
    
    
    
            pen.setColor(NOSE_CONE);
    
            Polygon po=new Polygon();
            po.addPoint(rocketNoseX1, rocketNoseY1);
            po.addPoint(rocketNoseX2, rocketNoseY2);
            po.addPoint(rocketNoseX3, rocketNoseY3);
            pen.fillPolygon(po);
    
    
    
    
        }
    
        void draw(Graphics pen) {
    
            pen.setColor(rocketColor);
            pen.fillRect(rocketBodyX, rocketBodyX, rocketBodyWidth, rocketBodyHeight);
    
            pen.setColor(LEFT_FIN);
            Polygon rocketLeftFin=new Polygon();
    
            rocketLeftFin.addPoint(rocketFinLeftX1, rocketFinLeftY1);
            rocketLeftFin.addPoint(rocketFinLeftX2, rocketFinLeftY2);
            rocketLeftFin.addPoint(rocketFinLeftX3, rocketFinLeftY3);
            rocketLeftFin.addPoint(rocketFinLeftX4, rocketFinLeftY4);
            pen.fillPolygon(rocketLeftFin);
    
            pen.setColor(RIGHT_FIN);
            Polygon rocketRightFin=new Polygon();
            rocketRightFin.addPoint(rocketFinRightX1, rocketFinRightY1);
            rocketRightFin.addPoint(rocketFinRightX2, rocketFinRightY2);
            rocketRightFin.addPoint(rocketFinRightX3, rocketFinRightY3);
            rocketRightFin.addPoint(rocketFinRightX4, rocketFinRightY4);
            pen.fillPolygon(rocketRightFin);
    
    
    
    
            pen.setColor(NOSE_CONE);
    
            Polygon po=new Polygon();
            po.addPoint(rocketNoseX1, rocketNoseY1);
            po.addPoint(rocketNoseX2, rocketNoseY2);
            po.addPoint(rocketNoseX3, rocketNoseY3);
            pen.fillPolygon(po);
    
    
    
        }
    
    
    
        }
    The Applet I am trying to display it in without having all of the rocket code in

    Java Code:
    package javaapplication15;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    
    /**
     *
     * @author Administrator
     */
    public class RocketApplet extends JApplet
    {
    
        private int offset=50;
    
        @SuppressWarnings("empty-statement")
    
      private static final int PANEL_WIDTH = 1000;
      private static final int PANEL_HEIGHT = 1000;
      private int imageHeight , imageWidth ;
      private int currX , currY ;
      private Color rocketColor;
      private int appletHeight, appletWidth;
      private final Rocket myRocket;
    
    
    
    
        @Override
        public void init()
            {
            appletWidth = 800;
            appletHeight = 800;
            setBackground(Color.white);
            setSize(appletWidth, appletHeight);
            imageHeight = 200 ;
            imageWidth = 100;
            currX = 100 ;
            currY = 200;
    
    
        }
    
        public RocketApplet()
        {
            myRocket = new Rocket(currX, currY, imageWidth, imageHeight, rocketColor);
    
    
        }
    
    
            public void paintComponent( Graphics pen )
        {
            myRocket.draw(pen);
    
         }
    
    
    
    }
    Thank you for your help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You'll want to re-read my suggestions again especially about drawing in a JPanel's paintComponent method not the JApplet's paint method. As for examples, if you search on animation, you should find several decent ones here on this site.

Similar Threads

  1. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  2. Problem with my animation applet
    By cblue in forum Java 2D
    Replies: 0
    Last Post: 08-24-2009, 04:24 PM
  3. Replies: 3
    Last Post: 05-06-2009, 12:11 AM
  4. Animation Applet
    By Unome in forum Java Applets
    Replies: 7
    Last Post: 10-17-2008, 07:34 AM
  5. Applet Vs JApplet ?? which is better for animation?
    By Mba7eth in forum New To Java
    Replies: 3
    Last Post: 09-09-2008, 04:55 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
  •