| | |
Code Problem (Modifying Book Code to adapt to program)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 46
Reputation:
Solved Threads: 0
I've been playing (more like struggling) with a few ways to illustrate an explosion for a Fireworks display in an application. One of my reference books dealing with Game Programming had a very nice solution, where the "explosion" is a progression of images. One problem: it relies or depends on an applet. I'm trying to use this in an application, and not on an applet. Does anybody have any suggestion on how to modify this code to remove the dependency for an applet??? Was trying a few different things, but still not getting there. I'll post the class file itself, and its parent class below.
THANKS in advance for any info anyone can provide....
ImageEntity class (the one in question)
BaseGameEntity (parent class for the above)
THANKS in advance for any info anyone can provide....
ImageEntity class (the one in question)
Java Syntax (Toggle Plain Text)
/********************************************************* * Base game image class for bitmapped game entities **********************************************************/ import java.awt.*; import java.awt.geom.*; import java.net.*; import java.applet.*; public class ImageEntity extends BaseGameEntity { //variables protected Image image; protected Applet applet; protected AffineTransform at; protected Graphics2D g2d; //default constructor ImageEntity(Applet a) { applet = a; setImage(null); setAlive(true); } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; double x = applet.getSize().width/2 - width()/2; double y = applet.getSize().height/2 - height()/2; at = AffineTransform.getTranslateInstance(x, y); } public int width() { if (image != null) return image.getWidth(applet); else return 0; } public int height() { if (image != null) return image.getHeight(applet); else return 0; } public double getCenterX() { return getX() + width() / 2; } public double getCenterY() { return getY() + height() / 2; } public void setGraphics(Graphics2D g) { g2d = g; } //************* private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); //url = new java.net.URL(applet.getCodeBase() + filename); } //catch (MalformedURLException e) { e.printStackTrace(); } catch (Exception e) { } return url; } //************* public void load(String filename) { image = applet.getImage(getURL(filename)); while(getImage().getWidth(applet) <= 0); double x = applet.getSize().width/2 - width()/2; double y = applet.getSize().height/2 - height()/2; at = AffineTransform.getTranslateInstance(x, y); } public void transform() { at.setToIdentity(); at.translate((int)getX() + width()/2, (int)getY() + height()/2); at.rotate(Math.toRadians(getFaceAngle())); at.translate(-width()/2, -height()/2); } public void draw() { g2d.drawImage(getImage(), at, applet); } //bounding rectangle public Rectangle getBounds() { Rectangle r; r = new Rectangle((int)getX(), (int)getY(), width(), height()); return r; } }
BaseGameEntity (parent class for the above)
Java Syntax (Toggle Plain Text)
/********************************************************* * Base game entity class **********************************************************/ public class BaseGameEntity extends Object { //variables protected boolean alive; protected double x,y; protected double velX, velY; protected double moveAngle, faceAngle; //accessor methods public boolean isAlive() { return alive; } public double getX() { return x; } public double getY() { return y; } public double getVelX() { return velX; } public double getVelY() { return velY; } public double getMoveAngle() { return moveAngle; } public double getFaceAngle() { return faceAngle; } //mutator methods public void setAlive(boolean alive) { this.alive = alive; } public void setX(double x) { this.x = x; } public void incX(double i) { this.x += i; } public void setY(double y) { this.y = y; } public void incY(double i) { this.y += i; } public void setVelX(double velX) { this.velX = velX; } public void incVelX(double i) { this.velX += i; } public void setVelY(double velY) { this.velY = velY; } public void incVelY(double i) { this.velY += i; } public void setFaceAngle(double angle) { this.faceAngle = angle; } public void incFaceAngle(double i) { this.faceAngle += i; } public void setMoveAngle(double angle) { this.moveAngle = angle; } public void incMoveAngle(double i) { this.moveAngle += i; } //default constructor BaseGameEntity() { setAlive(false); setX(0.0); setY(0.0); setVelX(0.0); setVelY(0.0); setMoveAngle(0.0); setFaceAngle(0.0); } }
To load the images you can either use ImageIO.read(java.io.File) or
Image image = Toolkit.getDefaultToolkit().getImage("image.gif"); All the other methods, such as getHeight() and getWidth(), should be available from whatever component you are displaying the image on, such as a JPanel. •
•
Join Date: Feb 2008
Posts: 46
Reputation:
Solved Threads: 0
•
•
•
•
To load the images you can either use ImageIO.read(java.io.File) or Image image = Toolkit.getDefaultToolkit().getImage("image.gif"); All the other methods, such as getHeight() and getWidth(), should be available from whatever component you are displaying the image on, such as a JPanel. So basically I can just deep six the applet variable inside the ImageEntity class and just use that? Hopefully that will work. Is that basically it?
![]() |
Other Threads in the Java Forum
- Previous Thread: Graph doubt
- Next Thread: Help, I don't think I'm asking for much. This is a simple java program.
| Thread Tools | Search this Thread |
Tag cloud for Java
addball android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time tree trolltech utility windows






