Add change color action

This commit is contained in:
Thibaud Gasser 2025-02-19 11:39:50 +01:00
parent aea90f5a93
commit 1b0284cb4d
2 changed files with 31 additions and 9 deletions

12
pom.xml
View File

@ -9,6 +9,18 @@
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
<groupId>ovh.gasser</groupId>
<artifactId>shapes</artifactId>

View File

@ -3,9 +3,11 @@ package ovh.gasser.newshapes.ui;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ovh.gasser.newshapes.Selection;
import ovh.gasser.newshapes.attributes.ColorAttributes;
import ovh.gasser.newshapes.shapes.SCollection;
import ovh.gasser.newshapes.shapes.Shape;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
@ -69,18 +71,26 @@ public class Controller {
private void handleKeyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DELETE:
deleteSelected();
break;
case KeyEvent.VK_C:
copySelection();
break;
default:
logger.warn("Pressed unhandled key: {}", evt.getKeyChar());
break;
case KeyEvent.VK_DELETE -> deleteSelected();
case KeyEvent.VK_C -> copySelection();
case KeyEvent.VK_A -> changeSelectionColor();
default -> logger.warn("Pressed unhandled key: {}", evt.getKeyChar());
}
}
private void changeSelectionColor(){
if (selection == null) {
logger.debug("No selection to change color of");
return;
}
if (selection.shape instanceof SCollection collection) {
collection.forEach(shape -> shape.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000)))));
} else {
selection.shape.addAttributes(new ColorAttributes(false, true, Color.BLACK, new Color((int) (Math.random() * 0x1000000))));
}
view.repaint();
}
private void copySelection() {
if (selection == null) {
logger.debug("No selection to copy");