diff --git a/java/2-7-Graphics/SimpleDraw.java b/java/2-7-Graphics/SimpleDraw.java new file mode 100644 index 0000000000000000000000000000000000000000..4dec448c5d0bd174b50c9bd62015033cc5bb7089 --- /dev/null +++ b/java/2-7-Graphics/SimpleDraw.java @@ -0,0 +1,61 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class SimpleDraw { + public static void main(String[] args) { + JFrame f = new JFrame("SimpleDraw"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(300, 300); + f.setContentPane(new Canvas()); + f.setVisible(true); + } +} + +// JComponent is a base class for custom components +class Canvas extends JComponent { + + Point M = new Point(); // mouse point + Point C = new Point(150, 150); // click point + + Canvas(){ + super(); + this.addMouseListener(new MouseAdapter(){ + public void mouseClicked(MouseEvent e){ + C.x = e.getX(); + C.y = e.getY(); + repaint(); + } + }); + + this.addMouseMotionListener(new MouseAdapter(){ + public void mouseMoved(MouseEvent e){ + M.x = e.getX(); + M.y = e.getY(); + repaint(); + } + }); + + } + + // custom graphics drawing + public void paintComponent(Graphics g) { + super.paintComponent(g); + // cast to get 2D drawing methods + Graphics2D g2 = (Graphics2D) g; + + g2.setColor(Color.BLACK); + g2.setStroke(new BasicStroke(2)); + g2.drawLine(C.x, C.y, M.x, M.y); + g2.fillOval(M.x - 5, M.y - 5, 10, 10); + + g2.setColor(Color.RED); + g2.fillOval(C.x - 5, C.y - 5, 10, 10); + + g2.drawString(String.format("%d,%d", C.x, C.y), C.x + 10, C.y + 10); + + g2.setColor(Color.BLACK); + g2.drawString(String.format("%d,%d", M.x, M.y), M.x + 10, M.y + 10); + } +} + diff --git a/java/2-7-Graphics/Transform1.java b/java/2-7-Graphics/Transform1.java new file mode 100644 index 0000000000000000000000000000000000000000..659e5b89aed54ad30d39fb439d768af5bdfd3d5a --- /dev/null +++ b/java/2-7-Graphics/Transform1.java @@ -0,0 +1,100 @@ +/* +* CS 349 Java Code Examples +* +* Transform1 Shows how to "manually" transform a shape model. NOTE: in practice, + you don't want do this. Use Graphics2D matrix tranformations instead. +* +*/ +import javax.swing.JFrame; +import javax.swing.JComponent; +import javax.swing.JButton; +import java.awt.*; +import java.awt.geom.*; +import java.util.ArrayList; +import javax.vecmath.*; +import java.lang.Math.*; +import java.util.Random; +import java.awt.event.*; + +// create the window and run the demo +public class Transform1 { + + public static void main(String[] args) { + // create the window + Canvas canvas = new Canvas(); + JFrame f = new JFrame("Transform1"); // jframe is the app window + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setSize(400, 400); // window size + f.setContentPane(canvas); // add canvas to jframe + f.setVisible(true); // show the window + } +} + +class Canvas extends JComponent { + + // the house shape model (position is centred at top left corner) + private Polygon shape = + new Polygon(new int[] { -50, 50, 50, 0, -50}, + new int[] { 75, 75, -25, -75, -25}, 5); + + Point2d M = new Point2d(); + + Canvas() { + // events + this.addMouseMotionListener(new MouseAdapter(){ + public void mouseMoved(MouseEvent e){ + M.x = e.getX(); + M.y = e.getY(); + repaint(); + }}); + } + + // custom graphics drawing + public void paintComponent(Graphics g) { + super.paintComponent(g); // JPanel paint + Graphics2D g2 = (Graphics2D)g; + + // the shape will get transformed into "world" coordinates + + // transform by hand + // get copy of shape + Polygon transShape = + new Polygon(shape.xpoints, shape.ypoints, shape.npoints); + + + rotate(transShape, 45); + scale(transShape, 2, 1); + translate(transShape, M.x, M.y); + + g2.setStroke(new BasicStroke(3)); + g2.drawPolygon(transShape.xpoints, transShape.ypoints, transShape.npoints); + } + + // non-matrix translate + void translate(Polygon s, double tx, double ty) { + for (int i = 0; i < s.npoints; i++) { + s.xpoints[i] += tx; + s.ypoints[i] += ty; + } + } + + // non-matrix scale + void scale(Polygon s, double sx, double sy) { + for (int i = 0; i < s.npoints; i++) { + s.xpoints[i] *= sx; + s.ypoints[i] *= sy; + } + } + + // no-matrix rotate + void rotate(Polygon s, double theta) { + double rad = Math.toRadians(theta); + for (int i = 0; i < s.npoints; i++) { + int x = s.xpoints[i]; + int y = s.ypoints[i]; + s.xpoints[i] = (int)((double)x * Math.cos(rad) - y * Math.sin(rad)); + s.ypoints[i] = (int)((double)x * Math.sin(rad) + y * Math.cos(rad)); + } + } + +} diff --git a/java/2-7-Graphics/makefile b/java/2-7-Graphics/makefile new file mode 100644 index 0000000000000000000000000000000000000000..01fa22b3110bed51755d14e133a0451e5602bb9e --- /dev/null +++ b/java/2-7-Graphics/makefile @@ -0,0 +1,25 @@ +# super simple makefile +# call it using 'make NAME=name_of_code_file_without_extension' +# (assumes a .java extension) +NAME = SimpleDraw +# you may need to pass OS=win to run on windows +OS = + +# HACK: vecmath is included regardless if needed +all: + @echo "Compiling..." + javac -cp vecmath.jar $(NAME).java + +run: all +# windows needs a semicolon +ifeq ($(OS),win) + @echo "Running on windows ..." + java -cp "vecmath.jar;." $(NAME) +# everyone else likes a colon +else + @echo "Running ..." + java -cp "vecmath.jar:." $(NAME) +endif + +clean: + rm -rf *.class diff --git a/java/2-7-Graphics/vecmath.jar b/java/2-7-Graphics/vecmath.jar new file mode 100644 index 0000000000000000000000000000000000000000..6d8b3a11c9c7a02d736a2ccc7f07fa70eb319832 Binary files /dev/null and b/java/2-7-Graphics/vecmath.jar differ