Skip to content

Commit

Permalink
show panel zoomed down and with crosshair
Browse files Browse the repository at this point in the history
  • Loading branch information
CHeuberger committed Jan 23, 2024
1 parent 4351a37 commit c6d4364
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 25 deletions.
8 changes: 8 additions & 0 deletions Puzzle Main.launch
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<setAttribute key="name.schedenig.eclipse.grepconsole.DisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.EnabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.FilterDisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.FilterEnabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.StatisticsDisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.StatisticsEnabledIds"/>
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/Puzzle/src/java/cfh/puzzle/Main.java"/>
</listAttribute>
Expand All @@ -11,6 +18,7 @@
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="cfh.puzzle.Main"/>
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="Puzzle"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="Puzzle"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-splash:cfh/puzzle/resources/splash.png"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:Puzzle/build/classes}"/>
Expand Down
50 changes: 25 additions & 25 deletions src/java/cfh/puzzle/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,8 +64,6 @@ public class GamePanel extends JPanel implements GameListener {

private JMenuItem showMenuItem;

private JDialog preview = null;

private final List<Piece> pieces = new ArrayList<>();

private static final Integer KEY_HISTORY = KeyEvent.VK_BACK_SPACE;
Expand Down Expand Up @@ -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);
}
Expand Down
64 changes: 64 additions & 0 deletions src/java/cfh/puzzle/ShowPanel.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit c6d4364

Please sign in to comment.