Advertisement
Guest User

Canvas

a guest
May 25th, 2015
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.47 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.geom.*;
  4.  
  5. /**
  6.  * Class Canvas - a class to allow for simple graphical
  7.  * drawing on a canvas.
  8.  
  9.  */
  10.  
  11. public class Canvas
  12. {
  13.     private JFrame frame;
  14.     private CanvasPane canvas;
  15.     private Graphics2D graphic;
  16.     private Color backgroundColor;
  17.     private Image canvasImage;
  18.  
  19.     /**
  20.      * Create a Canvas with default height, width and background color
  21.      * (300, 300, white).
  22.      * @param title  title to appear in Canvas Frame    
  23.      */
  24.     public Canvas(String title)
  25.     {
  26.         this(title, 300, 300, Color.white);        
  27.     }
  28.  
  29.  
  30.     /**
  31.      * Create a Canvas with default background color (white).
  32.      * @param title  title to appear in Canvas Frame
  33.      * @param width  the desired width for the canvas
  34.      * @param height  the desired height for the canvas
  35.      */
  36.     public Canvas(String title, int width, int height)
  37.     {
  38.         this(title, width, height, Color.white);
  39.     }
  40.  
  41.     /**
  42.      * Create a Canvas.
  43.      * @param title  title to appear in Canvas Frame
  44.      * @param width  the desired width for the canvas
  45.      * @param height  the desired height for the canvas
  46.      * @param bgClour  the desired background color of the canvas
  47.      */
  48.     public Canvas(String title, int width, int height, Color bgColor)
  49.     {
  50.         frame = new JFrame();
  51.         canvas = new CanvasPane();
  52.         frame.setContentPane(canvas);
  53.         frame.setTitle(title);
  54.         canvas.setPreferredSize(new Dimension(width, height));
  55.         backgroundColor = bgColor;
  56.         frame.pack();
  57.     }
  58.  
  59.     /**
  60.      * Set the canvas visibility and brings canvas to the front of screen
  61.      * when made visible. This method can also be used to bring an already
  62.      * visible canvas to the front of other windows.
  63.      * @param visible  boolean value representing the desired visibility of
  64.      * the canvas (true or false)
  65.      */
  66.     public void setVisible(boolean visible)
  67.     {
  68.         if(graphic == null) {
  69.             // first time: instantiate the offscreen image and fill it with
  70.             // the background color
  71.             Dimension size = canvas.getSize();
  72.             canvasImage = canvas.createImage(size.width, size.height);
  73.             graphic = (Graphics2D)canvasImage.getGraphics();
  74.             graphic.setColor(backgroundColor);
  75.             graphic.fillRect(0, 0, size.width, size.height);
  76.             graphic.setColor(Color.black);
  77.         }
  78.         frame.setVisible(true);
  79.     }
  80.  
  81.     /**
  82.      * Provide information on visibility of the Canvas.
  83.      * @return  true if canvas is visible, false otherwise
  84.      */
  85.     public boolean isVisible()
  86.     {
  87.         return frame.isVisible();
  88.     }
  89.  
  90.     /**
  91.      * Draw the outline of a given shape onto the canvas.
  92.      * @param  shape  the shape object to be drawn on the canvas
  93.      */
  94.     public void draw(Shape shape)
  95.     {
  96.         graphic.draw(shape);
  97.         canvas.repaint();
  98.     }
  99.  
  100.     /**
  101.      * Fill the internal dimensions of a given shape with the current
  102.      * foreground color of the canvas.
  103.      * @param  shape  the shape object to be filled
  104.      */
  105.     public void fill(Shape shape)
  106.     {
  107.         graphic.fill(shape);
  108.         canvas.repaint();
  109.     }
  110.  
  111.     /**
  112.      * Fill the internal dimensions of the given circle with the current
  113.      * foreground color of the canvas.
  114.      */
  115.     public void fillCircle(int xPos, int yPos, int diameter)
  116.     {
  117.         Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
  118.         fill(circle);
  119.     }
  120.  
  121.     /**
  122.      * Fill the internal dimensions of the given rectangle with the current
  123.      * foreground color of the canvas. This is a convenience method. A similar
  124.      * effect can be achieved with the "fill" method.
  125.      */
  126.     public void fillRectangle(int xPos, int yPos, int width, int height)
  127.     {
  128.         fill(new Rectangle(xPos, yPos, width, height));
  129.     }
  130.  
  131.     /**
  132.      * Erase the whole canvas.
  133.      */
  134.     public void erase()
  135.     {
  136.         Color original = graphic.getColor();
  137.         graphic.setColor(backgroundColor);
  138.         Dimension size = canvas.getSize();
  139.         graphic.fill(new Rectangle(0, 0, size.width, size.height));
  140.         graphic.setColor(original);
  141.         canvas.repaint();
  142.     }
  143.  
  144.     /**
  145.      * Erase the internal dimensions of the given circle. This is a
  146.      * convenience method. A similar effect can be achieved with
  147.      * the "erase" method.
  148.      */
  149.     public void eraseCircle(int xPos, int yPos, int diameter)
  150.     {
  151.         Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
  152.         erase(circle);
  153.     }
  154.  
  155.     /**
  156.      * Erase the internal dimensions of the given rectangle. This is a
  157.      * convenience method. A similar effect can be achieved with
  158.      * the "erase" method.
  159.      */
  160.     public void eraseRectangle(int xPos, int yPos, int width, int height)
  161.     {
  162.         erase(new Rectangle(xPos, yPos, width, height));
  163.     }
  164.  
  165.     /**
  166.      * Erase a given shape's interior on the screen.
  167.      * @param  shape  the shape object to be erased
  168.      */
  169.     public void erase(Shape shape)
  170.     {
  171.         Color original = graphic.getColor();
  172.         graphic.setColor(backgroundColor);
  173.         graphic.fill(shape);              // erase by filling background color
  174.         graphic.setColor(original);
  175.         canvas.repaint();
  176.     }
  177.  
  178.     /**
  179.      * Erases a given shape's outline on the screen.
  180.      * @param  shape  the shape object to be erased
  181.      */
  182.     public void eraseOutline(Shape shape)
  183.     {
  184.         Color original = graphic.getColor();
  185.         graphic.setColor(backgroundColor);
  186.         graphic.draw(shape);  // erase by drawing background color
  187.         graphic.setColor(original);
  188.         canvas.repaint();
  189.     }
  190.  
  191.     /**
  192.      * Draws an image onto the canvas.
  193.      * @param  image   the Image object to be displayed
  194.      * @param  x       x co-ordinate for Image placement
  195.      * @param  y       y co-ordinate for Image placement
  196.      * @return  returns boolean value representing whether the image was
  197.      *          completely loaded
  198.      */
  199.     public boolean drawImage(Image image, int x, int y)
  200.     {
  201.         boolean result = graphic.drawImage(image, x, y, null);
  202.         canvas.repaint();
  203.         return result;
  204.     }
  205.  
  206.     /**
  207.      * Draws a String on the Canvas.
  208.      * @param  text   the String to be displayed
  209.      * @param  x      x co-ordinate for text placement
  210.      * @param  y      y co-ordinate for text placement
  211.      */
  212.     public void drawString(String text, int x, int y)
  213.     {
  214.         graphic.drawString(text, x, y);  
  215.         canvas.repaint();
  216.     }
  217.  
  218.     /**
  219.      * Erases a String on the Canvas.
  220.      * @param  text     the String to be displayed
  221.      * @param  x        x co-ordinate for text placement
  222.      * @param  y        y co-ordinate for text placement
  223.      */
  224.     public void eraseString(String text, int x, int y)
  225.     {
  226.         Color original = graphic.getColor();
  227.         graphic.setColor(backgroundColor);
  228.         graphic.drawString(text, x, y);  
  229.         graphic.setColor(original);
  230.         canvas.repaint();
  231.     }
  232.  
  233.     /**
  234.      * Draws a line on the Canvas.
  235.      * @param  x1   x co-ordinate of start of line
  236.      * @param  y1   y co-ordinate of start of line
  237.      * @param  x2   x co-ordinate of end of line
  238.      * @param  y2   y co-ordinate of end of line
  239.      */
  240.     public void drawLine(int x1, int y1, int x2, int y2)
  241.     {
  242.         graphic.drawLine(x1, y1, x2, y2);  
  243.         canvas.repaint();
  244.     }
  245.    
  246.  
  247.  
  248.     /**
  249.      * Sets the foreground color of the Canvas.
  250.      * @param  newColor   the new color for the foreground of the Canvas
  251.      */
  252.     public void setForegroundColor(Color newColor)
  253.     {
  254.         graphic.setColor(newColor);
  255.     }
  256.  
  257.     /**
  258.      * Returns the current color of the foreground.
  259.      * @return   the color of the foreground of the Canvas
  260.      */
  261.     public Color getForegroundColor()
  262.     {
  263.         return graphic.getColor();
  264.     }
  265.  
  266.     /**
  267.      * Sets the background color of the Canvas.
  268.      * @param  newColor   the new color for the background of the Canvas
  269.      */
  270.     public void setBackgroundColor(Color newColor)
  271.     {
  272.         backgroundColor = newColor;  
  273.         graphic.setBackground(newColor);
  274.     }
  275.  
  276.     /**
  277.      * Returns the current color of the background
  278.      * @return   the color of the background of the Canvas
  279.      */
  280.     public Color getBackgroundColor()
  281.     {
  282.         return backgroundColor;
  283.     }
  284.  
  285.     /**
  286.      * changes the current Font used on the Canvas
  287.      * @param  newFont   new font to be used for String output
  288.      */
  289.     public void setFont(Font newFont)
  290.     {
  291.         graphic.setFont(newFont);
  292.     }
  293.  
  294.     /**
  295.      * Returns the current font of the canvas.
  296.      * @return     the font currently in use
  297.      **/
  298.     public Font getFont()
  299.     {
  300.         return graphic.getFont();
  301.     }
  302.  
  303.     /**
  304.      * Sets the size of the canvas.
  305.      * @param  width    new width
  306.      * @param  height   new height
  307.      */
  308.     public void setSize(int width, int height)
  309.     {
  310.         canvas.setPreferredSize(new Dimension(width, height));
  311.         Image oldImage = canvasImage;
  312.         canvasImage = canvas.createImage(width, height);
  313.         graphic = (Graphics2D)canvasImage.getGraphics();
  314.         graphic.drawImage(oldImage, 0, 0, null);
  315.         frame.pack();
  316.     }
  317.  
  318.     /**
  319.      * Returns the size of the canvas.
  320.      * @return     The current dimension of the canvas
  321.      */
  322.     public Dimension getSize()
  323.     {
  324.         return canvas.getSize();
  325.     }
  326.  
  327.     /**
  328.      * Waits for a specified number of milliseconds before finishing.
  329.      * This provides an easy way to specify a small delay which can be
  330.      * used when producing animations.
  331.      * @param  milliseconds  the number
  332.      */
  333.     public void wait(int milliseconds)
  334.     {
  335.         try
  336.         {
  337.             Thread.sleep(milliseconds);
  338.         }
  339.         catch (InterruptedException e)
  340.         {
  341.             // ignoring exception at the moment
  342.         }
  343.     }
  344.  
  345.     /************************************************************************
  346.      * Inner class CanvasPane - the actual canvas component contained in the
  347.      * Canvas frame. This is essentially a JPanel with added capability to
  348.      * refresh the image drawn on it.
  349.      */
  350.     private class CanvasPane extends JPanel
  351.     {
  352.         public void paint(Graphics g)
  353.         {
  354.             g.drawImage(canvasImage, 0, 0, null);
  355.         }
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement