|
||||||
How to Add Text to Java Applet ApplicationsAdding a Formatted Message into a Java Game Embedded in a Web PageFancy 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:
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 TextThe simplest graphic to add to any applet is text. All that's required are:
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 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 AppletAt 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 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
The copyright of the article How to Add Text to Java Applet Applications in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Add Text to Java Applet Applications in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||