Skip to content

Commit

Permalink
Abstract Class, Circle Class, Dimensioning System
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliu2016 committed Jun 19, 2017
1 parent eecf524 commit b6b5f78
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 189 deletions.
8 changes: 7 additions & 1 deletion src/ca/wtcs/ics3u/gc/BufferedCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* ICS3U - Culminating Project - A compass-and-straightedge construction tool
*
* More information at https://github.com/yuliu2016/GeometryConstructor
*
* The code in this file is from an answer on Stack Overflow.
* It is not written by me, however I made some modifications.
* The source is found at https://stackoverflow.com/a/10508090
*/


Expand Down Expand Up @@ -39,13 +43,15 @@ class BufferedCanvas extends Canvas {
public void update(Graphics g) {

Image bufferedImage = createImage(getWidth(), getHeight());

Graphics bufferedGraphics = bufferedImage.getGraphics();

bufferedGraphics.setColor(getBackground());

bufferedGraphics.fillRect(0, 0, getWidth(), getHeight());
bufferedGraphics.setColor(getForeground());

paint(bufferedGraphics);

g.drawImage(bufferedImage, 0, 0, this);

}
Expand Down
102 changes: 102 additions & 0 deletions src/ca/wtcs/ics3u/gc/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ca.wtcs.ics3u.gc;


/*
* Created by Yu Liu on 2017-06-15
* ICS3U - Culminating Project - A compass-and-straightedge construction tool
*
* More information at https://github.com/yuliu2016/GeometryConstructor
*/


import java.awt.*;


/**
* A <code>Circle</code> is a {@link Geometry}
* object that represents a circle on a plane
* with the center at one point through another,
* with the distance between these points the radius
* of the circle. The position of a circle must be
* dynamically computed, meaning the two {@link Point}
* that defines the object must also have computed
* or fixed values.
*/


public class Circle
extends Geometry {

/**
* The width of the circle's stroke when it's hovered
*/

private static final int THICK_STROKE_WIDTH;


static {
THICK_STROKE_WIDTH = 4;
}

/**
* The two points that defines the circle
*/

private Point center, throughPoint;


/**
*
*/

Circle(Point center, Point throughPoint) {
this.center = center;
this.throughPoint = throughPoint;
}


private double radius(ViewPort viewPort) {
return ViewPort.mag(viewPort.computeX(throughPoint.getX()) - viewPort.computeX(center.getX()),
viewPort.computeY(throughPoint.getY()) - viewPort.computeY(center.getY()));
}

/**
* Paints the geometry entity
*
* @param g the graphic context to be drawn on
* @param viewPort the viewport context to determine position
*/

@Override
public void paint(Graphics g, ViewPort viewPort) {

if (!isHidden()) {
int cx, cy, r;

cx = viewPort.computeX(center.getX());
cy = viewPort.computeY(center.getY());
r = (int) radius(viewPort);

g.setColor(getColor());
g.drawOval(cx - r, cy - r, r * 2, r * 2);

}
}


/**
* Determines if a mouse position is hovering over the geometry
*
* @param mouseX the X position of the mouse
* @param mouseY the Y position of the mouse
* @param viewPort the viewport context to determine position
* @return if the mouse is over geometry
*/

@Override
public boolean isMouseOver(int mouseX, int mouseY, ViewPort viewPort) {
return Math.abs(radius(viewPort) - ViewPort.mag(mouseX - viewPort.computeX(center.getX()),
mouseY - viewPort.computeY(center.getY()))) < THICK_STROKE_WIDTH;
}

}
70 changes: 60 additions & 10 deletions src/ca/wtcs/ics3u/gc/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,31 @@
* also be able to add itself into a {@link GraphView}
*
* @author Yu Liu
* @see Geometry1D
*/


public interface Geometry {
abstract class Geometry {


/**
* The selected state of the geometry
*/

private boolean selected;


/**
* The hidden state of the geometry
*/

private boolean hidden;


/**
* The hovered state of the geometry
*/

private boolean hovered;


/**
Expand All @@ -33,7 +53,7 @@ public interface Geometry {
* @param viewPort the viewport context to determine position
*/

void paint(Graphics g, ViewPort viewPort);
abstract void paint(Graphics g, ViewPort viewPort);


/**
Expand All @@ -45,7 +65,7 @@ public interface Geometry {
* @return if the mouse is over geometry
*/

boolean isMouseOver(int mouseX, int mouseY, ViewPort viewPort);
abstract boolean isMouseOver(int mouseX, int mouseY, ViewPort viewPort);


/**
Expand All @@ -54,39 +74,69 @@ public interface Geometry {
* @return if the geometry is selected
*/

boolean getSelected();
boolean isSelected() {
return selected;
}

/**
* Sets the selected state of geometry
*
* @param selected the selected state of geometry
*/

void setSelected(boolean selected);
void setSelected(boolean selected) {
this.selected = selected;
}

/**
* Determines if the geometry is hidden
*
* @return if the geometry is hidden
*/

boolean getHidden();
boolean isHidden() {
return hidden;
}

/**
* Sets the hidden state of geometry
*
* @param hidden the hidden state of geometry
* @param hidden the hidden state of the geometry
*/

void setHidden(boolean hidden);
void setHidden(boolean hidden) {
this.hidden = hidden;
}

/**
* Determines if the geometry is on mouseover
*
* @return if the geometry is hidden
*/

boolean isHovered() {
return hovered;
}

/**
* Sets the hovered state of geometry
*
* @param hovered the hovered state of geometry
*/

void setHovered(boolean hovered);
void setHovered(boolean hovered) {
this.hovered = hovered;
}

/**
* Determines the color of the geometric object according to its state.
* Overriding paint method should call this method to determine color
*
* @return The unified colour of the object
*/

Color getColor() {
return hovered || selected ? Color.BLUE : Color.BLACK;
}

}
25 changes: 0 additions & 25 deletions src/ca/wtcs/ics3u/gc/Geometry1D.java

This file was deleted.

24 changes: 12 additions & 12 deletions src/ca/wtcs/ics3u/gc/GeometryConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class GeometryConstructor {


static {
DEFAULT_DIMENSION = new Dimension(600, 450);
DEFAULT_DIMENSION = new Dimension(800, 600);
TITLE = "Geometry Constructor";
}

Expand All @@ -54,33 +54,33 @@ public class GeometryConstructor {

public static void main(String[] args) {

Frame application = new Frame(TITLE);
Frame app = new Frame(TITLE);
GraphView graphView = new GraphView();

application.setSize(DEFAULT_DIMENSION);
application.setLayout(null);
application.setResizable(true);
app.setSize(DEFAULT_DIMENSION);
app.setLayout(null);
app.setResizable(true);

application.add(graphView);
app.add(graphView);

application.addWindowListener(new WindowAdapter() {
app.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
application.dispose();
app.dispose();
System.exit(0);
}
});

application.addComponentListener(new ComponentAdapter() {
app.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Dimension newSize = application.getSize();
Dimension newSize = app.getSize();
graphView.setSize(newSize.width, newSize.height);
}
});

application.setMinimumSize(GraphView.MINIMUM_DIMENSION);
application.setVisible(true);
app.setMinimumSize(GraphView.MINIMUM_DIMENSION);
app.setVisible(true);
}

}
Loading

0 comments on commit b6b5f78

Please sign in to comment.