/** * Set the canvas visibility and brings canvas to the front of screen when made visible. This * method can also be used to bring an already visible canvas to the front of other windows. * * @param visible boolean value representing the desired visibility of the canvas (true or false) */ public void setVisible(boolean visible) { if (graphic == null) { // first time: instantiate the offscreen image and fill it with // the background color Dimension size = canvas.getSize(); canvasImage = canvas.createImage(size.width, size.height); graphic = (Graphics2D) canvasImage.getGraphics(); graphic.setColor(backgroundColor); graphic.fillRect(0, 0, size.width, size.height); graphic.setColor(Color.black); } frame.setVisible(visible); }
/** Erase the whole canvas. (Does not repaint.) */ private void erase() { Color original = graphic.getColor(); graphic.setColor(backgroundColor); Dimension size = canvas.getSize(); graphic.fill(new Rectangle(0, 0, size.width, size.height)); graphic.setColor(original); }
/** * Erases a String on the Canvas. * * @param text the String to be displayed * @param x x co-ordinate for text placement * @param y y co-ordinate for text placement */ public void eraseString(String text, int x, int y) { Color original = graphic.getColor(); graphic.setColor(backgroundColour); graphic.drawString(text, x, y); graphic.setColor(original); canvas.repaint(); }
/** Redraw ell shapes currently on the Canvas. */ private void redraw() { erase(); for (Object shape : objects) { shapes.get(shape).draw(graphic); } canvas.repaint(); }
/** * Erases a given shape's outline on the screen. * * @param shape the shape object to be erased */ public void eraseOutline(Shape shape) { Color original = graphic.getColor(); graphic.setColor(backgroundColour); graphic.draw(shape); // erase by drawing background colour graphic.setColor(original); canvas.repaint(); }
/** Redraw ell shapes currently on the Canvas. */ private void redraw() { erase(); for (Iterator i = objects.iterator(); i.hasNext(); ) { shapes.get(i.next()).draw(graphic); } canvas.repaint(); }
/** * Create a Canvas. * * @param title title to appear in Canvas Frame * @param width the desired width for the canvas * @param height the desired height for the canvas * @param bgClour the desired background colour of the canvas */ private Canvas(String title, int width, int height, Color bgColour) { frame = new JFrame(); canvas = new CanvasPane(); frame.setContentPane(canvas); frame.setTitle(title); canvas.setPreferredSize(new Dimension(width, height)); backgroundColour = bgColour; frame.pack(); }
/** * Create a Canvas. * * @param title title to appear in Canvas Frame * @param width the desired width for the canvas * @param height the desired height for the canvas * @param bgClour the desired background colour of the canvas */ private Canvas(String title, int width, int height, Color bgColour) { frame = new JFrame(); canvas = new CanvasPane(); frame.setContentPane(canvas); frame.setTitle(title); canvas.setPreferredSize(new Dimension(width, height)); backgroundColour = bgColour; frame.pack(); objects = new ArrayList<Object>(); shapes = new HashMap<Object, ShapeDescription>(); }
/** * Fills the internal dimensions of a given shape with the current foreground colour of the * canvas. * * @param shape the shape object to be filled */ public void fill(Shape shape) { graphic.fill(shape); canvas.repaint(); }
/** * Draws a given shape onto the canvas. * * @param shape the shape object to be drawn on the canvas */ public void draw(Shape shape) { graphic.draw(shape); canvas.repaint(); }
/** * Returns the size of the canvas. * * @return The current dimension of the canvas */ public Dimension getSize() { return canvas.getSize(); }
/** * Sets the size of the canvas. * * @param width new width * @param height new height */ public void setSize(int width, int height) { canvas.setPreferredSize(new Dimension(width, height)); frame.pack(); }
/** * Draws a line on the Canvas. * * @param x1 x co-ordinate of start of line * @param y1 y co-ordinate of start of line * @param x2 x co-ordinate of end of line * @param y2 y co-ordinate of end of line */ public void drawLine(int x1, int y1, int x2, int y2) { graphic.drawLine(x1, y1, x2, y2); canvas.repaint(); }
/** * Draws a String on the Canvas. * * @param text the String to be displayed * @param x x co-ordinate for text placement * @param y y co-ordinate for text placement */ public void drawString(String text, int x, int y) { graphic.drawString(text, x, y); canvas.repaint(); }
/** * Draws an image onto the canvas. * * @param image the Image object to be displayed * @param x x co-ordinate for Image placement * @param y y co-ordinate for Image placement * @return returns boolean value representing whether the image was completely loaded */ public boolean drawImage(Image image, int x, int y) { boolean result = graphic.drawImage(image, x, y, null); canvas.repaint(); return result; }