要在jpanel上显示图像,调用了哪几个关键的类方法? 经过debug,总结如下:
无论选择按钮或菜单来触发事件,打开图像文件
分别调用的方法依次是
按钮或菜单的事件处理方法(buttonopen_mouseclicked())->
自定义的加载方法(loadmyimage()) ->
graphics对象的drawimage()->
jpanel的imageupdate() ->
jpanel的repaint()
以下代码仅是示例,不完全
void buttonopen_mouseclicked(mouseevent e) {
panel.loadchemimg(imagefile.getabsolutepath());
}
public void loadmyimage(string s) {
originalimage = toolkit.getdefaulttoolkit().getimage(s);
if (ibuff == null) {
init_buff();
}
buffgra.drawimage(originalimage, 0, 0, this);
//这里会调用imageupdate()
}
public boolean imageupdate(image originalimage, int flags, int x, int y, int w, int h) {
color col;
if (flags == imageobserver.allbits) {
col = buffgra.getcolor();
buffgra.setcolor(color.white);
buffgra.fillrect(0, 0, getwidth(), getheight());
buffgra.drawimage(originalimage, 0, 0, this);
buffgra.setcolor(col);
repaint();
//完全加载后返回false,防止再次调用
return false;
}
return true;
}
闽公网安备 35060202000074号