在面板内放大和缩小

2022-09-03 01:02:44

我有一个面板,其中一些2D对象正在移动。我已经根据需要覆盖了paintComponent()。现在我希望能够放大和缩小该区域。放大时,将出现滚动条,通过滚动条可以通过滚动查看整个字段。在放大和缩小时,2D 对象的大小应相应增加或减少。哪种 Swing 组件或组件组合将有助于实现这一目标?


答案 1

最简单的方法是修改面板并引入一个双精度值来指示您的缩放级别。此双精度值表示您的比例尺,其中 1 表示正常,放大后为较高。您可以在 .Graphics2DpaintComponent

如:

Graphics2D g2 = (Graphics2D) g;
int w = // real width of canvas
int h = // real height of canvas
// Translate used to make sure scale is centered
g2.translate(w/2, h/2);
g2.scale(scale, scale);
g2.translate(-w/2, -h/2);

对于滚动,请将面板放在JScrollPane中,并将其与也使用缩放比例的getP referSize相结合。JScrollPane 使用您放入其中的组件的首选大小。如果首选大小超过其自身大小,它将显示滚动条。

如果更改面板的首选大小,以便缩放它返回的宽度和高度,则应该没问题。基本上,您可以返回如下内容:

return new Dimension(w * scale, h * scale)

答案 2

我知道这个问题已经很老了,但我认为我可以发布我的解决方案,以防将来对某人有用。

因此,我创建了一个扩展 JPanel 的类,该类实现了 MouseWheelListener,以便检测用户何时滚动鼠标。我的类还会侦听拖动,以便在用户单击和拖动时移动内容。

代码说明

首先,在构造函数中,必须将其设置为 MouseWheelListener

 addMouseWheelListener(this);

对于放大和缩小,我使用了一个布尔值(以指示用户何时使用鼠标滚动)和两个加倍(以保持对象大小的乘以的当前因子)和(对于上一个缩放因子)。zoomerzoomFactorprevZoomFactor

private double zoomFactor = 1;
private double prevZoomFactor = 1;
private boolean zoomer;

我还覆盖了JPanel的方法,其中(在绘制任何内容之前)当用户缩放(=true)时,我将图形缩放为.法典:paint()zoomerzoomFactor

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    if (zoomer) {
        AffineTransform at = new AffineTransform();
        at.scale(zoomFactor, zoomFactor);
        prevZoomFactor = zoomFactor;
        g2.transform(at);
        zoomer = false;
    }
    // All drawings go here
}

最后,我重写了 MouseWheelListener 的方法,在该方法中,我增加了(如果用户向上滚动)或减少了(如果用户向下滚动)。法典:mouseWheelMovedzoomFactorzoomFactor

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    zoomer = true;
    //Zoom in
    if (e.getWheelRotation() < 0) {
        zoomFactor *= 1.1;
        repaint();
    }
    //Zoom out
    if (e.getWheelRotation() > 0) {
        zoomFactor /= 1.1;
        repaint();
    }
}

工作实例

如果您还想使用拖动函数并希望根据鼠标的位置进行缩放,则可以使用下面的类,该类将 BufferedImage 作为构造函数中的参数,以便在屏幕上显示某些内容。

我还在GitHub上上传了一个名为Zoomable-Java-Panel的项目,其中有一个我上面展示的功能示例,您可以对其进行测试并了解如何将其实现到项目中。

package zoomable.panel;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Thanasis1101
 * @version 1.0
 */
public class MainPanel extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener {

    private final BufferedImage image;

    private double zoomFactor = 1;
    private double prevZoomFactor = 1;
    private boolean zoomer;
    private boolean dragger;
    private boolean released;
    private double xOffset = 0;
    private double yOffset = 0;
    private int xDiff;
    private int yDiff;
    private Point startPoint;

    public MainPanel(BufferedImage image) {

        this.image = image;
        initComponent();

    }

    private void initComponent() {
        addMouseWheelListener(this);
        addMouseMotionListener(this);
        addMouseListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2 = (Graphics2D) g;

        if (zoomer) {
            AffineTransform at = new AffineTransform();

            double xRel = MouseInfo.getPointerInfo().getLocation().getX() - getLocationOnScreen().getX();
            double yRel = MouseInfo.getPointerInfo().getLocation().getY() - getLocationOnScreen().getY();

            double zoomDiv = zoomFactor / prevZoomFactor;

            xOffset = (zoomDiv) * (xOffset) + (1 - zoomDiv) * xRel;
            yOffset = (zoomDiv) * (yOffset) + (1 - zoomDiv) * yRel;

            at.translate(xOffset, yOffset);
            at.scale(zoomFactor, zoomFactor);
            prevZoomFactor = zoomFactor;
            g2.transform(at);
            zoomer = false;
        }

        if (dragger) {
            AffineTransform at = new AffineTransform();
            at.translate(xOffset + xDiff, yOffset + yDiff);
            at.scale(zoomFactor, zoomFactor);
            g2.transform(at);

            if (released) {
                xOffset += xDiff;
                yOffset += yDiff;
                dragger = false;
            }

        }

        // All drawings go here

        g2.drawImage(image, 0, 0, this);

    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {

        zoomer = true;

        //Zoom in
        if (e.getWheelRotation() < 0) {
            zoomFactor *= 1.1;
            repaint();
        }
        //Zoom out
        if (e.getWheelRotation() > 0) {
            zoomFactor /= 1.1;
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Point curPoint = e.getLocationOnScreen();
        xDiff = curPoint.x - startPoint.x;
        yDiff = curPoint.y - startPoint.y;

        dragger = true;
        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        released = false;
        startPoint = MouseInfo.getPointerInfo().getLocation();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        released = true;
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}

推荐