diff --git a/Puzzle Main.launch b/Puzzle Main.launch index 64b8d49..0198184 100644 --- a/Puzzle Main.launch +++ b/Puzzle Main.launch @@ -1,5 +1,12 @@ + + + + + + + @@ -11,6 +18,7 @@ + diff --git a/src/java/cfh/puzzle/GamePanel.java b/src/java/cfh/puzzle/GamePanel.java index 11d98bf..85060b5 100644 --- a/src/java/cfh/puzzle/GamePanel.java +++ b/src/java/cfh/puzzle/GamePanel.java @@ -16,7 +16,6 @@ import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; @@ -65,8 +64,6 @@ public class GamePanel extends JPanel implements GameListener { private JMenuItem showMenuItem; - private JDialog preview = null; - private final List pieces = new ArrayList<>(); private static final Integer KEY_HISTORY = KeyEvent.VK_BACK_SPACE; @@ -390,30 +387,33 @@ private void doArrange(ActionEvent ev) { protected void doShow(ActionEvent ev) { if (image != null) { - if (preview == null) { - Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); - ImageIcon icon; - if (image.getWidth() > screen.width-20 || image.getHeight() > screen.height-100) { - double w = (double) image.getWidth() / (screen.width-20); - double h = (double) image.getHeight() / (screen.height-100); - double scale = Math.max(w, h); - System.out.println(scale); - icon = new ImageIcon(image.getScaledInstance((int)(image.getWidth()/scale), (int)(image.getHeight()/scale), Image.SCALE_SMOOTH)); - } else { - icon = new ImageIcon(image); - } - JLabel msg = new JLabel(icon); - preview = new JDialog(SwingUtilities.windowForComponent(this)); - preview.setTitle("Preview - " + title); - preview.setModal(false); - preview.add(new JScrollPane(msg)); - msg.addMouseListener(new MouseAdapter() { - @Override - public void mousePressed(MouseEvent ev) { + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + double scale; + if (image.getWidth() > screen.width-20 || image.getHeight() > screen.height-100) { + double w = (double) image.getWidth() / (screen.width-20); + double h = (double) image.getHeight() / (screen.height-100); + scale = Math.max(w, h); + } else { + scale = 1.0; + } + if (ev ==null || (ev.getModifiers() & ev.CTRL_MASK) == 0) { + scale *= 2; + } + + ShowPanel panel = new ShowPanel(scale, image); + JDialog preview = new JDialog(SwingUtilities.windowForComponent(this)); + preview.setTitle("Preview - " + title); + preview.setModal(false); + preview.add(new JScrollPane(panel)); + panel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent ev) { + if (ev.getButton() == ev.BUTTON1) { preview.setVisible(false); + preview.dispose(); } - }); - } + } + }); preview.pack(); preview.setVisible(true); } diff --git a/src/java/cfh/puzzle/ShowPanel.java b/src/java/cfh/puzzle/ShowPanel.java new file mode 100644 index 0000000..bbffe64 --- /dev/null +++ b/src/java/cfh/puzzle/ShowPanel.java @@ -0,0 +1,64 @@ +/* + * Copyright: Carlos F. Heuberger. All rights reserved. + * + */ +package cfh.puzzle; + +import static java.util.Objects.requireNonNull; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.JPanel; + +/** + * @author Carlos F. Heuberger, 2024-01-23 + * + */ +public class ShowPanel extends JPanel { + + private final double scale; + private final Image image; + + private Point crosshair = null; + + public ShowPanel(double scale, Image image) { + assert scale > 0 : scale; + this.scale = scale; + this.image = requireNonNull(image, "image"); + + setPreferredSize(new Dimension((int)(image.getWidth(this)/scale), (int)(image.getHeight(this)/scale))); + addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent ev) { + if (ev.getButton() == ev.BUTTON3) { + crosshair = crosshair==null ? ev.getPoint() : null; + repaint(); + } + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D gg = (Graphics2D) g; + gg.scale(1/scale, 1/scale); + gg.drawImage(image, 0, 0, this); + + if (crosshair != null) { + gg.setXORMode(Color.WHITE); + gg.scale(scale, scale); + int x = crosshair.x; + int y = crosshair.y; + gg.drawLine(0, y, getWidth(), y); + gg.drawLine(x, 0, x, getHeight()); + } + } +}