|
@@ -2,12 +2,16 @@ package com.mes.ui;
|
|
|
|
|
|
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.ImageIcon;
|
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPanel;
|
|
|
|
|
+import javax.swing.Scrollable;
|
|
|
import java.awt.Dimension;
|
|
import java.awt.Dimension;
|
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics;
|
|
|
|
|
+import java.awt.Graphics2D;
|
|
|
import java.awt.Image;
|
|
import java.awt.Image;
|
|
|
|
|
+import java.awt.Rectangle;
|
|
|
|
|
+import java.awt.RenderingHints;
|
|
|
|
|
|
|
|
-/** Displays an image at its native size so engineering annotations remain readable. */
|
|
|
|
|
-public class ImagePanel extends JPanel {
|
|
|
|
|
|
|
+/** Displays an image scaled proportionally to the available viewport. */
|
|
|
|
|
+public class ImagePanel extends JPanel implements Scrollable {
|
|
|
private final Image image;
|
|
private final Image image;
|
|
|
|
|
|
|
|
public ImagePanel(String resourcePath) {
|
|
public ImagePanel(String resourcePath) {
|
|
@@ -18,6 +22,46 @@ public class ImagePanel extends JPanel {
|
|
|
@Override
|
|
@Override
|
|
|
protected void paintComponent(Graphics graphics) {
|
|
protected void paintComponent(Graphics graphics) {
|
|
|
super.paintComponent(graphics);
|
|
super.paintComponent(graphics);
|
|
|
- graphics.drawImage(image, 0, 0, this);
|
|
|
|
|
|
|
+ int imageWidth = image.getWidth(this);
|
|
|
|
|
+ int imageHeight = image.getHeight(this);
|
|
|
|
|
+ if (imageWidth <= 0 || imageHeight <= 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ double scale = Math.min((double) getWidth() / imageWidth, (double) getHeight() / imageHeight);
|
|
|
|
|
+ int drawWidth = Math.max(1, (int) Math.round(imageWidth * scale));
|
|
|
|
|
+ int drawHeight = Math.max(1, (int) Math.round(imageHeight * scale));
|
|
|
|
|
+ int x = (getWidth() - drawWidth) / 2;
|
|
|
|
|
+ int y = (getHeight() - drawHeight) / 2;
|
|
|
|
|
+
|
|
|
|
|
+ Graphics2D graphics2D = (Graphics2D) graphics.create();
|
|
|
|
|
+ graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
|
+ graphics2D.drawImage(image, x, y, drawWidth, drawHeight, this);
|
|
|
|
|
+ graphics2D.dispose();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Dimension getPreferredScrollableViewportSize() {
|
|
|
|
|
+ return getPreferredSize();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
|
|
|
|
|
+ return 24;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
|
|
|
|
|
+ return orientation == javax.swing.SwingConstants.VERTICAL ? visibleRect.height : visibleRect.width;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean getScrollableTracksViewportWidth() {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean getScrollableTracksViewportHeight() {
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|