Code Problem (Modifying Book Code to adapt to program)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 39
Reputation: CaffeineCoder is an unknown quantity at this point 
Solved Threads: 0
CaffeineCoder CaffeineCoder is offline Offline
Light Poster

Code Problem (Modifying Book Code to adapt to program)

 
0
  #1
Mar 9th, 2008
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)
  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)
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 516
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #2
Mar 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 39
Reputation: CaffeineCoder is an unknown quantity at this point 
Solved Threads: 0
CaffeineCoder CaffeineCoder is offline Offline
Light Poster

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

 
0
  #3
Mar 9th, 2008
Originally Posted by Ezzaral View Post
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 516
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #4
Mar 9th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 39
Reputation: CaffeineCoder is an unknown quantity at this point 
Solved Threads: 0
CaffeineCoder CaffeineCoder is offline Offline
Light Poster

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

 
0
  #5
Mar 9th, 2008
Originally Posted by Ezzaral View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC