Skip to content

Commit

Permalink
add feature: change line type and color
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 18, 2024
1 parent b64c88b commit 823d410
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void mouseMoved(MouseEvent e) {
if (roundedRadius == 0)
return;


currentWrapper.setShape(getCircle((int) center.getX(), (int) center.getY(), roundedRadius));
canvas.repaint();
return;
Expand Down Expand Up @@ -174,7 +173,8 @@ private void handleMousePressed(MouseEvent e) {
// first coordinate of line has been selected

// create a shape wrapper
currentWrapper = new ShapeWrapper();
currentWrapper = new ShapeWrapper(model.getFillColor(), model.getFillColor(), model.getLineType(),
model.getLineThickness());
currentWrapper.getPlottedPoints().add(polySpaceMousePosition);

// save wrapper
Expand All @@ -201,7 +201,8 @@ private void handleMousePressed(MouseEvent e) {
// center has been selected

// create a shape wrapper
currentWrapper = new ShapeWrapper();
currentWrapper = new ShapeWrapper(model.getFillColor(), model.getFillColor(), model.getLineType(),
model.getLineThickness());
currentWrapper.getPlottedPoints().add(polySpaceMousePosition);

// save wrapper
Expand Down Expand Up @@ -229,7 +230,8 @@ private void handleMousePressed(MouseEvent e) {
// first foci has been selected

// create a shape wrapper
currentWrapper = new ShapeWrapper();
currentWrapper = new ShapeWrapper(model.getFillColor(), model.getFillColor(), model.getLineType(),
model.getLineThickness());
currentWrapper.getPlottedPoints().add(polySpaceMousePosition);

// save wrapper
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/github/creme332/view/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.swing.JPanel;

import com.github.creme332.model.CanvasModel;
import com.github.creme332.model.LineType;
import com.github.creme332.model.ShapeWrapper;

import java.awt.BasicStroke;
Expand All @@ -12,6 +13,7 @@
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

Expand Down Expand Up @@ -178,6 +180,8 @@ public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
setAntialiasing(g2);

final Stroke defaultStroke = g2.getStroke();

if (model.isGuidelinesEnabled()) {
drawGuidelines(g2);
}
Expand All @@ -189,13 +193,15 @@ public void paintComponent(Graphics g) {

for (ShapeWrapper wrapper : model.getShapes()) {
g2.setColor(wrapper.getLineColor());
g2.setStroke(getStroke(wrapper.getLineType(), wrapper.getLineThickness()));

if (wrapper.getShape() != null) {
Shape s1 = model.toUserSpace(wrapper.getShape());
g2.draw(s1);
}

// plot points
g2.setStroke(defaultStroke);
g2.setColor(wrapper.getFillColor());
for (Point2D p : wrapper.getPlottedPoints()) {
Shape point = createPointAsShape(model.toUserSpace(p));
Expand All @@ -207,6 +213,24 @@ public void paintComponent(Graphics g) {
}
}

private Stroke getStroke(LineType lineType, int thickness) {
switch (lineType) {
case SOLID:
// Set the stroke of the copy, not the original
return new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
0, new float[] { 1 }, 0);
case DASHED:
return new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
0, new float[] { 12 }, 0);
case DOTTED:
return new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
0, new float[] { 4 }, 0);
default:
return new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
0, new float[] { 1 }, 0);
}
}

/**
*
* @param mySpaceCoord
Expand Down

0 comments on commit 823d410

Please sign in to comment.