Skip to content

This project implements a command-based drawing tool inspired by the Logo programming language. The application interprets a sequence of textual instructions to control a virtual turtle that moves on a 2D canvas, drawing lines according to the commands. Programs can include loops and user-defined functions, allowing the creation of complex drawings

Notifications You must be signed in to change notification settings

maurosuetta/Drawing-Tool-Java

Repository files navigation

Drawing-Tool-Java

Logo Drawing Tool in Java

Overview

This project implements a command-based drawing tool inspired by the Logo programming language. The application interprets a sequence of textual instructions to control a virtual turtle that moves on a 2D canvas, drawing lines according to the commands. Programs can include loops and user-defined functions, allowing the creation of complex drawings such as hearts, squares, and custom shapes. The drawing is rendered graphically in a Java Swing JPanel (Logo).

Architecture and Class Structure

The project follows an object-oriented design, separating concerns between instruction representation, execution, validation, and rendering.

Instruction Hierarchy

  1. Instruction: Base class representing a generic instruction with a word and optional parameter bounds (lowerBound, upperBound). Provides the isLegal(int p) method to check parameter validity.
  2. TurtleInstruction (abstract, extends Instruction): Abstract class for instructions that act on a Turtle. Declares apply(int p) to be implemented by concrete subclasses.
    1. PenInstruction: Activates or deactivates the turtle's pen (turtle.setPen(p)).
    2. RotInstruction: Rotates the turtle by a specified angle (turtle.rotate(p)).
    3. FwdInstruction: Moves the turtle forward a given distance (turtle.moveFwd(p)).
  3. Function (extends Instruction): Represents a user-defined function, associating a name with a Program that can be executed recursively.

Program Representation

Statement: Represents a single instruction and its integer parameter (word, param). Program: A container for an ordered list of Statement objects. Supports adding statements individually or as Statement objects. Validator: Provides static analysis of a Program, checking for invalid instructions, parameter violations, and unbalanced loops (REP/END). Reports detailed error messages.

Execution and Rendering

  1. Turtle: Maintains the state of the turtle, including position (x, y), angle, and pen status. Responsible for moving and drawing segments on the canvas. Implements a paint(Graphics g) method to render the turtle itself.
  2. Segment: Represents a drawable line segment with start/end coordinates, color, and thickness. Encapsulates the actual graphics drawing.
  3. Logo (extends JPanel): Core canvas class. Maintains the Turtle, instruction dictionary (HashMap<String, Instruction>), and all drawn Segments. Handles the rendering pipeline via paint(Graphics g) and allows registering user-defined functions.
  4. Interpreter: Executes a Program over a Logo instance. Handles loops (REP/END), turtle instructions, and function calls. Maintains an internal loop stack to manage nested iterations. Executes instructions sequentially and updates the canvas in real-time.

Usage

  1. Create a Logo instance (the drawing canvas).
  2. Define Programs with Statements representing turtle movements, rotations, pen commands, loops (REP/END), and function calls.
  3. Use Validator to check programs for errors before execution.
  4. Add user-defined Functions to Logo if needed.
  5. Create an Interpreter with the Logo and Program, and call run() to execute the drawing.
  6. Add the Logo panel to a Swing JFrame for visualization.
Logo logo = new Logo();
Program heart = new Program();
// Add statements to heart program...
Function fHeart = new Function("HRT", heart);
logo.addFunction("HRT", fHeart.getProgram());

Program mainProgram = new Program();
// Add statements, loops, and function calls...
Interpreter interpreter = new Interpreter(logo, mainProgram);
interpreter.run();

JFrame frame = new JFrame();
frame.setContentPane(logo);
frame.pack();
frame.setVisible(true);

Possible Improvements

  • Enhanced Graphics: Support for multiple colors, variable stroke widths, and shapes beyond line segments.
  • User Interface: Implement a GUI for interactive command entry and real-time visualization.
  • Extended Language Features: Add variables, conditionals, and nested function definitions.
  • Error Handling: Improve runtime error reporting (e.g., divide-by-zero for parameters, invalid function calls).
  • Performance Optimization: Optimize rendering for large numbers of segments or deep loop nesting.
  • Persistence: Save and load programs to/from files.

About

This project implements a command-based drawing tool inspired by the Logo programming language. The application interprets a sequence of textual instructions to control a virtual turtle that moves on a 2D canvas, drawing lines according to the commands. Programs can include loops and user-defined functions, allowing the creation of complex drawings

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages