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
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.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6823)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6554)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6420)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3994)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3786)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3729)
The Ultimate iOS Interview Playbook by Avi Tsadok(3705)
