Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Author:Allen B. Downey
Language: eng
Format: mobi
Tags: Version 5.1.2
Published: 0101-01-01T00:00:00+00:00


210

Chapter 16. GridWorld: Part 3

of Life.

Now fill in the bodies of countLiveNeighbors and updateStatus according

to the rules and see if the system behaves as expected.

16.8

Initial conditions

To change the initial conditions, you can use the GridWorld pop-up menus to

set the status of the Rocks by invoking setAlive. Or you can write methods

to automate the process.

In LifeRunner, add a method called makeRow that creates an initial config-

uration with n live Rocks in a row in the middle of the grid. What happens

for different values of n?

Add a method called makePentomino that creates an r-pentomino in the

middle of the Grid. The initial configuration should look like this:

If you run this configuration for more than a few steps, it reaches the end

of the Grid. The boundaries of the Grid change the behavior of the system;

in order to see the full evolution of the r-pentomino, the Grid has to be big

enough. You might have to experiment to find the right size, and depending

on the speed of your computer, it might take a while.

The Game of Life web page describes other initial conditions that yield in-

teresting results (http://www.conwaylife.com/). Choose one you like and implement it.

16.9. Exercises

211

There are also variations of the Game of Life based on different rules. Try

one out and see if you find anything interesting.

16.9

Exercises

Exercise 16.1. Starting with a copy of BlueBug.java, write a class defini-

tion for a new kind of Bug that finds and eats flowers. You can “eat” a flower

by invoking removeSelfFromGrid on it.

Exercise 16.2. Now you know what you need to know to read Part 3 of the

GridWorld Student Manual and do the exercises.

Exercise 16.3. If you implemented the Game of Life, you are well prepared

for Part 4 of the GridWorld Student Manual. Read it and do the exercises.

Congratulations, you’re done!

212

Chapter 16. GridWorld: Part 3

Appendix A

Graphics

A.1

Java 2D Graphics

This appendix provides examples and exercises that demonstrate Java graph-

ics. There are several ways to create graphics in Java; the simplest is to use

java.awt.Graphics. Here is a complete example:

import java.awt.Canvas;

import java.awt.Graphics;

import javax.swing.JFrame;

public class MyCanvas extends Canvas {

public static void main(String[] args) {

// make the frame

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add the canvas

Canvas canvas = new MyCanvas();

canvas.setSize(400, 400);

frame.getContentPane().add(canvas);

// show the frame

214

Appendix A. Graphics

frame.pack();

frame.setVisible(true);

}

public void paint(Graphics g) {

// draw a circle

g.fillOval(100, 100, 200, 200);

}

}

You

can

download

this

code

from

http://thinkapjava.com/code/

MyCanvas.java.

The first lines import the classes we need from java.awt and javax.swing.

MyCanvas extends Canvas, which means that a MyCanvas object is a kind of

Canvas that provides methods for drawing graphical objects.

In main we

1. Create a JFrame, which is a window that can contain the canvas, but-

tons, menus, and other window components;

2. Create MyCanvas, set its width and height, and add it to the frame;

and

3. Display the frame on the screen.

paint is a special method that gets invoked when MyCanvas needs to be

drawn. If you run this code, you should see a black circle on a gray back-

ground.

A.2

Graphics methods

To draw on the Canvas, you invoke methods on the Graphics object. The pre-

vious example uses fillOval. Other methods include drawLine, drawRect

and more.

You can read the documentation of these methods at http:

//download.oracle.com/javase/6/docs/api/java/awt/Graphics.html.

Here is the prototype for fillOval:

A.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.