This hour's first project is an applet that displays the string "Saluton mondo!", the traditional Esperanto greeting that is becoming more traditional by the hour. You'll take a look at how applets are structured by re-creating the Saluton application from Hour 2, "Writing Your First Program," as a program that can run on a web page. Load your word processor and create a new file called SalutonApplet.java. Enter the text of Listing 17.1 into the file and save it when you're done. Listing 17.1. The Full Text of SalutonApplet.java1: import java.awt.*;
2:
3: public class SalutonApplet extends javax.swing.JApplet {
4: String greeting;
5:
6: public void init() {
7: greeting = "Saluton mondo!";
8: }
9:
10: public void paint(Graphics screen) {
11: Graphics2D screen2D = (Graphics2D)screen;
12: screen2D.drawString(greeting, 25, 50);
13: }
14: }
This applet does not need to use the start(), stop(), or destroy() methods, so they are not included in the program. Compile the program with the javac compiler tool, if you're an JDK user, or another tool. Drawing in An Applet WindowText is displayed in an applet window by using the drawString() method of the Graphics2D class, which draws text in a graphical user interface component. The drawString() method is similar in function to the System.out.println() method that displays information to the system's standard output device. Before you can use the drawString() method, you must have a Graphics or Graphics2D object that represents the applet window. The paint() method of all applets includes a Graphics object as its only argument, which can be cast to a Graphics2D object: Graphics2D screen2D = (Graphics2D)screen; When you have created a Graphics2D object like this, you can call its drawString() method to display text on the area represented by the object. The following three arguments are sent to drawString():
The (x,y) coordinate system in an applet is used with several methods. It begins with the (0,0) point in the upper-left corner of the applet window. Figure 17.1 shows how the (x,y) coordinate system works in conjunction with the statement on Line 12 of SalutonApplet.java. |
Monday, May 16, 2011
A Sample Applet
Popular Posts
-
QUESTION 1 Marks Write four different Java statements where each statement adds 1 to integer variable x. 4 Answer x =x+1; x+=1; ++x; x...
-
BEGIN PROGRAM Main_menu Use variables: choice OF TYPE Integer Report1,Report2 OF TYPE String ACCEPT file DISPLAY “En...
-
All the elements of a communication network should be hardened so that it is resistant to various security attacks. Maintaining latest up...
-
One of the easiest ways into a system is by using a USB flash drive that implements the U3 standard. The U3 system is a secondary partition...
-
I. What is Linux? Linux is basically an operating system (OS for short). The Windows machine you're (probably) using now uses the Mcf...
-
Fatal error: the system has become unstable or is busy," it says. "Enter to return to Windows or press Control-Alt-Delete to rest...
-
BEGIN RECORD member HAS FIELDS Number : Integer Name : String Contact no. : String ...
-
This hour's first project is an applet that displays the string "Saluton mondo!", the traditional Esperanto greeting that ...
-
Linux refers to the family of Unix-like computer operating systems using the Linux kernel . Linux can be installed on a wide va...


10:56 AM
mitraaa
0 comments:
Post a Comment