Java : applet: do i need to create a Class too?

Page 1 of 1 [ 9 posts ] 

lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 56
Gender: Female
Posts: 4,113
Location: belgium

21 Feb 2008, 12:34 pm

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 ?



shaggydaddy
Toucan
Toucan

User avatar

Joined: 21 Oct 2007
Age: 43
Gender: Male
Posts: 262
Location: California

21 Feb 2008, 1:06 pm

yes, you need to compile pretty much any java code you deploy. A notable exception is a JSP file, which the servlet container compiles on the fly for you.


_________________
If you suffer from Autism, you're doing it wrong.


lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 56
Gender: Female
Posts: 4,113
Location: belgium

21 Feb 2008, 1:18 pm

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
Yellow-bellied Woodpecker

User avatar

Joined: 16 Oct 2006
Age: 35
Gender: Male
Posts: 50
Location: West of the Occident, East of the Orient

21 Feb 2008, 9:48 pm

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.



lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,619
Location: Somerset UK

22 Feb 2008, 1:02 pm

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:

Code:
$ javac CopyTest2.java
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


lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,619
Location: Somerset UK

22 Feb 2008, 1:12 pm

Just to get rid of the warning, I added the one line...

Code:
import java.applet.Applet;
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


lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,619
Location: Somerset UK

22 Feb 2008, 1:40 pm

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.

Code:
£!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "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


lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 56
Gender: Female
Posts: 4,113
Location: belgium

22 Feb 2008, 2:54 pm

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



lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,619
Location: Somerset UK

22 Feb 2008, 6:18 pm

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