image position

hi, I have a small image on an applet screen

is there a method like getX() and getY() which will return the coordinates of the image...note that the image can be any where on the screen

ie.

Image pic;

..........

pic = getImage(getDocumentBase(),"apic.gif");

........

int x = pic.getX()//something like this

int y = pic.getY()//something like this

thanks

[570 byte] By [zainuluka] at [2007-9-27 23:16:27]
# 1

the code you have there, creates an offscreen Image.

It has no x or y position. Images are not part of the AWT/Swing Component heirarchy, and hence are not displayable in the way.

If you want to draw an offscreen image, there are 2 ways of doing it

1.

override the void paint(Graphics g) method,

with code looking similar to

public void paint(Graphics g)

{

g.drawImage(yourImageObject,x,y,null);

}

this code will draw the image at position (x,y) each time a repaint event is posted for your Component.

2.

create an ImageIcon from the Image.

create a JLabel using the ImageIcon as the parameter

add the JLabel to the Container in which you want the image drawn.

I don't have the API handy atm, but the code would look something like

add(new JLabel(new ImageIcon(youtImageObject)));

Abusea at 2007-7-7 15:11:23 > top of Java-index,Other Topics,Java Game Development...