943,809 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 859
  • Java RSS
Mar 9th, 2008
0

Code Problem (Modifying Book Code to adapt to program)

Expand Post »
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)
Java Syntax (Toggle Plain Text)
  1. /*********************************************************
  2.  * Base game image class for bitmapped game entities
  3.  **********************************************************/
  4. import java.awt.*;
  5. import java.awt.geom.*;
  6. import java.net.*;
  7. import java.applet.*;
  8.  
  9. public class ImageEntity extends BaseGameEntity {
  10. //variables
  11. protected Image image;
  12. protected Applet applet;
  13. protected AffineTransform at;
  14. protected Graphics2D g2d;
  15.  
  16. //default constructor
  17. ImageEntity(Applet a) {
  18. applet = a;
  19. setImage(null);
  20. setAlive(true);
  21. }
  22.  
  23. public Image getImage() { return image; }
  24.  
  25. public void setImage(Image image) {
  26. this.image = image;
  27. double x = applet.getSize().width/2 - width()/2;
  28. double y = applet.getSize().height/2 - height()/2;
  29. at = AffineTransform.getTranslateInstance(x, y);
  30. }
  31.  
  32. public int width() {
  33. if (image != null)
  34. return image.getWidth(applet);
  35. else
  36. return 0;
  37. }
  38. public int height() {
  39. if (image != null)
  40. return image.getHeight(applet);
  41. else
  42. return 0;
  43. }
  44.  
  45. public double getCenterX() {
  46. return getX() + width() / 2;
  47. }
  48. public double getCenterY() {
  49. return getY() + height() / 2;
  50. }
  51.  
  52. public void setGraphics(Graphics2D g) {
  53. g2d = g;
  54. }
  55.  
  56. //*************
  57. private URL getURL(String filename) {
  58. URL url = null;
  59. try {
  60. url = this.getClass().getResource(filename);
  61. //url = new java.net.URL(applet.getCodeBase() + filename);
  62. }
  63. //catch (MalformedURLException e) { e.printStackTrace(); }
  64. catch (Exception e) { }
  65.  
  66. return url;
  67. }
  68. //*************
  69. public void load(String filename) {
  70. image = applet.getImage(getURL(filename));
  71. while(getImage().getWidth(applet) <= 0);
  72. double x = applet.getSize().width/2 - width()/2;
  73. double y = applet.getSize().height/2 - height()/2;
  74. at = AffineTransform.getTranslateInstance(x, y);
  75. }
  76.  
  77. public void transform() {
  78. at.setToIdentity();
  79. at.translate((int)getX() + width()/2, (int)getY() + height()/2);
  80. at.rotate(Math.toRadians(getFaceAngle()));
  81. at.translate(-width()/2, -height()/2);
  82. }
  83.  
  84. public void draw() {
  85. g2d.drawImage(getImage(), at, applet);
  86. }
  87.  
  88. //bounding rectangle
  89. public Rectangle getBounds() {
  90. Rectangle r;
  91. r = new Rectangle((int)getX(), (int)getY(), width(), height());
  92. return r;
  93. }
  94.  
  95. }

BaseGameEntity (parent class for the above)
Java Syntax (Toggle Plain Text)
  1. /*********************************************************
  2.  * Base game entity class
  3.  **********************************************************/
  4.  
  5. public class BaseGameEntity extends Object {
  6. //variables
  7. protected boolean alive;
  8. protected double x,y;
  9. protected double velX, velY;
  10. protected double moveAngle, faceAngle;
  11.  
  12. //accessor methods
  13. public boolean isAlive() { return alive; }
  14. public double getX() { return x; }
  15. public double getY() { return y; }
  16. public double getVelX() { return velX; }
  17. public double getVelY() { return velY; }
  18. public double getMoveAngle() { return moveAngle; }
  19. public double getFaceAngle() { return faceAngle; }
  20.  
  21. //mutator methods
  22. public void setAlive(boolean alive) { this.alive = alive; }
  23. public void setX(double x) { this.x = x; }
  24. public void incX(double i) { this.x += i; }
  25. public void setY(double y) { this.y = y; }
  26. public void incY(double i) { this.y += i; }
  27. public void setVelX(double velX) { this.velX = velX; }
  28. public void incVelX(double i) { this.velX += i; }
  29. public void setVelY(double velY) { this.velY = velY; }
  30. public void incVelY(double i) { this.velY += i; }
  31. public void setFaceAngle(double angle) { this.faceAngle = angle; }
  32. public void incFaceAngle(double i) { this.faceAngle += i; }
  33. public void setMoveAngle(double angle) { this.moveAngle = angle; }
  34. public void incMoveAngle(double i) { this.moveAngle += i; }
  35.  
  36. //default constructor
  37. BaseGameEntity() {
  38. setAlive(false);
  39. setX(0.0);
  40. setY(0.0);
  41. setVelX(0.0);
  42. setVelY(0.0);
  43. setMoveAngle(0.0);
  44. setFaceAngle(0.0);
  45. }
  46. }
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008
Mar 9th, 2008
0

Re: Code Problem (Modifying Book Code to adapt to program)

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 9th, 2008
0

Re: Code Problem (Modifying Book Code to adapt to program)

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008
Mar 9th, 2008
0

Re: Code Problem (Modifying Book Code to adapt to program)

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).
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 9th, 2008
0

Re: Code Problem (Modifying Book Code to adapt to program)

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
CaffeineCoder is offline Offline
54 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Graph doubt
Next Thread in Java Forum Timeline: Help, I don't think I'm asking for much. This is a simple java program.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC