Lecture 11





Lecture Topic:
Abstract Window Toolkit
-- Abstract Window Toolkit(AWT) implements most common window functionality.
It contains a set of window object classes that creates windows, menus, and
dialogs.
Following diagram is a hierarchy of components within containers
of Abstract Window Toolkit.
Following code using synchronized method to paint the clock.
Public Static Digital Threads clock_thread;
Public Static void main( String[]args) {
clock_thread = new Digital Threads();
void start_clock() {
clock_thread g = getgraphic(); // get grapic
clock_thread f = main_frame; // get frame
clock_thread.start(); // start clock draw
}
}
Synchronized Public void Paint(graphics g) {
start_clock();
draw_grid(g);
compute_range();
int numShapes = drawnShapes.Size();
Shape S;
for (int i = 0; i < numShapes; i++) {
S = (Shape)drawnShapes.elementAt(i);
S.draw(g);
}
}
Graphic Class
-- The Graphics class is part of the java.awt package. It can be used
to draw lines, shapes, images and characters to the screen.
Java's coordinate system has the origin (0,0) in the top
left corner. Positive x values are to the right, and positive y values are
down. All pixel values are integers; there are no partial or fractional
pixels.Below is a simple square within this coordinate system.
Usage:
import java.awt.Graphics;
public class MyClass extends java.applet.Applet {
...
}
The Graphics class provides a set of simple built-in graphics
primitives for drawing, including lines, rectangles, polygons, ovals, and
arcs.
Example 1: function draws grid.
void drawgrid(graphics g) {
Ractangle r;
r = bounds();
int grid_spacing = 20;
for (int x=0; x < r.width; x=x+grid_spacing) {
g.drawLine(x, 0, x, r.height);
}
for (int y=0; y < r.winth; y=y+grid_spacing) {
g.drawLine(0, y, r.width, y);
}
}
Example 2: function draws a special line.
void line(Graphics g, point P1, point P2) {
g.drawLine(Sx(P1.x), Sy(P1.y), Sx(P2.x), Sy(P2.y));
}
int Sx(double x) {
return round(display.scale.x*x + display.translate.x);
}
int SY(double y) {
return round(display.scale.y*y - display.translate.y);
}
Example 3: functions draws string and fills circle.
void string(Graphics g, string str, Point p) {
g.drawString(str, Sx(p.x), Sy(p.y));
{
void fill_circle(Graphics g, Point p, int radius) {
g.fillOval(Sx(p.x), Sy(p.y), radius, radius);
}
Transformation class
-- It performs translation, rotation, rescale and shear.
Example :
Xform display = center;
Xform center = new Xform(120, -120, 1, -1);
class Xform {
public Point translate;
public Point scale;
Xform(double tx, double ty, double sx, double sy) {
translate = new Point(tx, ty);
translate = new Point(sx, sy);
}
// translate the position of the matrix
void move(double dx, double dy) {
translate.x = translate.x + dx;
translate.y = translate.y + dy;
}
// translate the scale of the matrix
void rscale(double sx, double sy) {
translate.x = translate.x * sx;
translate.y = translate.y * sy;
}
}
Lecture11Simple
Quiz
1. AWT is a class library made up of various windows component classes.
a. True.
b. False.
2. Graphics Class is in the Abstract Window Toolkit.
a. True.
b. False.





Last Update: 11/20/96
Copyright © 1996 - Douglas Lyon
Lyon@cse.bridgeport.edu