Docsity

Java Programming: Chapter Two - Programming by Example, Lab Reports of Computer Science

Partial preview of the text

Download Java Programming: Chapter Two - Programming by Example and more Lab Reports Computer Science in PDF only on Docsity!

Mat 2170

Chapter Two: Programming by Example

Java Programming

Mathematics and Computer Science Department Eastern Illinois University

Spring 2009

Student Responsibilities

Reading: Textbook, Chapter 2

Lab 1 submissions (printout, electronic) & web publishing

Lab 2, Prelab (due at beginning of Lab 2, Thursday), & Postlab (due at beginning of Lab 3, next week)

Attendance

2.6 Graphical Programs

The GraphicsProgram class makes it possible to create simple pictures on the screen.

The conceptual model is that of a collage composed of objects on a canvas or a felt board.

Running a GraphicsProgram creates a window that serves as the background canvas for the collage.

You create picture by creating graphical objects of various kinds, and then adding those objects to the canvas.

We will be learning how to work with labels (textual graphics), rectangles, ovals, and lines using the classes GLabel, GRect, GOval, and GLine.

The complete set of graphics classes is discussed in Chapter 9.

GLabel Objects — Unnamed vs Named

Create and Send Directly to Graphics Window:

public void run() { add(new GLabel("Hello, World!", 100, 75)); }

— VS —

Declare a Named Object (–MyLabel–) of type GLabel, which is then sent to Graphics Window:

public void run() { GLabel MyLabel = new GLabel("Hello, World!", 100, 75); add(MyLabel); }

Sending Messages to Objects

To send a message to an object, Java uses the following syntax: receiver.methodName(arguments); where:

receiver is the (named) object to which the message is directed methodName identifies which message is sent arguments is a list of values used to specify any other information associated with the message

Sending Messages to a GLabel

This program illustrates sending a message to an object. Note that the label doesn’t appear until it is added to the canvas.

public class HelloProgram extends GraphicsProgram { public void run() { GLabel MyLabel = new GLabel("Hello",100,75); MyLabel.setFont("SansSerif-36"); MyLabel.setColor(Color.RED); add(MyLabel); } }

contents of MyLabel: (^) “Hello”

The GObject Hierarchy

The classes that represent graphical objects form a hierarchy, part of which looks like this:

GObject

GLabel GRect GOval GLine

Operations are defined at each level of the hierarchy. Operations that apply to all graphical objects are specified at the GObject level — where they are inherited by each subclass. Operations that apply to a particular subclass are specified as part of the definition of that class.

Operations on the GObject Class

The following operations apply to all GObjects

object.setColor(color)

Sets the color of the object to the specified color constant

object.setLocation(x, y)

Changes the location of the ob- ject to the point (x, y)

object.move(dx, dy)

Moves the object on the screen by adding the displacements dx and dy to its current coordinates

Operations on the GLabel Class

Constructor: new GLabel(text, x, y) Creates a label containing the specified text that begins at the point (x, y).

GLabel Example

public void run() { GLabel msg = new GLabel("Hello, World!", 100, 75); add(msg); }

Methods Shared by the GRect and GOval Classes

object.setFilled(fill)

If fill is true, fills in the interior of the object; if false, shows only the outline.

object.setFillColor(color)

Sets the color used to fill the interior, which can be different from the border.

The GRectPlusGOval Program

public class GRectPlusGOval extends GraphicsProgram { public void run() { // Create & draw a red rectangle GRect MyRect = new GRect(100, 50, 125, 60); MyRect.setFilled(true); MyRect.setColor(Color.RED); add(MyRect);

// Create & draw a green oval inside GOval MyOval = new GOval(100, 50, 125, 60); MyOval.setFilled(true); MyOval.setFillColor(Color.GREEN); add(MyOval); } }

What would we need to change in the last program to have the rectangle be drawn after the oval? What would happen in that case?

What would we need to change if we wanted a cyan oval with a blue border?

What is the difference between the setColor() and the setFillColor() methods for GRects and GOvals?

What GraphicsProgram method displays a GObject in the graphics window?

Suppose we’d like to draw a stick figure in a graphics window. How could we go about determining the coordinates of the parts?

The Stick Figure