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" . 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 );
}
}
Yes that does work, but my problem actually is that I want to make other classes as well who will draw images (this DeskTop class is just a small test to draw such an image in a class).
I have had one year of Java at school now an I know how to write these classes etc., but I never used images before.
They aren't handled in my book ("En dan is er... Java" from Academic Service) and the way you told me is the same way I found on the net .
But my way doesn't seem to work to load external images in other classes.
Maybe you know how I should do this or know a site where I can find a clear explanation?
Note: F:\www\sachavdk.be\maclook\app\App.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
And when I compiled it with the -Xlint argument, I got these errors:
F:\www\sachavdk.be\maclook\app\App.java:28: warning: [deprecation] show() in java.awt.Window has been deprecated
a.show();
^
F:\www\sachavdk.be\maclook\app\App.java:12: warning: [serial] serializable class App has no definition of serialVersionUID
public class App extends Frame {
^
F:\www\sachavdk.be\maclook\app\App.java:32: warning: [serial] serializable class DeskTop has no definition of serialVersionUID
class DeskTop extends Frame {
^
3 warnings
I've had a lot of errors but never ones like these.
(However I hope to get this working, then I've learned something)
when i run this program on NetBeans IDE 5.0
I didnot see any problem, also i just learn Java only 6 months,
so i have no much experience on Java. OK i will try another way
I just compile (javac App.java) and run (java App) , this working,
You have to make sure , Using Set Classpath=classPath ;C:\......
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);
}
}
(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.
Last edited by sjalle; 07-13-2006 at 09:02 PM.
Reason: io exception
I think I have to clearly explain what I'm out to make.
The idea is that I create some kind of MDI window (more windows desktop-style).
The DeskTop class shoul be the background, and a TaskBar class should hold some buttons, a clock, ...)
A WinForm class would represent a window with elements.
Etc.
Now these classes all use images, and to draw them easily in each class I am looking for a way to draw them in the class self (using a method paint( Graphics g) and in de main public class App I use the paint() method to call the paint methods of the subclasses.
I hope this brings some clearity
PS: it's only the image drawing that causes my problem , not the rest of the code if you should think "he wants to make something without knowing what he is doing" or so . I never worked with them and my book doesn't really give me a good explaination about images...
PPS: your (sjalle) code is working for my public class App but I would like to draw an image in
If you use swing you should override the paintComponent(Graphics g) method of JComponent. That class is part of the base for (nearly) all visible components. This method, if used in the ImagePanel, will give you a panel that you can draw on, while at the same time you add components to it, like buttons, other panels, etc. Just remember to use absolute positioning (setBounds) for these components and a layoutmanager = null for the panel.
You could also make your DeskTop an extension of JDesktopPane and override its paintComponent method for doing the drawing. The JDesktopPane is the base for MDI applications in java, you can add JInternalFrames to it. Check out the documentation for this.
Made some progress .
Writing from the DeskTop class works, but it doesn't fully support the ImagePanel.
Code:
class DeskTop extends JComponent {
public ImagePanel imgPan=null;
private Image img;
public DeskTop() {
getContentPane().setLayout(new GridLayout());
imgPan=new ImagePanel();
getContentPane().add(imgPan);
img = getNamedImage( "bg_Tiger.jpg" );
imgPan.setImage( img );
}
public void paintComponent( Graphics g ) {
g.drawImage( img, 0, 0, App.WIDTH, App.HEIGHT, this );
}
private Image getNamedImage(String name) {
Image img=getImage( getDocumentBase(),"img/"+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;
}
}
I've the feeling I'm doing something wrong with the ImagePanel class
The errors returned:
F:\www\sachavdk.be\look\app\App.java:52: cannot find symbol
symbol : method getContentPane()
location: class DeskTop
getContentPane().setLayout(new GridLayout());
^
F:\www\sachavdk.be\look\app\App.java:54: cannot find symbol
symbol : method getContentPane()
location: class DeskTop
getContentPane().add(imgPan);
^
F:\www\sachavdk.be\look\app\App.java:64: cannot find symbol
symbol : method getDocumentBase()
location: class DeskTop
Image img=getImage( getDocumentBase(),"img/"+name);
^
3 errors
You have made your DeskTop class an extension of JComponent, don't do that, its too high in the class hierachy for your use. Make it a JApplet extension. An applet is really just a panel (component) equipped for travelling on the internet . - and its a JComponent descendant.
So. change
Code:
class DeskTop extends JComponent
to
Code:
class DeskTop extends JApplet
then just rename your constructor:
Code:
public DeskTop()
to
Code:
public void init()
the init method is invoked by the browser when your applet page is shown.
Btw: I have included a hybrid version (applet and application in one) here, its really no big deal..
Code:
/**
* A hybrid applet, runs as an application and as applet
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.Image;
public class HybridApplet extends JApplet {
private ImagePanel imgPan=null;
private boolean isStandalone=false; // true when run from main()
private static String localPath="c:\\tmp"; // image file location on local computer
/**
* Initialize the applet
*/
public void init() {
getContentPane().setLayout(new GridLayout());
imgPan=new ImagePanel();
getContentPane().add(imgPan);
Image img = getNamedImage("\\imagerolls/rd_59_2_1055.gif");// relative to document root (web)
imgPan.setImage(img);
}
/**
* Tell the applet where (what) it is...
* @param isStandalone
*/
public void setStandalone (boolean isStandalone) {
this.isStandalone=isStandalone;
}
/**
* This metod retrieves image from local disk or over the net depending on
* the isStandalone flag.
* @param name
* @return Image
*/
private Image getNamedImage(String name) {
Image img=null;
if (isStandalone) {
try {
/* application */
FileInputStream in = new FileInputStream(localPath+name);// add on local path
int n = in.available();
byte[] b = new byte[n];
in.read(b);
in.close();
img=Toolkit.getDefaultToolkit().createImage(b);
}
catch (IOException ex) {
ex.printStackTrace(); // bummer
img=null;
}
} else {
/* applet */
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) {
ie.printStackTrace();
img = null;
}
}
}
return img;
}
/**
* This main() method is only executed when the applet is
* run standalone like an application. If this code is published
* in a web page the main() will have no effect, as the browser will
* do the initialization and embed the applet in its browser window.
* Note: the applet is in effect just a JPanel when used like this, it
* is quite handy for quick testing.
*/
public static void main(String[] args) {
JFrame f=new JFrame("Standalone applet");
HybridApplet theApplet=new HybridApplet();
f.getContentPane().setLayout(new GridLayout());
f.getContentPane().add(theApplet);
theApplet.setStandalone(true); // important
theApplet.init(); // must do it yourself here, no browser
f.addWindowListener(new WindowAdapter() { // ensure proper terminaton
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setBounds(20,20, 500,400); // whatever...
f.setVisible(true);
}
}
/* 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 paintComponent(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);
}
}
Last edited by sjalle; 07-16-2006 at 10:07 AM.
Reason: typos