Results 1 to 11 of 11
Thread: Image Loading
- 09-06-2010, 04:09 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Image Loading
I'm having a problem with loading images, than drawing them into a tile map. I read several articles about loading images but cant seem to get it right. Right now it just flashes the whole area that the map should be at green (probably the grass) then goes blank. I get no errors or anything so I'm not sure what is going on. I used this same way to draw a map but in the if files with the drawimage it was g.setColor and after the if statements g.drawRect and that worked so this looks like it should adapt fine.
Java Code:import java.applet.*; import java.awt.*; public class helloworld extends Applet { int[][] map = { //simple map each square is 16 x 16 pixels might change it to 32 {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1}, //the height and width is 240 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; int x = 0; // x,y for the array int y = 0; int xmap = 0; // x,y for drawing the array int ymap = 0; public void paint (Graphics g) { //Image grass = getImage(getCodeBase(),"grass.gif"); //also tried it this way //Image water = getImage(getCodeBase(),"water.gif"); Image grass = Toolkit.getDefaultToolkit().getImage(getClass().getResource("grass.gif")); Image water = Toolkit.getDefaultToolkit().getImage(getClass().getResource("water.gif")); g.drawString("Ran",10,290); //just says if it ran or not while (y<=14) //draws out the map { if (map[y][x] == 0) // grass { g.drawImage(grass, xmap, ymap, this); } if (map[y][x] == 1) // wall { g.drawImage(water, xmap, ymap, this); } if (map[y][x] == 2) // start { g.drawImage(grass, xmap, ymap, this); } if (map[y][x] == 3) // end { g.drawImage(grass, xmap, ymap, this); } x++; xmap = xmap + 32; if (x >= 15) { x = 0; xmap = 0; y++; ymap = ymap + 32; } } } }
- 09-06-2010, 04:48 PM #2
Try debugging your code by adding a println() in the paint method inside the loop:
System.out.println("y=" + y + ", x=" + x);
And this just before starting the loop:
System.out.println("Init y=" + y + ", x=" + x);
This will show you what is happening in your code.
- 09-06-2010, 05:41 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
I dunno, I noticed that it printed out init y more than once and that doesn't sound right is it just because the loop is in the paint method? This is driving me crazy....
- 09-06-2010, 05:58 PM #4
That would show how many times the paint method was being called.it printed out init y more than once
Also it shows what the values of x and y are when the paint method is called the second and following times. What do you expect/need to values of x, xmap, ymap and y to be in paint?
Do you ever reset them?
You need to consider that the paint() method is called when the JVM thinks the component needs to be repainted. It can be more than once as you see with the printouts.
-
A couple of recs:
How about using nested for loops rather than while loops?
You really don't want to be reading in image files from within a paint method, but rather should read the images in once and let the paint method be short, lean, and fast. Else your drawing will slow to a crawl.
- 09-06-2010, 06:35 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Ok always thought paint ran once unless called again so I never thought of resetting the integers. Is there a way to draw the graphics from a different method kinda like below.
I could not find anything on it since I'm not sure what it would be called and its kinda vague. Also I know this is not the best way to make a tile map for a game but this wouldn't be that bad if I made a game with it would it? Thanks for the help so far works perfectly now :)Java Code:cut down to show the basic thing that im getting public void drawmap () //tried drawmap(Graphics g) it was worth a try { g.drawRect(); } public void paint (Graphics g) { drawmap(); }
Ok I will work on the for loops instead of using the while. Reading the image file was out of the paint method first, but I started jumbling were it was since it wasn't working and I thought that could be the problem. Moved it out of the paint method now thanks for reminding me I would have completely forgot about it.Last edited by hobbles; 09-06-2010 at 06:48 PM.
- 09-06-2010, 07:05 PM #7
That doesn't solve the problem since paint calls your other method every time it is called.Is there a way to draw the graphics from a different method kinda like below.
An idea: if what you are drawing does NOT change, you could create a BufferedImage, get its graphics content and the graphics object to draw on that bufferedimage to create an image that you would save. This would be done one time in an initialization method
Then in paint draw that image with a single drawImage call.
- 09-06-2010, 07:53 PM #8
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
It wasn't to solve anything everything is working I just wanted to take it out of the paint method before that area starts getting messy but you're right I should make the map a buffered image. I would want different maps or a big map with a small section showing (like pokemon) so I'm sure I could get that to work. Last thing, I made a sprite that is half water and half sand. Since the water needs to be facing out of the island of sand should I rotate the image in java or should I have an already flipped image. Which way would be the more efficient way, I would think that flipping it in paint would be the easiest way but will need to load more images. Rotating it in java sounds like it could be more efficient and I wouldn't mind learning how to do that stuff (scale, rotate, ect). Thanks for all the help again this is the last question I needed help with then I'm off to get this done.
- 09-06-2010, 07:58 PM #9
Sorry, I don't have any experience flipping images, etc
-
I wonder why you are doing this in AWT and not Swing? With Swing you could user your images to create a few ImageIcons and then populate your grid with a bunch of JLabels each holding an appropriate ImageIcon.
Regarding the flipping of images, I know that it can be done with AffineTransforms, and since the images are small, I suppose you'll want to create four images/ImageIcons for each one to represent all four possible rotations.
- 09-06-2010, 08:30 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Problem with loading image
By m00nchile in forum New To JavaReplies: 5Last Post: 05-25-2010, 01:34 PM -
Problem in loading Image
By rahulm87 in forum Java 2DReplies: 1Last Post: 09-08-2009, 04:45 PM -
Loading image in applet
By syarizma in forum Advanced JavaReplies: 0Last Post: 08-06-2009, 09:02 AM -
Rambling about image loading...
By Zamppa in forum Java AppletsReplies: 0Last Post: 04-14-2009, 03:55 PM -
Loading An Image Help Please!
By shaungoater in forum Java 2DReplies: 2Last Post: 01-09-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks