Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Applets: workaround for missing classes in MSIE?

Thread view: 
A. Farber - 21 Apr 2006 11:02 GMT
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
Chris Uppal - 21 Apr 2006 12:37 GMT
> 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();
   }
}
========================
Chris Uppal - 21 Apr 2006 13:32 GMT
I wrote:

>                 return Class
>                             .forName("GoodMassager")
>                                 .newIntstance()

That should be "ModernMassager", not "GoodMassager".  Typo...

   -- chris
Chris Uppal - 21 Apr 2006 13:42 GMT
I wrote:

> Typo...

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. Farber - 21 Apr 2006 14:44 GMT
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... ?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.