2

How can I debug or run a class that extends java.applet.Applet? I am able to run the applet from the command line with appletviewer.

appletviewer runapp.html

Here is what class looks like:

import java.awt.Graphics;
import java.applet.Applet;

public class FancyApplet extends Applet {
    @Override
    public void paint(Graphics g) {
        g.drawString("Hello World!", 20, 20);
    }
}

Here is what the HTML file looks like

<!doctype html>

<head>
    <meta charset="utf-8">
    <title>A Fancy Applet</title>
</head>

<object type="application/java" width="200" height="300">
    <param name="code" value="FancyApplet.class">
</object>

How do I have to organize those files within my project and what settings do I have to use to debug the Applet in NetBeans? The thread Running a java applet from netbeans? did not help unfortunately. I did what was suggested, but get a message telling me that the main class was not found. What I took from it though was that in my case the HTML file is not even needed? But just because I am curious is it still possible or recommended to use it?

Community
  • 1
  • 1
urbaindepuce
  • 503
  • 1
  • 5
  • 17
  • You've broken the paint chain, you really should call `super.paint`. Do you really need to use `java.awt.Applet`? The API was replaced with Swing some 15+ years ago. In fact, do you really need to use applets? They have a lot of issues and difficulties that might be best avoided if you are just learning... – MadProgrammer Dec 19 '14 at 20:21
  • I know that it is recommended to use Swing or JavaFX instead. I am just learning all that stuff and was told that Swing is build on AWT, so it is useful to have an understanding of it. – urbaindepuce Dec 19 '14 at 20:50
  • Yes and no. You don't really need a "deep" understanding of AWT to learn Swing, as it's use/interaction is semi-transparent. If you're just learning, then I suggest you stay away from applets (personally) and focus on desktop applications, as they are less restrictive and present a more gential learning curve - IMHO – MadProgrammer Dec 19 '14 at 20:54
  • You also might find [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) useful – MadProgrammer Dec 19 '14 at 20:55

1 Answers1

2

As you don't have public static void main(String[] args) in your FancyApplet class, so the above error is produced.

You can run your java applet in the NetBeans IDE by pressing Shift + F6 key from your main class(keeping cursor inside FancyApplet class here). It'll run the applet program without searching for the main method.

This is an alternate shortcut method of running individual classes in NetBeans IDE.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • All that time I did not think about that I can run individual files. When doing this the HTML file to run the Applet will be automatically created in the build folder of the project. A bit too late I found this useful link explaining it all: https://netbeans.org/kb/docs/web/applets.html – urbaindepuce Dec 19 '14 at 20:53