-
JCreator image
Hello,
I have started using Java very recently, and I want to display an image in an applet. I'm pretty sure theres nothing wrong with the coding, but when I run the applet the applet window appears blank. I am using JCreator, and I think that the image isn't showing because it can't find the image. (The image is a 32 by 32 GIF) I have heard that you need to place the needed images in a certain folder or you have to include the image path, or something. And I am also wondering can you display an image in an applet before JARing it?
Any code snippets, full codes or instructions on where to place the image to use it in an applet are appreciated.
-
have you tried converting the GIF to a JPG?
-
I don't know about JCreator but I might be able to help you show an image in an applet.
Since the applet security sandbox doesn't allow us to make a new File or URL it is usually easier to use the Class method getResource. This method delegates to the class loader which will look for the resource, your image, on the classpath. That's the first thing to be aware of: the resource must be on your classpath to be found. Also, if there are multiple resources only the first one found will be returned. This method will look for resources in jar files.
To help with debugging along the way you can write out the returned URL to see where java thinks the file is.
Another possibility is to load the image in another class (ie, outside the applet).
Generally you can put your images in a folder named "images" and put this folder in the folder you do your developing in. So your current directory, the one you are working in will have a folder named "images" and it will contain your image(s).
Then you fetch the image with:
Code:
String path = "images/myImage.ext";
URL url = getClass().getResource(path);
System.out.println("url = " + url);
You can hand this url to any of:
1 — the newest way to load images: ImageIO.[i]read[/i],
2 — the older ImageIcon constructor or
3 — any of the antique Applet/Toolkit.[i]getImage[/i] ot Toolkit.[i]createImage[/i] methods that have a URL parameter.
The extension, "ext" above, depends on what methods and/or java version you are using.
ImageIcon and the antique methods(3) will load gif, jpg and png image formats.
ImageIO will load all of these plus, in j2se 1.5+, bmp and wbmp images.
For feedback about loading errors:
1 — ImageIO throws (IllegalArgument) exceptions
2 — ImageIcon has a getImageLoadStatus method you can use
3 — the antique methods require a MediaTracker to actually load the image data and this class has methods you can use to track/query the loading success/status. Without this extra work you get no feedback about what went wrong: couldn't find the file, corrupted/unreadable image data, unrecognizable/unsupported format, etc.
can you display an image in an applet before JARing it
Yes, as above.
-
Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public class Test extends Applet {
Image img = null;
Image rot = null;
int buffer[] = new int[32 * 32];
int rotate[] = new int[32 * 32];
public void init() {
try {
MediaTracker tracker = new MediaTracker (this);
img = getImage(new URL(getDocumentBase(), "gumby.gif"));
tracker.addImage (img, 0);
tracker.waitForAll();
PixelGrabber grabber =
new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32);
try {
grabber.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
for(int y = 0; y < 32; y++) {
for(int x = 0; x < 32; x++) {
rotate[((32-x-1)*32)+y] = buffer[(y*32)+x];
}
}
rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void update( Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0,this);
g.drawImage(rot,0, 40, this);
}
}
-
With most IDEs, when you make a new applet you have two main folders; "src" and "classes". In my IDE, (JCreator) "classes" has the HTML file and "src" has all of the coding. I created an "Images" Folder in src and added the picture. I used the code I displayed above and the applet appeared blank. What am I doing wrong?
-
Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
No. This
Code:
URL url = new URL(getDocumentBase(), "gumby.gif");
will have java look for the image "gumby.gif" in the same folder as your document/html file is in and not inside another folder.
If the "Images" folder is in the same folder as your document/html file then you would look for it with:
Code:
URL url = new URL(getDocumentBase(), "Images/gumby.gif");
I loaded a gif contained in an "images" folder with this:
Code:
MediaTracker tracker = new MediaTracker (this);
// your original url construction
URL url = new URL(getDocumentBase(), "gumby.gif");
// where will it point?
System.out.println("url = " + url);
// is there such a file?
System.out.println("exists: " +
new java.io.File(url.getPath()).exists());
// try duke in the "images" folder
url = getClass().getResource("images/dukeWaveRed.gif");
// where...
System.out.println("url = " + url);
// exists?
System.out.println("exists: " +
new java.io.File(url.getPath()).exists());
// this loaded the image okay
img = getImage(new URL(getDocumentBase(), "images/dukeWaveRed.gif"));
and got this in the console:
Code:
C:\jexp>javac testapplet.java
C:\jexp>appletviewer TestApplet.java
url = file:/C:/jexp/gumby.gif
exists: false
url = file:/C:/jexp/images/dukeWaveRed.gif
exists: true
The img loading was successful and both images showed up.
-
"classes" has the HTML file and "src" has all of the coding.
I would have thought it would be the other way around: the source (.java) files in the "src" folder and the compiled byte code (.class files) in the "classes" folder. But that's not important.
The documentBase should point to the folder where your html file is located and the codeBase should be the folder that contains the compiled (.class) file(s).
You can write these out with System.out.println to see where it takes you.
I added an html comment at the top of your source file and compile/run the applet with appletviewer (html files are too chaotic for development because of caching problems):
Code:
// <applet code="TestApplet" width="200" height="200"></applet>
Then adding these statements in init
Code:
System.out.println("documentBase: " + getDocumentBase());
System.out.println("codeBase: " + getCodeBase());
gives this in the console:
Code:
C:\jexp>appletviewer TestApplet.java
documentBase: file:/C:/jexp/TestApplet.java
codeBase: file:/C:/jexp/
-
How should I incorporate your code with my Test applet code, displaying only the gumby.gif (rotating)
-
This is the revised version of my code. The applet still appeared blank. I had the image folder in and everything, could you revise my code?
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public class TowerGame extends Applet {
Image img = null;
Image rot = null;
int buffer[] = new int[32 * 32];
int rotate[] = new int[32 * 32];
public void init() {
try {
MediaTracker tracker = new MediaTracker (this);
img = getImage(new URL(getDocumentBase(), "gumby.gif"));
tracker.addImage (img, 0);
tracker.waitForAll();
System.out.println("documentBase: " + getDocumentBase());
System.out.println("codeBase: " + getCodeBase());
PixelGrabber grabber =
new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32);
try {
grabber.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
for(int y = 0; y < 32; y++) {
for(int x = 0; x < 32; x++) {
rotate[((32-x-1)*32)+y] = buffer[(y*32)+x];
}
}
rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void update( Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0,this);
g.drawImage(rot,0, 40, this);
}
}
-
You're still looking for the image in the same folder as your html file.
You can copy the image to that folder if you want to load it from there. If you want to load it from an "images" type folder you should include the folder in your relative path. So if your folder name is "images" your revised img assignment statement would look like this:
Code:
img = getImage(new URL(getDocumentBase(), "images/gumby.gif"));
This tells java: start in the folder in which the html file is located, ie, the documentBase, and find the "images" folder. Go inside this folder and find the "gumby.gif" file.
Since you are focusing on image loading let's try this:
Code:
// <applet code="TestApplet" width="400" height="200"></applet>
import java.applet.Applet;
import java.awt.*;
import java.net.*;
public class TestApplet extends Applet {
Image[] images = new Image[4];
public void init() {
try {
System.out.println("documentBase: " + getDocumentBase());
System.out.println("codeBase: " + getCodeBase());
String path = "images/dukeWaveRed.gif";
URL docBaseUrl = new URL(getDocumentBase(), path);
System.out.println("docBaseUrl = " + docBaseUrl);
images[0] = getImage(docBaseUrl);
URL codeBaseUrl = new URL(getCodeBase(), path);
System.out.println("codeBaseUrl = " + codeBaseUrl);
images[1] = getImage(codeBaseUrl);
URL resourceUrl = getClass().getResource(path);
System.out.println("resourceUrl = " + resourceUrl);
images[2] = getImage(resourceUrl);
URL directUrl = new URL("file:/C:/jexp/images/dukeWaveRed.gif");
System.out.println("directUrl = " + directUrl);
images[3] = getImage(directUrl);
MediaTracker tracker = new MediaTracker(this);
for(int i = 0; i < images.length; i++) {
tracker.addImage (images[i], 0);
}
tracker.waitForAll();
} catch(Exception e) {
System.out.println(e.getClass().getName() + ": " +
e.getMessage());
}
}
public void update( Graphics g) {
paint(g);
}
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
int iw = images[0].getWidth(this);
int ih = images[0].getHeight(this);
int pad = 5;
int x0 = (w - images.length*iw - (images.length-1)*pad)/2;
int y = (h - ih)/2;
int x = x0;
for(int i = 0; i < images.length; i++) {
g.drawImage(images[i], x, y, this);
x += iw + pad;
}
}
}
I'm not having any trouble with sandbox security issues while running this in the appletviewer.
Here's the console output:
Code:
C:\jexp>javac testapplet.java
C:\jexp>appletviewer TestApplet.java
documentBase: file:/C:/jexp/TestApplet.java
codeBase: file:/C:/jexp/
docBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif
codeBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif
resourceUrl = file:/C:/jexp/images/dukeWaveRed.gif
directUrl = file:/C:/jexp/images/dukeWaveRed.gif
Make sure you change the path statement in the code above to suit your folder/imagefile:
String path = "images/dukeWaveRed.gif";
-
Oh my gosh, hardwired you are a LIFE SAVER. Thank you so much!!! I can FINALLY display an image, you have no idea how frustrating it has been (Well, if you are a programmer, frustration is probably one thing you understand very well)
Thank You!
(How do you mark a forum string [SOLVED]?)