27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Applets<br />

--<br />

Mickey Segal<br />

Q: Why do I get message like “wrong magic number” when I am trying to run applet? What is a<br />

magic number?<br />

Answer: The first thing a JVM does when it loads a class is check that the first four bytes are (in hex)<br />

CA FE BA BE. This is the "magic number" and thats why you are getting that error, you are trying to<br />

load a file that isnt a class and so the class loader in the JVM is throwing out that exception.<br />

Make sure you transfer the class files to site in binary mode, rather than text or ASCII mode.<br />

An error from the browser saying "cannot start applet ... bad magic number" usually means that one of<br />

the class files on the server is corrupted. '<br />

Replace your class binary files on the web server; clean up the cache of your browser, and reload<br />

your applet.<br />

Q: I want to use more fonts in my applet... say for example Arial... which is not avilable in the<br />

present jdk package...<br />

How can i deal with it?<br />

Answer: import java.awt.Toolkit;<br />

....<br />

Toolkit tools : new Toolkit();<br />

String[] fontList = tools.getFontList();<br />

Q: How can I slow down my applet?<br />

I have a game applet that is running too fast on newer systems that have high-end video cards. Its<br />

easy enough to slow down the game by having it sleep between thread cycles, but I need to be able<br />

to<br />

determine how fast a users machine is before I determine how long to sleep for.<br />

I have been muddling through the documentation but cannot find any calls that will tell my applet what<br />

the users configuration is as regards to CPU speed and other components they may have on their<br />

system.<br />

Answer: Simple create a new Date (), then perform a standard lengthy operation on the order of<br />

something that takes about one second on your machine, like a long loop, then create another new<br />

Date() and compare it to the first. If it takes 1/2 of the time compared to your machine, then the CPU<br />

is probably about 2 times faster. if it takes 3 times the duration compared to your machine, the CPU is<br />

probably 1/3 as fast as yours.<br />

Do this dynamically, and it might help with speed changes when there's lots of action happening as<br />

well - unless this issue is already being dealt with using threads, that is.<br />

-by<br />

Max Polk<br />

Q: Why do I see applet in applet viewer and do not in a browser?<br />

When I try to view my applet on a web page i get the error<br />

java.lang.NoSuchMethodError: java/lang/Double: method<br />

parseDouble(Ljava/lang/String;)D not found<br />

Which is weird as it compiles fine on Borland and with the JDK using applet viewer<br />

file:///F|/350_t/350_tips/applets.htm (4 of 10) [2002-02-27 21:17:49]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!