Loading Images in Java Applications/Frames
Hi, I'm making a game for my Java class in school, I've been looking around for about a week and read through a lot of tutorials about loading 2D Java Images...none of them have worked.
Can anyone provide me with a simple source code, or something that will load an image through a JFrame, or even allow it to run through GUI?
NOOO APPLETS!! That's all I've seen, and I've gotten it to work on an applet, just not in an actual application.
This code illustrates a really easy was to create an image in your JFrame from a picturefile on your local disc. note that a Mediatracker is used to prevent you from using the image before it has loaded. "picturefilepath" might e.g be "apple.gif" (located in the same folder as the classfile:
Image img = getToolkit().getImage("picturefilepath");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try{
mt.waitForID(0);
}catch(InterruptedException ie){
;
}
}
This snippet should draw the image using the local Graphics object:
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
follow this link it may help u, http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html#Java2D-Tutorial-Paint-Tiled-Images
nada at 2007-7-13 9:07:40 >

package myprojects.imageloadertest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @(#)ImageLoaderTest.java
*
* JFC Sample application
*
* @author
* @version 1.00 06/02/14
*/
public class ImageLoaderTestFrame extends JFrame {
/**
* The constructor.
*/
public ImageLoaderTestFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
Image img = getToolkit().getImage("crono.gif");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try{
mt.waitForID(0);
}catch(InterruptedException ie){
;
}
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageLoaderTestFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("ImageLoaderTestFrame");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ImageLoaderTestFrame.this.windowClosed();
}
}
);
}
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
That is what I have so far, except I can't find the place to actually load it up.
Its as easy as this:
package foo;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
public class Test extends Canvas{
BufferedImage img;
public Test(){
try{
System.out.println(getClass().getResource("/bar/trisk.png"));
img=ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("/bar/trisk.png")));
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
setSize(img.getWidth(),img.getHeight());
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
}
public void paint(Graphics g){
if(img!=null)
g.drawImage(img,0,0,null);
g.dispose();
}
public static void main(String[]args){
new Test();
}
}
Doubleclickable jar, contains the source too:
http://kaioa.com/k/jarimage.jar
If you dont want to load the images asynchronously, use ImageIO like I did.
Also note that I used a leading slash, which makes the path sorta absolute (relative to the roots of all jars/directories in the classpath).
oNyxa at 2007-7-13 9:07:40 >

package foo;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
public class Test extends Canvas{
BufferedImage img;
public Test(){
try{
System.out.println(getClass().getResource("C:/Source/crono.gif"));
img=ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("C:/Source/crono.gif")));
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
setSize(img.getWidth(),img.getHeight());
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
}
public void paint(Graphics g){
if(img!=null)
g.drawImage(img,0,0,null);
g.dispose();
}
public static void main(String[]args){
new Test();
}
}
That's the code I have...
Error
null
Exception in thread "main" java.lang.NullPointerException
at Test.<init>(Test.java:17)
at Test.main(Test.java:30)
img=ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("C:/Source/crono.gif")));
That doesnt work (apparently).
If you want to use absolute pathes... say with some File object, you got from an JFileChooser... you have to do something like:
img=ImageIO.read(new FileInputStream(file));
or
img=ImageIO.read(new FileInputStream("C:/Source/crono.gif"));
oNyxa at 2007-7-13 9:07:40 >

Ooops... ImageIO can read from files directly, too.Eg:img=ImageIO.read(file);orimg=ImageIO.read(new File("C:/Source/crono.gif"));
oNyxa at 2007-7-13 9:07:40 >
