According to the New Scientist plot and graphics are not paramount in video game success. It reports that research presented at the Human-Computer Interaction conference in Cambridge, UK showed that the key factors are:
- social interaction (such as an online presence and a shared family experience)
- appropriate pricing
Of course, this is good news for the Java games programmer. That's because Java makes it easy to add graphics to an applet, giving the game developer plenty of time to consider how to implement the social interaction.
Simple Graphics in a Java Applet : Adding Text
The simplest graphic to add to any applet is text. All that's required are:
- the text to be displayed
- the coordinates for location of the text on the applet screen
And so the first step, as always is to define the new applet class (for more information on creating Java applets read How to Add a Java Applet to a Web Page: Creating Java Applications for the Internet). The text is then written to the screen as part of the paint method and by using the drawString method. This requires three inputs:
- the text itself
- the horizontal distance (in pixels) from the top left corner of the applet
- the vertical distance (in pixels) from the top left corner of the applet
The end result is a simple Java applet class:
Here a simple text message is written to the applet screen (as shown in figure 1 at the bottom of this article). And it is not much more difficult to make the text more interesting by formatting it.
Formatting Text in a Java Applet
At the moment the programmer can control exactly where the string is displayed in the applet. However, the text will always look the same. Therefore in addition placing the text on the screen the to the programmer may also want to set:
- the font to be used
- the colors to be used
The fonts are create as part of another class method. The init method is run when the applet is created:
Here the size of the applet window has been set and any required fonts have been created by the init method, but it's the paint method which uses these fonts:
The formatted text can be seen in figure 2, and it shows just how easy it is to add text to a Java applet and to format it.