How to Add Text to Java Applet Applications

Adding a Formatted Message into a Java Game Embedded in a Web Page

How to Add Text to a Java Applet Application - Mark Alexander Bain
How to Add Text to a Java Applet Application - Mark Alexander Bain
Fancy graphics and involved plot do not always produce a popular game. A lot be achieved with simple graphics, and the starting point for any Java applet is some text

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:

import java.applet.*;
import java.awt.*;
public class simpleText extends Applet
{
public void paint (Graphics g)
{
g.drawString ("The graphics are quite simple", 40, 75);
}
}

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:

int applet_width = 300;
int applet_height = 225;
private Font titleFont;
private Font bodyFont;
public void init ()
{
setSize (applet_width, applet_height);
String fontName = "Freestyle Script";
titleFont = new Font (fontName, Font.BOLD, 24);
bodyFont = new Font (fontName, Font.PLAIN, 12);
}

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:

public void paint (Graphics g)
{
String message;
message = "Here's a New Game";
g.setColor(Color.blue);
g.setFont(titleFont);
g.drawString (message, 0, 20);
message = "The graphics are quite simple";
g.setColor(Color.green);
g.setFont(bodyFont);
g.drawString (message, 40, 75);
}

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.

BNC101

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement