import java.awt.*; import java.awt.image.BufferedImage; import java.util.Date; //import java.text.DateFormat; import java.text.SimpleDateFormat; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; /** * This program is used by the students of CS1401 and CS1420 as a learning device for introductory programming.
* Creates a panel that acts as a blank canvas where pixels can be drawn.
* Defined colors are: red, green, blue, white, cyan, magenta, yellow, purple, gray, silver, orange and black.
* Colors can used by invoking JRaster.color example: JRaster.red * @author Dr. Eric Freudenthal * @author Alexandria N. Ogrey * @author Oscar Veliz * @version 2012.9.21 * @version 2013.10.1 Luc Longpre (commented out unused import and methods) */ public class JRaster{ private JRaster0 jr0; // JRaster is really a facade for a JRaster0 // colors are 3 bytes: red,green,blue // Each byte is 2 hex digits from 0x00 to 0xff (0 to 255). // 0x80=128; 0xaf=175, 0x40=64 static final int red = 0xff8080; static final int green = 0x80ff80; static final int blue = 0x8080ff; static final int white = 0xffffff; static final int black = 0x0; static final int cyan = 0x00ffff; static final int magenta = 0xff00ff; static final int yellow = 0xffff00; static final int purple = 0x990099; static final int gray = 0x999999; static final int silver = 0xc0c0c0; static final int orange = 0xff6600; static final int darkYellow = 0xa0a000; static final int veryDarkYellow = 0x303000; /** * This constructor is used to define a width x height size of JRaster. * @param w width of the JRaster * @param h height of the JRaster */ public JRaster(int w, int h) { jr0 = new JRaster0(w,h); } /** * Default constructor for JRaster. Creates 200x200 size JRaster. */ public JRaster(){ this(200,200); } /** * Returns the width of the image * @return image width */ public int getWidth() { return jr0.getWidth(); } /** * Returns the height of the image * @return image height */ public int getHeight(){ return jr0.getHeight(); } /** * Draws a single white pixel on JRaster. * @param x the column specified * @param y the row specified */ public void set(double x, double y){ set(x, y, JRaster.white); } /** * Draws a single pixel of a given color on JRaster. * @param x the column specified * @param y the row specified * @param color either the hexidecimal value i.e. 0x0fda01 or defined colors i.e. JRaster.red */ public void set(double x, double y, int color) { jr0.set((int)x,(int)y, color); } /** * Draws a single pixel of a given color on JRaster. * @param x the column specified * @param y the row specified * @param color a string of the specified colors */ /** * Clears the JRaster. */ public void clear() { jr0.clear(); } /** * Saves the JRaster to an image in the Portable Network Graphic (png) format * using the current year-month-day--hour(0-23)-minute-second-millisecond.png as the * image name. !WARNING! You will not be able to edit the JRaster after saving. */ public void save(){ jr0.save(); } /** * Saves the JRaster to an image in the Portable Network Graphic (png) format * using the current year-month-day--hour(0-23)-minute-second-millisecond.png as the * image name. !WARNING! You will not be able to edit the JRaster after saving. * @param filename the name of the image including the extension ex: "pic.png" or "pic.jpg" or "pic.gif" */ public void save(String filename){ jr0.save(filename); } /** * Sets whether image should be repainted automatically after every update (default true). * Returns previous setting. */ public boolean setAutoRepaint(boolean newMode) { return jr0.setAutoRepaint(newMode); } /** * A private class that does the calculations required by JRaster */ private class JRaster0 extends JPanel { BufferedImage image; private boolean autoRepaint = true; private void buildJRaster0(int w, int h) { BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); this.image = image; setOpaque(true); setLayout(new SpringLayout()); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(this); f.setSize(w+15, h+38); f.setVisible(true); } public JRaster0(int width, int height){ buildJRaster0(width, height); } // public JRaster0(){ // this(200,200); // } public int getWidth(){ return image.getWidth(); } public int getHeight(){ return image.getHeight(); } // public void set(double x, double y){ // set(x, y, white); // } public void set(double x, double y, int color){ try { // ignore all exceptions! y = getHeight() - y - 1; // move origin to lower-left corner image.setRGB((int)x, (int)y, color); } catch (Exception e) {} if (autoRepaint) repaint(); } public void clear() { setAutoRepaint(false); for (int x = 0; x < getWidth(); x++) { for (int y = 0; y < getHeight(); y++) { image.setRGB(x, y, JRaster.black); } } setAutoRepaint(true); repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); // Draw image centered. int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } /** * Saves the BufferedImage to an image in the Portable Network Graphic (png) format * using the current year-month-day--hour(0-23)-minute-second-millisecond.png as the * image name. WARNING: You will not be able to edit the JRaster after saving. */ public void save(){ try{ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd--HH-mm-ss-S"); ImageIO.write(image,"png",new File(formatter.format(new Date())+".png")); }catch(Exception e){} } public void save(String filename){ try{ String extension = filename.substring(filename.lastIndexOf('.')+1); extension = extension.toLowerCase(); if(!(extension.equals("png") || extension.equals("gif") || extension.equals("jpg"))){//incase you forgot extension = "png"; filename = filename + "." + extension; } ImageIO.write(image,extension,new File(filename)); }catch(Exception e){} } /** * Sets whether image should be repainted automatically after every update (default true). * Repaint() is called when auto repaint is enabled. * @param newMode specifies new autoRepaint setting. * @return setAutoRepaint retruns the prior autoRepaint setting. */ public boolean setAutoRepaint(boolean newMode) { boolean oldMode = autoRepaint; autoRepaint = newMode; if (newMode && !oldMode) repaint(); return oldMode; } } }