newDrawing Button
This commit is contained in:
parent
e15cc147c2
commit
e3f9143a85
33
src/main/java/de/ph87/kindermalen/Environment.java
Normal file
33
src/main/java/de/ph87/kindermalen/Environment.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package de.ph87.kindermalen;
|
||||||
|
|
||||||
|
import de.ph87.kindermalen.drawing.Drawing;
|
||||||
|
import de.ph87.kindermalen.tools.Tools;
|
||||||
|
import de.ph87.kindermalen.util.Publisher;
|
||||||
|
import de.ph87.kindermalen.util.Subscription;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Environment {
|
||||||
|
|
||||||
|
private final Publisher<Drawing> onNewDrawing = new Publisher<>();
|
||||||
|
|
||||||
|
private final Tools tools = new Tools();
|
||||||
|
|
||||||
|
private Drawing drawing = null;
|
||||||
|
|
||||||
|
public Environment() {
|
||||||
|
newDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newDrawing() {
|
||||||
|
drawing = new Drawing(1920, 1080);
|
||||||
|
onNewDrawing.publish(drawing);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Subscription<Drawing> onNewDrawing(final Consumer<Drawing> next) {
|
||||||
|
return onNewDrawing.subscribe(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,11 +1,11 @@
|
|||||||
package de.ph87.kindermalen;
|
package de.ph87.kindermalen;
|
||||||
|
|
||||||
import de.ph87.kindermalen.tools.Tools;
|
|
||||||
import de.ph87.kindermalen.tools.ToolsPanel;
|
import de.ph87.kindermalen.tools.ToolsPanel;
|
||||||
import de.ph87.kindermalen.tools.tool.Tool;
|
import de.ph87.kindermalen.tools.tool.Tool;
|
||||||
import de.ph87.kindermalen.tools.tool.ToolPanel;
|
import de.ph87.kindermalen.tools.tool.ToolPanel;
|
||||||
import de.ph87.kindermalen.tools.tool.stamp.StampPanel;
|
import de.ph87.kindermalen.tools.tool.stamp.StampPanel;
|
||||||
import de.ph87.kindermalen.tools.tool.stamp.StampTool;
|
import de.ph87.kindermalen.tools.tool.stamp.StampTool;
|
||||||
|
import de.ph87.kindermalen.util.MouseListener;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -17,11 +17,20 @@ public class Sidebar extends JPanel {
|
|||||||
|
|
||||||
private ToolPanel toolPanel = null;
|
private ToolPanel toolPanel = null;
|
||||||
|
|
||||||
public Sidebar(final Tools tools) {
|
private StampPanel stamp;
|
||||||
|
|
||||||
|
public Sidebar(final Environment environment) {
|
||||||
setPreferredSize(new Dimension(300, 0));
|
setPreferredSize(new Dimension(300, 0));
|
||||||
setLayout(new GridBagLayout());
|
setLayout(new GridBagLayout());
|
||||||
add(new ToolsPanel(tools), C(0, 0, 1, 0.1));
|
|
||||||
tools.onToolSelected(this::onToolSelected);
|
final JButton newDrawing = new JButton("Neues Bild");
|
||||||
|
MouseListener.onPress(newDrawing, e -> environment.newDrawing());
|
||||||
|
add(newDrawing, C(0, 0, 1, 0));
|
||||||
|
|
||||||
|
final ToolsPanel toolsPanel = new ToolsPanel(environment.getTools());
|
||||||
|
add(toolsPanel, C(0, 1, 1, 0.1));
|
||||||
|
|
||||||
|
environment.getTools().onToolSelected(this::onToolSelected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -35,16 +44,20 @@ public class Sidebar extends JPanel {
|
|||||||
|
|
||||||
private void onToolSelected(final Tool tool) {
|
private void onToolSelected(final Tool tool) {
|
||||||
if (toolPanel != null) {
|
if (toolPanel != null) {
|
||||||
remove(toolPanel);
|
toolPanel.setVisible(false);
|
||||||
toolPanel = null;
|
toolPanel = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tool instanceof StampTool) {
|
if (tool instanceof StampTool) {
|
||||||
toolPanel = new StampPanel((StampTool) tool);
|
if (stamp == null) {
|
||||||
|
stamp = new StampPanel((StampTool) tool);
|
||||||
|
add(stamp, C(0, 2, 1, 0.9));
|
||||||
|
}
|
||||||
|
toolPanel = stamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toolPanel != null) {
|
if (toolPanel != null) {
|
||||||
add(toolPanel, C(0, 1, 1, 0.9));
|
toolPanel.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
repaint();
|
repaint();
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package de.ph87.kindermalen;
|
package de.ph87.kindermalen;
|
||||||
|
|
||||||
import de.ph87.kindermalen.drawing.Drawing;
|
|
||||||
import de.ph87.kindermalen.drawing.DrawingPanel;
|
import de.ph87.kindermalen.drawing.DrawingPanel;
|
||||||
import de.ph87.kindermalen.tools.Tools;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -11,40 +10,36 @@ import java.awt.event.KeyEvent;
|
|||||||
|
|
||||||
import static de.ph87.kindermalen.util.MyGridBagConstraints.C;
|
import static de.ph87.kindermalen.util.MyGridBagConstraints.C;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame {
|
||||||
|
|
||||||
private final Drawing drawing = new Drawing(1920, 1080);
|
private static final int SCREEN = 1;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public Window(final Environment environment) {
|
||||||
final Tools tools = new Tools();
|
|
||||||
final Window window = new Window(tools);
|
|
||||||
window.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Window(final Tools tools) {
|
|
||||||
setLayout(new GridBagLayout());
|
setLayout(new GridBagLayout());
|
||||||
|
|
||||||
final Sidebar sidebar = new Sidebar(tools);
|
final Sidebar sidebar = new Sidebar(environment);
|
||||||
add(sidebar, C(0, 0, 0, 1.0));
|
add(sidebar, C(0, 0, 0, 1.0));
|
||||||
|
|
||||||
final DrawingPanel drawingPanel = new DrawingPanel(tools, drawing);
|
final DrawingPanel drawingPanel = new DrawingPanel(environment);
|
||||||
add(drawingPanel, C(1, 0, 1, 1.0));
|
add(drawingPanel, C(1, 0, 1, 1.0));
|
||||||
|
|
||||||
pack();
|
pack();
|
||||||
|
|
||||||
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
|
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
|
||||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
setWindowPosition();
|
||||||
|
|
||||||
KeyboardFocusManager.getCurrentKeyboardFocusManager()
|
KeyboardFocusManager.getCurrentKeyboardFocusManager()
|
||||||
.addKeyEventDispatcher(
|
.addKeyEventDispatcher(
|
||||||
keyEvent -> {
|
keyEvent -> {
|
||||||
if (keyEvent.getID() == KeyEvent.KEY_PRESSED && keyEvent.getKeyCode() == KeyEvent.VK_Z) {
|
if (keyEvent.getID() == KeyEvent.KEY_PRESSED && keyEvent.getKeyCode() == KeyEvent.VK_Z) {
|
||||||
if (keyEvent.getModifiersEx() == InputEvent.CTRL_DOWN_MASK) {
|
if (keyEvent.getModifiersEx() == InputEvent.CTRL_DOWN_MASK) {
|
||||||
drawing.undo();
|
environment.getDrawing().undo();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (keyEvent.getModifiersEx() == InputEvent.SHIFT_DOWN_MASK + InputEvent.CTRL_DOWN_MASK) {
|
if (keyEvent.getModifiersEx() == InputEvent.SHIFT_DOWN_MASK + InputEvent.CTRL_DOWN_MASK) {
|
||||||
drawing.redo();
|
environment.getDrawing().redo();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,4 +48,26 @@ public class Window extends JFrame {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setWindowPosition() {
|
||||||
|
final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
final GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
|
||||||
|
final GraphicsDevice graphicsDevice;
|
||||||
|
if (SCREEN < graphicsDevices.length) {
|
||||||
|
graphicsDevice = graphicsDevices[SCREEN];
|
||||||
|
} else if (graphicsDevices.length > 0) {
|
||||||
|
graphicsDevice = graphicsDevices[0];
|
||||||
|
} else {
|
||||||
|
log.error("No Screens Found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Rectangle bounds = graphicsDevice.getDefaultConfiguration().getBounds();
|
||||||
|
setLocation(bounds.x, bounds.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final Environment environment = new Environment();
|
||||||
|
final Window window = new Window(environment);
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,7 @@ public class Drawing {
|
|||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
current = new Layer(width, height);
|
current = new Layer(width, height);
|
||||||
|
log.info("New Drawing: {}", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Graphics2D getGraphics() {
|
public Graphics2D getGraphics() {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package de.ph87.kindermalen.drawing;
|
package de.ph87.kindermalen.drawing;
|
||||||
|
|
||||||
import de.ph87.kindermalen.tools.Tools;
|
import de.ph87.kindermalen.Environment;
|
||||||
import de.ph87.kindermalen.tools.tool.Tool;
|
import de.ph87.kindermalen.tools.tool.Tool;
|
||||||
import de.ph87.kindermalen.util.MouseListener;
|
import de.ph87.kindermalen.util.MouseListener;
|
||||||
import de.ph87.kindermalen.util.Subscription;
|
import de.ph87.kindermalen.util.Subscription;
|
||||||
@ -24,9 +24,7 @@ public class DrawingPanel extends JPanel {
|
|||||||
|
|
||||||
private static final File SAVE_DIR = new File("./data/images");
|
private static final File SAVE_DIR = new File("./data/images");
|
||||||
|
|
||||||
private final Tools tools;
|
private final Environment environment;
|
||||||
|
|
||||||
private final Drawing drawing;
|
|
||||||
|
|
||||||
private Subscription<Tool> toolSubscription = null;
|
private Subscription<Tool> toolSubscription = null;
|
||||||
|
|
||||||
@ -38,20 +36,20 @@ public class DrawingPanel extends JPanel {
|
|||||||
|
|
||||||
private AffineTransform inverseTransform;
|
private AffineTransform inverseTransform;
|
||||||
|
|
||||||
public DrawingPanel(final Tools tools, final Drawing drawing) {
|
public DrawingPanel(final Environment environment) {
|
||||||
this.tools = tools;
|
this.environment = environment;
|
||||||
this.drawing = drawing;
|
|
||||||
MouseListener.onPress(this, this::onPress);
|
MouseListener.onPress(this, this::onPress);
|
||||||
MouseListener.onRelease(this, this::onRelease);
|
MouseListener.onRelease(this, this::onRelease);
|
||||||
MouseListener.onMove(this, this::onMove);
|
MouseListener.onMove(this, this::onMove);
|
||||||
MouseListener.onDrag(this, this::onDrag);
|
MouseListener.onDrag(this, this::onDrag);
|
||||||
tools.onToolSelected(tool -> {
|
environment.getTools().onToolSelected(tool -> {
|
||||||
repaint();
|
repaint();
|
||||||
if (toolSubscription != null) {
|
if (toolSubscription != null) {
|
||||||
toolSubscription.unsubscribe();
|
toolSubscription.unsubscribe();
|
||||||
}
|
}
|
||||||
toolSubscription = tool.onChange(ignore -> repaint());
|
toolSubscription = tool.onChange(ignore -> repaint());
|
||||||
});
|
});
|
||||||
|
environment.onNewDrawing(drawing -> repaint());
|
||||||
|
|
||||||
addComponentListener(new ComponentListener() {
|
addComponentListener(new ComponentListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -75,7 +73,7 @@ public class DrawingPanel extends JPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void calculateTransform() {
|
private void calculateTransform() {
|
||||||
final Image image = drawing.getImage();
|
final Image image = environment.getDrawing().getImage();
|
||||||
final Rectangle box = fitInside(image.getWidth(null), image.getHeight(null), getWidth(), getHeight(), false);
|
final Rectangle box = fitInside(image.getWidth(null), image.getHeight(null), getWidth(), getHeight(), false);
|
||||||
final double scale = (double) box.width / image.getWidth(null);
|
final double scale = (double) box.width / image.getWidth(null);
|
||||||
transform = new AffineTransform();
|
transform = new AffineTransform();
|
||||||
@ -96,17 +94,17 @@ public class DrawingPanel extends JPanel {
|
|||||||
|
|
||||||
private void onPress(final MouseEvent mouseEvent) {
|
private void onPress(final MouseEvent mouseEvent) {
|
||||||
if (mouseEvent.getButton() == 3) {
|
if (mouseEvent.getButton() == 3) {
|
||||||
drawing.undo();
|
environment.getDrawing().undo();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
drawing.newRevision();
|
environment.getDrawing().newRevision();
|
||||||
lastPoint = tools.getTool().apply(null, drawing, transform(mouseEvent.getPoint()));
|
lastPoint = environment.getTools().getTool().apply(null, environment.getDrawing(), transform(mouseEvent.getPoint()));
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onDrag(final MouseEvent mouseEvent) {
|
private void onDrag(final MouseEvent mouseEvent) {
|
||||||
cursor = transform(mouseEvent.getPoint());
|
cursor = transform(mouseEvent.getPoint());
|
||||||
lastPoint = tools.getTool().apply(lastPoint, drawing, transform(mouseEvent.getPoint()));
|
lastPoint = environment.getTools().getTool().apply(lastPoint, environment.getDrawing(), transform(mouseEvent.getPoint()));
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +115,7 @@ public class DrawingPanel extends JPanel {
|
|||||||
|
|
||||||
private void onRelease(final MouseEvent mouseEvent) {
|
private void onRelease(final MouseEvent mouseEvent) {
|
||||||
try {
|
try {
|
||||||
drawing.save(SAVE_DIR);
|
environment.getDrawing().save(SAVE_DIR);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
log.error(ex.toString());
|
log.error(ex.toString());
|
||||||
}
|
}
|
||||||
@ -130,17 +128,17 @@ public class DrawingPanel extends JPanel {
|
|||||||
g.fillRect(0, 0, getWidth(), getHeight());
|
g.fillRect(0, 0, getWidth(), getHeight());
|
||||||
g.setTransform(transform);
|
g.setTransform(transform);
|
||||||
|
|
||||||
if (drawing == null) {
|
if (environment == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Image image = drawing.getImage();
|
final Image image = environment.getDrawing().getImage();
|
||||||
|
|
||||||
g.setColor(Color.white);
|
g.setColor(Color.white);
|
||||||
g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
|
g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
|
||||||
g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
|
g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
|
||||||
|
|
||||||
final BufferedImage preview = tools.getTool().getPreview();
|
final BufferedImage preview = environment.getTools().getTool().getPreview();
|
||||||
if (cursor != null && preview != null) {
|
if (cursor != null && preview != null) {
|
||||||
g.drawImage(preview, cursor.intX() - preview.getWidth() / 2, cursor.intY() - preview.getHeight() / 2, null);
|
g.drawImage(preview, cursor.intX() - preview.getWidth() / 2, cursor.intY() - preview.getHeight() / 2, null);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user