2

アプリケーションを最大化すると、JPanel 内の画像のサイズが変更されません。ウィンドウが最大化されたときに JPanel とその内容を調整するにはどうすればよいですか?

編集:私は BufferedImage を使用しています

4

2 に答える 2

5

それは自由回答形式の質問です。

領域に合わせて拡大または縮小したいですか、それとも縦横比を気にしませんか??

スケール トゥ フィルとスケール トゥ フィットの違い

ここに画像の説明を入力 ここに画像の説明を入力

この例では、フレームのサイズの変更に反応し、リアルタイムで画像を再スケーリングします。

public class TestScaling {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ScalingPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class ScalingPane extends javax.swing.JPanel {

        private BufferedImage image;

        public ScalingPane() {
            try {
                image = ImageIO.read(getClass().getResource("/AtDesk.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }    
            setBackground(Color.red);
        }

        public double getScaleFactor(int iMasterSize, int iTargetSize) {    
            double dScale = 1;
            dScale = (double) iTargetSize / (double) iMasterSize;

            return dScale;    
        }

        public double getScaleFactorToFit(Dimension original, Dimension toFit) {    
            double dScale = 1d;

            if (original != null && toFit != null) {    
                double dScaleWidth = getScaleFactor(original.width, toFit.width);
                double dScaleHeight = getScaleFactor(original.height, toFit.height);

                dScale = Math.min(dScaleHeight, dScaleWidth);
            }    
            return dScale;
        }

        public double getScaleFactorToFill(Dimension masterSize, Dimension targetSize) {
            double dScaleWidth = getScaleFactor(masterSize.width, targetSize.width);
            double dScaleHeight = getScaleFactor(masterSize.height, targetSize.height);

            double dScale = Math.max(dScaleHeight, dScaleWidth);

            return dScale;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            double scaleFactor = Math.min(1d, getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize()));
//            double scaleFactor = Math.min(1d, getScaleFactorToFill(new Dimension(image.getWidth(), image.getHeight()), getSize()));

            int scaleWidth = (int) Math.round(image.getWidth() * scaleFactor);
            int scaleHeight = (int) Math.round(image.getHeight() * scaleFactor);

            Image scaled = image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - scaled.getWidth(this)) / 2;
            int y = (height - scaled.getHeight(this)) / 2;

            g.drawImage(scaled, x, y, this);
        }
    }
}

より良い解決策は、コンポーネントのサイズの変化に反応し、バックグラウンドで元のイメージを再スケーリングして、低品質と高品質のスケールを提供できるバックグラウンド スレッドを用意することです。

Image.getScaledInstanceは、最速または最高品質のスケーリング アルゴリズムではないことにも注意してください。詳細については、 Image.getScaledInstanceの危険性をご覧ください。

また、以下の興味深いものを見つけるかもしれません

Java: JPanel 背景画像の縦横比を維持する

于 2012-10-13T20:40:46.903 に答える
2

1 つの粗雑な方法..

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.draw(image,0,0,getWidth(),getHeight(),this);
    // ..
于 2012-10-13T20:25:49.967 に答える