How to get image to applet when using package
Hi everybody!
I'm newbie in Java programming and I'm not good in reading APIs.
I have problem viewing Images in applet when I use package.
Before I was able to do it so that I had my Applet.java and Image.gif files in /jdk/bin-folder and I just used getCodeBase()-method. However now when I use package it doesn't work that way I guess.
If I have understood it correctly I should read Image from InputStream but I don't know how. I don't want to print image on any label.
If someone could help me I would appreciate that.
I mean that thing what comes automatically if you do netbeans project. I don't know what else I should call it.
When I click projects node it displays following things: "Source Packages", "Test Packages", "Libraries", "Test Libraries".
I have my java-files in package, which I have created, under "Source Packages".
And my problem was that when I put my picture files (gifs, jpegs or png's) into this package. When I try to view that picture in my application nothing happens. I can't see that pic or anything.
This code works and shows the image when I have my Application.java and image.jpg files in /jdk1.6.0/bin -folder but not in netbeans project package (I just wrote this so it's not very beautiful animation):
/*if you have the picture this works when you first compile it and after that you type java Application.java" to commandline or terminal or whatever. */
//<APPLET code= Application.class width="800" height="800"></APPLET>
import java.awt.*;
public class Application extends java.applet.Applet implements Runnable {
int x=300;
int y=400;
int switcherX= 1;
int switcherY=1;
Thread thread;
// unnecessary doublebuffer things..
Graphics db;
Image i;
Image image;
public void init(){
i = createImage(getWidth(),getHeight());
db = i.getGraphics();
image = getImage(getCodeBase(),"image.jpg");
}
public void start(){
if (thread == null){
thread = new Thread(this);
thread.start();
}
}
public void run(){
while (thread == Thread.currentThread()){
x+=switcherX;
y+=switcherY;
if (x>=700){
switcherX*=-1;
}
if (x<=0){
switcherX*=-1;
}
if (y>=600){
switcherY*=-1;
}
if (y<=0){
switcherY*=-1;
}
repaint();
try{Thread.sleep(1);}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
db.setColor(Color.white);
db.fillRect(0,0,getWidth(),getHeight());
db.drawImage(tennis,x,y,this);
g.drawImage(i,0,0,this);
}
public void update(Graphics g){
paint(g);
}
}
yeah.... like j_shadinata said... try to copy yr image files into the build directory (or the target directory if u like)... b'coz when yr program got compiled, it compiles all your java files into classes, but not those images u have in the source folder... so, what u could do is to put those image files into the target directory instead... alternatively, u can put those images into a local directory (i.e. C:\images\), that way u can save yrself some times, also yr program can be a little bit smaller in size (coz u ain't gonna have those images attached to yr program)