我正在为Notch的“ Chambered的序曲”创建一个包装,并且希望这样做,以便您可以调整窗口的大小。我无法弄清楚如何缩放内容,我尝试覆盖onPaint。我无法修改Jar来完成此操作。

onPaint尝试:

package com.gudenau.pc.poc;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class JScaledFrame extends JFrame {

    private static final long serialVersionUID = 4044340683411982494L;

    public JScaledFrame(String title) {
        super(title);
    }

    @Override
    public void paint(Graphics graphics){
        Dimension min = getMinimumSize();
        Dimension size = getSize();

        BufferedImage image = new BufferedImage(min.width, min.height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics subGraphics = image.getGraphics();
        super.paint(subGraphics);
        subGraphics.dispose();

        graphics.drawImage(image, 0, 0, size.width, size.height, null);
    }
}

最佳答案

缩放内容非常困难。您要更改的是绘制基础组件的Graphics上下文的缩放上下文。

通常,您会做的是覆盖有问题的组件的paint方法并应用所需的缩放因子...

public class MyExtendedClass extends ... {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g.create();
        double scaleFactor = 1d;
        // Calculate the scaling factor to apply
        // based on the "default" size and the
        // current size...
        g2d.scale(scaleFactor, scaleFactor);
        super.paint(g2d);
        g2d.dispose();
    }
}


这可能会引起各种怪异,坦率地说,更可靠的解决方案可能是使用JXLayer / JLayer,例如...


Zooming JLayeredPane via JLayer and the LayerUI
How to add MouseListener to item on Java Swing Canvas


您可能还会发现使用AffineTransform更容易...

10-08 08:00