diff --git a/pom.xml b/pom.xml index a0bd618..42b9d27 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ commons-io 2.6 - + diff --git a/src/main/java/org/robusta/compiler/JvsCompiler.java b/src/main/java/org/robusta/compiler/JvsCompiler.java index 7fd4964..d2394dd 100644 --- a/src/main/java/org/robusta/compiler/JvsCompiler.java +++ b/src/main/java/org/robusta/compiler/JvsCompiler.java @@ -16,6 +16,7 @@ public class JvsCompiler { private static final File parentDirectory = new File(System.getProperty("user.dir")); + private static final String CLASS_NAME = "C"; public static void build(String jvsFilePath) { @@ -44,7 +45,7 @@ public static void build(String jvsFilePath) { } String jvsCode = readFile(jvsFile); String javaCode = translate(jvsCode); - JavaSourceFromString jsfs = new JavaSourceFromString("C", javaCode); + JavaSourceFromString jsfs = new JavaSourceFromString(CLASS_NAME, javaCode); Iterable fileObjects = Arrays.asList(jsfs); StringWriter output = new StringWriter(); boolean isCompilationSuccessful = compiler.getTask(output, fileManager, null, null, null, fileObjects).call(); @@ -72,7 +73,6 @@ private static String translate(String jvsCode) { } private static StringBuilder getHead() { - final String CLASS_NAME = "C"; StringBuilder head = new StringBuilder(); head.append("import static java.lang.Math.*;"); head.append("import static org.robusta.macros.Stdin.*;"); @@ -81,10 +81,16 @@ private static StringBuilder getHead() { head.append("import static org.robusta.macros.IO.*;"); head.append("import static org.robusta.compiler.RunTimeErrorHandler.*;"); head.append("import org.robusta.macros.Console;"); - head.append("public class " + CLASS_NAME + "{"); + head.append("import org.robusta.macros.Drawer;"); + head.append("import static org.robusta.macros.Drawer.reset;"); + head.append("import static org.robusta.macros.Drawer.setPixel;"); + head.append("import static org.robusta.macros.Drawer.getPixel;"); + head.append(String.format("public class %s {", CLASS_NAME)); head.append("public static void main(String[] args) {"); head.append("Console.getInstance();"); - head.append("try{ new C ().main(); } catch(Exception e) { handle(e); }"); + head.append("Drawer drawer = Drawer.getInstance();"); + head.append(String.format("try{ new %s ().main(); } catch(Exception e) { handle(e); }", CLASS_NAME)); + head.append("drawer.start();"); head.append("}"); return head; } diff --git a/src/main/java/org/robusta/macros/Console.java b/src/main/java/org/robusta/macros/Console.java index 48e7754..8542940 100644 --- a/src/main/java/org/robusta/macros/Console.java +++ b/src/main/java/org/robusta/macros/Console.java @@ -19,11 +19,6 @@ public class Console extends JFrame { private static volatile JScrollPane scrollFrame; private static final long serialVersionUID = 6543132165763L; - public static void main(String [] s) { - new Console(); - Integer i = Stdin.readInteger(); - } - private Console() { super("Robusta"); diff --git a/src/main/java/org/robusta/macros/Drawer.java b/src/main/java/org/robusta/macros/Drawer.java new file mode 100644 index 0000000..869e0ae --- /dev/null +++ b/src/main/java/org/robusta/macros/Drawer.java @@ -0,0 +1,68 @@ +package org.robusta.macros; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class Drawer extends JFrame { + + private static Drawer INSTANCE; + private static volatile DrawerSurface surface; + private static volatile boolean started = false; + private static final long serialVersionUID = 654313216000L; + + private Drawer() { + super("Drawer"); + surface = new DrawerSurface(); + started = false; + } + + public static synchronized Drawer getInstance() { + if(INSTANCE == null) { + return new Drawer(); + } else { + return INSTANCE; + } + } + + public static synchronized void reset(int width, int height) { + surface.reset(width * 2, height * 2); + surface.setPreferredSize(new Dimension(width * 2, height * 2)); + started = true; + } + + public void start() { + if(started) { + add(surface); + + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + surface.clear(); + } + }); + + setTitle("Points"); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + setVisible(true); + + pack(); + } + } + + public static synchronized void setPixel(int x, int y, int grayValue) { + surface.setPixel(x, y, grayValue); + } + + public static synchronized void setPixel(int x, int y, String color) { + surface.setPixel(x, y, color); + } + + public static synchronized int getPixel(int x, int y) { + return surface.getPixel(x, y); + } + +} diff --git a/src/main/java/org/robusta/macros/DrawerSurface.java b/src/main/java/org/robusta/macros/DrawerSurface.java new file mode 100644 index 0000000..6028bc2 --- /dev/null +++ b/src/main/java/org/robusta/macros/DrawerSurface.java @@ -0,0 +1,178 @@ +package org.robusta.macros; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.HashMap; +import java.util.Set; +import java.util.TreeSet; + + +class DrawerSurface extends JPanel implements ActionListener { + + private int width; + private int height; + private Set points; + + public DrawerSurface() { + this.width = 0; + this.height = 0; + this.points = new TreeSet<>(); + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + public void setPixel(int x, int y, int grayValue) { + checkBoundaries(x, y); + + int x_translation = Math.abs(width / 2); + int y_translation = Math.abs(height / 2); + + Point p = new Point(x + x_translation, y + y_translation, new Color(grayValue, grayValue, grayValue)); + this.points.add(p); + } + + public void setPixel(int x, int y, String color) { + checkBoundaries(x, y); + + int x_translation = Math.abs(width / 2); + int y_translation = Math.abs(height / 2); + + Point p = new Point(x + x_translation, y + y_translation, ColorFactory.getColor(color.toLowerCase())); + this.points.add(p); + } + + public int getPixel(int x, int y) { + checkBoundaries(x, y); + + int x_translation = Math.abs(width / 2); + int y_translation = Math.abs(height / 2); + + for(Point p : points) { + if(p.x == x + x_translation && p.y == y + y_translation) + return p.getColor().getRed(); + } + + // default value + return 0; + } + + public void reset(int width, int height) { + this.width = width; + this.height = height; + this.points.clear(); + } + + public void clear() { + this.width = 0; + this.height = 0; + this.points.clear(); + } + + private void draw(Graphics g) { + + Graphics2D g2d = (Graphics2D) g; + + BasicStroke bs1 = new BasicStroke(2f); + g2d.setStroke(bs1); + + for(Point p : this.points) { + g2d.setColor(p.getColor()); + g2d.drawLine(p.x, p.y, p.x, p.y); + } + + } + + private void checkBoundaries(int x, int y) throws IllegalArgumentException { + if (x > width/2 || x < - width/2) { + System.out.println("Width out of bound"); + throw new IllegalArgumentException("x value is out of window width boundary"); + + } + + if (y > height/2 || y < - height/2) { + throw new IllegalArgumentException("y value is out of window height boundary"); + } + + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + draw(g); + } + + @Override + public void actionPerformed(ActionEvent e) { + repaint(); + } + +} + +class Point implements Comparable { + int x; + int y; + private Color color; + + Point(int _x, int _y, Color c) { + x = _x; + y = _y; + color = c; + } + + public void setColor(Color c) { + this.color = c; + } + + public Color getColor() { + return color; + } + + public int compareTo(Object o) { + Point p = (Point) o; + int xEquality = p.x - x; + + if (xEquality != 0) return xEquality; + + return p.y - y; + } + +} + +class ColorFactory { + + private static HashMap colors = createColors(); + + private static HashMap createColors() { + HashMap colors = new HashMap<>(12); + + colors.put("black", Color.BLACK); + colors.put("blue", Color.BLUE); + colors.put("cyan", Color.CYAN); + colors.put("gray", Color.GRAY); + colors.put("grey", Color.GRAY); + colors.put("green", Color.GREEN); + colors.put("magenta", Color.MAGENTA); + colors.put("orange", Color.ORANGE); + colors.put("red", Color.RED); + colors.put("white", Color.WHITE); + colors.put("yellow", Color.YELLOW); + colors.put("pink", Color.PINK); + + return colors; + } + + + public static Color getColor(String color) { + return colors.getOrDefault(color, Color.BLACK); + } +} diff --git a/src/test/resources/hello-world.jvs b/src/test/resources/hello-world.jvs index 8bcac46..655be31 100644 --- a/src/test/resources/hello-world.jvs +++ b/src/test/resources/hello-world.jvs @@ -1,7 +1,9 @@ -void main(){ +void main() { -String i = readString(""); + String i = readString(""); println("hello world"); println("hello world"); -} \ No newline at end of file + reset(200, 100); + for (int j = 0; j < 200; j++) setPixel(3, j, (j % 255)); +}