Results 1 to 4 of 4
Thread: Help with Applet and Animation
- 11-11-2010, 05:19 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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
-
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.
- 11-11-2010, 06:23 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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
The Applet I am trying to display it in without having all of the rocket code inJava 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); } }
Thank you for your helpJava 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); } }
-
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
-
need help about animation ?
By h9h in forum Java 2DReplies: 1Last Post: 10-30-2009, 11:41 AM -
Problem with my animation applet
By cblue in forum Java 2DReplies: 0Last Post: 08-24-2009, 04:24 PM -
How can I get this to work properly -animation issue with an applet.
By AlejandroPe in forum New To JavaReplies: 3Last Post: 05-06-2009, 12:11 AM -
Animation Applet
By Unome in forum Java AppletsReplies: 7Last Post: 10-17-2008, 07:34 AM -
Applet Vs JApplet ?? which is better for animation?
By Mba7eth in forum New To JavaReplies: 3Last Post: 09-09-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks