3

My code compiles and works just fine in both my browser and Eclipse, but for some reason the AppletViewer just won't work with my VERY simple code. When I try to run it with appletviewer FirstApplet.html, the AppletViewer window appears as expected, but the upper-left part of the window just says "Applet" and the bottom-left part of the window says "Start: applet not initialized"

What could be the problem here? (Again, it works in my browser and Eclipse)

Thank you for any help! :)


FirstApplet.java

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

public class FirstApplet extends Applet {
    public void paint(Graphics g){
        g.drawString("This is my first Java Applet!", 20, 30);
    }
}

FirstApplet.html

<html>
    <head>
        <title>FirstApplet</title>
    </head>

    <body>
        <applet
            codebase    = "."
            code        = "FirstApplet.class"
            name        = "FirstApplet"
            width       = "640"
            height      = "480"
            hspace      = "0"
            vspace      = "0"
            align       = "middle"
        >

        </applet>
    </body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Rick_Sch
  • 456
  • 3
  • 10
  • 19
  • 1
    FirstApplet.html and FirstApplet.java - Are they on the same directory ? – exexzian Jun 17 '13 at 05:46
  • @Bingo Yes, they are. And I've also compiled FirstApplet.Java so there's a FirstAPplet.class file in the same folder as well. – Rick_Sch Jun 17 '13 at 05:52
  • 1
    1) there miss some code for visibily of Applet on the screen 2) use JApplet, 3) put there JPanel and custom painting in Swing is done by using paintComponent – mKorbel Jun 17 '13 at 06:06
  • @Rick_Sch I still doubt that html and class file are not on the same folder as I checked your prog and it works fine check out : http://i39.tinypic.com/ojpjiv.png – exexzian Jun 17 '13 at 06:12
  • 2
    1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jun 17 '13 at 07:09
  • @Bingo They are definitely in the same folder along with the other standard Eclipse mess. http://s23.postimg.org/c7rmn69aj/First_Applet.jpg – Rick_Sch Jun 17 '13 at 13:22
  • @AndrewThompson This is not from a teach, but from a 5 year old book called Beginning Java Programming. If what I'm doing is incorrect, it's because I don't know any better and I'm just doing what the book tells me. Is there a better way to embed Java programs into a website these days? Also, thanks for the Swing vs. AWT suggestion! :) – Rick_Sch Jun 17 '13 at 13:24
  • 2
    *"This is not from a teach, but from a 5 year old book"* That book is not something you should be learning from. Swing was introduced at least *5 years* before that, and a book teaching AWT at that stage is ..unforgivable. Use the [Swing tutorials](http://docs.oracle.com/javase/tutorial/uiswing/) (or a more recent book). – Andrew Thompson Jun 17 '13 at 14:24
  • You're welcome. Please drop dealing with both AWT & applets for the moment, and start with Swing based `JFrame` applications. Not only am I much more willing to help with them, but me, and other people willing to help, can actually *remember* enough details about Swing to *provide* useful tips. Later move on to the Swing based `JApplet`. But *only* if you need an app. embedded in a web page. That turns out to be very, *very*, ***very*** rarely. – Andrew Thompson Jun 17 '13 at 17:27

2 Answers2

3

I figured it out! It was an Eclipse problem after all!

I wrote and compiled the code in Eclipse. Eclipse's internal version of the AppletViewer worked just fine, and it worked just fine to open the HTML file that I made in my browsers.

The applet didn't work in the actual AppletViewer, however, until I compiled the code using something other than Eclipse. It appears that for some reason AppletViewer didn't like the .class file that Eclipse generated.

Mystery solved!

Rick_Sch
  • 456
  • 3
  • 10
  • 19
-1

I was experiencing the same problem.

First of all I had removed public from my class.That lead fine compilation but no initialization in applet:(.

Next I changed it public and the code threw an error to me.

To solve error I renamed my source code as per my public class name then I simply ran the code through appletviewer which executed all fine.

I apologize for Inconvenient explanation though. Here are the Snippets of three Stage of my problem Solving

Stage 1

Stage 2

Solution Stage

I have tried to demonstrate the aspect of code changes as well :) and the RHS pane is the code webpage.

Simply keep the name of class and main class containing Source same.

Avinash
  • 4,115
  • 2
  • 22
  • 41
Reitsu Y
  • 21
  • 3