Skip to content

Commit

Permalink
Added freeform shape tool
Browse files Browse the repository at this point in the history
  • Loading branch information
defano committed Feb 7, 2017
1 parent 81d1c4e commit d010823
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<packaging>jar</packaging>
<groupId>com.defano.jmonet</groupId>
<artifactId>jmonet</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/defano/jmonet/model/PaintToolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum PaintToolType {
LINE,
POLYGON,
SHAPE,
FREEFORM,
SELECTION,
LASSO,
TEXT,
Expand All @@ -24,11 +25,18 @@ public enum PaintToolType {
ROTATE,
MAGNIFIER;

/**
* Determines if the given tool type draws closed-path "shapes" (i.e., elements which can be
* filled with paint.
*
* @return True if the tool draws shapes; false otherwise.
*/
public boolean isShapeTool() {
return this == RECTANGLE ||
this == ROUND_RECTANGLE ||
this == OVAL ||
this == POLYGON ||
this == SHAPE;
this == SHAPE ||
this == FREEFORM;
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/defano/jmonet/tools/FreeformShapeTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.defano.jmonet.tools;

import com.defano.jmonet.model.PaintToolType;
import com.defano.jmonet.tools.base.AbstractPathTool;

import java.awt.*;
import java.awt.geom.Path2D;

/**
* Tool allowing user to draw a free-form path (like a paintbrush) that is closed upon completion
* and can thusly be filled with paint.
*/
public class FreeformShapeTool extends AbstractPathTool {

private Path2D path;

public FreeformShapeTool() {
super(PaintToolType.FREEFORM);
}

@Override
protected void startPath(Graphics2D g, Stroke stroke, Paint paint, Point initialPoint) {
path = new Path2D.Double();
path.moveTo(initialPoint.getX(), initialPoint.getY());
}

@Override
protected void addPoint(Graphics2D g, Stroke stroke, Paint paint, Point point) {
path.lineTo(point.getX(), point.getY());

g.setStroke(stroke);
g.setPaint(getStrokePaint());
g.draw(path);
}

@Override
protected void completePath(Graphics2D g, Stroke stroke, Paint paint) {
path.closePath();

if (getFillPaint() != null) {
g.setPaint(getFillPaint());
g.fill(path);
}

g.setStroke(stroke);
g.setPaint(getStrokePaint());
g.draw(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class AbstractPathTool extends PaintTool {

protected abstract void startPath(Graphics2D g, Stroke stroke, Paint paint, Point initialPoint);
protected abstract void addPoint(Graphics2D g, Stroke stroke, Paint paint, Point point);
protected void completePath(Graphics2D g, Stroke stroke, Paint paint) {}

public AbstractPathTool(PaintToolType type) {
super(type);
Expand All @@ -41,6 +42,10 @@ public void mouseDragged(MouseEvent e, Point imageLocation) {

@Override
public void mouseReleased(MouseEvent e, Point imageLocation) {
Graphics2D g2d = (Graphics2D) getCanvas().getScratchImage().getGraphics();
completePath(g2d, getStroke(), getFillPaint());
g2d.dispose();

getCanvas().commit(new ChangeSet(getCanvas().getScratchImage(), getComposite()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public PaintTool build() {
case MAGNIFIER:
selectedTool = new MagnifierTool();
break;
case FREEFORM:
selectedTool = new FreeformShapeTool();
break;

default:
throw new RuntimeException("Bug! Unimplemented builder for tool " + type);
Expand Down

0 comments on commit d010823

Please sign in to comment.