commit a74360df6989742868a979d11c7516623ad000c3 parent 86aa6f41ebb82d0e40ee3dd9f7d2c9dea8b45abd Author: thing1 <thing1@seacrossedlovers.xyz> Date: Wed, 28 Jan 2026 11:49:34 +0000 lots of java :( Diffstat:
| A | CS10120/28.01.26.md | | | 26 | ++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/Canvas.java | | | 236 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/Circle.java | | | 23 | +++++++++++++++++++++++ |
| A | CS12320/workshop/1/Main.java | | | 9 | +++++++++ |
| A | CS12320/workshop/1/More-circle-methods.txt | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/Square.java | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/Triangle.java | | | 29 | +++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Canvas.java | | | 236 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Main.java | | | 9 | +++++++++ |
| A | CS12320/workshop/1/inheritance/More-circle-methods.txt | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Shape.java | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Square.java | | | 25 | +++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Workshop02-materials/Canvas.java | | | 236 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Workshop02-materials/More-circle-methods.txt | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Workshop02-materials/loopy-circles.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/Workshop02-materials/shape-control.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/loopy-circles.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/inheritance/shape-control.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/loopy-circles.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/shape-control.txt | | | 24 | ++++++++++++++++++++++++ |
| A | CS12320/workshop/1/test.java.script | | | 4 | ++++ |
21 files changed, 1349 insertions(+), 0 deletions(-)
diff --git a/CS10120/28.01.26.md b/CS10120/28.01.26.md @@ -0,0 +1,26 @@ +# 28/01/26 + +- apparently i have to be kind... >:((( +- HTML is based on SGML +- browser wars between Netscape and IE +- URL stands for uniform resource locator +- URLs meet the following spec (for this module) + - {protocol}://dnsaddr[:port]/[resource] +- `<!DOCTYPE html>` must be at the top of every page +- `<html>` should be used to hold the whole document +- `<head>` should be used as well +- `<body>` should be used as well + +## common tags + +- `<hN>` heading +- `<p>` paragraph +- `<br/>` break/make gap +- `<hr/>` horizontal rule +- `<a>` hyperlink + + +## the `a` tag + +- `<a href="http://google.com>google</a>` +- the links is specified with href, and the name (blue thing on page) is in the tag diff --git a/CS12320/workshop/1/Canvas.java b/CS12320/workshop/1/Canvas.java @@ -0,0 +1,236 @@ +import javax.swing.*; +import java.awt.*; +import java.util.List; +import java.util.*; + +/** + * Canvas is a class to allow for simple graphical drawing on a canvas. + * This is a modification of the general purpose Canvas, specially made for + * the BlueJ "shapes" example. + * + * @author: Bruce Quig + * @author: Michael Kolling (mik) + * + * @version: 1.6 (shapes) + */ +public class Canvas +{ + // Note: The implementation of this class (specifically the handling of + // shape identity and colors) is slightly more complex than necessary. This + // is done on purpose to keep the interface and instance fields of the + // shape objects in this project clean and simple for educational purposes. + + private static Canvas canvasSingleton; + + /** + * Factory method to get the canvas singleton object. + */ + public static Canvas getCanvas() + { + if(canvasSingleton == null) { + canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, + Color.white); + } + canvasSingleton.setVisible(true); + return canvasSingleton; + } + + // ----- instance part ----- + + private JFrame frame; + private CanvasPane canvas; + private Graphics2D graphic; + private Color backgroundColour; + private Image canvasImage; + private List<Object> objects; + private HashMap<Object,Object> shapes; + + /** + * 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(); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + objects = new ArrayList<Object>(); + shapes = new HashMap<Object,Object> (); + } + + /** + * 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 colour + Dimension size = canvas.getSize(); + canvasImage = canvas.createImage(size.width, size.height); + graphic = (Graphics2D)canvasImage.getGraphics(); + graphic.setColor(backgroundColour); + graphic.fillRect(0, 0, size.width, size.height); + graphic.setColor(Color.black); + } + frame.setVisible(visible); + } + + /** + * Draw a given shape onto the canvas. + * @param referenceObject an object to define identity for this shape + * @param color the color of the shape + * @param shape the shape object to be drawn on the canvas + */ + // Note: this is a slightly backwards way of maintaining the shape + // objects. It is carefully designed to keep the visible shape interfaces + // in this project clean and simple for educational purposes. + public void draw(Object referenceObject, String color, Shape shape) + { + objects.remove(referenceObject); // just in case it was already there + objects.add(referenceObject); // add at the end + shapes.put(referenceObject, new ShapeDescription(shape, color)); + redraw(); + } + + /** + * Erase a given shape's from the screen. + * @param referenceObject the shape object to be erased + */ + public void erase(Object referenceObject) + { + objects.remove(referenceObject); // just in case it was already there + shapes.remove(referenceObject); + redraw(); + } + + /** + * Set the foreground colour of the Canvas. + * @param newColour the new colour for the foreground of the Canvas + */ + public void setForegroundColor(String colorString) + { + if(colorString.equals("red")) + graphic.setColor(Color.red); + else if(colorString.equals("black")) + graphic.setColor(Color.black); + else if(colorString.equals("blue")) + graphic.setColor(Color.blue); + else if(colorString.equals("yellow")) + graphic.setColor(Color.yellow); + else if(colorString.equals("green")) + graphic.setColor(Color.green); + else if(colorString.equals("magenta")) + graphic.setColor(Color.magenta); + else if(colorString.equals("white")) + graphic.setColor(Color.white); + else + graphic.setColor(Color.black); + } + + /** + * Wait for a specified number of milliseconds before finishing. + * This provides an easy way to specify a small delay which can be + * used when producing animations. + * @param milliseconds the number + */ + public void wait(int milliseconds) + { + try + { + Thread.sleep(milliseconds); + } + catch (Exception e) + { + // ignoring exception at the moment + } + } + + /** + * Redraw ell shapes currently on the Canvas. + */ + private void redraw() + { + erase(); + for(Iterator i=objects.iterator(); i.hasNext(); ) { + ((ShapeDescription)shapes.get(i.next())).draw(graphic); + } + canvas.repaint(); + } + + /** + * Get the width + */ + public int getWidth(){ + return canvas.getWidth(); + } + + /** + * Get the height + */ + public int getHeight(){ + return canvas.getHeight(); + } + + /** + * Erase the whole canvas. (Does not repaint.) + */ + private void erase() + { + Color original = graphic.getColor(); + graphic.setColor(backgroundColour); + Dimension size = canvas.getSize(); + graphic.fill(new Rectangle(0, 0, size.width, size.height)); + graphic.setColor(original); + } + + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class CanvasPane extends JPanel + { + public void paint(Graphics g) + { + g.drawImage(canvasImage, 0, 0, null); + } + } + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class ShapeDescription + { + private Shape shape; + private String colorString; + + public ShapeDescription(Shape shape, String color) + { + this.shape = shape; + colorString = color; + } + + public void draw(Graphics2D graphic) + { + setForegroundColor(colorString); + graphic.fill(shape); + } + } + +} diff --git a/CS12320/workshop/1/Circle.java b/CS12320/workshop/1/Circle.java @@ -0,0 +1,23 @@ +public class Circle { + private int diamiter; + private int x; + private int y; + private String color; + private boolean isVisible; + + public Circle() { + diamiter = 30; + x = 30; + y = 30; + color = "blue"; + isVisible = true; + } + + public String toString() { + return "diamiter: " + diamiter + + ", x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } +}; diff --git a/CS12320/workshop/1/Main.java b/CS12320/workshop/1/Main.java @@ -0,0 +1,9 @@ +public class Main { + public static void main(String[] args) { + Square s = new Square(); + + s.makeVisible(); + + s.makeInvisible(); + } +} diff --git a/CS12320/workshop/1/More-circle-methods.txt b/CS12320/workshop/1/More-circle-methods.txt @@ -0,0 +1,92 @@ + + // Comment: Move the circle a few pixels to the right. + public void moveRight(){ + moveHorizontal(20); + } + + // Comment: Move the circle a few pixels to the left. + public void moveLeft(){ + moveHorizontal(-20); + } + + // Comment: Move the circle a few pixels up. + public void moveUp(){ + moveVertical(-20); + } + + // Comment:Move the circle a few pixels down. + public void moveDown() + { + moveVertical(20); + } + + // Comment: Move the circle horizontally by 'distance' pixels. + public void moveHorizontal(int distance){ + erase(); + xPosition += distance; + draw(); + } + + // Comment: Move the circle vertically by 'distance' pixels. + public void moveVertical(int distance){ + erase(); + yPosition += distance; + draw(); + } + + // Comment: Slowly move the circle horizontally by 'distance' pixels. + public void slowMoveHorizontal(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + xPosition += delta; + draw(); + } + } + + // Comment:Slowly move the circle vertically by 'distance' pixels. + public void slowMoveVertical(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + yPosition += delta; + draw(); + } + } + + // Comment:Get the current diameter size + public int getSize(){ + return diameter; + } + + // Comment:Get the current color + public String getColor(){ + return color.toString(); + } + + // Comment: Get current X coordiate in the canvas + public int getXCoord(){ + return xPosition; + } + + // Comment:Get current Y coordiate in the canvas + public int getYCoord(){ + return yPosition; + } + diff --git a/CS12320/workshop/1/Square.java b/CS12320/workshop/1/Square.java @@ -0,0 +1,55 @@ +import java.awt.*; +import java.awt.geom.*; + +public class Square { + private int w; + private int h; + private int x; + private int y; + private String color; + private boolean isVisible; + + public Square() { + w = 30; + h = 30; + x = 30; + y = 30; + color = "blue"; + isVisible = false; + } + + public String toString() { + return "width: " + w + + ", height: " + h + + ", x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } + + private void draw() { + if (isVisible) { + Canvas canvas = Canvas.getCanvas(); + canvas.draw(this, color, + new Rectangle2D.Double(x, y, w, h)); + canvas.wait(1000); + } + } + + private void erase() { + if (isVisible) { + Canvas canv = Canvas.getCanvas(); + canv.erase(this); + } + } + + public void makeVisible() { + isVisible = true; + draw(); + } + + public void makeInvisible() { + erase(); + isVisible = false; + } +}; diff --git a/CS12320/workshop/1/Triangle.java b/CS12320/workshop/1/Triangle.java @@ -0,0 +1,29 @@ +public class Triangle { + private int w; + private int h; + private int angle; + private int x; + private int y; + private String color; + private boolean isVisible; + + public Triangle() { + w= 30; + h= 30; + angle = 30; + x = 30; + y = 30; + color = "blue"; + isVisible = true; + } + + public String toString() { + return "w: " + w + + ", h: " + h + + ", angle: " + angle + + ", x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } +}; diff --git a/CS12320/workshop/1/inheritance/Canvas.java b/CS12320/workshop/1/inheritance/Canvas.java @@ -0,0 +1,236 @@ +import javax.swing.*; +import java.awt.*; +import java.util.List; +import java.util.*; + +/** + * Canvas is a class to allow for simple graphical drawing on a canvas. + * This is a modification of the general purpose Canvas, specially made for + * the BlueJ "shapes" example. + * + * @author: Bruce Quig + * @author: Michael Kolling (mik) + * + * @version: 1.6 (shapes) + */ +public class Canvas +{ + // Note: The implementation of this class (specifically the handling of + // shape identity and colors) is slightly more complex than necessary. This + // is done on purpose to keep the interface and instance fields of the + // shape objects in this project clean and simple for educational purposes. + + private static Canvas canvasSingleton; + + /** + * Factory method to get the canvas singleton object. + */ + public static Canvas getCanvas() + { + if(canvasSingleton == null) { + canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, + Color.white); + } + canvasSingleton.setVisible(true); + return canvasSingleton; + } + + // ----- instance part ----- + + private JFrame frame; + private CanvasPane canvas; + private Graphics2D graphic; + private Color backgroundColour; + private Image canvasImage; + private List<Object> objects; + private HashMap<Object,Object> shapes; + + /** + * 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(); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + objects = new ArrayList<Object>(); + shapes = new HashMap<Object,Object> (); + } + + /** + * 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 colour + Dimension size = canvas.getSize(); + canvasImage = canvas.createImage(size.width, size.height); + graphic = (Graphics2D)canvasImage.getGraphics(); + graphic.setColor(backgroundColour); + graphic.fillRect(0, 0, size.width, size.height); + graphic.setColor(Color.black); + } + frame.setVisible(visible); + } + + /** + * Draw a given shape onto the canvas. + * @param referenceObject an object to define identity for this shape + * @param color the color of the shape + * @param shape the shape object to be drawn on the canvas + */ + // Note: this is a slightly backwards way of maintaining the shape + // objects. It is carefully designed to keep the visible shape interfaces + // in this project clean and simple for educational purposes. + public void draw(Object referenceObject, String color, Shape shape) + { + objects.remove(referenceObject); // just in case it was already there + objects.add(referenceObject); // add at the end + shapes.put(referenceObject, new ShapeDescription(shape, color)); + redraw(); + } + + /** + * Erase a given shape's from the screen. + * @param referenceObject the shape object to be erased + */ + public void erase(Object referenceObject) + { + objects.remove(referenceObject); // just in case it was already there + shapes.remove(referenceObject); + redraw(); + } + + /** + * Set the foreground colour of the Canvas. + * @param newColour the new colour for the foreground of the Canvas + */ + public void setForegroundColor(String colorString) + { + if(colorString.equals("red")) + graphic.setColor(Color.red); + else if(colorString.equals("black")) + graphic.setColor(Color.black); + else if(colorString.equals("blue")) + graphic.setColor(Color.blue); + else if(colorString.equals("yellow")) + graphic.setColor(Color.yellow); + else if(colorString.equals("green")) + graphic.setColor(Color.green); + else if(colorString.equals("magenta")) + graphic.setColor(Color.magenta); + else if(colorString.equals("white")) + graphic.setColor(Color.white); + else + graphic.setColor(Color.black); + } + + /** + * Wait for a specified number of milliseconds before finishing. + * This provides an easy way to specify a small delay which can be + * used when producing animations. + * @param milliseconds the number + */ + public void wait(int milliseconds) + { + try + { + Thread.sleep(milliseconds); + } + catch (Exception e) + { + // ignoring exception at the moment + } + } + + /** + * Redraw ell shapes currently on the Canvas. + */ + private void redraw() + { + erase(); + for(Iterator i=objects.iterator(); i.hasNext(); ) { + ((ShapeDescription)shapes.get(i.next())).draw(graphic); + } + canvas.repaint(); + } + + /** + * Get the width + */ + public int getWidth(){ + return canvas.getWidth(); + } + + /** + * Get the height + */ + public int getHeight(){ + return canvas.getHeight(); + } + + /** + * Erase the whole canvas. (Does not repaint.) + */ + private void erase() + { + Color original = graphic.getColor(); + graphic.setColor(backgroundColour); + Dimension size = canvas.getSize(); + graphic.fill(new Rectangle(0, 0, size.width, size.height)); + graphic.setColor(original); + } + + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class CanvasPane extends JPanel + { + public void paint(Graphics g) + { + g.drawImage(canvasImage, 0, 0, null); + } + } + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class ShapeDescription + { + private Shape shape; + private String colorString; + + public ShapeDescription(Shape shape, String color) + { + this.shape = shape; + colorString = color; + } + + public void draw(Graphics2D graphic) + { + setForegroundColor(colorString); + graphic.fill(shape); + } + } + +} diff --git a/CS12320/workshop/1/inheritance/Main.java b/CS12320/workshop/1/inheritance/Main.java @@ -0,0 +1,9 @@ +public class Main { + public static void main(String args[]) { + Shape s = new Shape(); + System.out.println(s); + + Square sq = new Square(); + System.out.println(sq); + } +} diff --git a/CS12320/workshop/1/inheritance/More-circle-methods.txt b/CS12320/workshop/1/inheritance/More-circle-methods.txt @@ -0,0 +1,92 @@ + + // Comment: Move the circle a few pixels to the right. + public void moveRight(){ + moveHorizontal(20); + } + + // Comment: Move the circle a few pixels to the left. + public void moveLeft(){ + moveHorizontal(-20); + } + + // Comment: Move the circle a few pixels up. + public void moveUp(){ + moveVertical(-20); + } + + // Comment:Move the circle a few pixels down. + public void moveDown() + { + moveVertical(20); + } + + // Comment: Move the circle horizontally by 'distance' pixels. + public void moveHorizontal(int distance){ + erase(); + xPosition += distance; + draw(); + } + + // Comment: Move the circle vertically by 'distance' pixels. + public void moveVertical(int distance){ + erase(); + yPosition += distance; + draw(); + } + + // Comment: Slowly move the circle horizontally by 'distance' pixels. + public void slowMoveHorizontal(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + xPosition += delta; + draw(); + } + } + + // Comment:Slowly move the circle vertically by 'distance' pixels. + public void slowMoveVertical(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + yPosition += delta; + draw(); + } + } + + // Comment:Get the current diameter size + public int getSize(){ + return diameter; + } + + // Comment:Get the current color + public String getColor(){ + return color.toString(); + } + + // Comment: Get current X coordiate in the canvas + public int getXCoord(){ + return xPosition; + } + + // Comment:Get current Y coordiate in the canvas + public int getYCoord(){ + return yPosition; + } + diff --git a/CS12320/workshop/1/inheritance/Shape.java b/CS12320/workshop/1/inheritance/Shape.java @@ -0,0 +1,41 @@ +import java.awt.*; +import java.awt.geom.*; + +public class Shape { + private int x; + private int y; + private String color; + private boolean isVisible; + + public Shape() { + x = 30; + y = 30; + color = "blue"; + isVisible = true; + } + + public String toString() { + return "x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public String getColor() { + return color; + } + + public boolean getVisible() { + return isVisible; + } + + public void draw() {} +}; diff --git a/CS12320/workshop/1/inheritance/Square.java b/CS12320/workshop/1/inheritance/Square.java @@ -0,0 +1,25 @@ +import java.awt.*; +import java.awt.geom.*; + +class Square extends Shape { + private int w, h; + + public String toString() { + return "width: " + w + + ", heigth " + h + + ", " + super.toString(); + }; + + public void draw() { + if (super.getVisible()) { + Canvas canvas = Canvas.getCanvas(); + canvas.draw(this, super.getColor(), + (Shape) new Rectangle2D.Double( + super.getX(), + super.getY(), + w, + h)); + + } + } +} diff --git a/CS12320/workshop/1/inheritance/Workshop02-materials/Canvas.java b/CS12320/workshop/1/inheritance/Workshop02-materials/Canvas.java @@ -0,0 +1,236 @@ +import javax.swing.*; +import java.awt.*; +import java.util.List; +import java.util.*; + +/** + * Canvas is a class to allow for simple graphical drawing on a canvas. + * This is a modification of the general purpose Canvas, specially made for + * the BlueJ "shapes" example. + * + * @author: Bruce Quig + * @author: Michael Kolling (mik) + * + * @version: 1.6 (shapes) + */ +public class Canvas +{ + // Note: The implementation of this class (specifically the handling of + // shape identity and colors) is slightly more complex than necessary. This + // is done on purpose to keep the interface and instance fields of the + // shape objects in this project clean and simple for educational purposes. + + private static Canvas canvasSingleton; + + /** + * Factory method to get the canvas singleton object. + */ + public static Canvas getCanvas() + { + if(canvasSingleton == null) { + canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, + Color.white); + } + canvasSingleton.setVisible(true); + return canvasSingleton; + } + + // ----- instance part ----- + + private JFrame frame; + private CanvasPane canvas; + private Graphics2D graphic; + private Color backgroundColour; + private Image canvasImage; + private List<Object> objects; + private HashMap<Object,Object> shapes; + + /** + * 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(); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + objects = new ArrayList<Object>(); + shapes = new HashMap<Object,Object> (); + } + + /** + * 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 colour + Dimension size = canvas.getSize(); + canvasImage = canvas.createImage(size.width, size.height); + graphic = (Graphics2D)canvasImage.getGraphics(); + graphic.setColor(backgroundColour); + graphic.fillRect(0, 0, size.width, size.height); + graphic.setColor(Color.black); + } + frame.setVisible(visible); + } + + /** + * Draw a given shape onto the canvas. + * @param referenceObject an object to define identity for this shape + * @param color the color of the shape + * @param shape the shape object to be drawn on the canvas + */ + // Note: this is a slightly backwards way of maintaining the shape + // objects. It is carefully designed to keep the visible shape interfaces + // in this project clean and simple for educational purposes. + public void draw(Object referenceObject, String color, Shape shape) + { + objects.remove(referenceObject); // just in case it was already there + objects.add(referenceObject); // add at the end + shapes.put(referenceObject, new ShapeDescription(shape, color)); + redraw(); + } + + /** + * Erase a given shape's from the screen. + * @param referenceObject the shape object to be erased + */ + public void erase(Object referenceObject) + { + objects.remove(referenceObject); // just in case it was already there + shapes.remove(referenceObject); + redraw(); + } + + /** + * Set the foreground colour of the Canvas. + * @param newColour the new colour for the foreground of the Canvas + */ + public void setForegroundColor(String colorString) + { + if(colorString.equals("red")) + graphic.setColor(Color.red); + else if(colorString.equals("black")) + graphic.setColor(Color.black); + else if(colorString.equals("blue")) + graphic.setColor(Color.blue); + else if(colorString.equals("yellow")) + graphic.setColor(Color.yellow); + else if(colorString.equals("green")) + graphic.setColor(Color.green); + else if(colorString.equals("magenta")) + graphic.setColor(Color.magenta); + else if(colorString.equals("white")) + graphic.setColor(Color.white); + else + graphic.setColor(Color.black); + } + + /** + * Wait for a specified number of milliseconds before finishing. + * This provides an easy way to specify a small delay which can be + * used when producing animations. + * @param milliseconds the number + */ + public void wait(int milliseconds) + { + try + { + Thread.sleep(milliseconds); + } + catch (Exception e) + { + // ignoring exception at the moment + } + } + + /** + * Redraw ell shapes currently on the Canvas. + */ + private void redraw() + { + erase(); + for(Iterator i=objects.iterator(); i.hasNext(); ) { + ((ShapeDescription)shapes.get(i.next())).draw(graphic); + } + canvas.repaint(); + } + + /** + * Get the width + */ + public int getWidth(){ + return canvas.getWidth(); + } + + /** + * Get the height + */ + public int getHeight(){ + return canvas.getHeight(); + } + + /** + * Erase the whole canvas. (Does not repaint.) + */ + private void erase() + { + Color original = graphic.getColor(); + graphic.setColor(backgroundColour); + Dimension size = canvas.getSize(); + graphic.fill(new Rectangle(0, 0, size.width, size.height)); + graphic.setColor(original); + } + + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class CanvasPane extends JPanel + { + public void paint(Graphics g) + { + g.drawImage(canvasImage, 0, 0, null); + } + } + + /************************************************************************ + * Inner class CanvasPane - the actual canvas component contained in the + * Canvas frame. This is essentially a JPanel with added capability to + * refresh the image drawn on it. + */ + private class ShapeDescription + { + private Shape shape; + private String colorString; + + public ShapeDescription(Shape shape, String color) + { + this.shape = shape; + colorString = color; + } + + public void draw(Graphics2D graphic) + { + setForegroundColor(colorString); + graphic.fill(shape); + } + } + +} diff --git a/CS12320/workshop/1/inheritance/Workshop02-materials/More-circle-methods.txt b/CS12320/workshop/1/inheritance/Workshop02-materials/More-circle-methods.txt @@ -0,0 +1,92 @@ + + // Comment: Move the circle a few pixels to the right. + public void moveRight(){ + moveHorizontal(20); + } + + // Comment: Move the circle a few pixels to the left. + public void moveLeft(){ + moveHorizontal(-20); + } + + // Comment: Move the circle a few pixels up. + public void moveUp(){ + moveVertical(-20); + } + + // Comment:Move the circle a few pixels down. + public void moveDown() + { + moveVertical(20); + } + + // Comment: Move the circle horizontally by 'distance' pixels. + public void moveHorizontal(int distance){ + erase(); + xPosition += distance; + draw(); + } + + // Comment: Move the circle vertically by 'distance' pixels. + public void moveVertical(int distance){ + erase(); + yPosition += distance; + draw(); + } + + // Comment: Slowly move the circle horizontally by 'distance' pixels. + public void slowMoveHorizontal(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + xPosition += delta; + draw(); + } + } + + // Comment:Slowly move the circle vertically by 'distance' pixels. + public void slowMoveVertical(int distance){ + int delta; + + if(distance < 0){ + delta = -1; + distance = -distance; + } + else{ + delta = 1; + } + + for(int i = 0; i < distance; i++){ + yPosition += delta; + draw(); + } + } + + // Comment:Get the current diameter size + public int getSize(){ + return diameter; + } + + // Comment:Get the current color + public String getColor(){ + return color.toString(); + } + + // Comment: Get current X coordiate in the canvas + public int getXCoord(){ + return xPosition; + } + + // Comment:Get current Y coordiate in the canvas + public int getYCoord(){ + return yPosition; + } + diff --git a/CS12320/workshop/1/inheritance/Workshop02-materials/loopy-circles.txt b/CS12320/workshop/1/inheritance/Workshop02-materials/loopy-circles.txt @@ -0,0 +1,24 @@ +Canvas c = Canvas.getCanvas(); +Circle circle1 = new Circle(); +int width = c.getWidth(); +int height = c.getHeight(); +int xPos = 0; +int currentXPos = 0; +int yPos = 0; +int currentYPos = 0; +java.util.Random rand = new java.util.Random(); +circle1.makeVisible(); + +int count = 0; + +while (count < 5){ + xPos = rand.nextInt(width); + yPos = rand.nextInt(height); + currentXPos = circle1.getXCoord(); + currentYPos = circle1.getYCoord(); + circle1.slowMoveHorizontal(xPos - currentXPos); + circle1.slowMoveVertical(yPos - currentYPos); + + count = count + 1; +} + diff --git a/CS12320/workshop/1/inheritance/Workshop02-materials/shape-control.txt b/CS12320/workshop/1/inheritance/Workshop02-materials/shape-control.txt @@ -0,0 +1,24 @@ +Circle circle1 = new Circle(); +circle1.makeVisible(); +circle1.moveHorizontal(50); +Circle circle2 = new Circle(); +circle2.makeVisible(); +circle2.changeColor("red"); + +String color = circle1.getColor(); +if (color.equals("red")) { + int currentSize = circle1.getSize(); + circle1.changeSize(currentSize * 2); +} + +color = circle2.getColor(); + +if (color.equals("red")) { + int currentSize = circle2.getSize(); + circle2.changeSize(currentSize * 2); +} + + + + + diff --git a/CS12320/workshop/1/inheritance/loopy-circles.txt b/CS12320/workshop/1/inheritance/loopy-circles.txt @@ -0,0 +1,24 @@ +Canvas c = Canvas.getCanvas(); +Circle circle1 = new Circle(); +int width = c.getWidth(); +int height = c.getHeight(); +int xPos = 0; +int currentXPos = 0; +int yPos = 0; +int currentYPos = 0; +java.util.Random rand = new java.util.Random(); +circle1.makeVisible(); + +int count = 0; + +while (count < 5){ + xPos = rand.nextInt(width); + yPos = rand.nextInt(height); + currentXPos = circle1.getXCoord(); + currentYPos = circle1.getYCoord(); + circle1.slowMoveHorizontal(xPos - currentXPos); + circle1.slowMoveVertical(yPos - currentYPos); + + count = count + 1; +} + diff --git a/CS12320/workshop/1/inheritance/shape-control.txt b/CS12320/workshop/1/inheritance/shape-control.txt @@ -0,0 +1,24 @@ +Circle circle1 = new Circle(); +circle1.makeVisible(); +circle1.moveHorizontal(50); +Circle circle2 = new Circle(); +circle2.makeVisible(); +circle2.changeColor("red"); + +String color = circle1.getColor(); +if (color.equals("red")) { + int currentSize = circle1.getSize(); + circle1.changeSize(currentSize * 2); +} + +color = circle2.getColor(); + +if (color.equals("red")) { + int currentSize = circle2.getSize(); + circle2.changeSize(currentSize * 2); +} + + + + + diff --git a/CS12320/workshop/1/loopy-circles.txt b/CS12320/workshop/1/loopy-circles.txt @@ -0,0 +1,24 @@ +Canvas c = Canvas.getCanvas(); +Circle circle1 = new Circle(); +int width = c.getWidth(); +int height = c.getHeight(); +int xPos = 0; +int currentXPos = 0; +int yPos = 0; +int currentYPos = 0; +java.util.Random rand = new java.util.Random(); +circle1.makeVisible(); + +int count = 0; + +while (count < 5){ + xPos = rand.nextInt(width); + yPos = rand.nextInt(height); + currentXPos = circle1.getXCoord(); + currentYPos = circle1.getYCoord(); + circle1.slowMoveHorizontal(xPos - currentXPos); + circle1.slowMoveVertical(yPos - currentYPos); + + count = count + 1; +} + diff --git a/CS12320/workshop/1/shape-control.txt b/CS12320/workshop/1/shape-control.txt @@ -0,0 +1,24 @@ +Circle circle1 = new Circle(); +circle1.makeVisible(); +circle1.moveHorizontal(50); +Circle circle2 = new Circle(); +circle2.makeVisible(); +circle2.changeColor("red"); + +String color = circle1.getColor(); +if (color.equals("red")) { + int currentSize = circle1.getSize(); + circle1.changeSize(currentSize * 2); +} + +color = circle2.getColor(); + +if (color.equals("red")) { + int currentSize = circle2.getSize(); + circle2.changeSize(currentSize * 2); +} + + + + + diff --git a/CS12320/workshop/1/test.java.script b/CS12320/workshop/1/test.java.script @@ -0,0 +1,4 @@ +System.out.println("Hi 1"); +System.out.println("Hi 2"); +System.out.println("Hi 3"); +System.out.println("Hi 4");