Image - Loading and Displaying problems

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

Join Date: Jun 2007
Posts: 10
Reputation: suriname0 is an unknown quantity at this point 
Solved Threads: 0
suriname0 suriname0 is offline Offline
Newbie Poster

Image - Loading and Displaying problems

 
0
  #1
Oct 3rd, 2009
Hello, I've been working on a fun project of my own. I didn't really start out with a plan in mind, but now it has turned into an attempt to make some Advance Wars type TBS game. The problem I ran into is trying to display the base "terrain" image beneath the grid. It produced a fairly sweet effect of "cascading" image tiles (See attachment).

The problems:
-Why is it repainting if the mouse isn't moving? (Should repaint on MouseMove)
-Why is the image not loading completely?

I found in the API that the Graphics.drawImage() displays whatever amount of the image is loaded, but why isn't the image loaded all the way, and once it is, why is it reverting back to an unloaded state between two calls of the same method?

I think I need to find some way to load the image beforehand (through a thread or something) so it stays in a loaded state. I'm afraid the API has been confusing me, and I really don't understand how Image finds and uses an image. Can someone give me a few pointers?

Thanks for any help!

Code:
The important bits:
  1. ...
  2. Images resource=new Images();
  3. int result=resource.collectResources();
  4. if (result == 0 )
  5. {
  6. JOptionPane.showMessageDialog(mainFrame, "One or more images could not be found or retreived.\nPlease try again! :)", "Fatal Error",JOptionPane.ERROR_MESSAGE);
  7. }
  8. ...
  9. private void paintTerrain(Graphics g)
  10. {
  11. for (int j=0;j<grid.rows; j++)
  12. {
  13. for (int k=0; k<grid.columns; k++)
  14. {
  15. if (grid.terrain[j][k].type == 0)
  16. {
  17. g.drawImage(/*grid.terrain[j][k].getImage()*/Images.grass01, GRID_OFFSET + (j*GRID_SIZE), GRID_OFFSET + (k*GRID_SIZE), this); //should use commented argument, but that produces no image whatsoever
  18. } // end Crucial, unit-detected, IF statement
  19. } //inner for
  20. } //outer for
  21. }
  22. ...
  23. class Images //has all the image resources
  24. {
  25. public static Image grass01;
  26. public static Image noImage;
  27.  
  28. public int collectResources()
  29. {
  30. grass01=new ImageIcon("resources/grass01.gif").getImage();
  31. noImage=new ImageIcon("resources/noImage.gif").getImage();
  32. return 1;
  33. }
  34. } //class Images ends here

The full code:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6. public class GridTest
  7. {
  8. public static void main(String[] args)
  9. {
  10. GridTestStarter starter=new GridTestStarter();
  11. starter.init();
  12. starter.start();
  13. }
  14. } //end of class GridTest
  15.  
  16. ////////////////////////////////////////////////////////////////////////////////
  17.  
  18. class GridTestStarter implements MouseMotionListener
  19. {
  20. JFrame mainFrame;
  21. JPanel mainPanel;
  22. GridPanel gridPanel;
  23. UnitGrid grid;
  24. int past_shade_x, past_shade_y=-1;
  25.  
  26. public void init()
  27. {
  28. mainFrame=new JFrame("Grid Test");
  29. mainFrame.setSize(800, 600);
  30. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.  
  32. mainPanel=new JPanel();
  33. mainPanel.setBackground(Color.WHITE);
  34.  
  35. grid=new UnitGrid(20, 20); //20 x 20 grid
  36. gridPanel=new UnitGridPanel(grid); //panel to display grid and units
  37. gridPanel.setGridOffset(20); //20 pixels away from upper left hand corner
  38. gridPanel.setGridSize(22); //22 x 22 pixels (don't know why, just seemed right)
  39. gridPanel.finalizeSize(); //use current settings to create correct Panel size in pixels
  40. gridPanel.setDoubleBuffered(false); //??? is this doing anything?
  41. gridPanel.setShading(false);
  42.  
  43. mainFrame.getContentPane().add(gridPanel);
  44.  
  45. Images resource=new Images();
  46. int result=resource.collectResources();
  47. if (result == 0 )
  48. {
  49. JOptionPane.showMessageDialog(mainFrame, "One or more images could not be found or retreived.\nPlease try again! :)", "Fatal Error",JOptionPane.ERROR_MESSAGE);
  50. }
  51. }
  52.  
  53. public void start()
  54. {
  55. mainFrame.setVisible(true);
  56. gridPanel.repaint();
  57. gridPanel.addMouseMotionListener(this);
  58. }
  59.  
  60. public void mouseMoved(MouseEvent e)
  61. {
  62. int x= e.getX() - gridPanel.getGridOffset(); //removes borders from x
  63. int y= e.getY() - gridPanel.getGridOffset();
  64. int shade_x2=-1;
  65. int shade_y2=-1;
  66. if ( ( (x>0) && (x<gridPanel.getGridSize()*grid.columns) ) && ( (y>0) && (y<gridPanel.getGridSize()*grid.rows) ) )
  67. { //if x and y are within the grid
  68. shade_x2=(int) (x/gridPanel.getGridSize() ); //discovers which square x is in x-wise
  69. shade_y2=(int) (y/gridPanel.getGridSize() ); //same for y
  70. if ( (past_shade_x != shade_x2) || (past_shade_y != shade_y2) )
  71. { //if the square is different then previous than set square to shade and repaint()
  72. gridPanel.setShade(shade_x2, shade_y2);
  73. gridPanel.repaint();
  74. }
  75. }
  76. else //x or y is outside the grid
  77. {
  78. if ( (past_shade_x != shade_x2) || (past_shade_y != shade_y2) )
  79. { //moved to outside the grid
  80. gridPanel.setShade(-1,-1); //will trigger ShadeAway procedure during repaint
  81. gridPanel.repaint();
  82. }
  83. }
  84. past_shade_x=shade_x2;
  85. past_shade_y=shade_y2;
  86. }
  87.  
  88. public void mouseDragged(MouseEvent e) {}
  89. } //end of Starter/Controller class
  90.  
  91. ////////////////////////////////////////////////////////////////////////////////
  92.  
  93. class Terrain
  94. {
  95. byte type;
  96. Image image;
  97.  
  98. public Terrain(byte type_given)
  99. {
  100. type=type_given;
  101. image=findImage();
  102. }
  103.  
  104. private Image findImage()
  105. {
  106. if (type == 0)
  107. {
  108. return Images.grass01;
  109. }
  110. return Images.noImage;
  111. }
  112.  
  113. public Image getImage()
  114. {
  115. return image;
  116. }
  117. }
  118.  
  119. ////////////////////////////////////////////////////////////////////////////////
  120.  
  121. class UnitGrid
  122. {
  123. public static final byte VISIBLE=0;
  124. public static final byte FOGGED=1;
  125. public static final byte UNDISCOVERED=2;
  126.  
  127. byte[][] unit;
  128. byte[][] FoW;
  129. Terrain[][] terrain;
  130.  
  131. int rows, columns=0;
  132.  
  133. public UnitGrid(int x1, int y1)
  134. {
  135. rows=x1;
  136. columns=y1;
  137. unit=new byte[rows][columns];
  138. terrain=new Terrain[rows][columns];
  139. FoW=new byte[rows][columns];
  140. for (int j=0;j<rows; j++)
  141. {
  142. for (int k=0; k<columns; k++)
  143. {
  144. unit[j][k]=0;
  145. terrain[j][k]= new Terrain( (byte) 0);
  146. FoW[j][k]=VISIBLE; //should be UNDISCOVERED after debugging
  147. }
  148. }
  149. }
  150. }
  151.  
  152. ////////////////////////////////////////////////////////////////////////////////
  153.  
  154. class UnitGridPanel extends GridPanel
  155. {
  156. UnitGrid grid;
  157.  
  158. public UnitGridPanel(UnitGrid gridToUse)
  159. {
  160. super(gridToUse.rows, gridToUse.columns);
  161. grid=gridToUse;
  162. setDrawGrid(false); //allows for additional drawing to occur before painting grid
  163. }
  164.  
  165. public void paint(Graphics g)
  166. {
  167. paintBackground(g); //clear to white
  168. paintTerrain(g); //do grass, etc.
  169. //paintFoW(g); //then shade black Fog of War
  170. //paintShade(g); //finally, do a shade (this should be removed and replaced with GIF drawing selection sprite)
  171. //paintUnits(g); //will draw the units onto the grid...
  172. paintGrid(g); //and finish off with the default black grid
  173. }
  174.  
  175. private void paintUnits(Graphics g)
  176. {
  177. for (int j=0;j<grid.rows; j++)
  178. {
  179. for (int k=0; k<grid.columns; k++)
  180. {
  181. if (grid.unit[j][k]!=0)
  182. {
  183.  
  184. } // end Crucial, unit-detected, IF statement
  185. } //inner for
  186. } //outer for
  187. } //end of function paintUnits()
  188.  
  189. private void paintTerrain(Graphics g)
  190. {
  191. for (int j=0;j<grid.rows; j++)
  192. {
  193. for (int k=0; k<grid.columns; k++)
  194. {
  195. if (grid.terrain[j][k].type == 0)
  196. {
  197. g.drawImage(/*grid.terrain[j][k].getImage()*/Images.grass01, GRID_OFFSET + (j*GRID_SIZE), GRID_OFFSET + (k*GRID_SIZE), this);
  198. } // end Crucial, unit-detected, IF statement
  199. } //inner for
  200. } //outer for
  201. }
  202.  
  203. private void paintFoW(Graphics g)
  204. {
  205. for (int j=0;j<grid.rows; j++)
  206. {
  207. for (int k=0; k<grid.columns; k++)
  208. {
  209. if (grid.FoW[j][k]!=UnitGrid.VISIBLE)
  210. {
  211.  
  212. } // end Crucial, unit-detected, IF statement
  213. } //inner for
  214. } //outer for
  215. }
  216. } //end of class UnitGridPanel
  217.  
  218. ////////////////////////////////////////////////////////////////////////////////
  219.  
  220. class GridPanel extends JPanel
  221. {
  222. protected int GRID_SIZE; //pixel width and height of the pixels
  223. protected int GRID_OFFSET; //width and height away from upper left-hand corner that grid begins
  224. private boolean SHADING;
  225. private boolean DRAW_GRID;
  226.  
  227. public Color gridColor;
  228. public Color shadeColor;
  229. public Color backgroundColor;
  230.  
  231. int columns, rows;
  232. private int shade_x, shade_y = 0;
  233.  
  234. public GridPanel(int rowss, int columnss)
  235. {
  236. gridColor=Color.BLACK;
  237. shadeColor=new Color(198,226,255); //BGR values for square shading (blueish)
  238. backgroundColor=Color.WHITE;
  239. SHADING=true;
  240. DRAW_GRID=true; //by default grid is drawn, and shading is active
  241. GRID_SIZE=20; //individual grid nodes 20 x 20 pixels
  242. GRID_OFFSET=0; //by default, grid is in Upper Left hand corner
  243. rows=rowss;
  244. columns=columnss; //ensure at least a size of 1
  245. }
  246.  
  247. public void finalizeSize()
  248. {
  249. setSize( (columns*GRID_SIZE) + columns, (rows*GRID_SIZE) + rows ); //set the size to what is needed for drawing and stuff.
  250. }
  251.  
  252. public void paint(Graphics g)
  253. {
  254. paintBackground(g); //clear drawing board, including gridoffset, with background color. Default white
  255. paintShade(g); //paints or removes shading before grid is drawn
  256. if (DRAW_GRID) //grid drawing can be turned off here, either for a different timing or another reason by subclasses.
  257. {
  258. paintGrid(g);
  259. }
  260. }
  261.  
  262. protected void paintBackground(Graphics g)
  263. {
  264. g.setColor(backgroundColor);
  265. g.fillRect(0,0,(GRID_OFFSET*2)+(GRID_SIZE*columns), (GRID_OFFSET*2)+(GRID_SIZE*rows) );
  266. }
  267.  
  268. protected void paintShade(Graphics g)
  269. {
  270. if (SHADING) //allow user to turn off shading
  271. {
  272. if (shade_x >= 0) //shade != -1 indicates shading required
  273. {
  274. shade(g); //shades individual square
  275. }
  276. else
  277. {
  278. shadeAway(g);
  279. }
  280. }
  281. }
  282.  
  283. protected void paintGrid(Graphics g)
  284. {
  285. g.setColor(gridColor); //default Black
  286. for (int j=0; j<=rows; j++)
  287. {
  288. g.drawLine( GRID_OFFSET,(j*GRID_SIZE)+GRID_OFFSET,GRID_SIZE*columns+GRID_OFFSET,(j*GRID_SIZE)+GRID_OFFSET );
  289. }
  290. for (int j=1; j<=columns; j++)
  291. {
  292. g.drawLine( (j*GRID_SIZE)+GRID_OFFSET, GRID_OFFSET, (j*GRID_SIZE)+GRID_OFFSET, GRID_SIZE*rows+GRID_OFFSET );
  293. }
  294. g.drawLine(GRID_OFFSET, GRID_OFFSET, GRID_OFFSET, GRID_OFFSET+(rows*GRID_SIZE) ); //not sure why this is here, and not just in the second For loop's iteration
  295. }
  296.  
  297. private void shade(Graphics g)
  298. {
  299. g.setColor(shadeColor); //default color
  300. g.fillRect(GRID_OFFSET + (GRID_SIZE * shade_x), GRID_OFFSET + (GRID_SIZE * shade_y), GRID_SIZE, GRID_SIZE);
  301. //starts drawing with whatever shade_x/_y is (0,0 indicates upper left node) and draws size equal in pixels to GRID_SIZE
  302. }
  303.  
  304. private void shadeAway(Graphics g) //exactly the same as paintBackground(), this ensures that shading is removed. ////Possibly obsolete/unnecessary?
  305. {
  306. g.setColor(backgroundColor);
  307. g.fillRect(GRID_OFFSET, GRID_OFFSET, (columns*GRID_SIZE)+GRID_OFFSET, (rows*GRID_SIZE)+GRID_OFFSET);
  308. }
  309.  
  310. public void setShade(int xs, int ys)
  311. {
  312. shade_x=xs;
  313. shade_y=ys;
  314. }
  315.  
  316. public void setShading(boolean tof)
  317. {
  318. SHADING=tof;
  319. }
  320.  
  321. public boolean getShading()
  322. {
  323. return SHADING;
  324. }
  325.  
  326. public void setDrawGrid(boolean tof)
  327. {
  328. DRAW_GRID=tof;
  329. }
  330.  
  331. public boolean getDrawGrid()
  332. {
  333. return DRAW_GRID;
  334. }
  335.  
  336. public void setGridOffset(int offsetInPx)
  337. {
  338. GRID_OFFSET=offsetInPx;
  339. }
  340.  
  341. public int getGridOffset()
  342. {
  343. return GRID_OFFSET;
  344. }
  345.  
  346. public void setGridSize(int sizeInPx)
  347. {
  348. GRID_SIZE=sizeInPx;
  349. }
  350.  
  351. public int getGridSize()
  352. {
  353. return GRID_SIZE;
  354. }
  355. } //end of GridPanel class
  356.  
  357. ////////////////////////////////////////////////////////////////////////////////
  358.  
  359. class Images //has all the image resources
  360. {
  361. public static Image grass01;
  362. public static Image noImage;
  363.  
  364. public int collectResources()
  365. {
  366. grass01=new ImageIcon("resources/grass01.gif").getImage();
  367. noImage=new ImageIcon("resources/noImage.gif").getImage();
  368. return 1;
  369. }
  370. } //class Images ends here
Attached Thumbnails
greenMess.png  
Reply With Quote Quick reply to this message  
Reply

Tags
image

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC