Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java applet does not display anything

Does anyone know why my Java applet does not display anything?

This is my first Java applet so I am new to creating one. I researched this problem and haven't found an answer specific to the current problem.

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

public class JavaRocksApplet extends Applet
{
public void paint( Graphics screen )
{
    Font f = new Font( "TimesRoman", Font.ITALIC, 36 );
    screen.setFont( f );
    Color c = new Color( 40, 80, 120 );
    screen.setColor( c );
    screen.drawString( "Java Rocks!!", 100, 60 );
}
}

It doesn't matter whether I use appletviewer in the command module or an html page.

<html>
<head>
<title>JavaRocksApplet</title>
</head>
<body>
    <applet code = "JavaRocksApplet.class" width = 400 height = 200> </applet>
</body>
</html>

There are no errors when compiling the Java program, so I am a little bit confused about why it doesn't work.

Also, I am using a MacBook Pro running OSX 10.8.2 Mountain Lion with Java SE 6

like image 351
user1933229 Avatar asked Jan 15 '13 21:01

user1933229


People also ask

Why is my applet not working?

The Java bit version must match your browser's bit version. If your browser is 32-bit, your Java must be 32-bit. If your browser is 64-bit, your Java must be 64-bit. If you have a 32-bit browser and 64-bit Java, or a 64-bit browser and 32-bit Java, applets will not load.

How do I display an applet output?

Which of these functions is called to display the output of an applet? Explanation: Whenever the applet requires to redraw its output, it is done by using method paint().


2 Answers

Both the applet and the HTML page appear correct, so its likely the problem is a result of a misconfiguration on either the server end, or the status of the Java plugin for your browser. I'd need to know more about your situation to completely diagnose it. [I tested this in Google Chrome using the 1.7.0.11 plugin, and it worked as expected]

On the server end, make sure you have the class file and the HTML file deployed to the same location on the server (or a local directory if you are loading it that way)

In addition, it is possible that the browser you are using either does not have Java installed or has an incompatible version of Java. This is the next thing I would check. Common problems include compiling the applet with against the 1.7 API, but trying to load it into a browser with an older Java plugin.

like image 177
JohnnyO Avatar answered Oct 04 '22 07:10

JohnnyO


Here is a screenshot of this working code. Except shortened from the applet height specified.

JavaRocksApplet

So, as Neet noted in a comment. 'It works here.'

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

/*
<applet code = "JavaRocksApplet.class" width = 400 height = 200> </applet>
*/
public class JavaRocksApplet extends Applet
{
    public void paint( Graphics screen )
    {
        Font f = new Font( "TimesRoman", Font.ITALIC, 36 );
        screen.setFont( f );
        Color c = new Color( 40, 80, 120 );
        screen.setColor( c );
        screen.drawString( "Java Rocks!!", 100, 60 );
    }
}

Likely more details on the actual cause of the problem can be found by viewing the console. See How do I enable and view the Java Console?

like image 26
Andrew Thompson Avatar answered Oct 04 '22 07:10

Andrew Thompson