//TO ADD //-Add alpha for colour //-Font colour? //-Recursive paint bucket? //-Include drawImage with resizing possibility? //version 0.95 (Jan 1, 2022) //-Added "brown" as a colour //version 0.94 (Nov 5, 2020) //-Added drawImage //version 0.931 (Mar 19 2017) //-Bug fix in drawString. It wasn't calling repaint. Now it does. //version 0.93 (Sept 16 2016) //-Added drawClosedShape function //-Added fillClosedShape function //version 0.92 //-Fixed the issue with the origin being at the very top left under border and toolbar //-Added drawArc function //-Added drawPoint function //-Added getBrushSize //-Added ability to work with fonts. //========================================================================= //version 0.9 (Original release) //Note: //This code is very similar to the code in StdDraw.java (Source: http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java). // It was redone to allow new users to use the same coordinates system as provided by Java. Also, all interactive options were removed // in an effort to clean the code up. //========================================================================= import java.awt.BasicStroke; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Graphics2D; import java.awt.geom.Arc2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.awt.image.DirectColorModel; import java.awt.image.WritableRaster; import java.io.File; import java.io.IOException; import java.util.Arrays; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SpringLayout; public class NOOPDraw { private static JFrame jf; private static BufferedImage onscreenImage; private static Graphics2D onscreen; private static double brushSize; private static Color bgColor; private static Color drawingColor; private static int width; private static int height; private static int fontSize; private static String fontFace; //=================================================================================== // Constructors //=================================================================================== public static void createWindow() { createWindow(500, 500, "NOOPDraw Project"); } public static void createWindow(int w, int h) { createWindow(w, h, "NOOPDraw Project"); } public static void createWindow(int w, int h, String title) { //Set static variables (state variables). bgColor = new Color(255,255,255); drawingColor = new Color(0,0,0); width = w; height = h; jf = new JFrame(); jf.setTitle(title); jf.setSize(width,height); fontSize = 14; fontFace = "Arial"; //Create image to draw to. onscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); onscreen = onscreenImage.createGraphics(); onscreen.setColor(drawingColor); onscreen.setFont(new Font(fontFace, Font.PLAIN, fontSize)); //Brush size. brushSize = 1.0; setBrushSize(brushSize); //Add image to JFrame. //Note: Without the SpringLayout, the image is placed at the very top left // of the window underneath the border and top toolbar. However, it // isn't necessary to add any constraints for this to work. ImageIcon icon = new ImageIcon(onscreenImage); JLabel draw = new JLabel(icon); Container cp = jf.getContentPane(); SpringLayout myLayout = new SpringLayout(); cp.setLayout(myLayout); cp.add(draw); //myLayout.putConstraint(SpringLayout.WEST, draw, 0, SpringLayout.WEST, cp); //not needed apparently //myLayout.putConstraint(SpringLayout.NORTH, draw, 0, SpringLayout.NORTH, cp); //not needed apparently //A few more settings. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows jf.requestFocusInWindow(); jf.setVisible(true); //clear screen (Sets the bgcolor) clearScreen(); } //=================================================================================== // Drawing and filling functions. //=================================================================================== public static void drawPoint(double x, double y) { onscreen.fill(new Ellipse2D.Double(x-2.5,y-2.5,5,5)); jf.repaint(); } public static void drawArc(double x, double y, double radius, double angle1, double angle2) { onscreen.draw(new Arc2D.Double(x,y,radius,radius,angle1,angle2,Arc2D.OPEN)); jf.repaint(); } public static void drawArc(double x, double y, double xradius, double yradius, double angle1, double angle2) { onscreen.draw(new Arc2D.Double(x,y,xradius,yradius,angle1,angle2,Arc2D.OPEN)); jf.repaint(); } public static void drawString(String s, double x, double y) { onscreen.drawString(s, (int)x, (int)y); //need int coordinates jf.repaint(); } public static void drawEllipse(double x, double y, double w, double h) { onscreen.draw(new Ellipse2D.Double(x,y,w,h)); jf.repaint(); } public static void fillEllipse(double x, double y, double w, double h) { onscreen.fill(new Ellipse2D.Double(x,y,w,h)); jf.repaint(); } public static void drawRectangle(double x, double y, double w, double h) { onscreen.draw(new Rectangle2D.Double(x,y,w,h)); jf.repaint(); } public static void fillRectangle(double x, double y, double w, double h) { onscreen.fill(new Rectangle2D.Double(x,y,w,h)); jf.repaint(); } public static void drawLine(double x1, double y1, double x2, double y2) { onscreen.draw(new Line2D.Double(x1, y1, x2, y2)); jf.repaint(); } public static void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3) { drawLine(x1,y1,x2,y2); drawLine(x2,y2,x3,y3); drawLine(x3,y3,x1,y1); jf.repaint(); } public static void fillTriangle(double x1, double y1, double x2, double y2, double x3, double y3) { Path2D.Double p = new Path2D.Double(); p.moveTo(x1, y1); p.lineTo(x2, y2); p.lineTo(x3, y3); p.closePath(); onscreen.fill(p); jf.repaint(); } public static void drawClosedShape(int... coords) { if(coords.length%2 != 0) { System.out.println("Error - Need an even # of coordinate values."); } else if(coords.length<6) { System.out.println("Error - Need at least three points (six coordinates) to make a closed shape."); } else { Path2D.Double p = new Path2D.Double(); p.moveTo(coords[0], coords[1]); for (int x=2; x