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

refinements to shape model and cleaned up some MVC code

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