Sample Chapters

The introduction of the book and two of the chapters (chapters 2 and 3) are available for download here for the purpose of evaluating the book.

Below is the full Table of Contents of the book. Click on the chapter headings of chapters 2 and 3 to download the full text of those chapters.


Table of Contents

Acknowledgements 11

Introduction 13

1 Getting to know Greenfoot 15

1.1 Getting started 15
1.2 Objects and classes 17
1.3 Interacting with objects 19
1.4 Return types 21
1.5 Parameters 22
1.6 Greenfoot execution 24
1.7 A second example 25
1.8 Understanding the class diagram 26
1.9 Playing with Asteroids 27
1.10 Source code 28
1.11 Summary 31

2 The first program: Little Crab 33

2.1 The Little Crab scenario 34
2.2 Making the crab move 35
2.3 Turning 36
2.4 Dealing with screen edges 39
2.5 Summary of programming techniques 44

3 Improving the Crab— more sophisticated programming 47

3.1 Adding random behavior 47
3.2 Adding worms 51
3.3 Eating worms 53
3.4 Creating new methods 55
3.5 Adding a Lobster 57
3.6 Keyboard control 58
3.7 Ending the game 60
3.8 Adding sound 63
3.9 Summary of programming techniques 65

4 Finishing the crab game 67

4.1 Adding objects automatically 67
4.2 Creating new objects 70
4.3 Animating images 71
4.4 Greenfoot images 72
4.5 Instance variables (fields) 73
4.6 Assignment 76
4.7 Using actor constructors 77
4.8 Alternating the images 78
4.9 The if/else statement 79
4.10 Counting worms 81
4.11 More ideas 83
4.12 Summary of programming techniques 84

Interlude 1: Sharing your scenarios 85

I1.1 Exporting your scenario 85
I1.2 Export to application 85
I1.3 Export to a web page 87
I1.4 Publishing on the Greenfoot Gallery 87

5 Making music: An on-screen piano 91

5.1 Animating the key 92
5.2 Producing the sound 95
5.3 Abstraction: Creating multiple keys 97
5.4 Building the piano 99
5.5 Using loops: the while loop 100
5.6 Using arrays 105
5.7 Summary of programming techniques 111

6 Interacting objects: Newton's Lab 113

6.1 The starting point: Newton!s Lab 114
6.2 Helper classes: SmoothMover and Vector 115
6.3 The existing Body class 118
6.4 First extension: Creating movement 122
6.5 Using Java library classes 122
6.6 Adding gravitational force 125
6.7 The List type 128
6.8 The for-each loop 130
6.9 Applying gravity 132
6.10 Trying it out 135
6.11 Gravity and music 137
6.12 Summary of programming techniques 140

7 Collision detection: Asteroids 143

7.1 Investigation: What is there? 144
7.2 Painting stars 145
7.3 Turning 150
7.4 Flying forward 150
7.5 Colliding with asteroids 153
7.6 Casting 157
7.7 Adding fire power: the proton wave 161
7.8 Growing the wave 161
7.9 Interacting with objects in range 166
7.10 Further development 169
7.11 Summary of programming techniques 170

Interlude 2: The Greeps competition 173

I2.1 How to get started 174
I2.2 Programming your Greeps 176
I2.3 Running the competition 177
I2.4 Technicalities 178

8 Creating images and sound 179

8.1 Preparation 179
8.2 Working with sound 181
8.3 Sound recording and editing 182
8.4 Sound file formats and file sizes 184
8.5 Working with images 186
8.6 Image files and file formats 186
8.7 Drawing images 189
8.8 Combining images files and dynamic drawing 192
8.9 Summary 195

9 Simulations 197

9.1 Foxes and rabbits 199
9.2 Ants 203
9.3 Collecting food 205
9.4 Setting up the world 209
9.5 Adding pheromones 209
9.6 Path forming 212
9.7 Summary 213

10 Additional scenario ideas 215

10.1 Marbles 216
10.2 Lifts 218
10.3 Boids 219
10.4 Circles 221
10.5 Explosion 222
10.6 Breakout 223
10.7 Platform jumper 224
10.8 Wave 225
10.9 Summary 226

Appendix A: Installing Greenfoot 228

Appendix B: Greenfoot API 229

Appendix C: Collision detection 230

Appendix D: Some Java details 231

Back to main page