PDA

View Full Version : Java Applet help


AsifTheManRahman
March 16, 2005, 12:09 AM
< //html >
< //body >
<//p >< applet code ="Applet1.class" width=700 height=400>
<///applet ><// /p>
< /body// >
< ////html >

So this is the html code. When I try running it on the browser, I get an "Applet Applet1 notinited" error, and the applet doesn't show up. I have my .class file in the same folder as my .html file. I've tried codebase and archive, but those don't work. I have Java enabled on my browser. Any ideas anyone?

I wouldn't have needed it done straightaway (since I am still working on the applets) hadn't it been for testing purposes.

Thanks

Edited on, March 16, 2005, 5:09 AM GMT, by AsifTheManRahman.

Mahmood
March 16, 2005, 09:39 AM
You can try this..

<#OBJECT codetype="application/java"
classid="java:Applet1.class"
width="700" height="400">
<#/OBJECT>

However, if still does not work, then the problem is in the applet itself. May be it needs some parameters?

You can pass parameters this way...

<#OBJECT codetype="application/java"
classid="Applet1"
width="700" height="400">
<#PARAM name="welcome" value="Hello.au|Welcome.au">
Java applet that plays a welcoming sound.
<#/OBJECT>

or...

<#APPLET code="Applet1" width="700" height="400">
<#PARAM name="welcome" value="Hello.au|Welcome.au">
Java applet that plays a welcoming sound.
<#/APPLET>

Edited on, March 16, 2005, 2:41 PM GMT, by Rajputro.
Reason: Ignore the # signs

AsifTheManRahman
March 16, 2005, 08:24 PM
Thanks.

None of them worked. I tried codebase again, and looks like I don't get the error anymore. However, whereas a grey colored box with a red cross at the top left corner representing the applet used to appear before, nothing appears at all now. I am beginning to wonder whether it's a prolem with my applet by any chance. This is what I have so far:

import java.applet.*;
import java.awt.*;


public abstract class Applet1 extends Applet implements Runnable{
private int radius = 10;
private int x = 5;
private int y = 50;
private Image im;
private Graphics gr;
// init - method is called the first time you enter the HTML site with the applet
public void init() {}

// start - method is called every time you enter the HTML - site with the applet
public void start() {
Thread thread = new Thread(this);
thread.start();
}
public void play(){
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while (true)
{
repaint();
try{
Thread.sleep(20);
}
catch (InterruptedException e){
}
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
x++;
}
}
// stop - method is called if you leave the site with the applet
public void stop() {}

// destroy method is called if you leave the page finally (e. g. closing browser)
public void destroy() {}

/** paint - method allows you to paint into your applet. This method is called e.g. if you move your browser window or if you call repaint() */
public void paint (Graphics ball) {
ball.setColor(Color.red);
ball.fillOval(x,y,2*radius,2*radius);
}
public void DbBuffer(Graphics bgIm)
{
if (im == null)
{
im = createImage(this.getSize().width, this.getSize().height);
gr = im.getGraphics();
}
gr.setColor(getBackground());
gr.fillRect(0,0,this.getSize().width, this.getSize().height);
gr.setColor(getForeground());
paint(gr);

bgIm.drawImage(im,0,0,this);
}
}

So this is my .class file....any ideas?:)

btw fyi - i created a jar file from eclipse and unzipped all the files into the folder that contains my .html

Mahmood
March 17, 2005, 10:08 AM
Here is the error dump from that applet...

load: Applet1.class can't be instantiated.

java.lang.InstantiationException

at sun.reflect.InstantiationExceptionConstructorAcces sorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at java.lang.Class.newInstance0(Unknown Source)

at java.lang.Class.newInstance(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

AsifTheManRahman
March 17, 2005, 10:14 AM
instantiation exception....hmmmm....

thanks

AsifTheManRahman
March 19, 2005, 10:44 PM
Success! Applet inited!

Problem was in the applet code - wasn't implementing method run in Runnable. Changed the play method to run and declared Applet as non-abstract (i.e. removed the word "Abstract" from class declaration).

Kool - now I can go on and continue developing my applets!

Mahmood
March 19, 2005, 10:57 PM
Great work.