java - 如何向 JPanel 添加缩放和平移功能?

标签 java swing gridbaglayout zooming pan

所以我有一个Java程序,它使用网格袋布局来创建单元格(如方格纸),如果鼠标位于其中一个单元格上,则该单元格将改变其颜色。我想使用鼠标滚轮实现缩放功能。另外我想添加一个功能,当您放大时,您可以通过单击并拖动放大的 Canvas 来平移。要指定平移,请考虑单击并拖动以在放大的 map (例如谷歌地图)中移动。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class Testing {

    public int RowI = 10;
    public int ColI = 10;

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            for (int row = 0; row < RowI; row++) {
                for (int col = 0; col < ColI; col++) {
                    gbc.gridx = col;
                    gbc.gridy = row;

                    CellPane cellPane = new CellPane();
                    Border border = null;
                    if (row < 4) { 
                        if (col < 4) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < 4) {
                            border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    cellPane.setBorder(border);
                    add(cellPane, gbc);
                }
            }
        }
    }

    public class CellPane extends JPanel { //The CellPane class changes the color of an individual cell based on whether or no the mouse I on a cell.

        private Color defaultBackground; //This is a private color that is only used by the mouseListener.

        public CellPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) { //If mouse is on cell turn cell the given color(in this case green).
                    defaultBackground = getBackground();
                    setBackground(Color.GREEN);
                }

                @Override
                public void mouseExited(MouseEvent e) {  //If mouse is not on cell revert to default background.
                    setBackground(defaultBackground);
                }
            });
        }

        @Override
        public Dimension getPreferredSize() { 
            return new Dimension(50, 50); //Cell size on x and y axis.
        }
    }
}

enter image description here

最佳答案

也许this question将回答您正在寻找的内容。

关于java - 如何向 JPanel 添加缩放和平移功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46361513/

相关文章:

java - Gradle停止构建任务

java - JTextField 正在清空我的 JPanels

java - 如何使用 GridBagConstraints setRows 和 setColumns?

java - 如何在断言 Selenium getCssValue ("background"返回的背景色 rgb(255,255,255) 时将#ffffff 转换为#fff 或将#fff 转换为#ffffff

java - 我的井字游戏方法之一出错

java - 如何通过UNIX mac终端调试maven项目?

java - Boxlayout 中 JPanel 周围的空白区域?

java - 如何在另一个 JComponent 上添加半透明的 JPanel

java - 使用 GridBagLayout 定位

java - GridBagLayout 的麻烦