jbang-examples icon indicating copy to clipboard operation
jbang-examples copied to clipboard

feat: a turtle graphics demo program

Open wfouche opened this issue 5 months ago • 14 comments

A turtle graphics example program for the kids (or young at heart) learning to program in Java using JBang

image

wfouche avatar Jul 10 '25 13:07 wfouche

Ready to be reviewed

wfouche avatar Jul 11 '25 07:07 wfouche

@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.

wfouche avatar Jul 11 '25 10:07 wfouche

What is the "best" turtle graphics library for Java?

  • https://gemini.google.com/app/916a6745a3128647

wfouche avatar Jul 11 '25 10:07 wfouche

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.

maxandersen avatar Jul 11 '25 17:07 maxandersen

What is the "best" turtle graphics library for Java?

  • https://gemini.google.com/app/916a6745a3128647

Link doesn't work

maxandersen avatar Jul 11 '25 17:07 maxandersen

This new TurtleDemo.java program can be run without relying on external dependencies.

$ jbang run TurtleDemo.java

wfouche avatar Aug 15 '25 17:08 wfouche

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

wfouche avatar Aug 15 '25 17:08 wfouche

Well running jbang https://github.com/jbangdev/jbang-examples/blob/ae3947cda944f2e354cf65a970d15b97a1084964/examples/turtle/TurtleDemo.java definitely works :-)

quintesse avatar Aug 17 '25 19:08 quintesse

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.

quintesse avatar Aug 17 '25 19:08 quintesse

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.

maxandersen avatar Aug 18 '25 09:08 maxandersen

Sure, but it would be nicer to do that upstream, right?

quintesse avatar Aug 18 '25 10:08 quintesse

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? :-) )

quintesse avatar Aug 18 '25 16:08 quintesse

I've submitted a pull request to resolve one of the warnings in the Turtle.java code

  • https://github.com/NicholasSeward/Turtle/pull/1

wfouche avatar Aug 19 '25 09:08 wfouche

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

image

wfouche avatar Aug 19 '25 09:08 wfouche