繁体   English   中英

如何在JPanel中添加缩放和平移功能?

[英]How to add zooming and panning functions to a JPanel?

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

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.
        }
    }
}

在此处输入图片说明

也许这个问题会回答您要寻找的东西。

暂无
暂无

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

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