• No results found

39LABORATORY 2: In-lab Exercise

39

LABORATORY 2: In-lab Exercise 2

Name

Hour/Period/Section Date

It is preferable to see the graphical form of the curve represented by the point list created in

DrawCurv.java rather than just have a list of points that would become a square (or dragon curve) if we “connected the dots.” Each point in our Point List ADT for a square or a dragon curve represents the location of a pixel in our graphic display. On the computer screen a pixel (or picture element) is a tiny dot that represents a very small piece of a picture. Today’s com- puter screens typically have a resolution of 1024 by 768 pixels or greater. Each computer sys- tem and programming language defines a coordinate system that allows us to refer to a pixel location on the screen. Fortunately, Java has a vast graphics library, officially called the abstract window toolkit (AWT), to help you create and manage your graphic display.

For the most part two revisions to the DrawCurv program are needed to display a graphic draw- ing in a simple window: (1) additions to the default constructor and (2) definition of a new sub- class of the Canvas class. Among the AWT classes used for this graphic display are the Frame class, the Canvas class, the WindowAdapter class, and the Graphics class.

Every graphical program in Java consists of one or more frame windows. Therefore, begin by defining the DrawCurvFrame class so that it extends Java’s Frame class as follows.

class DrawCurvFrame extends Frame

The default constructor for DrawCurvFrame has purposely been designed to define the runtime environment and initiate the processing for the DrawCurv program. To convert this console application (where the display is confined to a single terminal window) to a graphical applica- tion (that is capable of displaying both text and graphics) requires definition of a graphical runt- ime environment. To initialize the window frame for this graphical application, add the code fragments (as illustrated below) to the default constructor for DrawCurvFrame.

class DrawCurvFrame extends Frame {

... ...

// Create area/Canvas for drawings CurvCanvas cBoard = new CurvCanvas( );

// Default constructor

public DrawCurvFrame( ) throws IOException {

...

// Set up graphics windows -- Frame & added Canvas // Initialize the basic window Frame

setTitle("Curve Drawing - Laboratory 2"); // window title setSize(500, 400); // window size // Define how the window responds to click on close button addWindowListener(new MyLocalWindowAdapter( ));

...

// Add area/Canvas for drawings add(cBoard);

...

if ( dispPts == 'Y' || dispPts == 'y') {

// Iterate through the PointList ...

// Make Canvas invisible cBoard.setVisible(false); }

else {

// Show window display of curve - make frame visible setVisible(true);

}

} // default constructor: DrawCurvFrame( ) // Define MyLocalWindowAdapter

private class MyLocalWindowAdapter extends WindowAdapter {

public void windowClosing(WindowEvent event) {

dispose( ); System.exit(0); }

} // Inner class MyLocalWindowAdapter ...

...

}// class DrawCurvFrame

This code fragment gives the window/frame the title “Curve Drawing—Laboratory 2,” establishes its initial size at 500 by 400 pixels, and then adds a WindowListener for programming the desired response when the user clicks on the close button and a Canvas board in which the drawing will actually appear. In the if-clause, the setVisible method determines whether a particular compo- nent will be visible (setVisible(true)) or invisible (setVisible(false)). Also included is an inner class definition that allows us to override the windowClosing method in Java’s WindowAdapter class. (An inner class is a class definition embedded inside another class.) When the user clicks on the window’s close button, our windowClosing method in this inner class

LABORATORY 2

41

Finally, create a class definition for CurvCanvas that extends Java’s Canvas class as follows:

class CurvCanvas extends Canvas {

// Paint is the inherited method we override

// in order to draw our own image in an AWT window public void paint(Graphics g)

{

Point pt; // Point on curve

int startX, // (x, y) points for drawLine startY;

// Display the curve by iterating through the PointList (drawPts) if ( DrawCurvFrame.drawPts.gotoBeginning( ) )

{

// Call g.drawLine( int p1, int p2, int p3, int p4 )

}

} // class CurvCanvas

In Java a canvas is used to provide a dedicated drawing area for graphics. The paint method given above overrides the paint method in Java’s Canvas class. The paint method in

DrawCurv.java will contain the code for drawing a square or dragon curve on the computer screen—it will allow us to “connect the dots.”

The coordinate system on the computer screen is slightly different from the traditional two- dimensional coordinate system. As illustrated below, the origin (0, 0) is in the upper-left corner of the computer window and all coordinates are positive. The x-axis increases horizontally from left to right. And (most notable) the y-axis increases (not decreases) vertically from top to bottom. (1, 1) (3, 2) (4, 4) 1 2 3 4 1 2 3 4 (0, 0) y- a x i s x-axis

In the paint method the statement g.drawLine(startX, startY, endX, endY) is used to draw a line from point A (startX, startY) to point B (endX, endY) where g is the Graphics parameter passed to the paint method. In other words, the following statement would draw a line between points (1, 1) and (3, 2).

g.drawLine(1, 1, 3, 2);

These points without the drawn line are illustrated above.

Step 1: Modify your DrawCurv program (DrawCurve.java) so it produces a graphic display of the line segment connecting each pair of points in a point list. Segments where the comments are labeled ‘graphics:’ in the DrawCurve.jshlfile are to be implemented at this time.

Step 2: Test your modified program using a square and a dragon curve. A test plan is given below.

Step 3: If you discover mistakes in your graphics implementation of DrawCurv.java, correct them and execute your test plan again.

Test case

Expected curves

Checked

Square

Dragon curve (recursion depth 2)

Dragon curve (recursion depth 7)

Related documents