




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the course materials for eastern illinois university's mathematics and computer science department's mat 2170 - java programming course, focusing on chapter two: programming by example. It covers topics such as student responsibilities, graphical programs, sending messages to objects, and the java coordinate system. Students are expected to read the textbook, complete lab assignments, attend classes, and learn about graphical programs and their components.
Typology: Lab Reports
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























Java Programming
Mathematics and Computer Science Department Eastern Illinois University
Spring 2009
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
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.
Create and Send Directly to Graphics Window:
public void run() { add(new GLabel("Hello, World!", 100, 75)); }
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); }
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
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 classes that represent graphical objects form a hierarchy, part of which looks like this:
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.
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
Constructor: new GLabel(text, x, y) Creates a label containing the specified text that begins at the point (x, y).
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.
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?