From 0aa4692ec069ec06df77be2cf4a945a5fdf2b1ba Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 6 Dec 2022 19:36:51 +0300 Subject: [PATCH 1/9] Got it back to the previous PR's state. --- .gitHooks/pre-commit | 18 ++ .gitignore | 2 +- .project | 11 + .../Programs}/colouredbox | 0 .../Programs}/commandError | 0 .../Programs}/lozenge | 0 .../Programs}/redbox | 0 .../Programs/threeTurtles | 0 .../Programs}/twolines | 0 Turtle-Interpreter/pom.xml | 78 +++++++ .../name/hamdan/turtleinterpreter/App.java | 24 ++ .../name/hamdan/turtleinterpreter/Turtle.java | 172 ++++++++++++++ .../turtleinterpreter/TurtleInterpreter.java | 215 +++++++++++++++++ .../src/main/resources/Images}/TurtleBlue.png | Bin Turtle-Interpreter/src/site/site.xml | 26 +++ .../hamdan/turtleinterpreter/AppTest.java | 38 +++ src/Turtle.java | 182 --------------- src/TurtleInterpreter.java | 217 ------------------ 18 files changed, 583 insertions(+), 400 deletions(-) create mode 100755 .gitHooks/pre-commit rename {TurtlePrograms => Turtle-Interpreter/Programs}/colouredbox (100%) rename {TurtlePrograms => Turtle-Interpreter/Programs}/commandError (100%) rename {TurtlePrograms => Turtle-Interpreter/Programs}/lozenge (100%) rename {TurtlePrograms => Turtle-Interpreter/Programs}/redbox (100%) rename TurtlePrograms/ThreeTurtles => Turtle-Interpreter/Programs/threeTurtles (100%) rename {TurtlePrograms => Turtle-Interpreter/Programs}/twolines (100%) create mode 100644 Turtle-Interpreter/pom.xml create mode 100644 Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java create mode 100644 Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java create mode 100644 Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java rename {Images => Turtle-Interpreter/src/main/resources/Images}/TurtleBlue.png (100%) create mode 100644 Turtle-Interpreter/src/site/site.xml create mode 100644 Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java delete mode 100644 src/Turtle.java delete mode 100644 src/TurtleInterpreter.java diff --git a/.gitHooks/pre-commit b/.gitHooks/pre-commit new file mode 100755 index 0000000..caa3bf8 --- /dev/null +++ b/.gitHooks/pre-commit @@ -0,0 +1,18 @@ +#!/bin/bash + +# First: git config core.hooksPath `pwd`/.gitHooks + +# Copy jar file to repo root +# JAR_FILE=Turtle-Interpreter/target/Turtle-Interpreter-0.1.0.jar + +# cp ${JAR_FILE} . +# EXIT_CODES=( $? ) +# git add -A +# EXIT_CODES+=( $? ) + +# for e in ${EXIT_CODES[@]}; do +# if [[ $e -gt 0 ]] +# then +# exit 1 +# fi +# done diff --git a/.gitignore b/.gitignore index ae3c172..eb5a316 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/bin/ +target diff --git a/.project b/.project index e813fb4..c9fa3c0 100644 --- a/.project +++ b/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1670344239806 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/TurtlePrograms/colouredbox b/Turtle-Interpreter/Programs/colouredbox similarity index 100% rename from TurtlePrograms/colouredbox rename to Turtle-Interpreter/Programs/colouredbox diff --git a/TurtlePrograms/commandError b/Turtle-Interpreter/Programs/commandError similarity index 100% rename from TurtlePrograms/commandError rename to Turtle-Interpreter/Programs/commandError diff --git a/TurtlePrograms/lozenge b/Turtle-Interpreter/Programs/lozenge similarity index 100% rename from TurtlePrograms/lozenge rename to Turtle-Interpreter/Programs/lozenge diff --git a/TurtlePrograms/redbox b/Turtle-Interpreter/Programs/redbox similarity index 100% rename from TurtlePrograms/redbox rename to Turtle-Interpreter/Programs/redbox diff --git a/TurtlePrograms/ThreeTurtles b/Turtle-Interpreter/Programs/threeTurtles similarity index 100% rename from TurtlePrograms/ThreeTurtles rename to Turtle-Interpreter/Programs/threeTurtles diff --git a/TurtlePrograms/twolines b/Turtle-Interpreter/Programs/twolines similarity index 100% rename from TurtlePrograms/twolines rename to Turtle-Interpreter/Programs/twolines diff --git a/Turtle-Interpreter/pom.xml b/Turtle-Interpreter/pom.xml new file mode 100644 index 0000000..8c4e64b --- /dev/null +++ b/Turtle-Interpreter/pom.xml @@ -0,0 +1,78 @@ + + + + 4.0.0 + + name.hamdan.turtleinterpreter + Turtle-Interpreter + 0.1.0 + + Turtle-Interpreter + Turtle-Interpreter. + https://turtleinterpreter.hamdan.name + + + UTF-8 + 16 + 16 + + + + + junit + junit + 3.8.1 + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + name.hamdan.turtleinterpreter.App + + + + + + + + + + + + + + maven-project-info-reports-plugin + + + + diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java new file mode 100644 index 0000000..42d7b9d --- /dev/null +++ b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java @@ -0,0 +1,24 @@ +package name.hamdan.turtleinterpreter; + +import javax.swing.*; +import java.io.IOException; + +/** + * Turtle Interpreter + */ +public class App { + + public static void main(String[] args) throws IOException { + JFrame f = new JFrame(); + TurtleInterpreter turtleInterpreter = new TurtleInterpreter(); + JButton b = new JButton("Open File"); + turtleInterpreter.add(b); + b.addActionListener(turtleInterpreter); + f.add(turtleInterpreter); + f.setVisible(true); + f.setSize(turtleInterpreter.getFrameHeight(), turtleInterpreter.getFrameWidth()); + f.setResizable(false); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java new file mode 100644 index 0000000..cb45683 --- /dev/null +++ b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java @@ -0,0 +1,172 @@ +package name.hamdan.turtleinterpreter; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.InputStream; +import java.net.URI; + +// Need a separate class for Turtle to separate the logic/data from graphics and animation + +public class Turtle extends JComponent { + boolean writing; + boolean draw; + double angle; // rotation angle + private String name; + private int LocX1, LocY1, LocX2, LocY2; + private Color color; + private double heading; + private int delta; + private BufferedImage bimg = null; // our PNG image of turtle + + + public Turtle(String name, int x, int y) { + this.name = name; + LocX1 = 0; + LocY1 = 0; + LocX2 = 0; + LocY2 = 0; + angle = 0; // rotation angle + writing = true; // default pen down + color = Color.BLACK; // default color + heading = 90; // default facing north, 90 degrees + delta = 0; + draw = true; + try { + String imagePath = "Images/TurtleBlue.png"; + InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath); + if (imageStream == null){ + throw new IllegalArgumentException(imagePath + " not found."); + } +// bimg = ImageIO.read(new File("./Images/TurtleBlue.png")); + bimg = ImageIO.read(imageStream); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public String getName() { + return name; + } + + public boolean getdraw() { + return draw; + } + + public void setDraw(boolean b) { + draw = b; + } + + public BufferedImage getImage() { + return bimg; + } + + public int getDelta() { + return delta; + } + + public void setDelta(int d) { + delta = d; + } + + void rotateTurtle() { + AffineTransformOp op; + AffineTransform tx; + + int w = bimg.getWidth(); + int h = bimg.getHeight(); + + tx = new AffineTransform(); + tx.rotate(Math.toRadians(angle), w / 2, h / 2);// + + op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + bimg = op.filter(bimg, null); //(sourse,destination) + } + + public boolean getWriting() { + + return writing; + } + + public int getLocX1() { + return LocX1; + } + + public void setLocX1(int x) { + LocX1 = x; + } + + public int getLocY1() { + return LocY1; + } + + public void setLocY1(int y) { + LocY1 = y; + } + + public int getLocX2() { + return LocX2; + } + + public void setLocX2(int x) { + LocX2 = x; + } + + public int getLocY2() { + return LocY2; + } + + public void setLocY2(int y) { + LocY2 = y; + } + + public void TurnLeft(double angle) { + this.angle = -angle; + heading += angle; + heading = heading % 360; + } + + public void TurnRight(double angle) { + this.angle = angle; + heading -= angle; + if (heading < 0) heading = heading + 360; + } + + public void MoveForward(int delta, int FrameW, int FrameH) { + LocX2 = LocX1 + (int) (delta * Math.cos(Math.toRadians(heading))); + LocY2 = LocY1 + (int) (delta * Math.sin(Math.toRadians(heading))); + + if (LocX2 > FrameW) // Boundary checks and clipping + LocX2 = FrameW; + if (LocX2 < -FrameW) + LocX2 = -FrameW; + + if (LocY2 > FrameH) // Boundary checks and clipping + LocY2 = FrameH; + if (LocY2 < -FrameH) + LocY2 = -FrameH; + + } + + public void penDown() { + writing = true; + } + + public void penUp() { + writing = false; + } + + public Color getColor() { + return color; + } + + public void setColor(Color c) { + color = c; + } + +} \ No newline at end of file diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java new file mode 100644 index 0000000..18992df --- /dev/null +++ b/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java @@ -0,0 +1,215 @@ +package name.hamdan.turtleinterpreter; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +public class TurtleInterpreter extends JPanel implements ActionListener { + + private static final int FrameWidth = 800; + private static final int FrameHeight = 800; + private final BufferedImage image; + Scanner in = null; + boolean redraw; + private List TurtleList; + // private JTextField console = new JTextField(); + + public TurtleInterpreter() { + //add(console); + TurtleList = new LinkedList(); + setPreferredSize(new Dimension(FrameWidth, FrameHeight)); + image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); + setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); + redraw = false; + clear(); + } + + public int getFrameHeight() { + return FrameHeight; + } + + public int getFrameWidth() { + return FrameWidth; + } + + public void clear() { + + Graphics g = image.getGraphics(); + + g.setColor(Color.LIGHT_GRAY); + g.fillRect(0, 0, image.getWidth(), image.getHeight()); + } + + public void drawLine(Turtle t, int x1, int y1, int x2, int y2) { + + Graphics g = image.getGraphics(); + + g.setColor(t.getColor()); + g.drawLine(x1, y1, x2, y2); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + // render the image on the panel. + g.drawImage(image, 0, 0, null); + if (TurtleList.size() != 0) { + for (Turtle t : TurtleList) { + g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, 30, 30, this); + } + } + } + + + public void actionPerformed(ActionEvent e) { + String s = e.getActionCommand(); + if (s.equals("Open File")) { // read input commands from a text file + JFileChooser chooser = new JFileChooser(); + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + File selectedFile = chooser.getSelectedFile(); + try { + in = new Scanner(selectedFile); + } catch (FileNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + // start over, clear screen and initialise Turtle List + clear(); + TurtleList = new LinkedList(); + processfile(in); + } + } + + public Color ToColor(String s) { // only five colors are allowed + switch (s) { + case "red": + return Color.RED; + case "blue": + return Color.BLUE; + case "cyan": + return Color.CYAN; + case "green": + return Color.GREEN; + case "orange": + return Color.ORANGE; + } + return Color.BLACK; //default + } + + void processfile(Scanner in) { + int lineNumber = 1; + boolean terminate = false; + boolean done; + final int MAX_LINES = 100; // maximum number of command lines + while (in.hasNextLine() && lineNumber < MAX_LINES && !terminate) { + String[] line = in.nextLine().split(" "); + if (line.length < 2 || line.length >= 4) { + JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); + + terminate = true; + } else { + Iterator itr = TurtleList.iterator(); + + switch (line[0]) { + case "turtle": + Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center + TurtleList.add(turtle); + redraw = true; + break; + + case "move": + done = false; + while (itr.hasNext() & !done) { + Turtle t = (Turtle) itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.setDelta(Integer.parseInt(line[2])); + t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); + if (t.getWriting()) { + drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); + } + t.setLocX1(t.getLocX2()); // update Turtle position + t.setLocY1(t.getLocY2()); + } + } + redraw = true; + break; + + case "left": + done = false; + while (itr.hasNext() & !done) { + Turtle t = (Turtle) itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); + t.rotateTurtle(); + } + } + redraw = true; + break; + + case "right": + done = false; + while (itr.hasNext() & !done) { + Turtle t = (Turtle) itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.TurnRight(Math.abs(Double.parseDouble(line[2]))); + t.rotateTurtle(); + } + } + redraw = true; + break; + + case "pen": + done = false; + while (itr.hasNext() & !done) { + Turtle t = (Turtle) itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + if ((line[2]).equals("up")) + t.penUp(); + else if ((line[2]).equals("down")) + t.penDown(); + } + } + redraw = false; + break; + + case "colour": + done = false; + while (itr.hasNext() & !done) { + Turtle t = (Turtle) itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.setColor(ToColor(line[2])); // change color + } + } + redraw = false; + break; + + default: + JOptionPane.showMessageDialog(this, "Line " + lineNumber + " : Invalid command, Program Terminated"); + terminate = true; + } + } + + if (redraw) repaint(); + lineNumber++; + } + } + +} \ No newline at end of file diff --git a/Images/TurtleBlue.png b/Turtle-Interpreter/src/main/resources/Images/TurtleBlue.png similarity index 100% rename from Images/TurtleBlue.png rename to Turtle-Interpreter/src/main/resources/Images/TurtleBlue.png diff --git a/Turtle-Interpreter/src/site/site.xml b/Turtle-Interpreter/src/site/site.xml new file mode 100644 index 0000000..0435b60 --- /dev/null +++ b/Turtle-Interpreter/src/site/site.xml @@ -0,0 +1,26 @@ + + + + + Turtle-Interpreter + https://maven.apache.org/images/apache-maven-project.png + https://www.apache.org/ + + + + https://maven.apache.org/images/maven-logo-black-on-white.png + https://maven.apache.org/ + + + + org.apache.maven.skins + maven-fluido-skin + 1.7 + + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java b/Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java new file mode 100644 index 0000000..9779642 --- /dev/null +++ b/Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java @@ -0,0 +1,38 @@ +package name.hamdan.turtleinterpreter; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/src/Turtle.java b/src/Turtle.java deleted file mode 100644 index a866905..0000000 --- a/src/Turtle.java +++ /dev/null @@ -1,182 +0,0 @@ -import java.awt.Color; -import java.awt.geom.AffineTransform; -import java.awt.image.AffineTransformOp; -import java.awt.image.BufferedImage; -import java.io.File; -import javax.swing.*; - -import javax.imageio.ImageIO; - -// Need a separate class for Turtle to separate the logic/data from graphics and animation - -public class Turtle extends JComponent{ -private String name; -private int LocX1, LocY1, LocX2, LocY2; -boolean writing; -boolean draw; -private Color color; -private double heading; -double angle; // rotation angle -private int delta; -private BufferedImage bimg = null; // our PNG image of turtle - - -public Turtle(String name, int x, int y) { - this.name= name; - LocX1 = 0; - LocY1 = 0; - LocX2 = 0; - LocY2 = 0; - angle = 0; // rotation angle - writing = true; // default pen down - color = Color.BLACK; // default color - heading = 90; // default facing north, 90 degrees - delta = 0; - draw = true; - try { - bimg = ImageIO.read(new File("./Images/TurtleBlue.png")); - } catch (Exception e) { - e.printStackTrace(); - } -} - - -public String getName() -{ - return name; -} - -public boolean getdraw() -{ - return draw; -} - -public void setDraw(boolean b) -{ - draw = b; -} - -public BufferedImage getImage() -{ - return bimg; -} - - - -public void setDelta(int d) -{ - delta = d; -} - -public int getDelta() -{ - return delta; -} - -void rotateTurtle() -{ - AffineTransformOp op; - AffineTransform tx; - - int w= bimg.getWidth(); - int h= bimg.getHeight(); - - tx = new AffineTransform(); - tx.rotate(Math.toRadians(angle), w/2, h/2);// - - op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR); - bimg=op.filter(bimg,null); //(sourse,destination) -} - -public boolean getWriting() { - - return writing; -} - -public void setLocX1(int x) { - LocX1 = x; -} - -public int getLocX1() -{ - return LocX1; -} - - -public void setLocY1(int y) { - LocY1 = y; -} - -public int getLocY1() -{ - return LocY1; -} - -public void setLocX2(int x) { - LocX2 = x; -} - -public int getLocX2() -{ - return LocX2; -} - - -public void setLocY2(int y) { - LocY2 = y; -} - -public int getLocY2() -{ - return LocY2; -} - -public void TurnLeft(double angle) { - this.angle = -angle; - heading += angle; - heading = heading % 360; -} - -public void TurnRight(double angle) { - this.angle = angle; - heading -= angle; - if (heading < 0) heading = heading + 360; -} - -public void MoveForward(int delta, int FrameW, int FrameH) { - LocX2 = LocX1 + (int) (delta * Math.cos(Math.toRadians(heading))); - LocY2 = LocY1 + (int) (delta * Math.sin(Math.toRadians(heading))); - - if (LocX2 > FrameW) // Boundary checks and clipping - LocX2 = FrameW; - if (LocX2 < -FrameW) - LocX2 = -FrameW; - - if (LocY2 > FrameH) // Boundary checks and clipping - LocY2 = FrameH; - if (LocY2 < -FrameH) - LocY2 = -FrameH; - -} - -public void penDown() -{ - writing = true; -} - -public void penUp() -{ - writing = false; -} - -public void setColor(Color c) -{ - color = c; -} - -public Color getColor() -{ -return color; -} - -} \ No newline at end of file diff --git a/src/TurtleInterpreter.java b/src/TurtleInterpreter.java deleted file mode 100644 index 8056f26..0000000 --- a/src/TurtleInterpreter.java +++ /dev/null @@ -1,217 +0,0 @@ -/* Simple Turtle Interpreter - * @ M. Hamdan (m.hamdan@hw.ac.uk) - * Feb 2019 - * - */ - -import java.awt.Graphics; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.event.*; -import javax.swing.*; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.awt.image.BufferedImage; -import java.util.*; - -public class TurtleInterpreter extends JPanel implements ActionListener { - - private static final int FrameWidth = 800; - private static final int FrameHeight = 800; - private List TurtleList; - private BufferedImage image; - Scanner in = null; - boolean redraw; - // private JTextField console = new JTextField(); - - public TurtleInterpreter() { - //add(console); - TurtleList = new LinkedList(); - setPreferredSize(new Dimension(FrameHeight, FrameWidth)); - image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); - setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); - redraw = false; - clear(); - } - - public void clear() { - - Graphics g = image.getGraphics(); - - g.setColor(Color.LIGHT_GRAY); - g.fillRect(0, 0, image.getWidth(), image.getHeight()); - } - - public void drawLine(Turtle t, int x1, int y1, int x2, int y2) { - - Graphics g = image.getGraphics(); - - g.setColor(t.getColor()); - g.drawLine(x1, y1, x2, y2); - } - - public void paintComponent(Graphics g) { - super.paintComponent(g); - // render the image on the panel. - g.drawImage(image, 0, 0, null); - if (TurtleList.size() != 0) { - for (Turtle t: TurtleList) { - g.drawImage(t.getImage(), (t.getLocX2()-15) + FrameWidth/2, -(t.getLocY2()+15) + FrameHeight/2, 30,30, this); - } - } - } - - -public void actionPerformed(ActionEvent e) - { - String s = e.getActionCommand(); - if (s.equals("Open File")) { // read input commands from a text file - JFileChooser chooser = new JFileChooser(); - if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { - File selectedFile = chooser.getSelectedFile(); - try { - in = new Scanner(selectedFile); - } catch (FileNotFoundException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - } - // start over, clear screen and initialise Turtle List - clear(); - TurtleList = new LinkedList(); - processfile(in); - } - } - -public Color ToColor(String s) - { // only five colors are allowed - if (s.equals("red") == true) return Color.RED; - else if (s.equals("blue") == true) return Color.BLUE; - else if (s.equals("cyan") == true) return Color.CYAN; - else if (s.equals("green") == true) return Color.GREEN; - else if (s.equals("orange") == true) return Color.ORANGE; - return Color.BLACK; //default - } - -void processfile(Scanner in) { - int lineNumber = 1; - boolean terminate = false; - boolean done; - final int MAX_LINES = 100; // maximum number of command lines - while (in.hasNextLine() && lineNumber= 4) { - JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); - - terminate = true; - } else { - Iterator itr = TurtleList.iterator(); - - switch (line[0]) { - case "turtle": - Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center - TurtleList.add(turtle); - redraw = true; - break; - - case "move": - done = false; - while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.setDelta(Integer.parseInt(line[2])); - t.MoveForward(t.getDelta(), FrameWidth/2, FrameHeight/2); - if (t.getWriting() == true) { - drawLine(t, t.getLocX1() + FrameWidth/2, -t.getLocY1() + FrameHeight/2, t.getLocX2() + FrameWidth/2, -t.getLocY2() + FrameHeight/2); - } - t.setLocX1(t.getLocX2()); // update Turtle position - t.setLocY1(t.getLocY2()); - } - } - redraw = true; - break; - - case "left": - done = false; - while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); - t.rotateTurtle(); - } - } - redraw = true; - break; - - case "right": - done = false; - while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.TurnRight(Math.abs(Double.parseDouble(line[2]))); - t.rotateTurtle(); - } - } - redraw = true; - break; - - case "pen": - done = false; - while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); - String name = t.getName(); - if (name.equals(line[1]) == true) { - done = true; - if ((line[2]).equals("up") == true) - t.penUp(); - else if ((line[2]).equals("down") == true) - t.penDown(); - } - } - redraw = false; - break; - - case "colour": - done = false; - while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.setColor(ToColor(line[2])); // change color - } - } - redraw = false; - break; - - default: - JOptionPane.showMessageDialog(this, "Line " + lineNumber + " : Invalid command, Program Terminated"); - terminate = true; - } - } - - if (redraw) repaint(); - lineNumber++; - } - } - -public static void main(String args[]) throws IOException { - JFrame f = new JFrame(); - TurtleInterpreter p = new TurtleInterpreter(); - JButton b = new JButton("Open File"); - p.add(b); - b.addActionListener(p); - f.add(p); - f.setVisible(true); - f.setSize(FrameHeight,FrameWidth); - f.setResizable(false); - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } -} \ No newline at end of file From 6b1f47c97c8a76822ee47f57d728343f8e36db09 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 7 Dec 2022 19:41:53 +0300 Subject: [PATCH 2/9] Initial Makefile --- Turtle-Interpreter/Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Turtle-Interpreter/Makefile diff --git a/Turtle-Interpreter/Makefile b/Turtle-Interpreter/Makefile new file mode 100644 index 0000000..f014e70 --- /dev/null +++ b/Turtle-Interpreter/Makefile @@ -0,0 +1,8 @@ +test: + @mvn test + +run: + @mvn compile && mvn package && java -jar target/Turtle-Interpreter-0.1.0.jar + +update_deps: + @mvn versions:use-latest-releases From b8e79a1fea344928e5db6c0c22e8479d8cd04905 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 7 Dec 2022 19:53:40 +0300 Subject: [PATCH 3/9] Getting Maven working --- Turtle-Interpreter/.vscode/settings.json | 3 + Turtle-Interpreter/Makefile | 3 +- Turtle-Interpreter/dependency-reduced-pom.xml | 57 ++++++++++++++ Turtle-Interpreter/pom.xml | 15 ++-- Turtle-Interpreter/pom.xml.versionsBackup | 77 +++++++++++++++++++ .../App.java | 2 +- .../Turtle.java | 23 +++--- .../TurtleInterpreter.java | 25 +++--- .../AppTest.java | 22 +++--- 9 files changed, 181 insertions(+), 46 deletions(-) create mode 100644 Turtle-Interpreter/.vscode/settings.json create mode 100644 Turtle-Interpreter/dependency-reduced-pom.xml create mode 100644 Turtle-Interpreter/pom.xml.versionsBackup rename Turtle-Interpreter/src/main/java/{name/hamdan/turtleinterpreter => TurtleInterpreter}/App.java (94%) rename Turtle-Interpreter/src/main/java/{name/hamdan/turtleinterpreter => TurtleInterpreter}/Turtle.java (86%) rename Turtle-Interpreter/src/main/java/{name/hamdan/turtleinterpreter => TurtleInterpreter}/TurtleInterpreter.java (90%) rename Turtle-Interpreter/src/test/java/{name/hamdan/turtleinterpreter => TurtleInterpreter}/AppTest.java (54%) diff --git a/Turtle-Interpreter/.vscode/settings.json b/Turtle-Interpreter/.vscode/settings.json new file mode 100644 index 0000000..e0f15db --- /dev/null +++ b/Turtle-Interpreter/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/Turtle-Interpreter/Makefile b/Turtle-Interpreter/Makefile index f014e70..5a9015e 100644 --- a/Turtle-Interpreter/Makefile +++ b/Turtle-Interpreter/Makefile @@ -4,5 +4,6 @@ test: run: @mvn compile && mvn package && java -jar target/Turtle-Interpreter-0.1.0.jar -update_deps: +upgrade: + @mvn release:update-versions @mvn versions:use-latest-releases diff --git a/Turtle-Interpreter/dependency-reduced-pom.xml b/Turtle-Interpreter/dependency-reduced-pom.xml new file mode 100644 index 0000000..950c151 --- /dev/null +++ b/Turtle-Interpreter/dependency-reduced-pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + TurtleInterpreter + Turtle-Interpreter + Turtle-Interpreter + 0.1.0 + Turtle-Interpreter. + https://turtle.interpreter + + + + maven-clean-plugin + 3.1.0 + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + TurtleInterpreter.App + + + + + + + + + + + + maven-project-info-reports-plugin + + + + + 17 + 17 + UTF-8 + + diff --git a/Turtle-Interpreter/pom.xml b/Turtle-Interpreter/pom.xml index 8c4e64b..fd434b0 100644 --- a/Turtle-Interpreter/pom.xml +++ b/Turtle-Interpreter/pom.xml @@ -1,28 +1,27 @@ - + 4.0.0 - name.hamdan.turtleinterpreter + TurtleInterpreter Turtle-Interpreter 0.1.0 Turtle-Interpreter Turtle-Interpreter. - https://turtleinterpreter.hamdan.name + https://turtle.interpreter UTF-8 - 16 - 16 + 17 + 17 junit junit - 3.8.1 + 4.13.2 @@ -57,7 +56,7 @@ - name.hamdan.turtleinterpreter.App + TurtleInterpreter.App diff --git a/Turtle-Interpreter/pom.xml.versionsBackup b/Turtle-Interpreter/pom.xml.versionsBackup new file mode 100644 index 0000000..a27f9a4 --- /dev/null +++ b/Turtle-Interpreter/pom.xml.versionsBackup @@ -0,0 +1,77 @@ + + + + 4.0.0 + + TurtleInterpreter + Turtle-Interpreter + 0.1.1-SNAPSHOT + + Turtle-Interpreter + Turtle-Interpreter. + https://turtle.interpreter + + + UTF-8 + 17 + 17 + + + + + junit + junit + 3.8.1 + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + TurtleInterpreter.App + + + + + + + + + + + + + + maven-project-info-reports-plugin + + + + diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java similarity index 94% rename from Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java rename to Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java index 42d7b9d..830b39c 100644 --- a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/App.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java @@ -1,4 +1,4 @@ -package name.hamdan.turtleinterpreter; +package TurtleInterpreter; import javax.swing.*; import java.io.IOException; diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java similarity index 86% rename from Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java rename to Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java index cb45683..5ae26df 100644 --- a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/Turtle.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java @@ -1,4 +1,4 @@ -package name.hamdan.turtleinterpreter; +package TurtleInterpreter; import javax.imageio.ImageIO; import javax.swing.*; @@ -15,7 +15,7 @@ public class Turtle extends JComponent { boolean writing; boolean draw; - double angle; // rotation angle + double angle; // rotation angle private String name; private int LocX1, LocY1, LocX2, LocY2; private Color color; @@ -23,7 +23,6 @@ public class Turtle extends JComponent { private int delta; private BufferedImage bimg = null; // our PNG image of turtle - public Turtle(String name, int x, int y) { this.name = name; LocX1 = 0; @@ -32,24 +31,23 @@ public Turtle(String name, int x, int y) { LocY2 = 0; angle = 0; // rotation angle writing = true; // default pen down - color = Color.BLACK; // default color - heading = 90; // default facing north, 90 degrees + color = Color.BLACK; // default color + heading = 90; // default facing north, 90 degrees delta = 0; draw = true; try { String imagePath = "Images/TurtleBlue.png"; InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath); - if (imageStream == null){ + if (imageStream == null) { throw new IllegalArgumentException(imagePath + " not found."); } -// bimg = ImageIO.read(new File("./Images/TurtleBlue.png")); + // bimg = ImageIO.read(new File("./Images/TurtleBlue.png")); bimg = ImageIO.read(imageStream); } catch (Exception e) { e.printStackTrace(); } } - public String getName() { return name; } @@ -85,7 +83,7 @@ void rotateTurtle() { tx.rotate(Math.toRadians(angle), w / 2, h / 2);// op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); - bimg = op.filter(bimg, null); //(sourse,destination) + bimg = op.filter(bimg, null); // (sourse,destination) } public boolean getWriting() { @@ -134,19 +132,20 @@ public void TurnLeft(double angle) { public void TurnRight(double angle) { this.angle = angle; heading -= angle; - if (heading < 0) heading = heading + 360; + if (heading < 0) + heading = heading + 360; } public void MoveForward(int delta, int FrameW, int FrameH) { LocX2 = LocX1 + (int) (delta * Math.cos(Math.toRadians(heading))); LocY2 = LocY1 + (int) (delta * Math.sin(Math.toRadians(heading))); - if (LocX2 > FrameW) // Boundary checks and clipping + if (LocX2 > FrameW) // Boundary checks and clipping LocX2 = FrameW; if (LocX2 < -FrameW) LocX2 = -FrameW; - if (LocY2 > FrameH) // Boundary checks and clipping + if (LocY2 > FrameH) // Boundary checks and clipping LocY2 = FrameH; if (LocY2 < -FrameH) LocY2 = -FrameH; diff --git a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java similarity index 90% rename from Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java rename to Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java index 18992df..95557e8 100644 --- a/Turtle-Interpreter/src/main/java/name/hamdan/turtleinterpreter/TurtleInterpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java @@ -1,4 +1,4 @@ -package name.hamdan.turtleinterpreter; +package TurtleInterpreter; import javax.swing.*; import java.awt.*; @@ -20,10 +20,10 @@ public class TurtleInterpreter extends JPanel implements ActionListener { Scanner in = null; boolean redraw; private List TurtleList; - // private JTextField console = new JTextField(); + // private JTextField console = new JTextField(); public TurtleInterpreter() { - //add(console); + // add(console); TurtleList = new LinkedList(); setPreferredSize(new Dimension(FrameWidth, FrameHeight)); image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); @@ -62,15 +62,15 @@ public void paintComponent(Graphics g) { g.drawImage(image, 0, 0, null); if (TurtleList.size() != 0) { for (Turtle t : TurtleList) { - g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, 30, 30, this); + g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, + 30, 30, this); } } } - public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); - if (s.equals("Open File")) { // read input commands from a text file + if (s.equals("Open File")) { // read input commands from a text file JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); @@ -88,7 +88,7 @@ public void actionPerformed(ActionEvent e) { } } - public Color ToColor(String s) { // only five colors are allowed + public Color ToColor(String s) { // only five colors are allowed switch (s) { case "red": return Color.RED; @@ -101,7 +101,7 @@ public Color ToColor(String s) { // only five colors are allowed case "orange": return Color.ORANGE; } - return Color.BLACK; //default + return Color.BLACK; // default } void processfile(Scanner in) { @@ -135,7 +135,8 @@ void processfile(Scanner in) { t.setDelta(Integer.parseInt(line[2])); t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); if (t.getWriting()) { - drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); + drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, + t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); } t.setLocX1(t.getLocX2()); // update Turtle position t.setLocY1(t.getLocY2()); @@ -202,12 +203,14 @@ else if ((line[2]).equals("down")) break; default: - JOptionPane.showMessageDialog(this, "Line " + lineNumber + " : Invalid command, Program Terminated"); + JOptionPane.showMessageDialog(this, + "Line " + lineNumber + " : Invalid command, Program Terminated"); terminate = true; } } - if (redraw) repaint(); + if (redraw) + repaint(); lineNumber++; } } diff --git a/Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java b/Turtle-Interpreter/src/test/java/TurtleInterpreter/AppTest.java similarity index 54% rename from Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java rename to Turtle-Interpreter/src/test/java/TurtleInterpreter/AppTest.java index 9779642..9611b6e 100644 --- a/Turtle-Interpreter/src/test/java/name/hamdan/turtleinterpreter/AppTest.java +++ b/Turtle-Interpreter/src/test/java/TurtleInterpreter/AppTest.java @@ -1,4 +1,4 @@ -package name.hamdan.turtleinterpreter; +package TurtleInterpreter; import junit.framework.Test; import junit.framework.TestCase; @@ -7,32 +7,28 @@ /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ +public class AppTest + extends TestCase { /** * Create the test case * * @param testName name of the test case */ - public AppTest( String testName ) - { - super( testName ); + public AppTest(String testName) { + super(testName); } /** * @return the suite of tests being tested */ - public static Test suite() - { - return new TestSuite( AppTest.class ); + public static Test suite() { + return new TestSuite(AppTest.class); } /** * Rigourous Test :-) */ - public void testApp() - { - assertTrue( true ); + public void testApp() { + assertTrue(true); } } From 3de7f114e8fd390bd3db9c5a21ab35402f29ef8f Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 8 Dec 2022 19:29:31 +0300 Subject: [PATCH 4/9] restructuring --- .gitignore | 1 + Turtle-Interpreter/.vscode/settings.json | 8 ++++++-- .../src/main/java/TurtleInterpreter/App.java | 5 ++++- .../Interpreter.java} | 11 ++++++----- .../TurtleInterpreter/{ => userInterface}/Turtle.java | 6 ++---- 5 files changed, 19 insertions(+), 12 deletions(-) rename Turtle-Interpreter/src/main/java/TurtleInterpreter/{TurtleInterpreter.java => domain/Interpreter.java} (96%) rename Turtle-Interpreter/src/main/java/TurtleInterpreter/{ => userInterface}/Turtle.java (97%) diff --git a/.gitignore b/.gitignore index eb5a316..3df278e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ target +.vscode diff --git a/Turtle-Interpreter/.vscode/settings.json b/Turtle-Interpreter/.vscode/settings.json index e0f15db..c5311dc 100644 --- a/Turtle-Interpreter/.vscode/settings.json +++ b/Turtle-Interpreter/.vscode/settings.json @@ -1,3 +1,7 @@ { - "java.configuration.updateBuildConfiguration": "automatic" -} \ No newline at end of file + "markdown.extension.preview.autoShowPreviewToSide": true, + "cSpell.ignorePaths": [ + "**/*", + "!*.md" + ] +} diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java index 830b39c..e126405 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java @@ -1,6 +1,9 @@ package TurtleInterpreter; import javax.swing.*; + +import TurtleInterpreter.domain.Interpreter; + import java.io.IOException; /** @@ -10,7 +13,7 @@ public class App { public static void main(String[] args) throws IOException { JFrame f = new JFrame(); - TurtleInterpreter turtleInterpreter = new TurtleInterpreter(); + Interpreter turtleInterpreter = new Interpreter(); JButton b = new JButton("Open File"); turtleInterpreter.add(b); b.addActionListener(turtleInterpreter); diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java similarity index 96% rename from Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java rename to Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index 95557e8..1d4b80a 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/TurtleInterpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -1,4 +1,4 @@ -package TurtleInterpreter; +package TurtleInterpreter.domain; import javax.swing.*; import java.awt.*; @@ -12,7 +12,9 @@ import java.util.List; import java.util.Scanner; -public class TurtleInterpreter extends JPanel implements ActionListener { +import TurtleInterpreter.userInterface.Turtle; + +public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; @@ -22,7 +24,7 @@ public class TurtleInterpreter extends JPanel implements ActionListener { private List TurtleList; // private JTextField console = new JTextField(); - public TurtleInterpreter() { + public Interpreter() { // add(console); TurtleList = new LinkedList(); setPreferredSize(new Dimension(FrameWidth, FrameHeight)); @@ -77,7 +79,6 @@ public void actionPerformed(ActionEvent e) { try { in = new Scanner(selectedFile); } catch (FileNotFoundException e1) { - // TODO Auto-generated catch block e1.printStackTrace(); } } @@ -116,7 +117,7 @@ void processfile(Scanner in) { terminate = true; } else { - Iterator itr = TurtleList.iterator(); + Iterator itr = TurtleList.iterator(); switch (line[0]) { case "turtle": diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java similarity index 97% rename from Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java rename to Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java index 5ae26df..522cbf9 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/Turtle.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java @@ -1,4 +1,4 @@ -package TurtleInterpreter; +package TurtleInterpreter.userInterface; import javax.imageio.ImageIO; import javax.swing.*; @@ -6,9 +6,7 @@ import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; -import java.io.File; import java.io.InputStream; -import java.net.URI; // Need a separate class for Turtle to separate the logic/data from graphics and animation @@ -72,7 +70,7 @@ public void setDelta(int d) { delta = d; } - void rotateTurtle() { + public void rotateTurtle() { AffineTransformOp op; AffineTransform tx; From 4e6a95e98ab6184e449fba8b6ab9153b15df3eb2 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 14 May 2024 15:46:44 +0300 Subject: [PATCH 5/9] =?UTF-8?q?Back=20on=20the=20Camel=20=F0=9F=90=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 10 -- .gitignore | 2 +- Turtle-Interpreter/.idea/.gitignore | 8 ++ Turtle-Interpreter/.idea/compiler.xml | 13 ++ Turtle-Interpreter/.idea/encodings.xml | 7 + Turtle-Interpreter/.idea/jarRepositories.xml | 20 +++ Turtle-Interpreter/.idea/misc.xml | 12 ++ Turtle-Interpreter/.idea/vcs.xml | 6 + Turtle-Interpreter/.vscode/settings.json | 7 - .../src/main/java/TurtleInterpreter/App.java | 24 ++-- .../TurtleInterpreter/domain/Interpreter.java | 133 +++++++++--------- .../userInterface/Turtle.java | 119 +++++++++------- 12 files changed, 214 insertions(+), 147 deletions(-) delete mode 100644 .classpath create mode 100644 Turtle-Interpreter/.idea/.gitignore create mode 100644 Turtle-Interpreter/.idea/compiler.xml create mode 100644 Turtle-Interpreter/.idea/encodings.xml create mode 100644 Turtle-Interpreter/.idea/jarRepositories.xml create mode 100644 Turtle-Interpreter/.idea/misc.xml create mode 100644 Turtle-Interpreter/.idea/vcs.xml delete mode 100644 Turtle-Interpreter/.vscode/settings.json diff --git a/.classpath b/.classpath deleted file mode 100644 index f00af9b..0000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore index 3df278e..c507849 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ target -.vscode +.idea diff --git a/Turtle-Interpreter/.idea/.gitignore b/Turtle-Interpreter/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Turtle-Interpreter/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Turtle-Interpreter/.idea/compiler.xml b/Turtle-Interpreter/.idea/compiler.xml new file mode 100644 index 0000000..a06028e --- /dev/null +++ b/Turtle-Interpreter/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/encodings.xml b/Turtle-Interpreter/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/Turtle-Interpreter/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/jarRepositories.xml b/Turtle-Interpreter/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/Turtle-Interpreter/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/misc.xml b/Turtle-Interpreter/.idea/misc.xml new file mode 100644 index 0000000..b3cf2e0 --- /dev/null +++ b/Turtle-Interpreter/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/vcs.xml b/Turtle-Interpreter/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Turtle-Interpreter/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Turtle-Interpreter/.vscode/settings.json b/Turtle-Interpreter/.vscode/settings.json deleted file mode 100644 index c5311dc..0000000 --- a/Turtle-Interpreter/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "markdown.extension.preview.autoShowPreviewToSide": true, - "cSpell.ignorePaths": [ - "**/*", - "!*.md" - ] -} diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java index e126405..5936bd1 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/App.java @@ -1,27 +1,21 @@ package TurtleInterpreter; -import javax.swing.*; - -import TurtleInterpreter.domain.Interpreter; - import java.io.IOException; +import TurtleInterpreter.domain.Interpreter; /** * Turtle Interpreter */ public class App { - public static void main(String[] args) throws IOException { - JFrame f = new JFrame(); - Interpreter turtleInterpreter = new Interpreter(); - JButton b = new JButton("Open File"); - turtleInterpreter.add(b); - b.addActionListener(turtleInterpreter); - f.add(turtleInterpreter); - f.setVisible(true); - f.setSize(turtleInterpreter.getFrameHeight(), turtleInterpreter.getFrameWidth()); - f.setResizable(false); - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + public static void main(String[] args){ + Interpreter p = new Interpreter(); + try { + p.run(); + } catch (IOException e) { + System.err.println(e.getMessage()); + } + } } diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index 1d4b80a..ed52c00 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -1,32 +1,31 @@ package TurtleInterpreter.domain; +import TurtleInterpreter.userInterface.Turtle; + +import java.awt.Graphics; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.event.*; import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Scanner; - -import TurtleInterpreter.userInterface.Turtle; +import java.awt.image.BufferedImage; +import java.util.*; +import java.io.IOException; -public class Interpreter extends JPanel implements ActionListener { +public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; + private List TurtleList; private final BufferedImage image; Scanner in = null; boolean redraw; - private List TurtleList; - // private JTextField console = new JTextField(); + // private JTextField console = new JTextField(); public Interpreter() { - // add(console); - TurtleList = new LinkedList(); + //add(console); + TurtleList = new LinkedList<>(); setPreferredSize(new Dimension(FrameWidth, FrameHeight)); image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); @@ -34,14 +33,6 @@ public Interpreter() { clear(); } - public int getFrameHeight() { - return FrameHeight; - } - - public int getFrameWidth() { - return FrameWidth; - } - public void clear() { Graphics g = image.getGraphics(); @@ -62,62 +53,61 @@ public void paintComponent(Graphics g) { super.paintComponent(g); // render the image on the panel. g.drawImage(image, 0, 0, null); - if (TurtleList.size() != 0) { - for (Turtle t : TurtleList) { - g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, - 30, 30, this); + if (!TurtleList.isEmpty()) { + for (Turtle t: TurtleList) { + g.drawImage(t.getImage(), (t.getLocX2()-15) + FrameWidth/2, -(t.getLocY2()+15) + FrameHeight/2, 30,30, this); } } } - public void actionPerformed(ActionEvent e) { + + public void actionPerformed(ActionEvent e) + { String s = e.getActionCommand(); - if (s.equals("Open File")) { // read input commands from a text file + if (s.equals("Open Program File")) { // read input commands from a text file JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); try { in = new Scanner(selectedFile); } catch (FileNotFoundException e1) { - e1.printStackTrace(); + // TODO Auto-generated catch block + System.err.println(e1.getMessage()); } } // start over, clear screen and initialise Turtle List clear(); - TurtleList = new LinkedList(); - processfile(in); + TurtleList = new LinkedList<>(); + processFile(in); } } - public Color ToColor(String s) { // only five colors are allowed - switch (s) { - case "red": - return Color.RED; - case "blue": - return Color.BLUE; - case "cyan": - return Color.CYAN; - case "green": - return Color.GREEN; - case "orange": - return Color.ORANGE; - } - return Color.BLACK; // default + public Color toColor(String s) + { + // Only five colors are allowed + return switch (s) { + case "red" -> Color.RED; + case "blue" -> Color.BLUE; + case "cyan" -> Color.CYAN; + case "green" -> Color.GREEN; + case "orange" -> Color.ORANGE; + default -> Color.BLACK; + }; } - void processfile(Scanner in) { + void processFile(Scanner in) { int lineNumber = 1; boolean terminate = false; boolean done; final int MAX_LINES = 100; // maximum number of command lines - while (in.hasNextLine() && lineNumber < MAX_LINES && !terminate) { + while (in.hasNextLine() && lineNumber= 4) { + if (line.length <2 || line.length >= 4) { JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); terminate = true; } else { - Iterator itr = TurtleList.iterator(); + Iterator itr = TurtleList.iterator(); switch (line[0]) { case "turtle": @@ -129,15 +119,14 @@ void processfile(Scanner in) { case "move": done = false; while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); + Turtle t = itr.next(); String name = t.getName(); if (name.equals(line[1])) { done = true; t.setDelta(Integer.parseInt(line[2])); - t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); + t.MoveForward(t.getDelta(), FrameWidth/2, FrameHeight/2); if (t.getWriting()) { - drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, - t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); + drawLine(t, t.getLocX1() + FrameWidth/2, -t.getLocY1() + FrameHeight/2, t.getLocX2() + FrameWidth/2, -t.getLocY2() + FrameHeight/2); } t.setLocX1(t.getLocX2()); // update Turtle position t.setLocY1(t.getLocY2()); @@ -149,7 +138,7 @@ void processfile(Scanner in) { case "left": done = false; while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); + Turtle t = itr.next(); String name = t.getName(); if (name.equals(line[1])) { done = true; @@ -163,7 +152,7 @@ void processfile(Scanner in) { case "right": done = false; while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); + Turtle t = itr.next(); String name = t.getName(); if (name.equals(line[1])) { done = true; @@ -177,7 +166,7 @@ void processfile(Scanner in) { case "pen": done = false; while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); + Turtle t = itr.next(); String name = t.getName(); if (name.equals(line[1])) { done = true; @@ -193,27 +182,43 @@ else if ((line[2]).equals("down")) case "colour": done = false; while (itr.hasNext() & !done) { - Turtle t = (Turtle) itr.next(); + Turtle t = itr.next(); String name = t.getName(); if (name.equals(line[1])) { done = true; - t.setColor(ToColor(line[2])); // change color + t.setColor(toColor(line[2])); // change color } } redraw = false; break; default: - JOptionPane.showMessageDialog(this, - "Line " + lineNumber + " : Invalid command, Program Terminated"); + JOptionPane.showMessageDialog(this, "Line " + lineNumber + " : Invalid command, Program Terminated"); terminate = true; } } - if (redraw) - repaint(); + if (redraw) repaint(); lineNumber++; } } -} \ No newline at end of file + public void run() throws IOException { + JFrame f = new JFrame(); + JButton b = new JButton("Open Program File"); + + Interpreter p = new Interpreter(); + + + p.add(b); + b.addActionListener(p); + f.add(p); + f.setSize(FrameWidth, FrameHeight); + f.setResizable(false); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + f.setVisible(true); + } + + +} diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java index 522cbf9..70eeba4 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java @@ -10,117 +10,133 @@ // Need a separate class for Turtle to separate the logic/data from graphics and animation -public class Turtle extends JComponent { - boolean writing; - boolean draw; - double angle; // rotation angle +public class Turtle extends JComponent{ private String name; private int LocX1, LocY1, LocX2, LocY2; + boolean writing; + boolean draw; private Color color; private double heading; + double angle; // rotation angle private int delta; - private BufferedImage bimg = null; // our PNG image of turtle + private BufferedImage bufferedImage = null; // our PNG image of turtle + public Turtle(String name, int x, int y) { - this.name = name; + this.name= name; LocX1 = 0; LocY1 = 0; LocX2 = 0; LocY2 = 0; angle = 0; // rotation angle writing = true; // default pen down - color = Color.BLACK; // default color - heading = 90; // default facing north, 90 degrees + color = Color.BLACK; // default color + heading = 90; // default facing north, 90 degrees delta = 0; draw = true; - try { - String imagePath = "Images/TurtleBlue.png"; - InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath); + + String imagePath = "Images/TurtleBlue.png"; + try(InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath)) { if (imageStream == null) { throw new IllegalArgumentException(imagePath + " not found."); } - // bimg = ImageIO.read(new File("./Images/TurtleBlue.png")); - bimg = ImageIO.read(imageStream); + bufferedImage = ImageIO.read(imageStream); } catch (Exception e) { - e.printStackTrace(); + System.err.println(e.getMessage()); } } - public String getName() { + + public String getName() + { return name; } - public boolean getdraw() { + public boolean getdraw() + { return draw; } - public void setDraw(boolean b) { + public void setDraw(boolean b) + { draw = b; } - public BufferedImage getImage() { - return bimg; + public BufferedImage getImage() + { + return bufferedImage; } - public int getDelta() { - return delta; - } - public void setDelta(int d) { + + public void setDelta(int d) + { delta = d; } - public void rotateTurtle() { + public int getDelta() + { + return delta; + } + + public void rotateTurtle() + { AffineTransformOp op; AffineTransform tx; - int w = bimg.getWidth(); - int h = bimg.getHeight(); + float w= bufferedImage.getWidth(); + float h= bufferedImage.getHeight(); tx = new AffineTransform(); - tx.rotate(Math.toRadians(angle), w / 2, h / 2);// + tx.rotate(Math.toRadians(angle), w/2, h/2);// - op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); - bimg = op.filter(bimg, null); // (sourse,destination) + op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR); + bufferedImage =op.filter(bufferedImage,null); // (source, destination) } - public boolean getWriting() { + public boolean getWriting() { return writing; } - public int getLocX1() { - return LocX1; - } - public void setLocX1(int x) { LocX1 = x; } - public int getLocY1() { - return LocY1; + public int getLocX1() + { + return LocX1; } + public void setLocY1(int y) { LocY1 = y; } - public int getLocX2() { - return LocX2; + public int getLocY1() + { + return LocY1; } public void setLocX2(int x) { LocX2 = x; } - public int getLocY2() { - return LocY2; + public int getLocX2() + { + return LocX2; } + public void setLocY2(int y) { LocY2 = y; } + public int getLocY2() + { + return LocY2; + } + public void TurnLeft(double angle) { this.angle = -angle; heading += angle; @@ -130,40 +146,43 @@ public void TurnLeft(double angle) { public void TurnRight(double angle) { this.angle = angle; heading -= angle; - if (heading < 0) - heading = heading + 360; + if (heading < 0) heading = heading + 360; } public void MoveForward(int delta, int FrameW, int FrameH) { LocX2 = LocX1 + (int) (delta * Math.cos(Math.toRadians(heading))); LocY2 = LocY1 + (int) (delta * Math.sin(Math.toRadians(heading))); - if (LocX2 > FrameW) // Boundary checks and clipping + if (LocX2 > FrameW) // Boundary checks and clipping LocX2 = FrameW; if (LocX2 < -FrameW) LocX2 = -FrameW; - if (LocY2 > FrameH) // Boundary checks and clipping + if (LocY2 > FrameH) // Boundary checks and clipping LocY2 = FrameH; if (LocY2 < -FrameH) LocY2 = -FrameH; } - public void penDown() { + public void penDown() + { writing = true; } - public void penUp() { + public void penUp() + { writing = false; } - public Color getColor() { - return color; + public void setColor(Color c) + { + color = c; } - public void setColor(Color c) { - color = c; + public Color getColor() + { + return color; } } \ No newline at end of file From d8bc312d38ebb12c31f8fd7af6d0af5cea47f0f0 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 15 May 2024 15:32:12 +0300 Subject: [PATCH 6/9] working out how to make the turtle move slow enough to see --- .gitignore | 2 +- .project | 28 --------------- README.md | 34 +++++++++++-------- Turtle-Interpreter/.idea/.gitignore | 8 ----- Turtle-Interpreter/.idea/compiler.xml | 13 ------- Turtle-Interpreter/.idea/encodings.xml | 7 ---- Turtle-Interpreter/.idea/jarRepositories.xml | 20 ----------- Turtle-Interpreter/.idea/misc.xml | 12 ------- Turtle-Interpreter/.idea/vcs.xml | 6 ---- .../TurtleInterpreter/domain/Interpreter.java | 5 +-- .../userInterface/Turtle.java | 20 ++++++++--- 11 files changed, 39 insertions(+), 116 deletions(-) delete mode 100644 .project delete mode 100644 Turtle-Interpreter/.idea/.gitignore delete mode 100644 Turtle-Interpreter/.idea/compiler.xml delete mode 100644 Turtle-Interpreter/.idea/encodings.xml delete mode 100644 Turtle-Interpreter/.idea/jarRepositories.xml delete mode 100644 Turtle-Interpreter/.idea/misc.xml delete mode 100644 Turtle-Interpreter/.idea/vcs.xml diff --git a/.gitignore b/.gitignore index c507849..c8a6031 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ target -.idea +**/.idea/ diff --git a/.project b/.project deleted file mode 100644 index c9fa3c0..0000000 --- a/.project +++ /dev/null @@ -1,28 +0,0 @@ - - - TurtleInterpreter - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - - - 1670344239806 - - 30 - - org.eclipse.core.resources.regexFilterMatcher - node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ - - - - diff --git a/README.md b/README.md index baa4868..a2f0d78 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,22 @@ # Turtle-Interpreter -Simple Turtle Interpter. -To build: import in Eclipse. -Run TurtleInterpreter. -A window should appear with button "Open File", click on button and a dialog box should appear. -Select from folder TurtlePrograms any sample program. +Simple Turtle Interpreter. + +To run: $ make run + +A window should appear with button "Open Program File": +- Click on button and a dialog box should appear. +- Select from folder Programs any sample program. + The interpreter will go through the commands in file and move Turtles. -You can select more input files. -You can also write your input file using an editor of your choice and see how it behaves. + +You can select more input files. You can also write your input file using an editor of your choice and see how it behaves. + Language commands: -turtle name - create a new turtle identified by the given name -move name x - moves the named turtle forward by x units -left name x - rotate the turtle anticlockwise by x degrees -right name x - rotate the turtle clockwise by x degrees -pen name up - lift the pen off the “paper” -pen name down - put the pen down so that subsequent moves draw of the screen -colour name c - set the drawing colour of the turtle appropriately -Possible colours: red, blue, cyan, orange and green. Default is black or argument is unspecified colour. +- turtle name -> create a new turtle identified by the given name +- move name x -> moves the named turtle forward by x units +- left name x -> rotate the turtle anticlockwise by x degrees +- right name x -> rotate the turtle clockwise by x degrees +- pen name up -> lift the pen off the “paper” +- pen name down -> put the pen down so that subsequent moves draw of the screen +- color name c -> set the drawing color of the turtle appropriately + - Possible colors: red, blue, cyan, orange and green. Default is black or argument is unspecified color. diff --git a/Turtle-Interpreter/.idea/.gitignore b/Turtle-Interpreter/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/Turtle-Interpreter/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/Turtle-Interpreter/.idea/compiler.xml b/Turtle-Interpreter/.idea/compiler.xml deleted file mode 100644 index a06028e..0000000 --- a/Turtle-Interpreter/.idea/compiler.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/encodings.xml b/Turtle-Interpreter/.idea/encodings.xml deleted file mode 100644 index aa00ffa..0000000 --- a/Turtle-Interpreter/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/jarRepositories.xml b/Turtle-Interpreter/.idea/jarRepositories.xml deleted file mode 100644 index 712ab9d..0000000 --- a/Turtle-Interpreter/.idea/jarRepositories.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/misc.xml b/Turtle-Interpreter/.idea/misc.xml deleted file mode 100644 index b3cf2e0..0000000 --- a/Turtle-Interpreter/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/Turtle-Interpreter/.idea/vcs.xml b/Turtle-Interpreter/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/Turtle-Interpreter/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index ed52c00..bf32138 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -17,7 +17,7 @@ public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; - private List TurtleList; + private LinkedList TurtleList; private final BufferedImage image; Scanner in = null; boolean redraw; @@ -56,6 +56,7 @@ public void paintComponent(Graphics g) { if (!TurtleList.isEmpty()) { for (Turtle t: TurtleList) { g.drawImage(t.getImage(), (t.getLocX2()-15) + FrameWidth/2, -(t.getLocY2()+15) + FrameHeight/2, 30,30, this); + t.sleep(); } } } @@ -200,6 +201,7 @@ else if ((line[2]).equals("down")) if (redraw) repaint(); lineNumber++; + } } @@ -209,7 +211,6 @@ public void run() throws IOException { Interpreter p = new Interpreter(); - p.add(b); b.addActionListener(p); f.add(p); diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java index 70eeba4..1280f7b 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java @@ -7,6 +7,7 @@ import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.InputStream; +import java.lang.Thread; // Need a separate class for Turtle to separate the logic/data from graphics and animation @@ -20,12 +21,13 @@ public class Turtle extends JComponent{ double angle; // rotation angle private int delta; private BufferedImage bufferedImage = null; // our PNG image of turtle + private final int sleepDuration = 100; public Turtle(String name, int x, int y) { this.name= name; - LocX1 = 0; - LocY1 = 0; + LocX1 = x; + LocY1 = y; LocX2 = 0; LocY2 = 0; angle = 0; // rotation angle @@ -46,6 +48,14 @@ public Turtle(String name, int x, int y) { } } + public void sleep(){ + try{ + Thread.sleep(sleepDuration); + } catch (InterruptedException e) { + System.err.println(e.getMessage()); + } + } + public String getName() { @@ -67,8 +77,6 @@ public BufferedImage getImage() return bufferedImage; } - - public void setDelta(int d) { delta = d; @@ -92,6 +100,8 @@ public void rotateTurtle() op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR); bufferedImage =op.filter(bufferedImage,null); // (source, destination) + + sleep(); } public boolean getWriting() { @@ -147,6 +157,7 @@ public void TurnRight(double angle) { this.angle = angle; heading -= angle; if (heading < 0) heading = heading + 360; + sleep(); } public void MoveForward(int delta, int FrameW, int FrameH) { @@ -162,6 +173,7 @@ public void MoveForward(int delta, int FrameW, int FrameH) { LocY2 = FrameH; if (LocY2 < -FrameH) LocY2 = -FrameH; + sleep(); } From e269c6b7b90d9f8b304b4b30d502bb7710055dc4 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 16 May 2024 17:20:45 +0300 Subject: [PATCH 7/9] Prepararing to use javax.swing.Timer in actionPerformed ... --- .gitignore | 3 +- Turtle-Interpreter/.vscode/settings.json | 9 ++ Turtle-Interpreter/Makefile | 2 +- .../TurtleInterpreter/domain/Interpreter.java | 65 +++++------ .../userInterface/Turtle.java | 103 ++++++------------ 5 files changed, 81 insertions(+), 101 deletions(-) create mode 100644 Turtle-Interpreter/.vscode/settings.json diff --git a/.gitignore b/.gitignore index c8a6031..f4ceea7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -target -**/.idea/ +**/target/ diff --git a/Turtle-Interpreter/.vscode/settings.json b/Turtle-Interpreter/.vscode/settings.json new file mode 100644 index 0000000..eff8cf7 --- /dev/null +++ b/Turtle-Interpreter/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "cSpell.language": "en", + "cSpell.enabledLanguageIds": [ + "markdown" + ], + "files.exclude": { + "**/target": true + } +} \ No newline at end of file diff --git a/Turtle-Interpreter/Makefile b/Turtle-Interpreter/Makefile index 5a9015e..940dbab 100644 --- a/Turtle-Interpreter/Makefile +++ b/Turtle-Interpreter/Makefile @@ -2,7 +2,7 @@ test: @mvn test run: - @mvn compile && mvn package && java -jar target/Turtle-Interpreter-0.1.0.jar + @mvn compile && mvn package && java -jar -XX:+UseZGC -XX:+ZGenerational target/Turtle-Interpreter-0.1.0.jar upgrade: @mvn release:update-versions diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index bf32138..608fd2d 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -13,19 +13,19 @@ import java.util.*; import java.io.IOException; -public class Interpreter extends JPanel implements ActionListener { +public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; - private LinkedList TurtleList; + private LinkedList turtleList; private final BufferedImage image; Scanner in = null; boolean redraw; - // private JTextField console = new JTextField(); + // private JTextField console = new JTextField(); public Interpreter() { - //add(console); - TurtleList = new LinkedList<>(); + // add(console); + turtleList = new LinkedList<>(); setPreferredSize(new Dimension(FrameWidth, FrameHeight)); image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); @@ -53,38 +53,34 @@ public void paintComponent(Graphics g) { super.paintComponent(g); // render the image on the panel. g.drawImage(image, 0, 0, null); - if (!TurtleList.isEmpty()) { - for (Turtle t: TurtleList) { - g.drawImage(t.getImage(), (t.getLocX2()-15) + FrameWidth/2, -(t.getLocY2()+15) + FrameHeight/2, 30,30, this); - t.sleep(); + if (!turtleList.isEmpty()) { + for (Turtle t : turtleList) { + g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, + 30, 30, this); } } } - - public void actionPerformed(ActionEvent e) - { - String s = e.getActionCommand(); - if (s.equals("Open Program File")) { // read input commands from a text file + public void actionPerformed(ActionEvent event) { + String s = event.getActionCommand(); + if (s.equals("Open Program File")) { // read input commands from a text file JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); try { in = new Scanner(selectedFile); } catch (FileNotFoundException e1) { - // TODO Auto-generated catch block System.err.println(e1.getMessage()); } } - // start over, clear screen and initialise Turtle List + // start over, clear screen and initialize Turtle List clear(); - TurtleList = new LinkedList<>(); + turtleList = new LinkedList<>(); processFile(in); } } - public Color toColor(String s) - { + public Color toColor(String s) { // Only five colors are allowed return switch (s) { case "red" -> Color.RED; @@ -101,19 +97,18 @@ void processFile(Scanner in) { boolean terminate = false; boolean done; final int MAX_LINES = 100; // maximum number of command lines - while (in.hasNextLine() && lineNumber= 4) { + if (line.length < 2 || line.length >= 4) { JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); - terminate = true; } else { - Iterator itr = TurtleList.iterator(); + Iterator itr = turtleList.iterator(); switch (line[0]) { case "turtle": Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center - TurtleList.add(turtle); + turtleList.add(turtle); redraw = true; break; @@ -125,14 +120,16 @@ void processFile(Scanner in) { if (name.equals(line[1])) { done = true; t.setDelta(Integer.parseInt(line[2])); - t.MoveForward(t.getDelta(), FrameWidth/2, FrameHeight/2); + t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); if (t.getWriting()) { - drawLine(t, t.getLocX1() + FrameWidth/2, -t.getLocY1() + FrameHeight/2, t.getLocX2() + FrameWidth/2, -t.getLocY2() + FrameHeight/2); + drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, + t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); } t.setLocX1(t.getLocX2()); // update Turtle position t.setLocY1(t.getLocY2()); } } + redraw = true; break; @@ -147,6 +144,7 @@ void processFile(Scanner in) { t.rotateTurtle(); } } + redraw = true; break; @@ -161,6 +159,7 @@ void processFile(Scanner in) { t.rotateTurtle(); } } + redraw = true; break; @@ -177,6 +176,7 @@ else if ((line[2]).equals("down")) t.penDown(); } } + redraw = false; break; @@ -190,26 +190,30 @@ else if ((line[2]).equals("down")) t.setColor(toColor(line[2])); // change color } } + redraw = false; break; default: - JOptionPane.showMessageDialog(this, "Line " + lineNumber + " : Invalid command, Program Terminated"); + JOptionPane.showMessageDialog(this, + "Line " + lineNumber + " : Invalid command, Program Terminated"); terminate = true; } } - if (redraw) repaint(); + if (redraw) { + repaint(); + } lineNumber++; } } - public void run() throws IOException { + public void run() throws IOException { JFrame f = new JFrame(); JButton b = new JButton("Open Program File"); - Interpreter p = new Interpreter(); + Interpreter p = new Interpreter(); p.add(b); b.addActionListener(p); @@ -221,5 +225,4 @@ public void run() throws IOException { f.setVisible(true); } - } diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java index 1280f7b..9d85e3a 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/userInterface/Turtle.java @@ -7,38 +7,35 @@ import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.InputStream; -import java.lang.Thread; // Need a separate class for Turtle to separate the logic/data from graphics and animation -public class Turtle extends JComponent{ +public class Turtle extends JComponent { private String name; private int LocX1, LocY1, LocX2, LocY2; boolean writing; boolean draw; private Color color; private double heading; - double angle; // rotation angle + double angle; // rotation angle private int delta; - private BufferedImage bufferedImage = null; // our PNG image of turtle - private final int sleepDuration = 100; - + private BufferedImage bufferedImage = null; // our PNG image of turtle public Turtle(String name, int x, int y) { - this.name= name; - LocX1 = x; - LocY1 = y; - LocX2 = 0; - LocY2 = 0; + this.name = name; + LocX1 = 0; + LocY1 = 0; + LocX2 = x; + LocY2 = y; angle = 0; // rotation angle writing = true; // default pen down - color = Color.BLACK; // default color - heading = 90; // default facing north, 90 degrees + color = Color.BLACK; // default color + heading = 90; // default facing north, 90 degrees delta = 0; draw = true; String imagePath = "Images/TurtleBlue.png"; - try(InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath)) { + try (InputStream imageStream = getClass().getClassLoader().getResourceAsStream(imagePath)) { if (imageStream == null) { throw new IllegalArgumentException(imagePath + " not found."); } @@ -48,63 +45,46 @@ public Turtle(String name, int x, int y) { } } - public void sleep(){ - try{ - Thread.sleep(sleepDuration); - } catch (InterruptedException e) { - System.err.println(e.getMessage()); - } - } - - - public String getName() - { + public String getName() { return name; } - public boolean getdraw() - { + public boolean getdraw() { return draw; } - public void setDraw(boolean b) - { + public void setDraw(boolean b) { draw = b; } - public BufferedImage getImage() - { + public BufferedImage getImage() { return bufferedImage; } - public void setDelta(int d) - { + public void setDelta(int d) { delta = d; } - public int getDelta() - { + public int getDelta() { return delta; } - public void rotateTurtle() - { + public void rotateTurtle() { AffineTransformOp op; AffineTransform tx; - float w= bufferedImage.getWidth(); - float h= bufferedImage.getHeight(); + float w = bufferedImage.getWidth(); + float h = bufferedImage.getHeight(); tx = new AffineTransform(); - tx.rotate(Math.toRadians(angle), w/2, h/2);// + tx.rotate(Math.toRadians(angle), w / 2, h / 2);// - op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR); - bufferedImage =op.filter(bufferedImage,null); // (source, destination) + op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); + bufferedImage = op.filter(bufferedImage, null); // (source, destination) - sleep(); } - public boolean getWriting() { + public boolean getWriting() { return writing; } @@ -113,18 +93,15 @@ public void setLocX1(int x) { LocX1 = x; } - public int getLocX1() - { + public int getLocX1() { return LocX1; } - public void setLocY1(int y) { LocY1 = y; } - public int getLocY1() - { + public int getLocY1() { return LocY1; } @@ -132,18 +109,15 @@ public void setLocX2(int x) { LocX2 = x; } - public int getLocX2() - { + public int getLocX2() { return LocX2; } - public void setLocY2(int y) { LocY2 = y; } - public int getLocY2() - { + public int getLocY2() { return LocY2; } @@ -156,44 +130,39 @@ public void TurnLeft(double angle) { public void TurnRight(double angle) { this.angle = angle; heading -= angle; - if (heading < 0) heading = heading + 360; - sleep(); + if (heading < 0) + heading = heading + 360; } public void MoveForward(int delta, int FrameW, int FrameH) { LocX2 = LocX1 + (int) (delta * Math.cos(Math.toRadians(heading))); LocY2 = LocY1 + (int) (delta * Math.sin(Math.toRadians(heading))); - if (LocX2 > FrameW) // Boundary checks and clipping + if (LocX2 > FrameW) // Boundary checks and clipping LocX2 = FrameW; if (LocX2 < -FrameW) LocX2 = -FrameW; - if (LocY2 > FrameH) // Boundary checks and clipping + if (LocY2 > FrameH) // Boundary checks and clipping LocY2 = FrameH; if (LocY2 < -FrameH) LocY2 = -FrameH; - sleep(); } - public void penDown() - { + public void penDown() { writing = true; } - public void penUp() - { + public void penUp() { writing = false; } - public void setColor(Color c) - { + public void setColor(Color c) { color = c; } - public Color getColor() - { + public Color getColor() { return color; } From d5827f2d31abc3e32babe30a2971741dd29e85d3 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 17 May 2024 16:08:11 +0300 Subject: [PATCH 8/9] =?UTF-8?q?Working=20=F0=9F=99=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Turtle-Interpreter/.vscode/settings.json | 3 +- Turtle-Interpreter/dependency-reduced-pom.xml | 2 +- Turtle-Interpreter/pom.xml | 2 +- .../TurtleInterpreter/domain/Interpreter.java | 444 +++++++++++++----- 4 files changed, 336 insertions(+), 115 deletions(-) diff --git a/Turtle-Interpreter/.vscode/settings.json b/Turtle-Interpreter/.vscode/settings.json index eff8cf7..a8bf9bc 100644 --- a/Turtle-Interpreter/.vscode/settings.json +++ b/Turtle-Interpreter/.vscode/settings.json @@ -5,5 +5,6 @@ ], "files.exclude": { "**/target": true - } + }, + "java.configuration.updateBuildConfiguration": "automatic" } \ No newline at end of file diff --git a/Turtle-Interpreter/dependency-reduced-pom.xml b/Turtle-Interpreter/dependency-reduced-pom.xml index 950c151..2d7d993 100644 --- a/Turtle-Interpreter/dependency-reduced-pom.xml +++ b/Turtle-Interpreter/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ TurtleInterpreter Turtle-Interpreter Turtle-Interpreter - 0.1.0 + 0.1.1-SNAPSHOT Turtle-Interpreter. https://turtle.interpreter diff --git a/Turtle-Interpreter/pom.xml b/Turtle-Interpreter/pom.xml index fd434b0..45f355e 100644 --- a/Turtle-Interpreter/pom.xml +++ b/Turtle-Interpreter/pom.xml @@ -5,7 +5,7 @@ TurtleInterpreter Turtle-Interpreter - 0.1.0 + 0.1.1-SNAPSHOT Turtle-Interpreter Turtle-Interpreter. diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index 608fd2d..9449596 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -13,18 +13,232 @@ import java.util.*; import java.io.IOException; -public class Interpreter extends JPanel implements ActionListener { +// public class Interpreter extends JPanel implements ActionListener { + +// private static final int FrameWidth = 800; +// private static final int FrameHeight = 800; +// private LinkedList turtleList; +// private final BufferedImage image; +// Scanner in = null; +// boolean redraw; +// // private JTextField console = new JTextField(); + +// public Interpreter() { +// // add(console); +// turtleList = new LinkedList<>(); +// setPreferredSize(new Dimension(FrameWidth, FrameHeight)); +// image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); +// setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); +// redraw = false; +// clear(); +// } + +// public void clear() { + +// Graphics g = image.getGraphics(); + +// g.setColor(Color.LIGHT_GRAY); +// g.fillRect(0, 0, image.getWidth(), image.getHeight()); +// } + +// public void drawLine(Turtle t, int x1, int y1, int x2, int y2) { + +// Graphics g = image.getGraphics(); + +// g.setColor(t.getColor()); +// g.drawLine(x1, y1, x2, y2); +// } + +// public void paintComponent(Graphics g) { +// super.paintComponent(g); +// // render the image on the panel. +// g.drawImage(image, 0, 0, null); +// if (!turtleList.isEmpty()) { +// for (Turtle t : turtleList) { +// g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, +// 30, 30, this); +// } +// } +// } + +// public void actionPerformed(ActionEvent event) { +// String s = event.getActionCommand(); +// if (s.equals("Open Program File")) { // read input commands from a text file +// JFileChooser chooser = new JFileChooser(); +// if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { +// File selectedFile = chooser.getSelectedFile(); +// try { +// in = new Scanner(selectedFile); +// } catch (FileNotFoundException e1) { +// System.err.println(e1.getMessage()); +// } +// } +// // start over, clear screen and initialize Turtle List +// clear(); +// turtleList = new LinkedList<>(); +// processFile(in); +// } +// } + +// public Color toColor(String s) { +// // Only five colors are allowed +// return switch (s) { +// case "red" -> Color.RED; +// case "blue" -> Color.BLUE; +// case "cyan" -> Color.CYAN; +// case "green" -> Color.GREEN; +// case "orange" -> Color.ORANGE; +// default -> Color.BLACK; +// }; +// } + +// void processFile(Scanner in) { +// int lineNumber = 1; +// boolean terminate = false; +// boolean done; +// final int MAX_LINES = 100; // maximum number of command lines +// while (in.hasNextLine() && lineNumber < MAX_LINES && !terminate) { +// String[] line = in.nextLine().split(" "); +// if (line.length < 2 || line.length >= 4) { +// JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); +// terminate = true; +// } else { +// Iterator itr = turtleList.iterator(); + +// switch (line[0]) { +// case "turtle": +// Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center +// turtleList.add(turtle); +// redraw = true; +// break; + +// case "move": +// done = false; +// while (itr.hasNext() & !done) { +// Turtle t = itr.next(); +// String name = t.getName(); +// if (name.equals(line[1])) { +// done = true; +// t.setDelta(Integer.parseInt(line[2])); +// t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); +// if (t.getWriting()) { +// drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, +// t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); +// } +// t.setLocX1(t.getLocX2()); // update Turtle position +// t.setLocY1(t.getLocY2()); +// } +// } + +// redraw = true; +// break; + +// case "left": +// done = false; +// while (itr.hasNext() & !done) { +// Turtle t = itr.next(); +// String name = t.getName(); +// if (name.equals(line[1])) { +// done = true; +// t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); +// t.rotateTurtle(); +// } +// } + +// redraw = true; +// break; + +// case "right": +// done = false; +// while (itr.hasNext() & !done) { +// Turtle t = itr.next(); +// String name = t.getName(); +// if (name.equals(line[1])) { +// done = true; +// t.TurnRight(Math.abs(Double.parseDouble(line[2]))); +// t.rotateTurtle(); +// } +// } + +// redraw = true; +// break; + +// case "pen": +// done = false; +// while (itr.hasNext() & !done) { +// Turtle t = itr.next(); +// String name = t.getName(); +// if (name.equals(line[1])) { +// done = true; +// if ((line[2]).equals("up")) +// t.penUp(); +// else if ((line[2]).equals("down")) +// t.penDown(); +// } +// } + +// redraw = false; +// break; + +// case "colour": +// done = false; +// while (itr.hasNext() & !done) { +// Turtle t = itr.next(); +// String name = t.getName(); +// if (name.equals(line[1])) { +// done = true; +// t.setColor(toColor(line[2])); // change color +// } +// } + +// redraw = false; +// break; + +// default: +// JOptionPane.showMessageDialog(this, +// "Line " + lineNumber + " : Invalid command, Program Terminated"); +// terminate = true; +// } +// } + +// if (redraw) { +// repaint(); +// } +// lineNumber++; + +// } +// } + +// public void run() throws IOException { +// JFrame f = new JFrame(); +// JButton b = new JButton("Open Program File"); + +// Interpreter p = new Interpreter(); + +// p.add(b); +// b.addActionListener(p); +// f.add(p); +// f.setSize(FrameWidth, FrameHeight); +// f.setResizable(false); +// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + +// f.setVisible(true); +// } + +// } +public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; private LinkedList turtleList; private final BufferedImage image; - Scanner in = null; - boolean redraw; - // private JTextField console = new JTextField(); + private Scanner in = null; + private boolean redraw; + private javax.swing.Timer timer; + private String[] currentCommand; + private Iterator commandIterator; public Interpreter() { - // add(console); turtleList = new LinkedList<>(); setPreferredSize(new Dimension(FrameWidth, FrameHeight)); image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); @@ -34,24 +248,19 @@ public Interpreter() { } public void clear() { - Graphics g = image.getGraphics(); - g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, image.getWidth(), image.getHeight()); } public void drawLine(Turtle t, int x1, int y1, int x2, int y2) { - Graphics g = image.getGraphics(); - g.setColor(t.getColor()); g.drawLine(x1, y1, x2, y2); } public void paintComponent(Graphics g) { super.paintComponent(g); - // render the image on the panel. g.drawImage(image, 0, 0, null); if (!turtleList.isEmpty()) { for (Turtle t : turtleList) { @@ -61,6 +270,7 @@ public void paintComponent(Graphics g) { } } + @Override public void actionPerformed(ActionEvent event) { String s = event.getActionCommand(); if (s.equals("Open Program File")) { // read input commands from a text file @@ -94,118 +304,129 @@ public Color toColor(String s) { void processFile(Scanner in) { int lineNumber = 1; - boolean terminate = false; - boolean done; final int MAX_LINES = 100; // maximum number of command lines - while (in.hasNextLine() && lineNumber < MAX_LINES && !terminate) { + LinkedList commands = new LinkedList<>(); + + while (in.hasNextLine() && lineNumber < MAX_LINES) { String[] line = in.nextLine().split(" "); if (line.length < 2 || line.length >= 4) { JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); - terminate = true; + return; } else { - Iterator itr = turtleList.iterator(); - - switch (line[0]) { - case "turtle": - Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center - turtleList.add(turtle); - redraw = true; - break; - - case "move": - done = false; - while (itr.hasNext() & !done) { - Turtle t = itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.setDelta(Integer.parseInt(line[2])); - t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); - if (t.getWriting()) { - drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, - t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); - } - t.setLocX1(t.getLocX2()); // update Turtle position - t.setLocY1(t.getLocY2()); - } - } - - redraw = true; - break; - - case "left": - done = false; - while (itr.hasNext() & !done) { - Turtle t = itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); - t.rotateTurtle(); - } - } - - redraw = true; - break; - - case "right": - done = false; - while (itr.hasNext() & !done) { - Turtle t = itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.TurnRight(Math.abs(Double.parseDouble(line[2]))); - t.rotateTurtle(); - } - } - - redraw = true; - break; - - case "pen": - done = false; - while (itr.hasNext() & !done) { - Turtle t = itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - if ((line[2]).equals("up")) - t.penUp(); - else if ((line[2]).equals("down")) - t.penDown(); - } - } - - redraw = false; - break; - - case "colour": - done = false; - while (itr.hasNext() & !done) { - Turtle t = itr.next(); - String name = t.getName(); - if (name.equals(line[1])) { - done = true; - t.setColor(toColor(line[2])); // change color - } - } + commands.add(line); + } + lineNumber++; + } - redraw = false; - break; + commandIterator = commands.iterator(); + timer = new javax.swing.Timer(610, new TimerActionListener()); + timer.start(); + } - default: - JOptionPane.showMessageDialog(this, - "Line " + lineNumber + " : Invalid command, Program Terminated"); - terminate = true; + private class TimerActionListener implements ActionListener { + public void actionPerformed(ActionEvent e) { + if (commandIterator.hasNext()) { + currentCommand = commandIterator.next(); + executeCommand(currentCommand); + if (redraw) { + repaint(); } + } else { + timer.stop(); } + } + } - if (redraw) { - repaint(); - } - lineNumber++; + private void executeCommand(String[] line) { + Iterator itr = turtleList.iterator(); + boolean done; + switch (line[0]) { + case "turtle": + Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center + turtleList.add(turtle); + redraw = true; + break; + + case "move": + done = false; + while (itr.hasNext() & !done) { + Turtle t = itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.setDelta(Integer.parseInt(line[2])); + t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); + if (t.getWriting()) { + drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, + t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); + } + t.setLocX1(t.getLocX2()); // update Turtle position + t.setLocY1(t.getLocY2()); + } + } + redraw = true; + break; + + case "left": + done = false; + while (itr.hasNext() & !done) { + Turtle t = itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); + t.rotateTurtle(); + } + } + redraw = true; + break; + + case "right": + done = false; + while (itr.hasNext() & !done) { + Turtle t = itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.TurnRight(Math.abs(Double.parseDouble(line[2]))); + t.rotateTurtle(); + } + } + redraw = true; + break; + + case "pen": + done = false; + while (itr.hasNext() & !done) { + Turtle t = itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + if ((line[2]).equals("up")) + t.penUp(); + else if ((line[2]).equals("down")) + t.penDown(); + } + } + redraw = false; + break; + + case "colour": + done = false; + while (itr.hasNext() & !done) { + Turtle t = itr.next(); + String name = t.getName(); + if (name.equals(line[1])) { + done = true; + t.setColor(toColor(line[2])); // change color + } + } + redraw = false; + break; + default: + JOptionPane.showMessageDialog(this, "Invalid command, Program Terminated"); + timer.stop(); } } @@ -224,5 +445,4 @@ public void run() throws IOException { f.setVisible(true); } - } From 008741174faa0b36b933365c8dec097fb63ee78b Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 18 May 2024 18:47:21 +0300 Subject: [PATCH 9/9] t's crossed, i's dotted --- README.md | 4 +- Turtle-Interpreter/dependency-reduced-pom.xml | 2 +- Turtle-Interpreter/pom.xml | 2 +- .../TurtleInterpreter/domain/Interpreter.java | 216 +----------------- 4 files changed, 6 insertions(+), 218 deletions(-) diff --git a/README.md b/README.md index a2f0d78..7830c78 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Turtle-Interpreter Simple Turtle Interpreter. -To run: $ make run +To run: `$ cd Turtle-Interpreter && make run` A window should appear with button "Open Program File": - Click on button and a dialog box should appear. @@ -11,6 +11,7 @@ The interpreter will go through the commands in file and move Turtles. You can select more input files. You can also write your input file using an editor of your choice and see how it behaves. +``` Language commands: - turtle name -> create a new turtle identified by the given name - move name x -> moves the named turtle forward by x units @@ -20,3 +21,4 @@ Language commands: - pen name down -> put the pen down so that subsequent moves draw of the screen - color name c -> set the drawing color of the turtle appropriately - Possible colors: red, blue, cyan, orange and green. Default is black or argument is unspecified color. +``` \ No newline at end of file diff --git a/Turtle-Interpreter/dependency-reduced-pom.xml b/Turtle-Interpreter/dependency-reduced-pom.xml index 2d7d993..950c151 100644 --- a/Turtle-Interpreter/dependency-reduced-pom.xml +++ b/Turtle-Interpreter/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ TurtleInterpreter Turtle-Interpreter Turtle-Interpreter - 0.1.1-SNAPSHOT + 0.1.0 Turtle-Interpreter. https://turtle.interpreter diff --git a/Turtle-Interpreter/pom.xml b/Turtle-Interpreter/pom.xml index 45f355e..fd434b0 100644 --- a/Turtle-Interpreter/pom.xml +++ b/Turtle-Interpreter/pom.xml @@ -5,7 +5,7 @@ TurtleInterpreter Turtle-Interpreter - 0.1.1-SNAPSHOT + 0.1.0 Turtle-Interpreter Turtle-Interpreter. diff --git a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java index 9449596..095d676 100644 --- a/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java +++ b/Turtle-Interpreter/src/main/java/TurtleInterpreter/domain/Interpreter.java @@ -13,220 +13,6 @@ import java.util.*; import java.io.IOException; -// public class Interpreter extends JPanel implements ActionListener { - -// private static final int FrameWidth = 800; -// private static final int FrameHeight = 800; -// private LinkedList turtleList; -// private final BufferedImage image; -// Scanner in = null; -// boolean redraw; -// // private JTextField console = new JTextField(); - -// public Interpreter() { -// // add(console); -// turtleList = new LinkedList<>(); -// setPreferredSize(new Dimension(FrameWidth, FrameHeight)); -// image = new BufferedImage(FrameWidth, FrameHeight, BufferedImage.TYPE_INT_RGB); -// setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); -// redraw = false; -// clear(); -// } - -// public void clear() { - -// Graphics g = image.getGraphics(); - -// g.setColor(Color.LIGHT_GRAY); -// g.fillRect(0, 0, image.getWidth(), image.getHeight()); -// } - -// public void drawLine(Turtle t, int x1, int y1, int x2, int y2) { - -// Graphics g = image.getGraphics(); - -// g.setColor(t.getColor()); -// g.drawLine(x1, y1, x2, y2); -// } - -// public void paintComponent(Graphics g) { -// super.paintComponent(g); -// // render the image on the panel. -// g.drawImage(image, 0, 0, null); -// if (!turtleList.isEmpty()) { -// for (Turtle t : turtleList) { -// g.drawImage(t.getImage(), (t.getLocX2() - 15) + FrameWidth / 2, -(t.getLocY2() + 15) + FrameHeight / 2, -// 30, 30, this); -// } -// } -// } - -// public void actionPerformed(ActionEvent event) { -// String s = event.getActionCommand(); -// if (s.equals("Open Program File")) { // read input commands from a text file -// JFileChooser chooser = new JFileChooser(); -// if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { -// File selectedFile = chooser.getSelectedFile(); -// try { -// in = new Scanner(selectedFile); -// } catch (FileNotFoundException e1) { -// System.err.println(e1.getMessage()); -// } -// } -// // start over, clear screen and initialize Turtle List -// clear(); -// turtleList = new LinkedList<>(); -// processFile(in); -// } -// } - -// public Color toColor(String s) { -// // Only five colors are allowed -// return switch (s) { -// case "red" -> Color.RED; -// case "blue" -> Color.BLUE; -// case "cyan" -> Color.CYAN; -// case "green" -> Color.GREEN; -// case "orange" -> Color.ORANGE; -// default -> Color.BLACK; -// }; -// } - -// void processFile(Scanner in) { -// int lineNumber = 1; -// boolean terminate = false; -// boolean done; -// final int MAX_LINES = 100; // maximum number of command lines -// while (in.hasNextLine() && lineNumber < MAX_LINES && !terminate) { -// String[] line = in.nextLine().split(" "); -// if (line.length < 2 || line.length >= 4) { -// JOptionPane.showMessageDialog(this, "Line : " + lineNumber + " : Invalid command, Program Terminated"); -// terminate = true; -// } else { -// Iterator itr = turtleList.iterator(); - -// switch (line[0]) { -// case "turtle": -// Turtle turtle = new Turtle(line[1], 0, 0); // New Turtle in Turtle coordinates at center -// turtleList.add(turtle); -// redraw = true; -// break; - -// case "move": -// done = false; -// while (itr.hasNext() & !done) { -// Turtle t = itr.next(); -// String name = t.getName(); -// if (name.equals(line[1])) { -// done = true; -// t.setDelta(Integer.parseInt(line[2])); -// t.MoveForward(t.getDelta(), FrameWidth / 2, FrameHeight / 2); -// if (t.getWriting()) { -// drawLine(t, t.getLocX1() + FrameWidth / 2, -t.getLocY1() + FrameHeight / 2, -// t.getLocX2() + FrameWidth / 2, -t.getLocY2() + FrameHeight / 2); -// } -// t.setLocX1(t.getLocX2()); // update Turtle position -// t.setLocY1(t.getLocY2()); -// } -// } - -// redraw = true; -// break; - -// case "left": -// done = false; -// while (itr.hasNext() & !done) { -// Turtle t = itr.next(); -// String name = t.getName(); -// if (name.equals(line[1])) { -// done = true; -// t.TurnLeft(Math.abs(Double.parseDouble(line[2]))); -// t.rotateTurtle(); -// } -// } - -// redraw = true; -// break; - -// case "right": -// done = false; -// while (itr.hasNext() & !done) { -// Turtle t = itr.next(); -// String name = t.getName(); -// if (name.equals(line[1])) { -// done = true; -// t.TurnRight(Math.abs(Double.parseDouble(line[2]))); -// t.rotateTurtle(); -// } -// } - -// redraw = true; -// break; - -// case "pen": -// done = false; -// while (itr.hasNext() & !done) { -// Turtle t = itr.next(); -// String name = t.getName(); -// if (name.equals(line[1])) { -// done = true; -// if ((line[2]).equals("up")) -// t.penUp(); -// else if ((line[2]).equals("down")) -// t.penDown(); -// } -// } - -// redraw = false; -// break; - -// case "colour": -// done = false; -// while (itr.hasNext() & !done) { -// Turtle t = itr.next(); -// String name = t.getName(); -// if (name.equals(line[1])) { -// done = true; -// t.setColor(toColor(line[2])); // change color -// } -// } - -// redraw = false; -// break; - -// default: -// JOptionPane.showMessageDialog(this, -// "Line " + lineNumber + " : Invalid command, Program Terminated"); -// terminate = true; -// } -// } - -// if (redraw) { -// repaint(); -// } -// lineNumber++; - -// } -// } - -// public void run() throws IOException { -// JFrame f = new JFrame(); -// JButton b = new JButton("Open Program File"); - -// Interpreter p = new Interpreter(); - -// p.add(b); -// b.addActionListener(p); -// f.add(p); -// f.setSize(FrameWidth, FrameHeight); -// f.setResizable(false); -// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - -// f.setVisible(true); -// } - -// } - public class Interpreter extends JPanel implements ActionListener { private static final int FrameWidth = 800; private static final int FrameHeight = 800; @@ -319,7 +105,7 @@ void processFile(Scanner in) { } commandIterator = commands.iterator(); - timer = new javax.swing.Timer(610, new TimerActionListener()); + timer = new javax.swing.Timer(377, new TimerActionListener()); timer.start(); }