简体   繁体   中英

why picture doesn't show up in swing in java?

I want to show images in a Border-layout panel in center. I create image panel as below and then add it to the center of Border-layout. I don't know why image doesn't show up and there is no error too. What can be the reason and how can i fix it?

public class ImagePanel extends WebPanel {
private Image image;
private Image scaledImage;
private int imageWidth = 0;
private int imageHeight = 0;
//constructor
public ImagePanel() {
    super();
}
public void loadImage(String file) throws IOException {
    File filetest= new File("C:\\tmp\\axiuser\\Pictures\\CLA0014.png");
    image = ImageIO.read(filetest);//new File(file)
    imageWidth = image.getWidth(this);
    imageHeight = image.getHeight(this);
    setScaledImage();
}
//e.g., containing frame might call this from formComponentResized
public void scaleImage() {
    setScaledImage();
}
//override paintComponent
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if ( scaledImage != null ) {
        //System.out.println("ImagePanel paintComponent " + ++paintCount);
        g.drawImage(scaledImage, 0, 0, this);
    }
}
private void setScaledImage() {
    if ( image != null ) {
        //use floats so division below won't round
        float iw = imageWidth;
        float ih = imageHeight;
        float pw = this.getWidth();   //panel width
        float ph = this.getHeight();  //panel height
        if ( pw < iw || ph < ih ) {
            if ( (pw / ph) > (iw / ih) ) {
                iw = -1;
                ih = ph;
            } else {
                iw = pw;
                ih = -1;
            }
            //prevent errors if panel is 0 wide or high
            if (iw == 0) {
                iw = -1;
            }
            if (ih == 0) {
                ih = -1;
            }
            scaledImage = image.getScaledInstance(
                        new Float(iw).intValue(), new Float(ih).intValue(), Image.SCALE_DEFAULT);
        } else {
            scaledImage = image;
        }
    }
}

}

You never call your method

public void loadImage (String file) 

SSCCE:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;

public class ImagePanel extends JPanel {
    private Image image;
    private Image scaledImage;

    public void loadImage (String filename) throws IOException {
        image = ImageIO.read (new File (filename)); 
        setScaledImage ();
    }

    public void paintComponent (Graphics g) {
        super.paintComponent (g);
        if ( scaledImage != null) {
            // System.out.println ("ImagePanel paintComponent ");
            g.drawImage (scaledImage, 0, 0, this);
        }
    } 

    private void setScaledImage () {
        if (image != null) {
            //use floats so division below won't round
            int imageWidth = image.getWidth (this);
            int imageHeight = image.getHeight (this);
            float iw = imageWidth;
            float ih = imageHeight;
            float pw = this.getWidth ();   //panel width
            float ph = this.getHeight ();  //panel height
            if ( pw < iw || ph < ih) {
                if ( (pw / ph) > (iw / ih)) {
                    iw = -1;
                    ih = ph;
                } else {
                    iw = pw;
                    ih = -1;
                }
                //prevent errors if panel is 0 wide or high
                if (iw == 0) {
                    iw = -1;
                }
                if (ih == 0) {
                    ih = -1;
                }
                scaledImage = image.getScaledInstance (
                new Float (iw).intValue (), new Float (ih).intValue (), Image.SCALE_DEFAULT);
            } else {
                scaledImage = image;
            }
        }
    }   

    public static void main (String [] args) throws IOException {
        ImagePanel ip = new ImagePanel ();
        ip.loadImage ("./sample.png");
        JFrame jf = new JFrame ();
        jf.setLayout (new BorderLayout ());
        jf.add (ip, BorderLayout.CENTER);
        jf.setSize (400, 400); 
        jf.setLocation (150, 150);
        jf.setVisible (true);
        jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }
}

Scaling doesn't work yet, but here you have something to start. (Note that I renamed the image).

Just override getPreferredSize() of your JPanel , as you are doing with paintComponent(...) method. Let it return some Dimension object, something like :

public Dimension getPreferredSize()
{
    return (new Dimension(300, 300));
}

That will allow you to see your images.

也许没有为*.png定义的Reader。您可以使用ImageIO.getReaderFormatNames()查看可用的图像阅读器列表

我认为你应该将面板设置为不透明:

setOpaque(false)

Without knowing when your program will actually invoke paintComponent() it's impossible to tell. But if you call your loadImage method after you have already shown the GUI ( setVisible ) you would need to invoke repaint() or invalidate() on the panel in order to trigger a repaint of the component (and update the GUI that way).

Consider using JLabel for displaying images.

JLabel myLabel=new JLabel();
myLabel.setIcon(new ImageIcon("C:\\tmp\\axiuser\\Pictures\\CLA0014.png"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM