Applet Example: Drawing Strings and Graphics in Applets given with Screenshots in Simple terms for a Beginner.
After practicing the earlier simple life cycle program, let us write another program that prints some strings on applet window (not at DOS prompt as done previously). Now we use the java.awt.Graphics object passed as parameter to paint() method.
The application includes two files – Demo.java, applet file and Demo.html, HTML file. HTML file should be opened in a browser using File menu .
Applet Example: 1st File: Applet – Demo.java
import java.awt.*;
import java.applet.*;
public class Demo extends Applet
{
String s1, s2;
public void init()
{
s1 = "Welcome to way2java practices";
s2 = "on today " + new java.util.Date();
}
public void paint(Graphics g)
{
g.drawString(s1, 50, 50);
g.drawString(s2, 50, 70);
g.drawString("Best Wishes", 50, 90); // directly writing a string
}
public void destroy()
{
s1 = null;
s2 = null;
}
}
2nd File: HTML – Demo.html
Execution
Compile the Demo.java file as usual at DOS prompt as in the previous program. Let us run this program both using appletviewer and browser.
Execution using appletviewer: C:\snr\way2java\applets> appletviewer Demo.html
Execution using browser: Open the Demo.html file through File menu of browser.
Two strings are assigned with some values in init() method and used in paint() method. Graphics is an abstract class and includes many methods of drawing. Here we use drawString() method. The drawString() takes three parameters – the first one is a string that is to be drawn, the second and third parameters are integer values of x and y coordinates in pixels, the position of the string on the applet window. Remember, the left top corner of the window is 0, 0 pixels. The other methods of drawing, we come to know in AWT Graphics
Note: All the methods of Applet class are concrete methods (not abstract). The programmer can override what ever methods he would like and the other that are not used, are implicitly created and called by the browser (like the default constructor in an application).
Applet Example: Printing System Date
Let us practice one more applet that prints the system date.
1st File: Applet – DateDemo.java
import java.awt.*;
import java.applet.*;
import java.util.*;
public class DateInfo extends Applet
{
public void paint(Graphics g)
{
Date d = new Date();
String s1 = d.toString();
g.drawString(s1, 50, 80 );
}
}
2nd File: HTML – DD.html
The same execution procedure of previous applet is to be followed.
Date d = new Date();
The above statement creates Date object. Date is from java.util package.
String s1 = d.toString();
The drawString() method requires a string as first parameter. So, convert Date object d into a string using toString() method of Object class (or alternatively, valueOf() method of String class can also be used).
g.drawString(s1, 50, 80 );
The string s1 is painted at x-coordinate of 50 and y-coordinate of 80 pixels.
Applet Example: Drawing Graphics
Java permits to draw graphics with applets. The Graphics includes many methods to draw lines, rectangles and ovals, polygons etc. About these methods we discuss later clearly in AWT – Graphics topic.
File Name: DrawingGraphics.java
import java.awt.*;
import java.applet.*;
public class DrawingGraphics extends Applet
{
public void paint(Graphics g)
{
g.drawRect(60, 70, 110, 70);
g.drawOval(180, 70, 135, 70);
g.drawArc(50, 150, 120, 100, 0, 180);
g.drawLine(180, 150, 240, 240);
g.drawLine(260, 150, 180, 240);
}
}
File Name: DrawingGraphics.html
Screenshot of Applet Example DrawingGraphics.html
Let us see the methods of Graphics class used in the above program.
drawRect(60, 70, 110, 70) | Draws a rectangle at x and y coordinates of 60 and 70 with a width and height of 110 and 70 pixels |
drawOval(180, 70, 135, 70) | Draws a oval at x and y coordinates of 180 and 70 with a width and height of 135 and 70 |
drawArc(50, 150, 120, 100, 0, 180) | Draws an arc at x and y coordinates of 50 and 150, arc width of 120 and arc height of 100 and start angle with 0 and ending angle with 180 |
drawLine(180, 150, 240, 240) | Takes four integer parameters of line coordinates of x1, y1 and x2, y2 |
Applet Example Displaying Strings in Status Bar
An applet can display the messages on the applet window (using drawString()) and on status bar (using showStatus()). Status bar comes at the bottom of the browser and is used by the browser to display the status of the document opened. The applet can use this status bar to display strings (messages).
Let us see what Java Applet API says about showStatus() method;
- strong>void showStatus(String msg): Requests that the argument string be displayed in the “status window”. Many browsers and applet viewers provide such a window, where the application can inform users of its current state.
File Name: DisplayMethods.java
import java.awt.*;
import java.applet.*;
public class DisplayMethods extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50, 60);
showStatus("Best Wishes");
}
}
File Name: DisplayMethods.html
g.drawString(“Hello World”, 50, 60);
showStatus(“Best Wishes”);
drawString() method draws the string "Hello World" at x and y coordinates of 50 and 60.
showStatus() method displays the string "Best Wishes" on the status bar. This method does not take x and y coordinates.