实现了ImageObserver的类,其imageUpdate方法究竟在何种时间下才会得到调用?
以下代码无法达到目的,原因在哪?// 篇幅所限 import 部分省略
public class ImgObFrame extends JFrame{
public ImgObFrame() {
Container container = getContentPane();
JPanel panel = new ImgPanel();
container.add(panel);
}
public static void main(String[] args) {
ImgObFrame frame = new ImgObTest();
frame.setVisible(true);
}
}class ImgPanel extends JPanel {
Image myImage;
public void paintComponent(Graphics g) {
super.paintComponent(g);
try{
File f = new File("1.jpg");
myImage = ImageIO.read(f);
}catch(IOException ioe) {
System.out.println(ioe);
}
if(myImage == null) {
return;
}
//显示一幅图片,将自身设为ImageObserver
g.drawImage(myImage, 0, 0, this); }
// 重载imageUpdate方法
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
//但调试过程中该方法未曾调用!!!
boolean error;
if((infoflags & ALLBITS)!=0) {
error = false;
}else if((infoflags & ERROR)!=0) {
error = true;
}else {
error = false;
}
boolean done = !((infoflags & (ALLBITS | ERROR| ABORT)) == 0);
return !done;
}
}谢谢指点
以下代码无法达到目的,原因在哪?// 篇幅所限 import 部分省略
public class ImgObFrame extends JFrame{
public ImgObFrame() {
Container container = getContentPane();
JPanel panel = new ImgPanel();
container.add(panel);
}
public static void main(String[] args) {
ImgObFrame frame = new ImgObTest();
frame.setVisible(true);
}
}class ImgPanel extends JPanel {
Image myImage;
public void paintComponent(Graphics g) {
super.paintComponent(g);
try{
File f = new File("1.jpg");
myImage = ImageIO.read(f);
}catch(IOException ioe) {
System.out.println(ioe);
}
if(myImage == null) {
return;
}
//显示一幅图片,将自身设为ImageObserver
g.drawImage(myImage, 0, 0, this); }
// 重载imageUpdate方法
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
//但调试过程中该方法未曾调用!!!
boolean error;
if((infoflags & ALLBITS)!=0) {
error = false;
}else if((infoflags & ERROR)!=0) {
error = true;
}else {
error = false;
}
boolean done = !((infoflags & (ALLBITS | ERROR| ABORT)) == 0);
return !done;
}
}谢谢指点
解决方案 »
- 关于jtable的.getCellRenderer(0, 0)的问题,高手来
- 关于Applet初始化的问题,init()方法写了,为什么还说没有初始化?
- 面试题目
- 这句话有什么错么,为什么 disabled="false"或disabled="true".文本框里的值都不可改呢??
- 连接mysql
- 字符替换问题
- 应该属于类路径的设置问题
- 请问super(56, 1);代表什么意思??
- 在我的电脑上能编译出class文件 ,不知道什么原因不能运行,菜鸟问题,见笑!
- 我想用键盘来控制一个圆形的移动,为什么需要睡眠?
- mac操作系统上,如何打开一个本地的html文件
- 是否ImageObserver对ImageIO所读进的图像不起作用
File f = new File("1.jpg");
myImage = ImageIO.read(f);
改成
myImage = getImage(getCodeBase(),"1.jpg");
试试
调用drawImage()返回之后,只要有图像的新的部分被加载,图像观察者中的imageUpdate()就会被调用。