Advertisement
Guest User

Untitled

a guest
Aug 14th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. /*
  2.     Canvas class
  3.     Version 2.0
  4.     Copyright 2004-5 by Mordechai (Moti) Ben-Ari and Michael Kolling and Bruce Quig
  5.      
  6.     This program is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU General Public License
  8.     as published by the Free Software Foundation; either version 2
  9.     of the License, or (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.     See the GNU General Public License for more details.
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  17.     02111-1307, USA.
  18. */
  19. import javax.swing.*;
  20. import java.util.*;
  21. import java.awt.*;
  22. import java.awt.geom.AffineTransform;
  23. public class Canvas {
  24.     private static int width = 1000;
  25.     private static int height = 600;
  26.     private static Canvas canvasSingleton;
  27.     private JFrame frame;
  28.     private CanvasPane canvas;
  29.     private Graphics2D graphic;
  30.     private Color backgroundColour;
  31.     public Image canvasImage;
  32.  
  33.     // Create canvas singleton object.
  34.     static Canvas getCanvas() {
  35.         if(canvasSingleton == null)
  36.             canvasSingleton =
  37.                 new Canvas("Graphics V2.0. (C) 2005 M. Ben-Ari & M. Kolling",
  38.                     width, height, Color.white);
  39.         canvasSingleton.setVisible(true);
  40.         return canvasSingleton;
  41.     }
  42.     public static void changeSize(int w, int h) {
  43.         width = w;
  44.         height = h;
  45.         getCanvas().canvas.setPreferredSize(new Dimension(width, height));
  46.     }
  47.     public static void changeLocation(int x, int y) {
  48.         getCanvas().frame.setLocation(x, y);
  49.     }
  50.    
  51.     /**
  52.      * Create a Canvas.
  53.      * @param title  title to appear in Canvas Frame
  54.      * @param width  the desired width for the canvas
  55.      * @param height  the desired height for the canvas
  56.      * @param bgClour  the desired background colour of the canvas
  57.      */
  58.     Canvas(String title, int width, int height, Color bgColour) {
  59.         this.backgroundColour = bgColour;
  60.         this.canvas = new CanvasPane();
  61.         this.canvas.setPreferredSize(new Dimension(width, height));
  62.         this.frame = new JFrame();
  63.         this.frame.setContentPane(this.canvas);
  64.         this.frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65.         this.frame.setTitle(title);
  66.         this.frame.setSize(width, height);
  67.         this.frame.setLocationRelativeTo(null);
  68.         this.frame.pack();
  69.         this.frame.toFront();
  70.     }
  71.  
  72.     /**
  73.      * Sets the canvas visibility and brings canvas to the front of screen
  74.      * when made visible. This method can also be used to bring an already
  75.      * visible canvas to the front of other windows.
  76.      * @param visible  boolean value representing the desired visibility of
  77.      * the canvas (true or false)
  78.      */
  79.     private void setVisible(boolean visible) {
  80.         if(this.graphic == null) {
  81.             // first time: instantiate the offscreen image and fill it with
  82.             // the background colour
  83.             Dimension size = this.canvas.getSize();
  84.             this.canvasImage = this.canvas.createImage(size.width, size.height);
  85.             this.graphic = (Graphics2D)this.canvasImage.getGraphics();
  86.             this.graphic.setFont(new Font("Lucida Sans Typewriter", Font.BOLD, 16));
  87.             this.graphic.setColor(this.backgroundColour);
  88.             this.graphic.fillRect(0, 0, size.width, size.height);
  89.             this.graphic.setColor(Color.black);
  90.         }
  91.         this.frame.setVisible(visible);
  92.     }
  93.  
  94.     /**
  95.      * Fills the internal dimensions of a given shape with the current
  96.      * foreground colour of the canvas.
  97.      * @param  shape  the shape object to be filled
  98.      */
  99.     void fill(Shape shape) {
  100.         this.graphic.fill(shape);
  101.         this.canvas.repaint();
  102.     }
  103.  
  104.     /**
  105.      * Erases a given shape's interior on the screen.
  106.      * @param  shape  the shape object to be erased
  107.      */
  108.     void erase(Shape shape) {
  109.         Color original = this.graphic.getColor();
  110.         this.graphic.setColor(this.backgroundColour);
  111.         this.graphic.fill(shape);              // erase by filling background colour
  112.         this.graphic.setColor(original);
  113.         this.canvas.repaint();
  114.     }
  115.  
  116.     /**
  117.      * Draws a String on the Canvas.
  118.      * @param  text   the String to be displayed
  119.      * @param  x      x co-ordinate for text placement
  120.      * @param  y      y co-ordinate for text placement
  121.      */
  122.     void drawString(String text, int x, int y) {
  123.         this.graphic.drawString(text, x, y);  
  124.         this.canvas.repaint();
  125.     }
  126.  
  127.     /**
  128.      * Erases a String on the Canvas.
  129.      * @param  text     the String to be displayed
  130.      * @param  x        x co-ordinate for text placement
  131.      * @param  y        y co-ordinate for text placement
  132.      */
  133.     void eraseString(String text, int x, int y) {
  134.         Color original = this.graphic.getColor();
  135.         this.graphic.setColor(this.backgroundColour);
  136.         this.graphic.drawString(text, x, y);  
  137.         this.graphic.setColor(original);
  138.         this.canvas.repaint();
  139.     }
  140.  
  141.     /**
  142.      * Draws a line on the Canvas.
  143.      * @param  x1   x co-ordinate of start of line
  144.      * @param  y1   y co-ordinate of start of line
  145.      * @param  x2   x co-ordinate of end of line
  146.      * @param  y2   y co-ordinate of end of line
  147.      */
  148.     void drawLine(int x1, int y1, int x2, int y2) {
  149.         this.graphic.drawLine(x1, y1, x2, y2);  
  150.         this.canvas.repaint();
  151.     }
  152.  
  153.     /**
  154.      * Erases a line on the Canvas.
  155.      * @param  x1   x co-ordinate of start of line
  156.      * @param  y1   y co-ordinate of start of line
  157.      * @param  x2   x co-ordinate of end of line
  158.      * @param  y2   y co-ordinate of end of line
  159.      */
  160.     void eraseLine(int x1, int y1, int x2, int y2) {
  161.         //Color original = this.graphic.getColor();
  162.         this.graphic.setColor(this.backgroundColour);
  163.         this.graphic.drawLine(x1, y1, x2, y2);  
  164.         this.canvas.repaint();
  165.     }
  166.  
  167.     /**
  168.      * Sets the foreground colour of the Canvas.
  169.      * @param  newColour   the new colour for the foreground of the Canvas
  170.      */
  171.     void setForegroundColour(String colour) {
  172.         String c = colour.toLowerCase();
  173.         if(c.equals("red"))
  174.             this.graphic.setColor(Color.red);
  175.         else if(c.equals("black"))
  176.             this.graphic.setColor(Color.black);
  177.         else if(c.equals("blue"))
  178.             this.graphic.setColor(Color.blue);
  179.         else if(c.equals("yellow"))
  180.             this.graphic.setColor(Color.yellow);
  181.         else if(c.equals("green"))
  182.             this.graphic.setColor(Color.green);
  183.         else if(c.equals("magenta"))
  184.             this.graphic.setColor(Color.magenta);
  185.         else if(c.equals("white"))
  186.             this.graphic.setColor(Color.white);
  187.         else
  188.             this.graphic.setColor(Color.black);
  189.     }
  190.  
  191.     void wait(int milliseconds) {
  192.         try { Thread.sleep(milliseconds); }
  193.         catch (Exception e) { }
  194.     }
  195.  
  196.     private class CanvasPane extends JPanel {
  197.         public void paint(Graphics g) {
  198.             g.drawImage(Canvas.this.canvasImage, 0, 0, null);
  199.         }
  200.     }
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement