Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image on applications

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello All,

I'm new with Java and I'd like to know how can I insert an image(.gif) in an application. I have some examples , but they are for applets . Can somebody help me ?

Thanks in advance

Alan
 
hi alan

you can import gif and jpg format files very simply by creating an icon e.g.

Icon yourImage = new ImageIcon("YourImage.jpg");

The only thing you have to bare in mind is the path name to the image file you want to bring in. In the case above the image file is located in the same directory as the java file. If you want to include an image in another directory you will need tyo state the full path name before the file name.

Although if your directory is also included in your path variable this should also be OK.[pipe]

hope this helps. There are other ways (such as using the toolkit utilities) but this is probably the most straightforward.
 
Hello pipk,

it´s works

Do you know which metods can I use for autosize this picture?
Is this possible?

PS: I like that the image could be enlarged

Thanks and advance.

Alan.
 
I am not sure if you can resize Icons directly, you can only really specify the position on screen rather than the size. You will probably have to create a method and pass the icon in as an abstract Graphics object to allow you to manipulate it properly.

but you may have to create it as an image instead of an icon in the following way:

Image yourImage = Toolkit.getDefaultToolkit().getImage("Diver.jpg");

it works in the same way but will allow you to manipulate the image in a method that utilises the Graphics object such as

public void paintComponent(Graphics g)

and you will then be able to use the drawImage method of the Graphics object i.e. g.drawImage, to specify your image, its X and Y positions, its height and width values as well as the component you wish to draw it on (probably this) to allow a scaled version to be shown.

Yes, its a bit more complex but is a good learning exercise to get you into things.

good luck.[pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top