Java : applet: do i need to create a Class too?
I am studying my java class, course that is ( gui now)
in the documentation there is an explanation but it uses an applet,
which i haven't done before.
my question is : do i have to create a .Class from the .java file just like with java itself? (cause i get an error, doing so and i simply copied the code from the sun website)
maybe there is something missing or it should be done differently ?
ok, i'll need to search what is going on then ...
maybe somethings is wrong with the html file ?
i copy it here
<html>
<head>
</head>
<body>
<APPLET CODE="CopyTest2.class" WIDTH="250" HEIGHT="200"> </APPLET>
</body>
</html>
this is the message i get:
Exception in thread "main" java.lang.NoSuchMethodError: main
i guess that's ok, since it is an applet ?
here is the applet from sun (i want to run it in order to 'see' what happens) :
import java.applet.Applet;
import java.awt.*;
public class CopyTest2 extends Applet {
public void paint(Graphics g) {
setForeground(Color.yellow);
// the next line would do just as
// well as the following
// g.setColor(Color.yellow);
Graphics copy = getGraphics();
try {
System.out.println("g=" + g.getColor() +
" copy=" + copy.getColor());
copy.drawLine(0,0,
getSize().width-1, getSize().height-1);
}
finally {
copy.dispose();
}
}
}
willzzz
Yellow-bellied Woodpecker
Joined: 16 Oct 2006
Age: 35
Gender: Male
Posts: 50
Location: West of the Occident, East of the Orient
From the way I learned it, embedded Java applets are just normal Java classes that have a special main method and an init() method. From what you posted Sun's example, it looks like it's extending Applet and importing it from a Sun pre-made object of import java.applet.Applet. The applet should have a main() method also and it's complaining about that. If you're copying that code from Sun's examples then your using it in the wrong context.
It all worked fine for me, once I got over the hurdles of never having actually written an applet before.
The code merely replaces the "paint" method of the applet. It draws a diagonal line from top left to bottom right of the window.
I'm not sure why the "Color.yellow" juggling fails to do anything. I just get a black line.
I'm also not sure where the "System.out.println" should go. It doesn't seem to finish up anywhere obvious. Maybe if I ran my browser off a command window, it would appear there.
Using the compiler from "gcj", I did get the following warning when I compiled the code:
CopyTest2.java:4: warning: The serializable class CopyTest2 does not declare a static final serialVersionUID field of type long
public class CopyTest2 extends Applet {
^^^^^^^^^
1 problem (1 warning)
However, I'm sure this is not a problem... for test code.
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer
Just to get rid of the warning, I added the one line...
import java.awt.*;
public class CopyTest2 extends Applet {
private final static long serialVersionUID = 0x01010101;
public void paint(Graphics g) {
setForeground(Color.yellow);
// the next line would do just as
// well as the following
// g.setColor(Color.yellow);
Graphics copy = getGraphics();
try {
System.out.println("g=" + g.getColor() +
" copy=" + copy.getColor());
copy.drawLine(0,0,
getSize().width-1, getSize().height-1);
}
finally {
copy.dispose();
}
}
}
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer
Just out of interest, I played about with the html, and switched it to xml (with "tidy"), just to make it clearer(?) what was going on. Still no sign of anything yellow, and the applet seems to take no notice of the surrounding fg/bg colours.
I substituted all the "<" characters with "£", because WP decided to look inside the "code" tags, when it shouldn't have.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
£html xmlns="http://www.w3.org/1999/xhtml">
£head>
£meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
£title>Demo£/title>
£style type="text/css">
/*£![CDATA[*/
div.c1 {
text-align: center;
color: red;
background-color: green;
width: 300px;
height: 300px;
}
/*]]>*/
£/style>
£/head>
£body>
£div class="c1">
above £applet code="CopyTest2.class" width="250" height="200">
£/applet> below
£/div>
£/body>
£/html>
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer
don't worry, that was what the page was talking about,
if you are really interested here is the link of the page
http://java.sun.com/developer/Books/Graphics/
it works for me too now,
i think i just did something wrong with a name somewhere or so, i thought i had checked it all everywhere, but
well, it's always the same thing i guess ... just overlook one little minuscule mini tiny letter somewhere and
computer refuses arggggh
I seem to have discovered a rather large bug in SeaMonkey, and probably in Firefox.
Although reloading a page reloads the html, it seems almost impossible to persuade it to take any notice of a change java class file. Only by physically going in and deleting the cache directory do I seem to be able to get it to take any notice.
WRT to above test code, it's a real pain if you make changes, then can't see why the change has had no effect, until you realize the cached version is still being used.
I also discovered, as a side issue, that named colours seem to be accepted both as all lower case and all upper case, but not mixed case.
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer
Similar Topics | |
---|---|
Physicists Create 5-Lane Superhighway For Electrons |
13 May 2024, 7:42 pm |
Scientists Create a Diamond Film in Just 150 Minutes |
24 Apr 2024, 7:40 pm |
Hot Wheels works with ASAN to create ‘Flippin Fast’ |
02 Apr 2024, 10:03 pm |
Northern Lights Create Beautiful Show In Over 20 Years |
13 May 2024, 7:39 pm |