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)

/*********************************************************
 * 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)

/*********************************************************
 * 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);
    }
}

Recommended Answers

All 4 Replies

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.

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?

Yep. The image loading is just a little different. All of the other painting in the same, just done on JFrame or JPanel (or JLabel even).

Yep. The image loading is just a little different. All of the other painting in the same, just done on JFrame or JPanel (or JLabel even).

So assuming I change that part of the code the class files can be used? That would be good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.