Applets: workaround for missing classes in MSIE?

A

A. Farber

Hello,

I have a small multiplayer card game applet
( http://preferans.de ) which I try to keep runnable both by
the Sun's JVM and by the old MSIE VM (msjavax86.exe).

There is however one little thing which I'd like to add to it
w/o breaking it for MSIE users: the antialiased fonts.

So I've created this small test case - Anti.java:

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

public class Anti extends Applet {
public void init() {
setFont(new Font("Serif", Font.ITALIC, 36));
}

void enableAntiAlias(Graphics g) {
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
}

public void paint(Graphics g) {
/*
if
(System.getProperty("java.vendor").startsWith("Microsoft"))
System.err.println("MSIE detected");
else
enableAntiAlias(g);
*/
g.drawString("Hello World!", 10, getSize().height / 2);
}
}

And here is the html-file for your convenience:

<HTML>
<BODY>
<APPLET CODE="Anti" HEIGHT="100" WIDTH="300">
</APPLET>
</BODY>
</HTML>

If I comment the 4 lines in the paint() as listed above,
then MSIE loads and displays the applet w/o problems.

However once I remove the comments, I get this error:

Microsoft (R) VM for Java, 5.0 Release 5.0.0.3810
==============================================
java.lang.ClassNotFoundException: java.awt.Graphics2D
at com/ms/vm/loader/URLClassLoader.loadClass
at java/lang/ClassLoader.loadClassInternal
at Anti.paint
at com/ms/awt/WComponentPeer.doClearAndPaint
at com/ms/awt/WComponentPeer.paintNode
at com/ms/ui/windowmanager/PaintRequest.run
at com/ms/ui/windowmanager/RunnableMessage.run
at com/ms/awt/WSystemQueue.getMessage
at com/ms/awt/WEventQueue.getNextEvent
at java/awt/EventDispatchThread.run

I wonder if anyone have found a nice workaround for this
probably very frequent problem.

Please note, that I don't want to use JS or "Anti.cab"
or teach my users how to install the Sun's VM.
I just want them to play my little game with any browser.

Regards
Alex
 
C

Chris Uppal

A. Farber said:
Please note, that I don't want to use JS or "Anti.cab"
or teach my users how to install the Sun's VM.
I just want them to play my little game with any browser.

How about something like the following (not really tested) ?

-- chris

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

abstract class Massager
{
void massage(Graphics g);
}

class ModernMassager
{
void massage(Graphics g)
{
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
}
}

class NullMassager
{
void massage(Graphics g)
{
}
}

public class Anti
extends Applet
{
private Massager m_massager;

public void
init()
{
m_massager = makeMassager();
setFont(new Font("Serif", Font.ITALIC, 36));
}

public void
paint(Graphics g)
{
massager.massage(g);
g.drawString("Hello World!", 10, getSize().height / 2);
}

private Massager
{
if (! System.getProperty("java.vendor").startsWith("Microsoft"))
{
try
{
return Class
.forName("GoodMassager")
.newIntstance()
}
}
catch (Exception e)
{
// lazy....
Syste.err.println(e);
}
System.err.println("Using NullMassager");
return new NullMassager();
}
}
========================
 
C

Chris Uppal

I said:

Gack! In fact it seems I pasted the text from the wrong editor -- that version
won't even come close to compiling, let alone working. Here's a complete
repost. Sorry for the repetition.

-- chris

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

abstract class Massager
{
abstract void massage(Graphics g);
}

class ModernMassager
extends Massager
{
void massage(Graphics g)
{
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
}
}

class NullMassager
extends Massager
{
void massage(Graphics g)
{
}
}

public class Anti
extends Applet
{
private Massager m_massager;

public void
init()
{
m_massager = makeMassager();
setFont(new Font("Serif", Font.ITALIC, 36));
}

public void
paint(Graphics g)
{
m_massager.massage(g);
g.drawString("Hello World!", 10, getSize().height / 2);
}

private Massager
makeMassager()
{
if (! System.getProperty("java.vendor").startsWith("Microsoft"))
{
try
{
Object m = Class
.forName("ModernMassager")
.newInstance();
return (Massager)m;
}
catch (Exception e)
{
// lazy....
System.err.println(e);
}
}
System.err.println("Using NullMassager");
return new NullMassager();
}
}
===================================
 
A

A. Farber

Thanks Chris! I've ended up using forName() and a static method
(dunno if static is better here or not - I've just taken it):

======== Test.java =======
import java.awt.*;
import java.applet.*;

public class Test extends Applet {
Class aa;

public void init() {
setFont(new Font("Serif", Font.ITALIC, 36));
if
(System.getProperty("java.vendor").startsWith("Microsoft"))
System.err.println("MSIE detected, AntiAliasing
disabled");
else {
try {
aa = Class.forName("AntiAlias");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

public void paint(Graphics g) {
if (aa != null)
AntiAlias.enableAntiAlias(g);
g.drawString("Hello World", 10, getSize().height / 2);
}
}

==== AntiAlias.java ====

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

public class AntiAlias {
public static void enableAntiAlias(Graphics g) {
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
}
}

The funny thing is that the fonts now suddenly look smooth
in the MSIE anyway... ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top