【发布时间】:2015-05-19 05:53:50
【问题描述】:
我正在尝试将图像添加到 Java 画布。 我正在使用“ImageIO.read”来获取图像源。我面临的问题是我不知道如何在读取图像位置后将其显示在画布上。稍后我将需要加载不同的图像(例如按下按钮后)我该怎么做。更新(canvas.update)方法需要一个“图形”参数而不是图像。 下面你会发现我的代码被简化了(我省略了所有与画布问题无关的代码。)
public class MainWindow {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Canvas csStatusImage = new Canvas();
csStatusImage.setBounds(393, 36, 200, 200);
frame.getContentPane().add(csStatusImage);
Image iMg;
try {
iMg = ImageIO.read(new File("Images/Error_status_1.png"));
csStatusImage.imageUpdate(iMg, 10, 2, 2, 9, 10);
csStatusImage.checkImage(iMg, (ImageObserver) this);
csStatusImage.createImage((ImageProducer) iMg);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
【问题讨论】:
-
这是过去两天内第三次提出此类问题。您可能会考虑先进行预研究,并突出显示您已阅读的内容以及为什么下次它对您不起作用;)