//显示对象为jpg,gif canvas中画图使用double buffering,使用scroll bar显示,显示区域固定了,可以扩展成按照canvas的大小来显示的模式,因为canvas放在center位置。 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; class frameext extends frame { } public class images extends frame implements actionlistener, windowlistener ,windowstatelistener { //frameext fr = new frameext(); label label1 = new label(" ",label.left); //will show the file name button button1 = new button("open file"); int xpos,ypos; int imageheight; int w,h; image offimg, bg; graphics offg; boolean first=true; //extends canvas. this code will use its paint method. public mycanvas canvas1 = new mycanvas(); static scrollbar horizontal, vertical; //scrollbar used filedialog fd = new filedialog(this,"open", filedialog.load); image image1; public void initialize() { //system.out.println(" dddddd "+this.maximized_both); //this.setlocation(0, 0); //this.setsize(this.gettoolkit().getscreensize()); this.setsize(600,600); this.setlocation(30,30); this.setbackground(color.lightgray); canvas1.setbackground(color.white); canvas1.setsize(580,580); //colorchooserdialog colordialog = new colorchooserdialog(this, "@fff", this); this.add ("north", horizontal = new scrollbar (scrollbar.horizontal)); this.add ("east", vertical = new scrollbar (scrollbar.vertical)); panel pa=new panel(); pa.setbackground(color.white); label1.setbackground(color.lightgray); label1.setalignment(label.left); pa.add(label1); pa.add(button1); //add label and button on panel this.add("south",pa); //add panel at south this.add("center",canvas1); this.addwindowlistener(this); this.addwindowstatelistener (this); button1.addactionlistener(this); try { system.out.println ("getsize" + canvas1.getsize () + "getwidth" + canvas1.getwidth () + "getheight " + canvas1.getheight ()); } catch (exception e) {system.out.println(e.tostring()); } this.pack(); this.show(); } void imageload () { fd.setfile("*.jpg;*.gif"); //file speicific fd.show(); if(fd.getfile() == null) { label1.settext("you have not chosen any image files yet"); } else { string d = (fd.getdirectory() + fd.getfile()); image1 = toolkit.getdefaulttoolkit().getimage(d); if(image1==null) {system.out.println("image is null of this file:"+ d); return; } label1.settext(d); system.out.println("image loaded "+d); //it is important to use canvas1 as the imageobserver. mediatracker mt = new mediatracker(canvas1); //here should be noticed like the author say mt.addimage(image1, 0); try { mt.waitforall(); } catch (interruptedexception e) { system.out.println(e.tostring()); } w = image1.getwidth (this); h = image1.getheight (this); //set w and h the image's height and width system.out.println("image width and height is"+w+" "+h); if (offimg != null && (offimg.getheight (this) < h || offimg.getwidth (this) < w)) { //shall we recreate the offimg for double buffering if the image is larger system.out.println ("offimage w and h " + offimg.getheight (this) + " " + offimg.getwidth (this) + " w " + w + " h " + h); offimg = createimage (w, h); offg = offimg.getgraphics (); } vertical.setmaximum(h-390); //here important set scroll bar 's maximum //imagesize minus (showsize minus insects size first) , //you can determ the insects by program or simple set the maximum to //a certain number,and then you scroll it down and trace when it reach //the boundary,the value does not agree with the maximum number you set //and the difference may be the insects size you shall minus first // vertical.setvisibleamount((h-290)/10); //if we try to set the visibleamount to a variable ,the scroll by will be uncertain and //hard to contral horizontal.setmaximum(w-440); // horizontal.setvisibleamount((w-390)/10); canvas1.repaint(); //image loaded so repaint the canvas // canvas1.paint(); } } public boolean handleevent (event e) { // system.out.println("catched "+e);//we can see clearly what event happen if we don't comment this sentence if (e.id == event.window_destroy) { system.exit(0); } else if (e.target instanceof scrollbar) { system.out.println(" scoll of handleevent " ); if (e.target == horizontal) { xpos = ((integer)e.arg).intvalue(); //trace the scroll value for image show later } else if (e.target == vertical) { ypos = ((integer)e.arg).intvalue(); } system.out.println(" "+xpos+""+ypos); //system.out.println(" checking hereeeeeeeeeeeeeee" ); canvas1.repaint(); } return super.handleevent(e); } public void windowstatechanged (windowevent e) { system.out.println ("window state changed"); canvas1.repaint (); } public void windowclosing(windowevent e) { // use this.hide(); for subsequent forms in multi form applications system.exit(0); } public void windowactivated(windowevent e) {system.out.println ("window activated");canvas1.repaint();} public void windowclosed(windowevent e) {} public void windowdeactivated(windowevent e) {} public void windowdeiconified(windowevent e) {} public void windowiconified(windowevent e) {} public void windowopened(windowevent e) {} public void actionperformed(actionevent event) { system.out.println("catched2 "+event); button b = (button)event.getsource(); if(b == button1) { imageload(); } } public static void main(string args[]) { images a = new images(); a.initialize(); } /* public void update(graphics g) { canvas1.update(g); } public void paint(graphics g) { canvas1.paint(g); } */ //a simple inner class to show the basics. class mycanvas extends canvas { public void update(graphics g) { system.out.println(" update" ); if (offg != null&&image1!=null) { offg.clearrect(50,50, 500,450); offg.drawimage(image1,50,50, 500,450, xpos, ypos,450+xpos,400+ypos ,this); g.drawimage (offimg, 50, 50, this); system.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); } } public void paint(graphics g) { if (first) { offimg = createimage (getwidth (), getheight ()); offg = offimg.getgraphics (); system.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); first= false; } if (offg != null&&image1!=null) //if delete this when the window state change,the image will not show { //you must scroll the bar by hand then it will show we show take care what the function of paint when repaint offg.clearrect(50,50, 500,450); offg.drawimage(image1,50,50, 500,450, xpos, ypos,450+xpos,400+ypos ,this); g.drawimage (offimg, 50, 50, this); system.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); } system.out.println ("this is paint"); } }//end of inner class. }
闽公网安备 35060202000074号