-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
3,652 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.class | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
.idea/* | ||
*.iml | ||
target/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<packaging>jar</packaging> | ||
<groupId>com.defano</groupId> | ||
<artifactId>jmonet</artifactId> | ||
<version>0.0.1</version> | ||
|
||
</project> |
122 changes: 122 additions & 0 deletions
122
src/main/java/com/defano/jmonet/canvas/BasicCanvas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.defano.jmonet.canvas; | ||
|
||
import com.defano.jmonet.canvas.surface.AbstractSurface; | ||
import com.defano.jmonet.model.Provider; | ||
|
||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
|
||
public class BasicCanvas extends AbstractSurface implements Canvas { | ||
|
||
private Point imageLocation = new Point(0, 0); | ||
private Provider<Double> scale = new Provider<>(1.0); | ||
private Provider<Integer> gridSpacing = new Provider<>(1); | ||
|
||
private BufferedImage image; | ||
private BufferedImage scratch; | ||
|
||
public BasicCanvas() { | ||
this(null); | ||
} | ||
|
||
public BasicCanvas(BufferedImage initialImage) { | ||
|
||
if (initialImage == null) { | ||
image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); | ||
} else { | ||
image = initialImage; | ||
} | ||
|
||
scratch = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); | ||
} | ||
|
||
public void clearCanvas() { | ||
Graphics2D g2 = (Graphics2D) getScratchImage().getGraphics(); | ||
g2.setColor(Color.WHITE); | ||
g2.fillRect(0, 0, getWidth(), getHeight()); | ||
commit(AlphaComposite.getInstance(AlphaComposite.DST_OUT, 1.0f)); | ||
} | ||
|
||
protected void overlayImage(BufferedImage source, BufferedImage destination, AlphaComposite composite) { | ||
|
||
Graphics2D g2d = (Graphics2D) destination.getGraphics(); | ||
g2d.setComposite(composite); | ||
g2d.drawImage(source, 0,0, null); | ||
g2d.dispose(); | ||
} | ||
|
||
public BufferedImage getCanvasImage() { | ||
return image; | ||
} | ||
|
||
public BufferedImage getScratchImage() { | ||
return scratch; | ||
} | ||
|
||
@Override | ||
public void setCanvasImage(BufferedImage image) { | ||
this.image = image; | ||
} | ||
|
||
@Override | ||
public void setScratchImage(BufferedImage image) { | ||
this.scratch = image; | ||
} | ||
|
||
public void commit() { | ||
commit(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); | ||
} | ||
|
||
/** | ||
* Copies the image contents of the scratch buffer to the Canvas' image. | ||
*/ | ||
public void commit(AlphaComposite composite) { | ||
BufferedImage scratchImage = getScratchImage(); | ||
BufferedImage canvasImage = getCanvasImage(); | ||
|
||
overlayImage(scratchImage, canvasImage, composite); | ||
fireObservers(this, scratchImage, canvasImage); | ||
|
||
clearScratch(); | ||
invalidateCanvas(); | ||
} | ||
|
||
/** | ||
* Creates a clean scratch buffer (replacing all pixels in the graphics context with transparent pixels). | ||
*/ | ||
public void clearScratch() { | ||
scratch = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); | ||
} | ||
|
||
@Override | ||
public void setImageLocation(Point location) { | ||
imageLocation = location; | ||
} | ||
|
||
@Override | ||
public Point getImageLocation() { | ||
return imageLocation; | ||
} | ||
|
||
@Override | ||
public Provider<Double> getScaleProvider() { | ||
return scale; | ||
} | ||
|
||
@Override | ||
public void setGridSpacing(int grid) { | ||
this.gridSpacing.set(grid); | ||
} | ||
|
||
@Override | ||
public Provider<Integer> getGridSpacingProvider() { | ||
return gridSpacing; | ||
} | ||
|
||
@Override | ||
public void setScale(double scale) { | ||
this.scale.set(scale); | ||
invalidateCanvas(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.defano.jmonet.canvas; | ||
|
||
import com.defano.jmonet.canvas.surface.PaintableSurface; | ||
|
||
import java.awt.*; | ||
|
||
public interface Canvas extends PaintableSurface { | ||
boolean isVisible(); | ||
|
||
void setCursor(Cursor cursor); | ||
|
||
void addObserver(CanvasCommitObserver observer); | ||
boolean removeObserver(CanvasCommitObserver observer); | ||
|
||
void invalidateCanvas(); | ||
|
||
void commit(); | ||
void commit(AlphaComposite composite); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/defano/jmonet/canvas/CanvasCommitObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.defano.jmonet.canvas; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
public interface CanvasCommitObserver { | ||
/** | ||
* Fires when an new shape or image is committed from scratch onto the canvas. | ||
* | ||
* @param canvas The canvas on which the commit is occurring. | ||
* @param committedElement An image representing just the change being committed. | ||
* @param canvasImage The resulting canvas image (including the committed change) | ||
*/ | ||
void onCommit(Canvas canvas, BufferedImage committedElement, BufferedImage canvasImage); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/defano/jmonet/canvas/CanvasInteractionObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.defano.jmonet.canvas; | ||
|
||
import java.awt.event.*; | ||
|
||
public interface CanvasInteractionObserver extends KeyListener { | ||
|
||
/** | ||
* Invoked when the mouse button has been clicked (pressed | ||
* and released) on the canvas. | ||
*/ | ||
void mouseClicked(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when a mouse button has been pressed on a component. | ||
*/ | ||
void mousePressed(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when a mouse button has been released on a component. | ||
*/ | ||
void mouseReleased(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when the mouse enters a component. | ||
*/ | ||
void mouseEntered(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when the mouse exits a component. | ||
*/ | ||
void mouseExited(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when a mouse button is pressed on a component and then | ||
* dragged. <code>MOUSE_DRAGGED</code> events will continue to be | ||
* delivered to the component where the drag originated until the | ||
* mouse button is released (regardless of whether the mouse position | ||
* is within the bounds of the component). | ||
* <p> | ||
* Due to platform-dependent Drag&Drop implementations, | ||
* <code>MOUSE_DRAGGED</code> events may not be delivered during a native | ||
* Drag&Drop operation. | ||
*/ | ||
void mouseDragged(MouseEvent e, int scaleX, int scaleY); | ||
|
||
/** | ||
* Invoked when the mouse cursor has been moved onto a component | ||
* but no buttons have been pushed. | ||
*/ | ||
void mouseMoved(MouseEvent e, int scaleX, int scaleY); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.defano.jmonet.canvas; | ||
|
||
import javafx.application.Platform; | ||
import javafx.embed.swing.SwingNode; | ||
|
||
public class JFXCanvasNode extends SwingNode { | ||
|
||
private final BasicCanvas canvas; | ||
|
||
public JFXCanvasNode(BasicCanvas canvas) { | ||
this.canvas = canvas; | ||
Platform.runLater(() -> JFXCanvasNode.super.setContent(canvas)); | ||
} | ||
|
||
public Canvas getCanvas() { | ||
return canvas; | ||
} | ||
|
||
} |
Oops, something went wrong.