Archived

This topic is now archived and is closed to further replies.

[java] Draw on an Image

This topic is 5331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone. I''m making a game and it''s got a radar system in it. The map won''t change at all so it seems kind of wasteful to do two loops just to draw the radar screen. I want to know if there''s any way of making an image and drawing it, so in my example, I could just once write the base map to the screen and then all I have to do is drfaw the ships on top of it. If anyone here can help, that''d be great... if you don''t understand what I mean just reply and I''ll try to clarify. Thanks in advance.

Share this post


Link to post
Share on other sites
Maybe I am misunderstanding your question but it sounds to me like this shouldn''t be a problem. You have a static image that you are loading for the radar map, and you want to draw other images on top of that?

If that is the case then do so. When you are rendering there is nothing in java that stops you from drawing images that over lap.

If this isn''t the case could you be a bit more specific?


"And then 2 men appeared... Men in dark suits.. with dark soulless eyes. Men like this could have come from only one place..
The bank."

Share this post


Link to post
Share on other sites
I want to create an image at runtime. For example if I wanted to create an image 100x100 and put a few lines and stuff in it, so later when I want to draw that to the screen I only have to draw the image instead of having to draw each individual line again.

Share this post


Link to post
Share on other sites

  
//the image you want to draw stuff to

Image imageToDrawTo;
//get graphics object to enable you to draw on image

Graphics2D g2 = (Graphics2D)imageToDrawTo.getGraphics();

//drawing stuff here....

g2.drawLine(.....);



That should set up your image so that when you draw that image to screen, the whole image with the lines, etc will be drawn.

Share this post


Link to post
Share on other sites
I think Kajsngweaaweg means that the base map should be drawn ONLY once and then the other images drawn over it each frame. But I would usually redraw the base map each frame so does anyone know how to avoid having to draw the base map more than once? Maybe it involves "dirty" rectangles when refreshing the component that is being drawn on?

Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,
fasting, good works and so on. Up until Doom, no one seemed to have thought about the
double-barrel shotgun. Eat leaden death, demon...
-- Terry Pratchett

[edited by - Kentaro on May 8, 2003 11:55:37 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For my radar screen/minimap, it can take a litle while to draw it every time, cuz the code is kinda like this:

for (int i = 0; i < 40; i++) {
for (int k = 0; k < 40; k++) {
switch (map[k]) {
case 1:
//then some drawing
break;
case 2: //etc, there would eventually be like 20 different types here
}
}
}

So what I want to be able to do is just at the beginning of the game, do that but instead of drawing it to the screen, I want to be able to create an image at runtime and fill it with that data (which would change depending on the map) so during the paint method, I can just do:
g.drawImage(minimap,300,300,this);
Thanks in advance

Share this post


Link to post
Share on other sites