jbang-examples
jbang-examples copied to clipboard
feat: a turtle graphics demo program
A turtle graphics example program for the kids (or young at heart) learning to program in Java using JBang
Ready to be reviewed
@quintesse , @maxandersen
Just thinking out loud. Would it ever make sense to support the following functionaliy allowing the path to a local JAR file to be specified as a dependency?
//DEPS ./aplu5.jar
or maybe even (without having to first download the jar file) support the following syntax as well.
//DEPS https://java-online.ch/download/aplu5.jar
Some jars are not published to Maven Central, but can be downloaded from project specific web sites to a local folder. APLU5.jar is one of them. APLU5.jar is self-contained and do not depend on any other external Java libraries.
What is the "best" turtle graphics library for Java?
- https://gemini.google.com/app/916a6745a3128647
Standalone jars are not dependencies but class path entries. '--classpath %{url}' should work.
Not at laptop atm so can't verify if we ever got around to implement //CLASPATH in source files.
What is the "best" turtle graphics library for Java?
- https://gemini.google.com/app/916a6745a3128647
Link doesn't work
This new TurtleDemo.java program can be run without relying on external dependencies.
$ jbang run TurtleDemo.java
Link doesn't work
Unfortunately, none of the Turtle graphics libraries created for Java are availalbe from Maven Central. They are mostly available as source code or downloadable JAR files.
The Turtle Demo has been re-implemented using Turtle.java from
- https://github.com/NicholasSeward/Turtle
Well running jbang https://github.com/jbangdev/jbang-examples/blob/ae3947cda944f2e354cf65a970d15b97a1084964/examples/turtle/TurtleDemo.java definitely works :-)
But somehow I would find this even more "impressive":
///usr/bin/env jbang "$0" "$@" ; exit $?
//SOURCES https://github.com/NicholasSeward/Turtle/blob/main/Turtle.java
import java.awt.Color;
public class TurtleDemo {
public static void main(String[] args) {
Turtle joe = new Turtle();
joe.penColor(Color.RED);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 4; j++) {
joe.forward(150);
joe.right(90);
}
joe.left(36);
}
joe.left(90);
}
}
No need to copy a file, instead it gets taken directly from the source.
had same thought @quintesse.
One reason to copy it though could be so we could get rid of these warnings:
/Users/max/.jbang/cache/urls/8534b236b27dedef0acdcb3141b4961123c53487ef787f1ef59f09bc23ebda89/Turtle.java:41: warning: [removal] JApplet in javax.swing has been deprecated and marked for removal
private static JApplet applet;
^
/Users/max/.jbang/cache/urls/8534b236b27dedef0acdcb3141b4961123c53487ef787f1ef59f09bc23ebda89/Turtle.java:221: warning: [removal] JApplet in javax.swing has been deprecated and marked for removal
public static void startApplet(JApplet applet)
^
Note: /Users/max/.jbang/cache/urls/8534b236b27dedef0acdcb3141b4961123c53487ef787f1ef59f09bc23ebda89/Turtle.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Sure, but it would be nicer to do that upstream, right?
Btw, I just found another one-file library that could be nice for a JBang example: https://github.com/arkanovicz/essential-json/blob/master/src/main/java/com/republicate/json/Json.java it's a JSON parser in a single file (there's also a Maven artifact, but where's the fun in that? :-) )
I've submitted a pull request to resolve one of the warnings in the Turtle.java code
- https://github.com/NicholasSeward/Turtle/pull/1
Turtle.java itself has a main method with a nice demo that it runs.
$ jbang run https://github.com/NicholasSeward/Turtle/blob/main/Turtle.java