uni

Thing1's amazing uni repo
Log | Files | Refs

commit 333e2f161387238f784e08e3e2b708e7f9ebcd90
parent a74360df6989742868a979d11c7516623ad000c3
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Thu, 29 Jan 2026 10:48:39 +0000

workshop

Diffstat:
ACS10720/28.01.26.md | 7+++++++
ACS12320/workshop/2/Canvas.java | 236+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12320/workshop/2/Circle.java | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12320/workshop/2/Main.java | 22++++++++++++++++++++++
ACS12320/workshop/2/Square.java | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12320/workshop/2/Triangle.java | 29+++++++++++++++++++++++++++++
ACS12320/workshop/2/pong/Canvas.java | 235+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12320/workshop/2/pong/Circle.java | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12320/workshop/2/pong/Main.java | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 877 insertions(+), 0 deletions(-)

diff --git a/CS10720/28.01.26.md b/CS10720/28.01.26.md @@ -0,0 +1,7 @@ +# 28/01/26 + +- logs are inverse operations to powers, like roots, except they give us different inputs to the power +- lg means the normal log {log sub 10} +- lb or ld means {log sub 2} +- trunk(lb(x)) + 1 tells me the number of bits needed to store x +- {log sub b} x = {log sub c} x over {log sub c} b, this is very important diff --git a/CS12320/workshop/2/Canvas.java b/CS12320/workshop/2/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/2/Circle.java b/CS12320/workshop/2/Circle.java @@ -0,0 +1,105 @@ +import java.awt.*; +import java.awt.geom.*; + +public class Circle { + private int diamiter; + private int x; + private int y; + private String color; + private boolean isVisible; + + public Circle() { + diamiter = 30; + x = 40; + y = 30; + color = "blue"; + isVisible = true; + } + + public int getDiamiter() { + return diamiter; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public String getColor() { + return color; + } + + public boolean getisVisible() { + return isVisible; + } + + public String toString() { + return "diamiter: " + diamiter + + ", x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } + + public void draw() { + if (isVisible) { + Canvas canv = Canvas.getCanvas(); + canv.draw(this, color, new Ellipse2D.Double(x, y, diamiter, diamiter)); + canv.wait(10); + } + } + + private void erase() { + if (isVisible) { + Canvas canv = Canvas.getCanvas(); + canv.erase(this); + } + } + + public void move(int newX, int newY) { + erase(); + x = newX; + y = newY; + draw(); + } + + public void makeVisible() { + isVisible = true; + draw(); + } + + public void makeInvisible() { + erase(); + isVisible = false; + } + + public void changeColor(String newColor) { + color = newColor; + draw(); + } + + public void changeSize(int newDiamiter) { + erase(); + diamiter = newDiamiter; + draw(); + } + + public void moveLeft() { + move(x - 10, y); + }; + + public void moveRight() { + move(x + 10, y); + }; + + public void moveUp() { + move(x, y - 10); + }; + + public void moveDown() { + move(x, y + 10); + }; +}; diff --git a/CS12320/workshop/2/Main.java b/CS12320/workshop/2/Main.java @@ -0,0 +1,22 @@ +import java.util.Random; + +public class Main { + public static void main(String[] args) { + Random random = new Random(); + Canvas canvas = Canvas.getCanvas(); + int width= canvas.getWidth(); + int height = canvas.getHeight(); + + Circle c1 = new Circle(); + Circle c2 = new Circle(); + + c2.changeSize(50); + c2.changeColor("red"); + + for (;;) { + c1.makeVisible(); + c2.makeVisible(); + c2.move(random.nextInt() % width, random.nextInt() % height); + } + } +} diff --git a/CS12320/workshop/2/Square.java b/CS12320/workshop/2/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/2/Triangle.java b/CS12320/workshop/2/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/2/pong/Canvas.java b/CS12320/workshop/2/pong/Canvas.java @@ -0,0 +1,235 @@ +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("Pong", 500, 500, 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/2/pong/Circle.java b/CS12320/workshop/2/pong/Circle.java @@ -0,0 +1,118 @@ +import java.awt.*; +import java.awt.geom.*; +import java.util.Random; + +public class Circle { + private int diamiter; + private int x, y; + private int dx, dy; + private String color; + private boolean isVisible; + + public Circle() { + Random rand = new Random(); + diamiter = 30; + x = rand.nextInt(500); + y = rand.nextInt(500); + dx = 3; + dy = 3; + color = "blue"; + isVisible = true; + } + + public int getDiamiter() { + return diamiter; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getDx() { + return dx; + } + + public int getDy() { + return dy; + } + + public String getColor() { + return color; + } + + public boolean getisVisible() { + return isVisible; + } + + public String toString() { + return "diamiter: " + diamiter + + ", x: " + x + + ", y: " + y + + ", color: " + color + + ", visible: " + isVisible; + } + + public void draw() { + if (isVisible) { + Canvas canv = Canvas.getCanvas(); + canv.draw(this, color, new Ellipse2D.Double(x, y, diamiter, diamiter)); + canv.wait(10); + } + } + + private void erase() { + if (isVisible) { + Canvas canv = Canvas.getCanvas(); + canv.erase(this); + } + } + + public void move() { + erase(); + x += dx; + y += dy; + draw(); + } + + public void makeVisible() { + isVisible = true; + draw(); + } + + public void makeInvisible() { + erase(); + isVisible = false; + } + + public void changeColor(String newColor) { + color = newColor; + draw(); + } + + public void changeSize(int newDiamiter) { + erase(); + diamiter = newDiamiter; + draw(); + } + + public void setDx(int newDx) { + dx = newDx; + } + + public void setDy(int newDy) { + dy = newDy; + } + + public void setX(int newx) { + x = newx; + } + + public void setY(int newy) { + y = newy; + } + +}; diff --git a/CS12320/workshop/2/pong/Main.java b/CS12320/workshop/2/pong/Main.java @@ -0,0 +1,70 @@ +import java.util.Random; + +public class Main { + static int width, height; + static Random random; + + private static void moveball(Circle c) { + if (c.getX() >= width || c.getX() <= 0) + c.setDx(-c.getDx()); + if (c.getY() >= height || c.getY() <= 0) + c.setDy(-c.getDy()); + c.move(); + } + + private static boolean istouching(Circle c1, Circle c2) { + int rad = c2.getDiamiter() / 4; + int upperX = (c2.getX()) + rad; + int lowerX = (c2.getX()) - rad; + int upperY = (c2.getY()) + rad; + int lowerY = (c2.getY()) - rad; + + return ((c1.getX() <= upperX && c1.getX() >= lowerX) + || (c1.getY() <= upperY && c1.getY() >= lowerY)); + } + + public static void main(String[] args) { + Canvas canvas = Canvas.getCanvas(); + width = canvas.getWidth(); + height = canvas.getHeight(); + random = new Random(); + + Circle c1 = new Circle(); + Circle c2 = new Circle(); + + c1.changeColor("brown"); + c1.makeVisible(); + + c2.changeColor("green"); + c2.makeVisible(); + + for (;;) { + if (istouching(c1, c2)) { + c1.setDx(-c1.getDx() + random.nextInt() % 3); + c1.setDy(-c1.getDy() + random.nextInt() % 3); + c2.setDx(-c2.getDx() + random.nextInt() % 3); + c2.setDy(-c2.getDy() + random.nextInt() % 3); + moveball(c1); + moveball(c2); + } + + if (istouching(c2, c1)) { + c1.setDx(-c1.getDx() + random.nextInt() % 3); + c1.setDy(-c1.getDy() + random.nextInt() % 3); + c2.setDx(-c2.getDx() + random.nextInt() % 3); + c2.setDy(-c2.getDy() + random.nextInt() % 3); + moveball(c1); + moveball(c2); + } + + moveball(c1); + moveball(c2); + + + c1.setDx(c1.getDx() % 10); + c1.setDy(c1.getDy() % 10); + c2.setDx(c1.getDx() % 10); + c2.setDy(c1.getDy() % 10); + } + } +}