Java Lesson 16: Abstract Window Toolkit (AWT) – Shapes and Colors

1
29

Introduction

Java provides the Abstract Window Toolkit (AWT) to facilitate developing applets and programs with Graphical User Interfaces (GUI) instead of text-only console I/O. AWT includes dozens of classes and interfaces, and we’ll discuss several of the most common in this lesson. Applets and standalone Java windows programs use the AWT; we will use applets in this tutorial lesson because they make it easier to learn the concepts.

Drawing Shapes with the Graphics Class

The Graphics class provides several drawing methods to draw on the screen or to control how the drawing looks on the screen. We briefly touched on some of these in the Applets lesson, and will expand our coverage here. The methods include:

Graphics Method Description
public void drawLine(int x1, int y1, int x2, int y2) Draws a line in the foreground color from (x1,y2) to (x2,y2).
public void drawRect(int x, int y, int w, int h) Draws the outline of a rectangle or square with the upper-left corner at coordinates (x,y) with the given dimensions in the foreground color.
public void fillRect(int x, int y, int w, int h) Draws the outline of a rectangle or square of the given dimensions in the foreground color, and the entire rectangle is filled with the foreground color.
public void drawOval(int x, int y, int w, int h) Draws an ellipse (or circle) with the upper-left corner at coordinates (x,y) with the given dimensions in the foreground color.
public void drawString(String s, int x, int y) Draws the string s at coordinates (x,y) in the current font.
public void setFont(Font f) Takes an object of type Font, and uses it for all text output. Fonts guaranteed to be available on all systems include Helvetica, TimesRoman, Dialog, and Courier in point sizes 8, 10, 12, 14, 24, and 36.
public void setColor(Color c) Takes an object of type Color and uses it as the foreground color.

 

 

Diagram of (x,y) coordinates.
Figure 1. Coordinate system (x,y).

The graphics are drawn in a window. The origin of the window is at the top-left corner with coordinate position x = 0 and y = 0, or (0,0). Coordinates are measured in pixels. Usually the methods are called from an applet’s paint() or update() method. Graphics objects can be drawn as an outline shape with the current foreground color, or filled in with the foreground color.

 

Drawing Lines

You can draw lines with the drawLine() method by specifying the x and y coordinate positions of a point on each end of the line. Applet16a draws a line from (20,150) to (300,20). As we did in the Applets tutorial lesson, enter or copy-and-paste the following applet code into your text editor and save it with the filename Applet16a.java. Then compile it with the javac command. Finally, run it with the appletviewer, as shown in the screenshot below.

Note: In the following examples, the HTML commands are embedded within the Java applet source code, so there is no need to create or edit a separate HTML file to launch the applet.

Compile and execution of Applet16a.java, which draws a line in an applet window.

The appletviewer window running Applet16a.java

 

Drawing Rectangles and Filled Rectangles

Draw rectangles with the drawRect() method by specifying the x and y coordinate of a point denoting the top-left position of one corner of the rectangle, and the width and height of the rectangle. Draw filled rectangles with the fillRect() method in a similar way. Applet16b draws two rectangles, one of which is filled. Since we did not specify a foreground color, the default color black is used. As the previous example, enter or copy-and-paste the following applet code into your text editor and save it with the filename Applet16b.java. Be sure to note the HTML object tag within the Java applet, which specifies Applet16b (not Applet16a). Then compile it with the javac command. Finally, run it with the appletviewer, as shown in the screenshot below.

Note: To draw a square specify the same value for the width and height.

 

Compile and execution of Applet16b.java, which draws two rectangles.

The appletviewer window running Applet16b.java

Drawing Circles and Ovals (Ellipses)

Draw ovals (ellipses) and circles with the drawOval() method by specifying the x and y coordinate of a point denoting the top-left position of one corner of a rectangle which bounds (encloses) the oval, and the width and height of the bounding rectangle. Draw filled ellipses with the fillOval() method in a similar way. A circle is an oval which has the same value for width and height.

Note: Unlike some drawing systems, we do not specify the center of the oval.

Applet16c draws two ovals, one of which is filled. Since we did not specify a foreground color, the default color black is used. As in the previous example, enter or copy-and-paste the following applet code into your text editor and save it with the filename Applet16c.java. Then compile it with the javac command. Finally, run it with the appletviewer, as shown in the screenshot below.

Note: To draw a circle specify the same value for the width and height.

Compile and execution of Applet16c.java, which draws an oval and a filled circle.

The appletviewer window running Applet16c.java

Drawing Strings

Draw Strings (ie. text) with the drawString() method by specifying the x and y coordinate position of the starting point of the text string. Applet16d draws the text string “AWT is fun and easy” on two lines. If you draw more than one string it is up to you to make sure the strings do not overlap and are within the bounds of the window.

Compile and execution of Applet16d.java, which draws a text string in a window.

The appletviewer window running Applet16d.java

Make Components Interesting with Color

The Color class allows you to specify a color for use by future Graphics method calls. You can either specify the color by passing the red, green, and blue values of the desired color, or by using the following static variable colors provided by the class as shown in the following table.

  Static Variable Colors
 Color.BLACK  Color.BLUE  Color.CYAN
 Color.DARKGRAY  Color.GRAY  Color.GREEN
 Color.LIGHTGRAY  Color.MAGENTA  Color.ORANGE
 Color.PINK  Color.RED  Color.WHITE
 Color.YELLOW

The following statements set the color of future draw operations to myColor and Color.GREEN, respectively.

Drawing Several Shapes with Colors

You can draw as many shapes in as many colors as you wish. Our last example, Applet16e.java, draws several shapes with various colors.

Note: In this example, the HTML object tag requests a larger window with a width of 440 pixels and a height of 440 pixels.

Compile and execution of Applet16e.java, which draws several shapes with various colors.

The appletviewer window running Applet16e.java

 

Summary

This lesson introduced Java’s Abstract Window Toolkit (AWT) by demonstrating how to draw several shapes and text in a variety of colors.

Next Lesson

Getting input from the user is just as important as displaying output. The next tutorial lesson will examine handling events from the Java Abstract Window Toolkit AWT Buttons and Other Controls.

1 COMMENT

  1. […] way to develop GUI-based Java programs is with the Java Abstract Window Toolkit (AWT), which is the subject of our next tutorial […]

Leave a Reply