Refreshing StampButtonList when StampImage loaded
This commit is contained in:
parent
6b7dd83149
commit
6fff359ab1
14
src/main/java/de/ph87/kindermalen/Main.java
Normal file
14
src/main/java/de/ph87/kindermalen/Main.java
Normal file
@ -0,0 +1,14 @@
|
||||
package de.ph87.kindermalen;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Environment environment = new Environment();
|
||||
final Window window = new Window(environment);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
@ -17,8 +17,6 @@ public class Sidebar extends JPanel {
|
||||
|
||||
private ToolPanel toolPanel = null;
|
||||
|
||||
private StampPanel stamp;
|
||||
|
||||
public Sidebar(final Environment environment) {
|
||||
setPreferredSize(new Dimension(300, 0));
|
||||
setLayout(new GridBagLayout());
|
||||
@ -44,23 +42,20 @@ public class Sidebar extends JPanel {
|
||||
|
||||
private void onToolSelected(final Tool tool) {
|
||||
if (toolPanel != null) {
|
||||
toolPanel.setVisible(false);
|
||||
remove(toolPanel);
|
||||
toolPanel = null;
|
||||
}
|
||||
|
||||
if (tool instanceof StampTool) {
|
||||
if (stamp == null) {
|
||||
stamp = new StampPanel((StampTool) tool);
|
||||
add(stamp, C(0, 2, 1, 0.9));
|
||||
}
|
||||
toolPanel = stamp;
|
||||
toolPanel = new StampPanel((StampTool) tool);
|
||||
}
|
||||
|
||||
if (toolPanel != null) {
|
||||
add(toolPanel, C(0, 2, 1, 0.9));
|
||||
toolPanel.setVisible(true);
|
||||
}
|
||||
|
||||
repaint();
|
||||
revalidate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -64,10 +64,4 @@ public class Window extends JFrame {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ public class StampButton extends JPanel {
|
||||
private final StampTool tool;
|
||||
|
||||
@Getter
|
||||
private final Stamp stamp;
|
||||
private final StampImage image;
|
||||
|
||||
private final BufferedImage icon;
|
||||
|
||||
@ -23,19 +23,19 @@ public class StampButton extends JPanel {
|
||||
|
||||
private boolean hover = false;
|
||||
|
||||
public StampButton(final StampTool tool, final Stamp stamp) {
|
||||
public StampButton(final StampTool tool, final StampImage image) {
|
||||
this.tool = tool;
|
||||
this.stamp = stamp;
|
||||
this.image = image;
|
||||
setPreferredSize(new Dimension(STAMP_BUTTON_SIZE, STAMP_BUTTON_SIZE));
|
||||
MouseListener.onRelease(this, this::onRelease);
|
||||
MouseListener.onEnter(this, this::onEnter);
|
||||
MouseListener.onExit(this, this::onExit);
|
||||
icon = stamp.getSize(STAMP_BUTTON_ICON_REAL_SIZE);
|
||||
icon = image.getSize(STAMP_BUTTON_ICON_REAL_SIZE);
|
||||
position = new Point((STAMP_BUTTON_SIZE - icon.getWidth()) / 2, (STAMP_BUTTON_SIZE - icon.getHeight()) / 2);
|
||||
}
|
||||
|
||||
private void onRelease(final MouseEvent mouseEvent) {
|
||||
this.tool.setStamp(stamp);
|
||||
this.tool.setImage(image);
|
||||
this.tool.prepare();
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ public class StampButton extends JPanel {
|
||||
if (hover) {
|
||||
g2.setColor(Color.yellow);
|
||||
highlight = true;
|
||||
} else if (tool.getStamp() == stamp) {
|
||||
} else if (tool.getImage() == image) {
|
||||
g2.setColor(Color.magenta);
|
||||
highlight = true;
|
||||
} else {
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package de.ph87.kindermalen.tools.tool.stamp;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class StampButtonList extends JPanel {
|
||||
|
||||
public StampButtonList(final StampTool stampTool) {
|
||||
final FlowLayout flowLayout = new FlowLayout(FlowLayout.LEADING, 0, 0);
|
||||
setLayout(flowLayout);
|
||||
stampTool.onListChange(images -> add(stampTool, images));
|
||||
add(stampTool, stampTool.getImages());
|
||||
stampTool.onChange(ignore -> repaint());
|
||||
}
|
||||
|
||||
private void add(final StampTool stampTool, final List<StampImage> images) {
|
||||
removeAll();
|
||||
for (final StampImage image : images.stream().sorted(Comparator.comparing(StampImage::getFile)).toList()) {
|
||||
final StampButton button = new StampButton(stampTool, image);
|
||||
add(button);
|
||||
}
|
||||
revalidate();
|
||||
}
|
||||
|
||||
}
|
||||
@ -17,7 +17,7 @@ import static de.ph87.kindermalen.util.ImageHelper.RESIZE;
|
||||
@Slf4j
|
||||
@Getter
|
||||
@ToString
|
||||
public class Stamp {
|
||||
public class StampImage {
|
||||
|
||||
private final String name;
|
||||
|
||||
@ -27,7 +27,7 @@ public class Stamp {
|
||||
|
||||
private final Map<Integer, BufferedImage> sizes = new HashMap<>();
|
||||
|
||||
public Stamp(final File file) throws IOException {
|
||||
public StampImage(final File file) throws IOException {
|
||||
this.name = file.getName().substring(0, file.getName().lastIndexOf("."));
|
||||
this.file = file;
|
||||
this.original = ImageIO.read(file);
|
||||
@ -1,23 +0,0 @@
|
||||
package de.ph87.kindermalen.tools.tool.stamp;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
import static de.ph87.kindermalen.CONFIG.BORDERS;
|
||||
|
||||
@Slf4j
|
||||
public class StampListPanel extends JPanel {
|
||||
|
||||
public StampListPanel(final StampTool tool) {
|
||||
setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
|
||||
tool.getStamps().forEach(stamp -> add(new StampButton(tool, stamp)));
|
||||
tool.onChange(ignore -> repaint());
|
||||
repaint();
|
||||
if (BORDERS) {
|
||||
setBackground(Color.green.brighter().brighter());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,7 +5,7 @@ import java.awt.*;
|
||||
|
||||
import static de.ph87.kindermalen.CONFIG.BORDERS;
|
||||
|
||||
public class StampOptionsPanel extends JPanel {
|
||||
public class StampOptions extends JPanel {
|
||||
|
||||
private final JSlider sizeSlider = new JSlider(10, 1000, 100);
|
||||
|
||||
@ -13,7 +13,7 @@ public class StampOptionsPanel extends JPanel {
|
||||
|
||||
private final JSlider stepSlider = new JSlider(1, 1000, 25);
|
||||
|
||||
public StampOptionsPanel(final StampTool tool) {
|
||||
public StampOptions(final StampTool tool) {
|
||||
setLayout(new GridLayout(6, 1));
|
||||
|
||||
final JLabel sizeLabel = new JLabel("Größe:");
|
||||
@ -13,8 +13,8 @@ public class StampPanel extends ToolPanel {
|
||||
|
||||
public StampPanel(final StampTool tool) {
|
||||
setLayout(new GridBagLayout());
|
||||
add(new StampOptionsPanel(tool), C(0, 0, 1, 0, HORIZONTAL));
|
||||
add(new StampListPanel(tool), C(0, 1, 1, 1));
|
||||
add(new StampOptions(tool), C(0, 0, 1, 0, HORIZONTAL));
|
||||
add(new StampButtonList(tool), C(0, 1, 1, 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package de.ph87.kindermalen.tools.tool.stamp;
|
||||
import de.ph87.kindermalen.drawing.Drawing;
|
||||
import de.ph87.kindermalen.tools.tool.Tool;
|
||||
import de.ph87.kindermalen.util.Batch;
|
||||
import de.ph87.kindermalen.util.Publisher;
|
||||
import de.ph87.kindermalen.util.Subscription;
|
||||
import de.ph87.kindermalen.util.Vector;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -14,6 +16,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -28,10 +31,12 @@ public class StampTool extends Tool {
|
||||
|
||||
private static final File STAMPS_DIR = new File("./data/stamps");
|
||||
|
||||
private List<Stamp> stamps = new ArrayList<>();
|
||||
private final List<StampImage> images = new ArrayList<>();
|
||||
|
||||
private final Publisher<List<StampImage>> onListChange = new Publisher<List<StampImage>>();
|
||||
|
||||
@Setter
|
||||
private Stamp stamp = null;
|
||||
private StampImage image = null;
|
||||
|
||||
@Setter
|
||||
private int size = 100;
|
||||
@ -46,20 +51,21 @@ public class StampTool extends Tool {
|
||||
|
||||
public StampTool() {
|
||||
super("Stempel");
|
||||
final Stream<Supplier<Stamp>> runnables = SCAN_FILES(STAMPS_DIR).stream().map(stamp -> () -> StampTool.this.load(stamp));
|
||||
try {
|
||||
stamps = new Batch<>(runnables.toList()).join();
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e.toString());
|
||||
}
|
||||
if (stamp == null && !stamps.isEmpty()) {
|
||||
setStamp(stamps.get(0));
|
||||
final Stream<Supplier<StampImage>> runnables = SCAN_FILES(STAMPS_DIR).stream().map(stamp -> () -> load(stamp));
|
||||
new Batch<>(runnables.toList());
|
||||
if (image == null && !images.isEmpty()) {
|
||||
setImage(images.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
private Stamp load(final File file) {
|
||||
private StampImage load(final File file) {
|
||||
try {
|
||||
return new Stamp(file);
|
||||
final StampImage image = new StampImage(file);
|
||||
synchronized (images) {
|
||||
images.add(image);
|
||||
onListChange.publish(images);
|
||||
}
|
||||
return image;
|
||||
} catch (IOException e) {
|
||||
log.error(e.toString());
|
||||
return null;
|
||||
@ -67,12 +73,12 @@ public class StampTool extends Tool {
|
||||
}
|
||||
|
||||
public void prepare() {
|
||||
if (this.stamp == null || stamp.getOriginal() == null) {
|
||||
if (this.image == null || image.getOriginal() == null) {
|
||||
return;
|
||||
}
|
||||
prepared = RESIZE(stamp.getOriginal(), size, size);
|
||||
prepared = RESIZE(image.getOriginal(), size, size);
|
||||
ALPHA(prepared, alpha);
|
||||
log.info("Stamp prepared: {}", stamp.getName());
|
||||
log.info("Stamp prepared: {}", image.getName());
|
||||
onChange.publish(this);
|
||||
}
|
||||
|
||||
@ -101,4 +107,8 @@ public class StampTool extends Tool {
|
||||
return prepared;
|
||||
}
|
||||
|
||||
public Subscription<List<StampImage>> onListChange(final Consumer<List<StampImage>> next) {
|
||||
return onListChange.subscribe(next);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,8 +17,6 @@ public class Batch<T> {
|
||||
|
||||
private boolean stop = false;
|
||||
|
||||
private final List<T> results = new ArrayList<>();
|
||||
|
||||
public Batch(final List<Supplier<T>> suppliers) {
|
||||
this.suppliers = new ArrayList<>(suppliers);
|
||||
synchronized (threads) {
|
||||
@ -37,12 +35,11 @@ public class Batch<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> join() throws InterruptedException {
|
||||
public void join() throws InterruptedException {
|
||||
synchronized (threads) {
|
||||
while (!threads.isEmpty()) {
|
||||
threads.wait();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +52,6 @@ public class Batch<T> {
|
||||
return;
|
||||
}
|
||||
final T result = supplier.get();
|
||||
synchronized (results) {
|
||||
results.add(result);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
synchronized (threads) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user