Java 小程序的背景图像和填写小程序

Java 小程序的背景图像和填写小程序,java,japplet,Java,Japplet,我正在为我的JApplet设置一个图像作为背景,并使用调整大小的方法将图像填充到JApplet背景中。我目前正在使用 Image background; public void paint(Graphics g) { super.paint(g); g.drawImage(background, 0, 0, this); } public void init() { // TODO start asynchronous download of heavy resourc

我正在为我的JApplet设置一个图像作为背景,并使用调整大小的方法将图像填充到JApplet背景中。我目前正在使用

Image background;
public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(background, 0, 0, this);
}

public void init() {
    // TODO start asynchronous download of heavy resources
    background=resize(new ImageIcon(getClass().getResource("/org/me/pd/resources/music.png")),this.getWidth(),this.getHeight()).getImage();
    this.setLayout(new GridLayout(6,6));
    //Create();
}
这是我的调整大小方法

public ImageIcon resize(ImageIcon icon, int width, int height)
{
    BufferedImage converted = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = converted.createGraphics();
    icon.paintIcon(null, g, 0,0);
    g.dispose();
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(converted, 0, 0, width, height, null);
    g2d.dispose();
    ImageIcon correct=new ImageIcon(bi);
    return correct;
}
当小程序加载并填充图像时,此功能最初会起作用,但当小程序最大化时,图像不会随着小程序而最大化。它显示了最大化之前的状态


我做错了什么?

无需重新缩放图标

相反,您只需让绘画方法为您做到这一点:

g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
但是,您不应该通过重写小程序的paint()方法来执行此操作

相反,您应该重写JPanel的paintComponent()方法,并将面板添加到小程序中。然后,您的窗格将成为小程序的内容窗格。然后将面板的布局设置为BorderLayout,使其行为与默认内容窗格完全相同