简体   繁体   English

在较小的JFrame / Applet上显示大图像

[英]Big image on smaller JFrame/Applet

I have a pretty big image that I want to display on my JFrame and applet, the JFrame/Applet is 765x503 in size but the image is way larger. 我有一个很大的图像要显示在我的JFrame和Applet上,JFrame / Applet的尺寸为765x503,但图像要大得多。 I need a way to display the image in real size. 我需要一种以实际尺寸显示图像的方法。 How can I make it so I can move the screen around to display some of the image and be able to move around the image? 我该如何做才能使屏幕四处移动以显示某些图像并能够在图像四处移动?

I want to be able to drag the screen around, not use Scrollbars. 我希望能够拖动屏幕,而不要使用滚动条。

JScrollPane可能是您需要的。.添加垂直和水平滚动

The basic idea is you need to provide some kind of offset that should be applied to the image to be drawn. 基本思想是您需要提供某种应应用于要绘制的图像的偏移量。

When the user presses the mouse button at a point on the image, you record the click point. 当用户在图像上的某个点按下鼠标按钮时,将记录单击点。 When the drag the mouse, you delta the difference. 拖动鼠标时,您会产生差异。 This gives you the amount the offset is to be moved. 这给您要偏移的量。 Based on the original position, you simply add this difference to the and generate a new offset. 根据原始位置,您只需将此差异添加到并生成一个新的偏移量。

There are other ways to do it, but generally, this is the method I use. 还有其他方法可以执行此操作,但是通常,这是我使用的方法。

I've also added code to check to see if the drag would put the image out of bounds of the viewable area, you'll have to decide if you want to use it... 我还添加了代码来检查拖动是否会使图像超出可见区域的边界,您必须确定是否要使用它...

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BigImage {

    public static void main(String[] args) {
        new BigImage();
    }

    public BigImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage bg;
        private Point offset = new Point(0, 0);

        public TestPane() {
            try {
                bg = ImageIO.read(new File("/path/to/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            MouseAdapter ma = new MouseAdapter() {
                private Point clickPoint;
                private Point origin;

                @Override
                public void mousePressed(MouseEvent e) {
                    setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                    clickPoint = e.getPoint();
                    origin = new Point(offset);
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    setCursor(Cursor.getDefaultCursor());
                    clickPoint = null;
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    int x = e.getPoint().x - clickPoint.x;
                    int y = e.getPoint().y - clickPoint.y;
                    offset.x = origin.x + x;
                    offset.y = origin.y + y;

                    if (offset.x > 0) {
                        offset.x = 0;
                    }
                    if (offset.x + bg.getWidth() < getWidth()) {
                        offset.x = -bg.getWidth() + getWidth();
                    }

                    if (offset.y > 0) {
                        offset.y = 0;
                    }
                    if (offset.y + bg.getHeight() < getHeight()) {
                        offset.y = -bg.getHeight() + getHeight();
                    }
                    repaint();
                }
            };

            addMouseListener(ma);
            addMouseMotionListener(ma);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(bg, offset.x, offset.y, this);
                g2d.dispose();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM