Java Image Browser

Source: Internet
Author: User
The browser//Display object is Jpg,gif canvas with a double buffering, using the scroll bar display, the display area is fixed and can be extended to a pattern displayed according to the canvas size, because canvas is placed in the center location. 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, Windowstatel Istener {//frameext fr = new Frameext (); Label Label1 = new Label ("", label.left); Would 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 would use it 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.gethe Ight ()); 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 to 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 is 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 a),//you can determ the in Sects by program or simple set the maximum to//a certain number,and then you scroll it down and trace as it reach//the Boundary,the value does not agree with the maximum number your set//and the difference may is the insects size you shall Minus A/Vertical.setvisibleamount ((h-290)/10); If we try to set the Visibleamount to a variable, the scroll by would be uncertain and//hard to Contral Horizontal.setma Ximum (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: clearly what Event happen if W e 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 ("Windows State Changed"); Canvas1.repaint (); public void windowclosing (WindowEvent e) {//use This.hide (); For subsequent forms in multi form applications System.ex It (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 (W Indowevent e) {} public void windowiconified (WindowEvent e) {} public void windowopened (WindowEvent e) {}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) {Ca Nvas1.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&&amp 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, M, this); System.out.println ("W and H are" + W + "" + H + "and Count update is" + "OFFG" + OFFG);} public void Paint (Graphics g) {if (a) {offimg = CreateImage (GetWidth (), getheight ()); OFFG = Offimg.getgraphics ( ); System.out.println ("W and H are" + W + "" + H + "and Count update is" + "OFFG" + OFFG); first= FalsE } if (OFFG!= null&&image1!=null)//if Delete this "window state change,the image won't show {//you mu St Scroll the bar by hand then it'll show we-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, M, this); System.out.println ("W and H are" + W + "" + H + "and Count update is" + "OFFG" + OFFG);} System.out.println ("This is Paint"); }}//end of inner class. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.