Lab 7 (Feb 25-26)

Needless to say you can use the lab time to work on antyhing that you need to work on:

Introduction

Instantiate a GBFrame and draw some lines etc. Get comfortable with using paint() to produce a drawing of some sort on the graphics context of the frame. Then move to the second part: turtle graphics.

Notice that we define a Turtle to be basically a Vehicle from our previous assignments. It has three instance variables, that describe:

A Turtle also knows to do two things: With these we can produce turtle graphics: various designs which are the outcome of the turtle wandering in the plane. Turtles produce incremental, iterative graphics, that are hard to capture in closed formulas (as circles, lines, Bezier curves, splines, etc. can). The graphics produced by turtles are not smooth (in the mathematical sense) but they can be very complex.

Turtle Graphics

Look at the following program:

tucotuco.cs.indiana.edu% cat TurtleGraphics.java

import java.awt.*; import BreezyGUI.*; public class TurtleGraphics extends GBFrame { public void paint(Graphics g) { Turtle t = new Turtle(75, 75); t.produceDrawing(g); } public static void main(String[] args) { Frame f = new TurtleGraphics(); f.setSize(100, 100); f.setVisible(true); } } class Turtle { int x, y; int direction; Turtle(int x, int y) { this.x = x; this.y = y; direction = 90; } void forward(int distance, Graphics g) { double newX, newY; // This part actually moves in the direction "direction" newX = x + distance * Math.sin(Math.PI * direction / 180.0); newY = y + distance * Math.cos(Math.PI * direction / 180.0); // along a straight line for a distance of "distance". g.setColor(Color.black); // set the color to black g.drawLine(x, y, (int)newX, (int)newY); // draw the line x = (int)newX; // and move to the new position y = (int)newY; // (that is, update x and y) } void right(int angle) { direction += angle; direction %= 360; } void produceDrawing(Graphics g) { for (int i= 0; i < 4; i++) drawThing(g); } void drawThing(Graphics g) { forward(40, g); right(90); forward(40, g); right(90); forward(20, g); right(90); forward(20, g); right(90); forward(40, g); right(90); forward(10, g); right(90); forward(10, g); right(90); forward(20, g); } }
Study the Turtle.

Look at drawThing:

then at the same thing drawn 4 times in succession:
You see a few errors due to the rounding of the values of sin and cos functions.

Modify the program and come up with new designs.

I'll post some here in the morning.