我想在磁盘上显示的图片旁边添加URL中的图片,但是在第36行中发现错误,并且不知道如何对其进行排序。您能帮我解决吗?
它在线程“ AWT-EventQueue-0” java.lang.Error中返回异常:未解决的编译问题: 未处理的异常类型IOException
所以我应该抓住MalformedURLException e + IOException e吗?
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ObrazPanel extends JPanel {
private BufferedImage image;
private BufferedImage image2;
public ObrazPanel() {
super();
File imageFile = new File("C:\\Users\\KostrzewskiT\\eclipse-workspace\\zdjecie\\src\\zdjecie\\java.jpg");
//File imageFile2 = new File("C:\\Users\\KostrzewskiT\\eclipse-workspace\\zdjecie\\src\\zdjecie\\java2.jpg");
try {
image = ImageIO.read(imageFile);
// image2 = ImageIO.read(imageURL);
} catch (IOException e) {
System.err.println("Blad odczytu obrazka");
e.printStackTrace();
}
try {
URL imageURL = new URL ("http://beautyinspiration.pl/wp-content/uploads/2015/12/i-coach-otw.jpg");
image2 = ImageIO.read(imageURL);
} catch (MalformedURLException e) {
System.err.println("Blad odczytu obrazka");
e.printStackTrace();
}
//wymiar wyswietlanych obrazkow
Dimension dimension = new Dimension(image.getWidth() + image2.getWidth(), image2.getHeight());
setPreferredSize(dimension);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image, 0, 0, this); //punkty w ktorych jest wyswietlane
g2d.drawImage(image2, image.getWidth(), 0, this);
}
}
答案 0 :(得分:0)
我用
更正我的代码try {
URL imageURL = new URL ("path");
image2 = ImageIO.read(imageURL.openStream());
} catch (IOException d) {
System.err.println("Blad odczytu obrazka URL");
d.printStackTrace();
这是应该正确的方法还是超出最佳实践?