PDA

View Full Version : Java Drawing Multiple Images



Kodeine
February 28th, 2013, 08:53 PM
Iím attempting to create a simple point-n-click adventure game in Java. Iím currently trying to get the fundamental aspect of it down that is drawing images on the screen. The scenario is going to be the same almost all the time whereby I will need to draw a background image and then several other images (characters etc) over the top of the background. Later on I will need to move character images, I imagine this will involve needing to draw the background again followed by the same character images but moved slightly.

I was recently working under the idea of using ĎGraphic2Dí and thatís why you will see it popup in the code below. My original idea was to load an image and add it to a jlabel. Then use setLocation() to position the image in the frame.


public static Graphics2D Graphic = LoadImg("Images/Backdrops/Outside1.png").createGraphics();
//Graphic.setRenderingHint(RenderingHints.KEY_ANTIAL IASING,RenderingHints.VALUE_ANTIALIAS_ON);
//Create graphic that will hopefully hold all images displayed on the JFrame.

public static void main(String[] args)
{
JFrame Window = new JFrame();
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
Window.setSize(1000, 625);
Window.setVisible(true);
//Setup window using swing

Window.getContentPane().add(DrawImg("Images/Backdrops/Outside1.png",500,0));
//Window.getContentPane().add(DrawImg("Images/Characters/Test.png",500,0));
}

public static JLabel DrawImg(String ImageLoc, int x, int y)
{
BufferedImage Image = LoadImg(ImageLoc);

//Graphic.drawImage(Image, x, y, null);
//Graphic.dispose(); //!

JLabel ReadyLabel = new JLabel(new ImageIcon(Image));
ReadyLabel.setLocation(x,y); //Doesn't seem to maneuver the image?
return ReadyLabel;
}

public static BufferedImage LoadImg(String ImgPath)
{
BufferedImage buffer = null;
try
{
buffer = ImageIO.read(new File(ImgPath));
}
catch (Exception e){}
return buffer;
}
setLocation() however does not place the image where specified. There is also the problem of only displaying one image at a time. How can I accomplish these two things? Do I need to use either Graphic2D or Canvas?

Zugzwang
March 1st, 2013, 07:18 AM
Hi Kodeine,

I suggest that you look up what the "setLocation" location function does. It sets the location of a component *in a layout*, and not in pixels. If your layout contains only one element, then it doesn't matter if you put an element at position (500,0) or (0,0) - the other layout cells are simply not used.

If you want to get fine-grained control of where stuff is drawn, then you will need to paint on your own. Either you derive a JPanel and override the paining methods, *or* you allocate a BufferedImage of the correct size and you draw *into* that image what you want to draw. There are examples for that on the web. After finishing the BufferedImage, you can convert it to an ImageIcon and assign it to a JLabel. This approach of doing things is mainly used for the case that what you want to display doesn't change often.

slickymaster
March 1st, 2013, 12:29 PM
Hi Kodeine,

I suggest that you look up what the "setLocation" location function does. It sets the location of a component *in a layout*, and not in pixels. If your layout contains only one element, then it doesn't matter if you put an element at position (500,0) or (0,0) - the other layout cells are simply not used.

If you want to get fine-grained control of where stuff is drawn, then you will need to paint on your own. Either you derive a JPanel and override the paining methods, *or* you allocate a BufferedImage of the correct size and you draw *into* that image what you want to draw. There are examples for that on the web. After finishing the BufferedImage, you can convert it to an ImageIcon and assign it to a JLabel. This approach of doing things is mainly used for the case that what you want to display doesn't change often.

Completely correct.

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with it, because not only it manages the image in memory but also provides methods for storing, interpreting, and obtaining pixel data.