Skip to content

Commit 78b4387

Browse files
committed
docs(examples): show the demos in the Online IDE as their own files
Each demo source becomes one file in the Online IDE, under its own name and with its public static void main in place, so the project in the browser is the project a reader opens offline. Only the package, the imports and the demos/<folder>/ in front of asset paths go. DocumentationSnippetsTest compiles the files of an interactive example each under its own name instead of joined into one compilation unit, so a reference across files that only works in one file is caught. The cat platformer runs in the Online IDE: no Double.NaN or Double.isNaN (a FALLING sentinel instead) and no unparenthesised nested ternary.
1 parent 9481905 commit 78b4387

26 files changed

Lines changed: 442 additions & 287 deletions

‎docs/book/examples/cat-platformer.md‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ Click the stage so it takes the keyboard.
9090

9191
```java CatPlatformer.java
9292

93-
void main() {
94-
Window myWindow = new Window(800, 480);
95-
myWindow.setStage(new CatPlatformer());
96-
}
97-
9893
/**
9994
* The Scratch for Java cat in a small platformer: collect the coffee beans,
10095
* jump on the bugs (or punch them) and climb up to the steaming mug.
@@ -105,14 +100,17 @@ void main() {
105100
* beans, the bugs, the mug and the hearts are on the built-in cat sheet, the
106101
* tiles and the sounds are Kenney's.
107102
*/
108-
class CatPlatformer extends Stage {
103+
public class CatPlatformer extends Stage {
109104

110105
/** The height the ground reaches up to. */
111106
public static final double GROUND_TOP = -176;
112107

113108
/** Where the ladder ends: the top of the platform with the mug. */
114109
public static final double LADDER_TOP = 112;
115110

111+
/** What landingHeight answers while there is nothing to land on, far below the stage. */
112+
public static final double FALLING = -100000;
113+
116114
private final List<Platform> platforms = new ArrayList<>();
117115
private final Heart[] hearts = new Heart[3];
118116

@@ -167,7 +165,7 @@ class CatPlatformer extends Stage {
167165

168166
private void addPlatform(double left, double top, int tiles) {
169167
for (int i = 0; i < tiles; i++) {
170-
String costume = i == 0 ? "grassHalf_left" : i == tiles - 1 ? "grassHalf_right" : "grassHalf_mid";
168+
String costume = i == 0 ? "grassHalf_left" : (i == tiles - 1 ? "grassHalf_right" : "grassHalf_mid");
171169
Platform tile = new Platform(costume, left + 32 + i * 64, top);
172170
this.platforms.add(tile);
173171
this.add(tile);
@@ -188,18 +186,18 @@ class CatPlatformer extends Stage {
188186
* from feetBefore down to feetAfter; if a platform top (or the ground) lies
189187
* in between, below x, that is where it stands.
190188
*
191-
* @return the height of what it lands on, or NaN while it is still falling
189+
* @return the height of what it lands on, or FALLING while it is still falling
192190
*/
193191
public double landingHeight(double x, double feetBefore, double feetAfter) {
194-
double best = Double.NaN;
192+
double best = FALLING;
195193
for (Platform platform : this.platforms) {
196194
boolean above = x > platform.getLeft() && x < platform.getRight();
197195
double top = platform.getTop();
198-
if (above && feetBefore >= top && feetAfter <= top && !(best >= top)) {
196+
if (above && feetBefore >= top && feetAfter <= top && top > best) {
199197
best = top;
200198
}
201199
}
202-
if (Double.isNaN(best) && feetAfter <= GROUND_TOP) {
200+
if (best == FALLING && feetAfter <= GROUND_TOP) {
203201
best = GROUND_TOP;
204202
}
205203
return best;
@@ -242,10 +240,18 @@ class CatPlatformer extends Stage {
242240
text.showText(message);
243241
this.add(text);
244242
}
243+
244+
public static void main(String[] args) {
245+
Window myWindow = new Window(800, 480);
246+
myWindow.setStage(new CatPlatformer());
247+
}
245248
}
249+
```
250+
251+
```java Platform.java
246252

247253
/** One tile of a platform the cat can stand on. */
248-
class Platform extends Sprite {
254+
public class Platform extends Sprite {
249255

250256
private final double top;
251257

@@ -270,9 +276,12 @@ class Platform extends Sprite {
270276
return this.getX() + 32;
271277
}
272278
}
279+
```
280+
281+
```java Heart.java
273282

274283
/** One of the cat's lives, shown in the top left corner. */
275-
class Heart extends Sprite {
284+
public class Heart extends Sprite {
276285

277286
public Heart(double x, double y) {
278287
this.addCostume("heart_full");
@@ -289,9 +298,12 @@ class Heart extends Sprite {
289298
this.switchCostume("heart_empty");
290299
}
291300
}
301+
```
302+
303+
```java Ground.java
292304

293305
/** One grass tile of the ground. */
294-
class Ground extends Sprite {
306+
public class Ground extends Sprite {
295307

296308
public Ground(double x) {
297309
this.addCostume("grassMid");
@@ -301,9 +313,12 @@ class Ground extends Sprite {
301313
this.setY(CatPlatformer.GROUND_TOP - 32);
302314
}
303315
}
316+
```
317+
318+
```java Ladder.java
304319

305320
/** One piece of the ladder. Touching it, the cat can climb with the up and down keys. */
306-
class Ladder extends Sprite {
321+
public class Ladder extends Sprite {
307322

308323
public Ladder(double x, double y, boolean top) {
309324
this.addCostume(top ? "ladderTop" : "ladderMid");
@@ -312,9 +327,12 @@ class Ladder extends Sprite {
312327
this.setY(y);
313328
}
314329
}
330+
```
331+
332+
```java Bug.java
315333

316334
/** A bug walking to and fro. Jump on it or punch it - but do not walk into it. */
317-
class Bug extends Sprite {
335+
public class Bug extends Sprite {
318336

319337
/** The costume is 32 pixels high, the legs end 3 pixels above its bottom. */
320338
private static final double FEET = 13;
@@ -362,9 +380,12 @@ class Bug extends Sprite {
362380
}
363381
}
364382
}
383+
```
384+
385+
```java Goal.java
365386

366387
/** The steaming mug at the end of the level. */
367-
class Goal extends Sprite {
388+
public class Goal extends Sprite {
368389

369390
public Goal(double x, double floor) {
370391
this.addAnimation("steam", "goal_mug_%d", 4);
@@ -378,9 +399,12 @@ class Goal extends Sprite {
378399
this.playAnimation("steam");
379400
}
380401
}
402+
```
403+
404+
```java Cat.java
381405

382406
/** The Scratch for Java cat. Every one of its animations is on the built-in cat sheet. */
383-
class Cat extends Sprite {
407+
public class Cat extends Sprite {
384408

385409
/** The costumes are 64 pixels high with the feet on the bottom row. */
386410
private static final double FEET = 32;
@@ -551,7 +575,7 @@ class Cat extends Sprite {
551575
this.fallSpeed -= GRAVITY;
552576
this.changeY(this.fallSpeed);
553577
double landing = this.level().landingHeight(this.getX(), before, this.feet());
554-
if (!Double.isNaN(landing) && this.fallSpeed <= 0) {
578+
if (landing != CatPlatformer.FALLING && this.fallSpeed <= 0) {
555579
if (!this.onGround && this.fallSpeed < -4) {
556580
this.landFrames = 8;
557581
}
@@ -630,9 +654,12 @@ class Cat extends Sprite {
630654
}
631655
}
632656
}
657+
```
658+
659+
```java Bean.java
633660

634661
/** A spinning coffee bean to collect. */
635-
class Bean extends Sprite {
662+
public class Bean extends Sprite {
636663

637664
public Bean(double x, double y) {
638665
this.addAnimation("spin", "coffee_bean_%d", 4);

‎docs/book/examples/cat.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ itself behind.
2020

2121
```java CatSketch.java
2222

23-
void main() {
24-
Window myWindow = new Window(800, 600);
25-
myWindow.setStage(new CatSketch());
26-
}
27-
28-
class CatSketch extends Stage {
23+
public class CatSketch extends Stage {
2924

3025
// begin stage designer
3126
private CatSprite myCat;
@@ -37,6 +32,11 @@ class CatSketch extends Stage {
3732
this.add(myCat);
3833
// end stage designer
3934
}
35+
36+
public static void main(String[] args) {
37+
Window myWindow = new Window(800, 600);
38+
myWindow.setStage(new CatSketch());
39+
}
4040
}
4141

4242
class CatSprite extends Sprite {

‎docs/book/examples/clock.md‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ smoothly instead of ticking.
2323

2424
```java ClockStage.java
2525

26-
void main() {
27-
Window myWindow = new Window(800, 800);
28-
myWindow.setStage(new ClockStage());
29-
}
30-
31-
class ClockStage extends Stage {
26+
public class ClockStage extends Stage {
3227

3328
// begin stage designer
3429
private ClockSprite clockSprite;
@@ -49,17 +44,28 @@ class ClockStage extends Stage {
4944
this.add(hourHandSprite);
5045
// end stage designer
5146
}
47+
48+
public static void main(String[] args) {
49+
Window myWindow = new Window(800, 800);
50+
myWindow.setStage(new ClockStage());
51+
}
5252
}
53+
```
54+
55+
```java ClockSprite.java
5356

54-
class ClockSprite extends Sprite {
57+
public class ClockSprite extends Sprite {
5558
public ClockSprite() {
5659
// begin sprite editor
5760
this.addCostume("clock", "sprites/clock.png");
5861
// end sprite editor
5962
}
6063
}
64+
```
65+
66+
```java SecondHandSprite.java
6167

62-
class SecondHandSprite extends Sprite {
68+
public class SecondHandSprite extends Sprite {
6369

6470
public SecondHandSprite() {
6571
// begin sprite editor
@@ -77,8 +83,11 @@ class SecondHandSprite extends Sprite {
7783
}
7884
}
7985
}
86+
```
8087

81-
class MinuteHandSprite extends Sprite {
88+
```java MinuteHandSprite.java
89+
90+
public class MinuteHandSprite extends Sprite {
8291
public MinuteHandSprite() {
8392
// begin sprite editor
8493
this.addCostume("hand", "sprites/minute.png");
@@ -90,8 +99,11 @@ class MinuteHandSprite extends Sprite {
9099
this.setDirection(90 + minute / 60.0 * 360);
91100
}
92101
}
102+
```
103+
104+
```java HourHandSprite.java
93105

94-
class HourHandSprite extends Sprite {
106+
public class HourHandSprite extends Sprite {
95107
public HourHandSprite() {
96108
// begin sprite editor
97109
this.addCostume("hand", "sprites/hour.png");

‎docs/book/examples/coin-collector.md‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ arrow keys, jump with space, and collect all six coins.
4444

4545
```java CoinCollector.java
4646

47-
void main() {
48-
Window myWindow = new Window(800, 480);
49-
myWindow.setStage(new CoinCollector());
50-
}
51-
5247
/**
5348
* A small game that only uses assets built into Scratch for Java.
5449
*
@@ -59,7 +54,7 @@ void main() {
5954
* https://scratch4j.openpatch.org/sprites and
6055
* https://scratch4j.openpatch.org/sounds
6156
*/
62-
class CoinCollector extends Stage {
57+
public class CoinCollector extends Stage {
6358

6459
// begin stage designer
6560
private Player player;
@@ -118,10 +113,18 @@ class CoinCollector extends Stage {
118113
this.add(won);
119114
}
120115
}
116+
117+
public static void main(String[] args) {
118+
Window myWindow = new Window(800, 480);
119+
myWindow.setStage(new CoinCollector());
120+
}
121121
}
122+
```
123+
124+
```java Player.java
122125

123126
/** The alien you steer with the arrow keys. */
124-
class Player extends Sprite {
127+
public class Player extends Sprite {
125128

126129
private static final int SIZE = 45;
127130

@@ -208,9 +211,12 @@ class Player extends Sprite {
208211
}
209212
}
210213
}
214+
```
215+
216+
```java Ground.java
211217

212218
/** One grass tile of the ground. */
213-
class Ground extends Sprite {
219+
public class Ground extends Sprite {
214220

215221
public Ground(double x) {
216222
// begin sprite editor
@@ -222,9 +228,12 @@ class Ground extends Sprite {
222228
this.setY(CoinCollector.GROUND_TOP - 32);
223229
}
224230
}
231+
```
232+
233+
```java Coin.java
225234

226235
/** A coin the player can pick up. It bobs up and down a little. */
227-
class Coin extends Sprite {
236+
public class Coin extends Sprite {
228237

229238
private final double startY;
230239
private double angle = 0;

0 commit comments

Comments
 (0)