I have a problem with java applet and graphics. I'm trying to run it in Eclipse and it fails. Im new in java and i hope you can help me. I have two files: Say.java and SayWhat.java. Say.java:
public class Say {
SayWhat word = new SayWhat("Hello World");
}
SayWhat.java:
import java.applet.Applet;
import java.awt.Graphics;
@SuppressWarnings("serial")
public class SayWhat extends Applet {
Graphics g;
String what;
public SayWhat(String what) {
this.what=what;
}
public void paint(Graphics g){
g.drawString(what, 20, 20);
}
}
Error that appears is:
load: SayWhat.class can't be instantiated.
java.lang.InstantiationException: SayWhat
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Can you please tell me what am i doing wrong?
An applet needs to have a public no-arg constructor (either by having an explicit public no-arg constructor, or by having no explicit constructors at all; in the latter case, the compiler will supply a public no-arg constructor as a default). Your class's sole constructor takes an argument:
public SayWhat(String what) {
so the class can't be instantiated without that argument, so it can't be used as an applet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With