Image loading and showing problem
Hi,
I'm trying to make a little java application for my website, but I have
to load images. The compiling of the code gives no errors, but when I
run it, it says "Applet not initialized" :SICK: . I know wich row the message
causes, but I don't know how to solve it.
Here's the code:
Code:
import java.awt.*;
import java.applet.*;
import java.awt.Image.*;
public class App extends Applet {
DeskTop desktop;
public void init() {
desktop = new DeskTop( );
}
public void paint(Graphics g) {
desktop.paint( g );
}
}
class DeskTop extends App {
private String desktopimagename;
private Image desktopimage;
public DeskTop( ) {
desktopimagename = "bg_Tiger.jpg";
desktopimage = super.getImage(super.getDocumentBase(), "img/" +
desktopimagename);
}
public void paint( Graphics g ) {
g.drawImage( desktopimage, 0, 0, super.WIDTH, super.HEIGHT, this );
}
}
The code wich causes the message is the 23rd row:
Code:
desktopimage = super.getImage(super.getDocumentBase(), "img/" +
desktopimagename);
Does anybody know something that can solve this :confused: ?
Thanks in advance... :)
PS: what I also tried was:
add a void to my App class:
Code:
public Image getImg( String imagename ) {
Image picture;
picture = this.getImage( this.getDocumentBase(), "img/" + imagename );
return picture;
}
and instead of using
Code:
desktopimage = super.getImage(super.getDocumentBase(), "img/" +
desktopimagename);
i used
Code:
desktopimage = super.getImg( desktopimagename );
1 Attachment(s)
A very basic image applet ++
If you want to make "something" in java that shows images you should make a class that extends a JPanel and limit its capabilities to the image rendering only. That way you have a component that you "feed" images to and it can be placed anywhere, in an applet or frame, as single or multiple instances.
Code:
import java.awt.*;
import java.awt.Image;
import java.awt.image.*;
/* a very basic image display panel */
class ImagePanel extends JPanel {
private Image img=null;
public void setImage(Image img) {
this.img=img;
repaint();
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
Color c=g.getColor();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(), getHeight());
if (img==null) {
g.setColor(Color.black);
g.drawString("No image loaded yet", 20, 20);
} else {
g.drawImage(img, 0,0,this);
}
g.setColor(c);
}
}
Then implement this in the applet:
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class App extends JApplet {
private ImagePanel imgPan=null;
public void init() {
getContentPane().setLayout(new GridLayout());
imgPan=new ImagePanel();
getContentPane().add(imgPan);
Image img = getNamedImage("\\image/bg_Tiger.jpg");
imgPan.setImage(img);
}
private Image getNamedImage(String name) {
Image img=getImage(getDocumentBase(),name);
if (img != null) {
// wait for image to arrive
MediaTracker mt=new MediaTracker(this);
mt.addImage(img, 0);
try {
mt.waitForAll();
} catch (InterruptedException ie) {
img=null;
}
}
return img;
}
}
}
(note; some io exception handling must be added...)
I have also included a slightly more elaborate image applet that allows for client interaction for zooming and panning of an image. It takes an applet parameter named "image-url" for the image server path.