Commit 7ac9f0c1 authored by Daniel Vogel's avatar Daniel Vogel

refinements to shape model and cleaned up some MVC code

parent a807328f
......@@ -33,8 +33,9 @@ public class Transform1 {
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);
private Polygon shape =
new Polygon(new int[] { -50, 50, 50, 0, -50},
new int[] { 75, 75, -25, -75, -25}, 5);
Point2d M = new Point2d();
......@@ -57,10 +58,12 @@ class Canvas extends JComponent {
// transform by hand
// get copy of shape
Polygon transShape = new Polygon(shape.xpoints, shape.ypoints, shape.npoints);
Polygon transShape =
new Polygon(shape.xpoints, shape.ypoints, shape.npoints);
scale(transShape, 2, 1);
rotate(transShape, 45);
scale(transShape, 2, 1);
translate(transShape, M.x, M.y);
g2.setStroke(new BasicStroke(3));
......
......@@ -2,16 +2,11 @@
* Shape: See ShapeDemo for an example how to use this class.
*
*/
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.*;
import java.awt.geom.*;
import javax.vecmath.*;
// simple shape model class
class Shape {
......@@ -41,7 +36,7 @@ class Shape {
// shape is polyline or polygon
Boolean isClosed = true;
Boolean isClosed = false;
public Boolean getIsClosed() {
return isClosed;
......@@ -52,7 +47,7 @@ class Shape {
}
// if polygon is filled or not
Boolean isFilled = true;
Boolean isFilled = false;
public Boolean getIsFilled() {
return isFilled;
......@@ -64,7 +59,7 @@ class Shape {
// drawing attributes
Color colour = Color.BLACK;
float strokeThickness = 3.0f;
float strokeThickness = 3.0f;
public Color getColour() {
return colour;
......@@ -111,6 +106,7 @@ class Shape {
// let the shape draw itself
// (note this isn't good separation of shape View from shape Model)
public void draw(Graphics2D g2) {
// don't draw if points are empty (not shape)
......@@ -139,14 +135,14 @@ class Shape {
// reset the transform to what it was before we drew the shape
g2.setTransform(M);
}
// let shape handle it's own hit testing
// let shape handle its own hit testing
// (x,y) is the point to test against
// (x,y) needs to be in same coordinate frame as shape, you could add
// a work to shape transform as an extra parater to this function
// a panel-to-shape transform as an extra parameter to this function
// (note this isn't good separation of shape Controller from shape Model)
public boolean hittest(double x, double y)
{
if (points != null) {
......
......@@ -22,20 +22,21 @@ public class ShapeDemo extends JPanel {
public void mousePressed(MouseEvent e) {
shape = new Shape();
shape.setIsClosed(false);
shape.setIsFilled(false);
// change shape type
// shape.setIsClosed(true);
// shape.setIsFilled(true);
shape.setColour(Color.BLUE);
// try uncommenting this block ...
// remember transformations are post-multipied ...
// let's mirror the shape ...
AffineTransform T = new AffineTransform();
// then scale
T.concatenate(AffineTransform.getScaleInstance(-1, 1));
// translate first
T.concatenate(AffineTransform.getTranslateInstance(-300,0));
shape.setTransform(T);
// // remember transformations are post-multipied ...
// // let's mirror the shape ...
// AffineTransform T = new AffineTransform();
// // then scale
// T.concatenate(AffineTransform.getScaleInstance(-1, 1));
// // translate first
// T.concatenate(AffineTransform.getTranslateInstance(-300,0));
// shape.setTransform(T);
repaint();
}
......
......@@ -8,8 +8,10 @@ interface IView {
}
public class Model {
// the data in the model, just a counter
private int counter;
// the view
IView view;
......
......@@ -10,8 +10,10 @@ interface IView {
}
public class Model {
// the data in the model, just a counter
private int counter;
// all views of this model
private ArrayList<IView> views = new ArrayList<IView>();
......
......@@ -21,6 +21,7 @@ class View2 extends JPanel implements IView {
// set the model
this.model = model;
// setup the event to go to the controller
addMouseListener(controller);
//addActionListener(controller);
......
......@@ -10,6 +10,7 @@ interface IView {
}
public class Model {
// the data in the model, just a counter
private int counter;
// all views of this model
......
......@@ -17,7 +17,7 @@ class View extends JPanel implements IView {
// the model that this view is showing
private Model model;
View(Model model_) {
public View(Model model) {
// create the view UI
button = new JButton("?");
button.setMaximumSize(new Dimension(100, 50));
......@@ -28,7 +28,7 @@ class View extends JPanel implements IView {
this.add(button, new GridBagConstraints());
// set the model
model = model_;
this.model = model;
// setup the event to go to the "controller"
// (this anonymous class is essentially the controller)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment