0

I am new to Java (and programming in general) so I thought that making a simple test case applet would help to form a basic understanding of the language.

So, I decided to make a basic applet that would display a green rectangle. The code looks like:

import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Graphics;

public class Box extends JApplet{

public void paint(Graphics page){
    page.setColor(Color.green);
    page.fillRect(0,150,400,50);

    }

 }

The HTML file (test.html) that I then embedded that into looks like:

<html>
<body>
<applet code="Box", height="200" width="400">
</applet>
</body>
</html>

I then compiled/saved the Java bit, and put the two into the same folder. However, when I attempt to view the html file, all I see is an "Error. Click for details" box. I tested this in both the most current version of Fire Fox and Opera, and too did I make sure that the Java plug-in was enabled and up to date for both.

So what exactly am I forgetting to do here?

Juser1167589
  • 415
  • 7
  • 16
  • 1
    `` and it works fine for me. – RanRag Jan 24 '12 at 18:51
  • 1) `Error. Click for details` ..and what were the details? It astonishes me that people think we can solve the problem without knowing the details. 2) Do **any** applets work in that browser? Visit [test VM](http://java.com/en/download/testjava.jsp) to check. – Andrew Thompson Jan 25 '12 at 03:21

3 Answers3

0

@Juser1167589 I hope your not still having issues with this, but if you are, try going into your program files, delete the JAVA folder, then redownload java from the big red button on 'java.com'. If there is no JAVA folder then * FACEPALM * GO DOWNLOAD JAVA. another possible answer to why you were seeing the errors on the other sites is that they might not have the required resources to run it anymore.

Dylan
  • 47
  • 2
  • 8
0

It seems as if everything is close to OK.

Once the .class file is in the same folder as your HTML file it should come up. Your code might contain a typos (comma after "Box").

Example :

<Applet Code="MyApplet.class" width=200 Height=100>

See also :

http://www.echoecho.com/applets01.htm

blo0p3r
  • 6,790
  • 8
  • 49
  • 68
  • Ok, I changed it, removing the comma and adding ".class" after the "Box" bit, and It still doesn't work. Also, I went to http://www.cs.colostate.edu/helpdocs/JavaInDOS.html#APPLETS looking for another example and the java applet that is posted half way down the page shows me the same error. – Juser1167589 Jan 24 '12 at 18:25
  • try [this one](http://profs.etsmtl.ca/mmcguffin/learn/java/01-drawingLines/). If none of these work, then it's probably related to a browser issue. Check your java configuration. – blo0p3r Jan 26 '12 at 22:04
-1

Applets are not a good place to start.

They are a very old technology and really not very widely used compared to other parts of the Java technology stack.

If you're new to programming in general, I really wouldn't start with applets.

Instead, you should try learning basic programming and Java by building some simple console apps. I've added some general comments about how to do this. After your confidence rises, you can then start worrying about adding extra complexity, applets etc.

First of all download an IDE. Eclipse is one obvious choice (there are also NetBeans and IntelliJ). All modern developers work within an IDE - don't be tempted to try to muddle through without one.

Then, you should have a "scratchpad" - a class where you can try out some simple language features. Here's one which might be useful:

package scratch.misc;

public class ScratchImpl {
    private static ScratchImpl instance = null;

    // Constructor
    public ScratchImpl() {
        super();
    }

    /*
     * This is where your actual code will go
     */
    private void run() {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        instance = new ScratchImpl();
        instance.run();
    }
}

To use this, save this as a .java file. It can be a template for other simple experiments with Java. If you want to experiment with a language feature (inheritance, or polymorphism, or collections or whatever you want to learn) - then copy the template (use the copy and rename features inside your IDE, rather than manually copying the file and changing the type names) to a new name for your experiment.

You may also find some of my answer here to be useful.

Community
  • 1
  • 1
kittylyst
  • 5,640
  • 2
  • 23
  • 36