我在谷歌搜索过这个主题,但我不应该清楚自己应该做些什么。 我的问题是:
我的代码出了什么问题?如何修改它。
许多网站的答案总是使用网址,我怎么知道我的图片的网址?
以下是代码:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TwoPanel {
public static void main(String[] args) {
JFrame frame = new JFrame ("Key test");
MyDrawPanel1 dp1 = new MyDrawPanel1();
//MyDrawPanel2 dp2 = new MyDrawPanel2();
//MyDrawPanel3 dp3 = new MyDrawPanel3();
//MyDrawPanel4 dp4 = new MyDrawPanel4();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());
p.add (dp1);
//p.add (dp2);
//p.add(dp3);
//p.add(dp4);
frame.getContentPane().add(p);
frame.pack();
}
}
然后是drawpanel:
public class MyDrawPanel1 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
Image image = new ImageIcon("Koala.jpg").getImage();/*something new: if i use the
path to the disk name(D:/Java/workspace/firstJava/src/com/founder/panel/Koala.jpg'),
it can work.*/
public void paintComponent (Graphics g) {
super.paintComponent(g);
//Graphics2D g2 = (Graphics2D) g;
g.drawImage(image, 3, 4, null);
}
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(null), image.getHeight(null));
}
return super.getPreferredSize(); // default
}
}
这个面板可以工作,虽然我不明白为什么其他不起作用
package com.founder.panel;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class MyDrawPanel4 extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String URL_PATH = "file:///D:/Java/workspace/firstJava/src/com/founder/panel/" +
"Koala.jpg";
BufferedImage image = null;
public MyDrawPanel4() {
// TODO Auto-generated constructor stub
setBackground(Color.white);
try {
image = ImageIO.read(new URL(URL_PATH));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(), image.getHeight());
}
return super.getPreferredSize(); // default
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}
}
答案 0 :(得分:2)
问题是ImageIcon(String)
正在当前目录中查找已执行该程序的文件Koala.jpg
。
您的图片就是所谓的嵌入式资源,您无法像文件一样访问它,而是需要使用Class#getResource
,例如......
Image image = new ImageIcon(MyDrawPanel1.class.getResource("Koala.jpg")).getImage();
优选ImageIO
是更好的选择......
class MyDrawPanel1 extends JPanel {
private BufferedImage image;
pubic MyDrawPanel1() {
try {
// If the image resides in the same package as the class
image = ImageIO.read(getClass().getResource("Koala.jpg"));
// If the image resides somewhere else...
image = ImageIO.read(getClass().getResource("/path/to/image/Koala.jpg"));
} catch (IOException exp) {
exp.printStackTrace();
}
ImageIO
能够读取比ImageIcon
更多的格式,并且如果由于某种原因无法读取文件(例如它不存在),也会抛出IOException
< / p>
答案 1 :(得分:1)
我的猜测是没有加载图像,使用文件的路径作为ImageIO.read(new File("Koala.jpg"))
的参数。使用ImageIO
:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TwoPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showMainWindow();
}
});
}
public static void showMainWindow() {
JFrame frame = new JFrame ("Key test");
MyDrawPanel1 dp1 = new MyDrawPanel1();
//MyDrawPanel2 dp2 = new MyDrawPanel2();
//MyDrawPanel3 dp3 = new MyDrawPanel3();
//MyDrawPanel4 dp4 = new MyDrawPanel4();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());
p.add (dp1);
//p.add (dp2);
//p.add(dp3);
//p.add(dp4);
frame.getContentPane().add(p);
frame.setLocationRelativeTo(null); // center frame
frame.setMinimumSize(new Dimension(300,400)); // make the frame at least 300x400
frame.pack();
frame.setVisible(true);
}
}
MyDrawPanel1
:
class MyDrawPanel1 extends JPanel {
private static final long serialVersionUID = 1L;
//Image image = new ImageIcon("Koala.jpg").getImage();
BufferedImage image;
MyDrawPanel1() {
super();
try {
this.image = ImageIO.read(getClass().getResource("com/founder/pane/Koala.jpg"))
} catch (IOException e) {
System.err.println("Koala.jpg does not exist!");
e.printStackTrace();
}
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawImage(image, 3, 4, null);
}
}