如何制作与面板相同尺寸的图像

如何制作与面板相同尺寸的图像

本文介绍了如何制作与面板相同尺寸的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我创建了一个包含图像的简单框架程序.但是图像的大小与框架的大小不同.如果我放大框架,图像尺寸会保持不变吗?

I created a simple frame program that includes a image. But the image don't have the same size as the frame. If i enlarge the frame the image size stays the same?

如何使图像的尺寸与框架的尺寸相同?

How can i make the image the same size as the frame?

这是我当前的代码:

    JPanel panel1 = new JPanel();
    ImageIcon image1 = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
    JLabel label1 = new JLabel(image1);
    panel1.add(label1);
    Color color1 = new Color (200, 0 ,100);
    panel1.setBorder(BorderFactory.createLineBorder(color1, 3));
    JFrame f = new JFrame("Frame");
    f.setLayout(new BorderLayout(5,5));
    f.add((panel1),BorderLayout.WEST);
    f.setSize(320,200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

推荐答案

您可以绘制图像而不是使用标签.

You can paint the image instead of using a label.

ImageIcon icon = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
Image image = icon.getImage();
JPanel panel1 = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight());
    }
};

也不确定,但是我想您可能想将面板添加到CENTER而不是向西添加(如果您希望图像在框架中居中).

Also not sure, but I think you may want to add the panel to the CENTER and not the west (if you want the image to be centered in the frame).

此外,如果您想要面板的PreferredSize,则还必须覆盖面板的getPreferredSize().

Also not, if you want a preferredSize for the panel, you will have to override the getPreferredSize() of the panel also.

JPanel panel1 = new JPanel() {
    ...
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 200);
    }
};

然后您只需pack()框架,这是首选,而不是设置尺寸

Then you can just pack() the frame, which is preferred, instead of setting the size

f.pack();
//f.setSize(320, 200);



import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestBackgroundResize {
    public TestBackgroundResize() {
        JFrame frame = new JFrame();
        frame.setContentPane(createBackgroundPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createBackgroundPanel() {
        return new JPanel() {
            BufferedImage image;
            {
                try {
                    image = ImageIO.read(getClass().getResource("/marioblobs/mario.png"));
                } catch (IOException ex) {
                    Logger.getLogger(TestBackgroundResize.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 200);
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestBackgroundResize();
            }
        });
    }
}

这篇关于如何制作与面板相同尺寸的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:00