当我根据JPanel的大小重新缩放图像时,我在JPanel上显示图像时遇到问题。图像没有出现。
public class createGUII extends JFrame{
String [] background = {"c1.jpg","c2.jpg","c3.jpg","c4.jpg"};
ArrayList<String> bgPicturesFiles = new ArrayList<String>(Arrays.asList(background));
JPanel panel;
ImagePanel imgBg;
public createGUII(){
GridBagLayout m = new GridBagLayout();
Container c = getContentPane();
c.setLayout (m);
GridBagConstraints con = new GridBagConstraints();
//Panel for background
panel = new JPanel();
panel.setSize(600, 600);
con = new GridBagConstraints();
con.anchor=GridBagConstraints.CENTER;
con.gridy = 1; con.gridx = 0;
con.gridwidth = 1; con.gridheight = 1;
m.setConstraints(panel, con);
c.add(panel);
//randomized the image files
Random r = new Random();
int random = r.nextInt(bgPicturesFiles.size());
//rescale the image according to the size of the JPanel
imgBg = new ImagePanel(new ImageIcon(bgPicturesFiles.get(random)).getImage().getScaledInstance(panel.getHeight(), panel.getWidth(),Image.SCALE_SMOOTH));
panel.add(imgBg);
setResizable(false);
setVisible(true);
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new createGUII();
}
});
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
答案 0 :(得分:1)
请看一下:JPanel background image,JPanel with background image, with other panels overlayed
第二个链接提到您必须为缩放做一些自定义绘画。这是个问题。我不会在paintComponent方法中每次都缩放图像,但是如果自上次调用后宽度和高度已经更改,则执行一次,在这种情况下,重新创建包含图像的BufferedImage你在调用超类paintComponent之前每次都搞定一次,缩放到合适的大小(使用Image scaling does not work when original image height & width is smaller the scaling height & width之类的东西)。我可以看到一个问题,当你调用超类paintComponent方法时,它可能会尝试用颜色填充面板,但是你必须进行实验。
答案 1 :(得分:0)
我没有看到你实际在哪里读图像,如本例中所示。
附录:我添加了缩放示例。另请参阅Don't Use getScaledInstance()
和The Perils of Image.getScaledInstance()
。
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/questions/4170463 */
public class LoadImage extends JPanel {
private Image image;
public LoadImage() {
super(new GridLayout());
try {
image = ImageIO.read(new File("image.jpg"));
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
int w = image.getWidth(null) / 2;
int h = image.getHeight(null) / 2;
this.add(new JLabel(new ImageIcon(
image.getScaledInstance(w, h, Image.SCALE_SMOOTH))));
}
private void display() {
JFrame f = new JFrame("LoadImage");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LoadImage().display();
}
});
}
}
答案 2 :(得分:0)
问题是在Java图像中异步加载。由于以下原因,上述代码存在以下几个问题:
new Dimension(600, 600)
),也可能无法加载图像本身。要确保加载,请添加以下内容:
new ImageIcon(img).getImage();
在ImagePanel的构造函数中this.img = img;
之后。
请注意,这部分是黑客攻击 - 我不是GUI专家,但我知道上面的内容写得更好。也许其他人可能会有更多的光明。
以下是一些可能有用的链接:
希望这有帮助。
答案 3 :(得分:0)
这是一个应该有用的link。然而,我找到了一种更适合我所遇到的问题的方法this就是我所发现的,以下是我从中得到的。
我希望这会有所帮助。
Container con = getContentPane();
final String backgroundPath = "C:\\background.jpg";
ImageIcon imh = new ImageIcon(backgroundPath);
setSize(imh.getIconWidth(), imh.getIconHeight());
JPanel pnlBackground = new JPanel()
{
public void paintComponent(Graphics g)
{
Image img = new ImageIcon(backgroundPath).getImage();
g.drawImage(img, 0, 0, null);
}
};
con.add(pnlBackground);
pnlBackground.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());