Image in applet

Hi,

I'm trying to import an image to my applet. But when runnning the applet, the image won't appear. I get no errors when compiling, and the url to the imagefile is correct.

Does someone understand why this isn't working? It should work, shouldn't it?

import java.awt.*;

import java.applet.*;

public class ImageDemo extends java.applet.Applet {

private Image image;

public void paint(Graphics gr)

{

gr.drawImage(image, 10,10, this);

}

public void init() {

super.init();

image = getImage(getCodeBase(), "testimage.jpg");

resize(620,220);

}

}

[650 byte] By [Mexela] at [2007-11-27 11:18:03]
# 1

Because your not calling the paint method.

Firstly next time you post use the code tags.

Secondly, add this in somewhere:

public void init() {

}

public void start() {

paint(this.getGraphics);

}

Havent tested this code but *should* work

Futurisdom_Developera at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 2

No, sorry it doesn't work. I already have a init method, which gets the image. And a call to paint(or I guess you mean 'repaint'. You never call 'paint'. Only the VM calls 'paint') shouldn't be necessary, since 'paint' will be called automaticly by the VM when the applet starts.

Yes, I should use code tags. But how do I do that? =) I'm new to this forum.

Appreciate your help.

/Mexel

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 3

// <applet code="ImageDemoRx" width="100" height="100"></applet>

import java.applet.Applet;

import java.awt.*;

public class ImageDemoRx extends Applet {

private Image image;

public void paint(Graphics gr) {

gr.drawImage(image, 10,10, this);

}

public void init() {

super.init();

image = getImage(getCodeBase(), "testimage.jpg");

//"images/bclynx.jpg");

loadImage();

resize(620,220);

}

private void loadImage() {

MediaTracker tracker = new MediaTracker(this);

tracker.addImage(image, 0);

try {

tracker.waitForID(0);

} catch(InterruptedException e) {

System.out.println("loading interrupted");

}

int status = tracker.statusID(0, false);

String s = "";

if((status & MediaTracker.LOADING) == MediaTracker.LOADING)

s += "LOADING ";

if((status & MediaTracker.ABORTED) == MediaTracker.ABORTED)

s += "ABORTED ";

if((status & MediaTracker.ERRORED) == MediaTracker.ERRORED)

s += "ERRORED ";

if((status & MediaTracker.COMPLETE) == MediaTracker.COMPLETE)

s += "COMPLETE";

System.out.println("Image loading status = " + s);

}

}

crwooda at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 4

No, that doesn't work for me either.

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 5

code taging my code

import java.awt.*;

import java.applet.*;

public class ImageDemo extends java.applet.Applet {

private Image image;

public void paint(Graphics gr)

{

gr.drawImage(image, 10,10, this);

}

public void init() {

super.init();

image = getImage(getCodeBase(), "testimage.jpg");

resize(620,220);

}

}

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 6

why are you using applet instead of japplet? why painting directly on an applet rather than painting on a panel of sorts that is added to the applet. I've seen images imported onto imageicons that are shown on jlabels, though i don't know if this works with applets.

petes1234a at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 7

I'm missing something. I've tried example code from books and webpages. But they don't work either. I can draw figures and strings in the applet. But no image appears when using 'drawImage'. I don't get it. Anybody?

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 8

ok, thanks. I will check that out. But I still want an answer why it doesn't work for me. =)

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 9

could your image path be off? I can load into an applet like so:

import java.awt.*;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageDemo extends java.applet.Applet

{

private Image image;

private String imageString = ".\\petes\\brief\\img_0109b.jpg";

public void paint(Graphics gr)

{

gr.drawImage(image, 10, 10, this);

}

public void init()

{

super.init();

try

{

File file = new File(imageString);

image = ImageIO.read(file);

resize(620, 220);

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

petes1234a at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 10

Another way to do this using JApplet and JPanel is like so:

The JPanel that holds the drawing:

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class DrawOnJPanel extends JPanel

{

private String imageString = ".\\petes\\brief\\img_0109b.jpg";

private ImageIcon myImageIcon = new ImageIcon(imageString);

private JLabel myLabel = new JLabel(myImageIcon);

public DrawOnJPanel()

{

super();

add(myLabel);

}

}

The JApplet that holds the JPanel

import javax.swing.JApplet;

public class DrawOnJApplet extends JApplet

{

private DrawOnJPanel drawPane = new DrawOnJPanel();

public void init()

{

getContentPane().add(drawPane);

}

}

petes1234a at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 11

Hmm..my applet says "The applet has not been initialised" when I'm trying your code. I checked the path several times, so it it's not that. But I've discovered that I don't get any errors if I change the filename in the code to some filename that isn't valid. You would expect it to protest then, but it doesn't. Maybe that's a clue.

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 12

OK, I'll try that too

Mexela at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 13

Mexel.

If you call the paint method yourself, you can make your applet more flexible (i.e have it with more parameters).

public void init() {

}

public void start() {

paint(this.getGraphics, "This message can change");

}

public void paint(Graphics g, String msg) {

g.drawString("msg", 5, 15);

}

Ill copy some example code of my game that uses images.

import java.applet.*;

import java.awt.*;

public class Client extends Applet implements Runnable {

Image grass;

public void init() {

grass = getImage(getCodeBase(),"image/grass.png");

}

public void start() {

new Thread(this).start();

}

public void run() {

while(true) {

runReturn();

try {

Thread.sleep(10);

} catch(InterruptedException e) {

}

}

}

public void runReturn() {

paint(this.getGraphics());

}

public void paint(Graphics g) {

g.drawImage(grass,0,0,null);

}

}

I tested the above code and it does in fact work.

Futurisdom_Developera at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 14

I wish I could have helped you. At one time I was keen on using Swing and developing UI for users. It didn't take me much time to lose interest in it. A boring job, which requires very less thinking.

qUesT_foR_knOwLeDgea at 2007-7-29 14:28:29 > top of Java-index,Java Essentials,Java Programming...
# 15

Futurisdom_Developer .

When I create a new applet, copy-paste your code and run it, I get "Applet has not been initialised". I think we should start from there. Have you any idea why my applet won't start, but yours will with the same code? I seem to do something wrong.

If I run my own code however(the one above), atleast my applet starts and I can draw figures and text. But I can't display images.

Mexela at 2007-7-29 14:28:33 > top of Java-index,Java Essentials,Java Programming...