【问题标题】:Moving a view port over a larger image; JLablel+JScrollPane在更大的图像上移动视口; JLabel+JScrollPane
【发布时间】:2013-11-15 17:17:45
【问题描述】:

我有一个JScrollPane m_jScrollPane,其中显示了一个JLabel m_jlImagem_jlImage 是一个屏幕截图,在用户最后一次点击屏幕的位置绘制了一个红点。我希望将m_jScrollPane 的查看区域移动(阅读滚动)m_jlImage 上的红点。 lastClick 是用户最后点击的位置,与m_jlImage 位于同一坐标中。

事实证明这比我想象的要困难。

我决定获取点击点的值与屏幕沿同一轴的整个长度的比率,并以与其最大值的相同比率滚动相应的滚动条。这似乎仅在屏幕上最后一次单击的点位于左上角时才有效。

当点击点位于屏幕边缘时,我不确定如何处理这种情况。这种情况会产生一个比例,导致滚动条以相同的比例滚动,但红点因为位于屏幕边缘而被滚动到视野之外。关于我该如何克服这个问题的任何建议?

    public void scrollViewToLastClick()
    {
        int clckH = lastClick.y;
        int clckW = lastClick.x;

        int picH = this.m_jlImage.getHeight();
        int picW = this.m_jlImage.getWidth();

        int ratW = (int)(m_jScrollPane.getWidth()*(double)clckW/(double)picW);
        int ratH = (int)(m_jScrollPane.getHeight()*(double)clckH/(double)picH);

        m_jScrollPane.getHorizontalScrollBar().setValue(ratW);
        m_jScrollPane.getVerticalScrollBar().setValue(ratH);
    }

【问题讨论】:

  • 为什么不直接使用JComponent#scrollRectToVisble呢?
  • 用户是在点击JLabel 还是在录制(以某种方式)实时屏幕?能否提供一个简单的可运行示例?
  • 我正在使用JNativeHook 捕获鼠标点击,当点击发生时,我会截屏并将信息放入Image 对象中,我在该对象的位置画一个小红点最后一次单击发生,然后我将 JLabel 的图标设置为由编辑图像制成的图标。然后我调用上面的方法将滚动窗格滚动到 JLabel 上的红点。
  • 我假设您正在缩放图像?
  • 不,我将图像与屏幕保持相同大小并从中创建一个图标,并希望向上/向下和向左/向右滚动滚动窗格以查看屏幕上的红点图标。

标签: java swing jscrollpane jlabel jviewport


【解决方案1】:

这是一个非常基本的示例。它使用图像文件并将其放置在滚动窗格中(以圆形方式)。

从那里,它只是使用 Swing Timer 随机生成点(在图像的范围内)。

每次生成新点时,我只需使用scrollToRectVisible,将我要渲染的点的位置和大小传递给它。这将确保新点(和点)在滚动窗格中可见。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ScrollTest {

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

    private JScrollPane scrollPane;
    private DesktopPane desktopPane;

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

                try {
                    desktopPane = new DesktopPane();
                    scrollPane = new JScrollPane(desktopPane);

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(scrollPane);
                    frame.setSize(desktopPane.getPreferredSize().width / 2, desktopPane.getPreferredSize().height / 2);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    public class DesktopPane extends JLayeredPane {

        private List<Point> points;

        public DesktopPane() throws IOException {
            points = new ArrayList<>(25);
            final BufferedImage img = ImageIO.read(new File("Desktop.jpg"));
            final JLabel desktop = new JLabel(new ImageIcon(img));
            final JPanel overlay = new JPanel() {

                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    int xOff = desktop.getX();
                    int yOff = desktop.getY();
                    int count = 0;
                    FontMetrics fm = g.getFontMetrics();
                    int height = fm.getHeight();
                    for (Point p : points) {
                        g.setColor(Color.RED);
                        String text = Integer.toString(++count);
                        int width = fm.stringWidth(text);
                        int radius = Math.max(width, height) + 5;
                        int x = xOff + p.x - radius / 2;
                        int y = yOff + p.y - radius / 2;
                        g.fillOval(x, y, radius, radius);
                        g.setColor(Color.WHITE);
                        x += (radius - width) / 2;
                        y += ((radius - height) / 2) + fm.getAscent();
                        g.drawString(text, x, y);
                    }
                }
            };
            overlay.setOpaque(false);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;

            add(desktop, gbc);
            add(overlay, gbc);

            setLayer(desktop, 0);
            setLayer(overlay, 5);

            overlay.setBorder(new LineBorder(Color.RED));

            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int x = (int) Math.round(Math.random() * img.getWidth());
                    int y = (int) Math.round(Math.random() * img.getHeight());
                    points.add(new Point(x, y));
                    repaint();

                    FontMetrics fm = getFontMetrics(overlay.getFont());
                    int height = fm.getHeight();
                    String text = Integer.toString(points.size() - 1);
                    int width = fm.stringWidth(text);
                    int radius = Math.max(width, height) + 5;

                    scrollRectToVisible(new Rectangle(x - radius / 2, y - radius / 2, radius, radius));
                }
            });
            timer.start();
        }
    }
}

现在,如果您想将点显示在尽可能靠近中心的位置,那将需要额外的工作...

现在,如果您真的想玩得开心,请将延迟设置为 50 - 100 毫秒;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多