The swing of Java learning notes basically realizes the function of QQ screen-cutting __swing

Source: Internet
Author: User
Tags abs gety

Cpframe.java, Capturescreenhh.java and Icon.png.

Cpframe.java

Package captuhhhhh;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.image.BufferedImage;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.InputStreamReader;

Import Java.io.OutputStreamWriter;
Import Javax.imageio.ImageIO;
Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import javax.swing.SwingUtilities;

Import Javax.swing.UIManager;
Import org.dyno.visual.swing.layouts.Constraints;
Import Org.dyno.visual.swing.layouts.GroupLayout;

Import org.dyno.visual.swing.layouts.Leading;
VS4E-does not REMOVE this line!
	public class Cpframe extends JFrame {private static final long serialversionuid = 1L; Private JButton JButton0;
	Private JButton JButton1;
	Private JLabel jLabel0;
	private static final String Preferred_look_and_feel = "Javax.swing.plaf.metal.MetalLookAndFeel";
	Public Cpframe () {initcomponents ();
		private void Initcomponents () {setlayout (New grouplayout ());
		Add (GetJButton1 (), New Constraints (new leading (), 198, new leading (109, 10, 10));
		Add (GetJButton0 (), New Constraints (new leading (), 199, new leading (47, 10, 10));
		Add (GetJLabel0 (), New Constraints (new leading (), new leading (151, 10, 10));
	SetSize (320, 240);
			Private JLabel getJLabel0 () {if (jLabel0 = null) {jLabel0 = new JLabel ();
		Jlabel0.settext ("Double-click the Selection Save");
	return jLabel0;
			Private JButton GetJButton1 () {if (JButton1 = null) {JButton1 = new JButton ();
			Jbutton1.settext ("constituency preservation"); Jbutton1.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent event) {Jbutton1acti
				Onactionperformed (event);
		}
			}); Return JBUtton1;
			Private JButton GetJButton0 () {if (JButton0 = null) {JButton0 = new JButton ();
			Jbutton0.settext ("Full screen save"); Jbutton0.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent event) {Jbutton0acti
				Onactionperformed (event);
		}
			});
	return jButton0;
			private static void Installlnf () {try {String lnfclassname = Preferred_look_and_feel;
			if (Lnfclassname = = null) Lnfclassname = Uimanager.getcrossplatformlookandfeelclassname ();
		Uimanager.setlookandfeel (Lnfclassname); The catch (Exception e) {System.err.println ("Cannot install" + Preferred_look_and_feel + "on this platform:" + E.
		GetMessage ());
	 }/** * Main entry of the class.
	 * Note:this class is a created so, can easily preview the result at runtime.
	 * It is isn't expected to being managed by the designer.
	 * You can modify it as your like.
		*/public static void main (string[] args) {installlnf (); Swingutilities.invoKelater (New Runnable () {@Override public void run () {Cpframe frame = new Cpframe ();
				Frame.setdefaultcloseoperation (Cpframe.exit_on_close);
				Frame.settitle ("Cpframe");
				Frame.getcontentpane (). Setpreferredsize (Frame.getsize ());
				Frame.pack ();
				Frame.setlocationrelativeto (NULL);
			Frame.setvisible (TRUE);
	}
		});
	    }//Full-screen private void Jbutton0actionactionperformed (ActionEvent event) {this.setvisible (false);
			try{FileInputStream Fis2=null;
			InputStreamReader Isr2=null;
			BufferedReader Br2=null;
			FileOutputStream Fos2=null;
			OutputStreamWriter Osw2=null;
			
			BufferedWriter Bw2=null; 
			JFileChooser dlg = new JFileChooser ();
			Dlg.setdialogtype (Jfilechooser.save_dialog);
			
			Dlg.showsavedialog (NULL);
			
			File Getnewfile=dlg.getselectedfile ();
			if (getnewfile==null) {this.setvisible (true);
				}else{String Getnewpath=getnewfile.getpath ();
				String Getnewname=getnewfile.getpath (); String Getnewparent=getnewfile.getParent ();
					Writes a new file try {Robot Robot = the newer Robot ();
			        BufferedImage get = robot.createscreencapture (new Rectangle (Toolkit. Getdefaulttoolkit (). Getscreensize ())); int wideth=get.getwidth (NULL); Get the source map wide int height=get.getheight (NULL);
			        Get the source graph long//write new file Newfile=new files (getnewpath);
			        if (!newfile.exists ()) {newfile.createnewfile ();
					} fos2=new FileOutputStream (newFile);
					
					Osw2=new OutputStreamWriter (FOS2); Imageio.write (GET, "jpg", newFile);//This method either saves directly, or saves} catch (FileNotFoundException e) {e.printstacktrace in the following ways (
				);
					}finally{if (bw2!=null) {bw2.close ();}
					if (osw2!=null) {osw2.close ();}
					if (fos2!=null) {fos2.close ();}
					if (br2!=null) {br2.close ();}
				if (isr2!=null) {isr2.close ();} Joptionpane.showmessagedialog (NULL, "saved successfully.)
		    	");
			This.setvisible (TRUE); } catch (Exception exe) {exe.printstacktrace ();
		 }//Selection private void Jbutton1actionactionperformed (ActionEvent event) {this.setvisible (false);
		 New Capturescreenhh ();
	This.setvisible (TRUE);
 }

}


capturescreenhh.java//This code to someone else's

Package captuhhhhh;
Import java.awt.*;
Import java.awt.event.*;

Import javax.swing.*;

Import java.io.*;

Import javax.imageio.*;
Import Com.sun.image.codec.jpeg.JPEGCodec;

Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
Import java.awt.image.*;  /** * @author Jacky.zhu * * */public class CAPTURESCREENHH extends JFrame implements actionlistener{private JButton
  Start,cancel,save;
  private static bufferedimage get;
  /** creates a new instance of Capturescreen * * Public capturescreenhh () {Dostart ();
      private void Dostart () {try{Robot ro=new Robot ();
      Toolkit Tk=toolkit.getdefaulttoolkit ();
      Dimension di=tk.getscreensize ();
      Rectangle rec=new Rectangle (0,0,di.width,di.height);
      BufferedImage bi=ro.createscreencapture (REC);
      JFrame jf=new JFrame ();
      Jf.getcontentpane (). Add (New Temp (jf,bi,di.width,di.height));
      Jf.setundecorated (TRUE);
      Jf.setsize (DI);
      Jf.setvisible (TRUE);
 Jf.setalwaysontop (TRUE);   }catch (Exception exe) {exe.printstacktrace (); } public static void Dosave (BufferedImage get) throws nullpointerexception{try{FileInputStream fis2=null
		;
		InputStreamReader Isr2=null;
		BufferedReader Br2=null;
		FileOutputStream Fos2=null;
		OutputStreamWriter Osw2=null;
		
		BufferedWriter Bw2=null; 
		JFileChooser dlg = new JFileChooser ();
		Dlg.setdialogtype (Jfilechooser.save_dialog);
		
		Dlg.showsavedialog (NULL);
		
		File Getnewfile=dlg.getselectedfile ();
			if (getnewfile==null) {}else{String getnewpath=getnewfile.getpath ();
			String Getnewname=getnewfile.getpath ();
			
			String getnewparent=getnewfile.getparent ();

		        Write new file try {int wideth=get.getwidth (null);//get source map width int height=get.getheight (null);//Get Source graph length
		        Write a new file Newfile=new files (getnewpath);
		        if (!newfile.exists ()) {newfile.createnewfile ();
				} fos2=new FileOutputStream (newFile); Osw2=new outputsTreamwriter (FOS2);  Imageio.write (GET, "jpg", newFile);//This method is either saved directly or saved////With graphics operation, can change the size of the saved image, Wideth,wideth//BufferedImage
Tag = new BufferedImage (WIDETH,WIDETH,BUFFEREDIMAGE.TYPE_INT_RGB); 
Tag.getgraphics (). DrawImage (Get,0,0,wideth,height,null); Fos2=new FileOutputStream (Getnewpath);
Output to file stream//JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (FOS2); Encoder.encode (tag);
			JPEG codec catch (FileNotFoundException e) {e.printstacktrace ();
				}finally{if (bw2!=null) {bw2.close ();}
				if (osw2!=null) {osw2.close ();}
				if (fos2!=null) {fos2.close ();}
				if (br2!=null) {br2.close ();}
			if (isr2!=null) {isr2.close ();} Joptionpane.showmessagedialog (NULL, "saved successfully.)
	    	
		");
    } catch (Exception exe) {exe.printstacktrace ();
    } public void actionperformed (ActionEvent ae) {if (Ae.getsource () ==start) {Dostart (); else if (Ae.getsource () ==cancel) {//system.exit (0);
    else if (Ae.getsource () ==save) {dosave (get);
  } public static void Main (String args[]) {new capturescreenhh (); }}///////capturescreenhh-end---//////////////////////////////////////////////////////////////////////////////// Displays the current screen image class Temp extends JPanel implements mouselistener,mousemotionlistener{enum states{NOR Th_west (New Cursor (Cursor.nw_resize_cursor)),//indicates Northwest corner north (New Cursor (Cursor.n_resize_cursor)), North_east (New Cur Sor (Cursor.ne_resize_cursor)), East (New Cursor (Cursor.e_resize_cursor)), South_east (New Cursor (CURSOR.SE_RESIZE_CU rsor)), SOUTH (New Cursor (Cursor.s_resize_cursor)), South_west (New Cursor (Cursor.sw_resize_cursor)), West (new C
		  Ursor (Cursor.w_resize_cursor)), Move (new Cursor (Cursor.move_cursor)), DEFAULT (New Cursor (cursor.default_cursor));

		  Private Cursor CS;
		  States (Cursor cs) {This.cs=cs;
		  Public Cursor GetCursor () {return CS;
	
 }
		} Private BufferedImage bi;
  private int width,height;
  private int startx,starty,endx,endy,tempx,tempy;
  Private JFrame JF; Private Rectangle select=new Rectangle (0,0,0,0);//indicates the selected area private Cursor cs;//represents the general situation of the mouse state private States Current=stat Es. default;//represents the current edit state private rectangle[] rec;//represents eight edit points in the area public Temp (JFrame jf,bufferedimage bi,int width,int
    HT) {THIS.JF=JF;
    This.bi=bi;
    This.width=width;
    This.height=height;
    This.addmouselistener (this);
    This.addmousemotionlistener (this);
    Image Icon=toolkit.getdefaulttoolkit (). CreateImage (This.getclass (). GetResource ("Icon.png"));
    Cs=toolkit.getdefaulttoolkit (). Createcustomcursor (Icon,new Point (0,0), "icon");
    This.setcursor (CS);
  Initrecs ();
    private void Initrecs () {rec=new rectangle[8];
    for (int i=0;i<rec.length;i++) {rec[i]=new Rectangle ();
    } public void Paintcomponent (Graphics g) {g.drawimage (bi,0,0,width,height,this);
G.setcolor (color.red);    G.drawline (Startx,starty,endx,starty);
    G.drawline (Startx,endy,endx,endy);
    G.drawline (Startx,starty,startx,endy);
    
    
    G.drawline (Endx,starty,endx,endy);
    int x=startx<endx?startx:endx;
    int y=starty<endy?starty:endy;
    Select=new Rectangle (X,y,math.abs (ENDX-STARTX), Math.Abs (Endy-starty));
    int x1= (STARTX+ENDX)/2;
    int y1= (Starty+endy)/2;
    G.fillrect (x1-2,starty-2,5,5);
    G.fillrect (x1-2,endy-2,5,5);
    G.fillrect (startx-2,y1-2,5,5);
    G.fillrect (endx-2,y1-2,5,5);
    G.fillrect (startx-2,starty-2,5,5);
    G.fillrect (startx-2,endy-2,5,5);
    G.fillrect (endx-2,starty-2,5,5);
    G.fillrect (endx-2,endy-2,5,5);
    Rec[0]=new Rectangle (x-5,y-5,10,10);
    Rec[1]=new Rectangle (x1-5,y-5,10,10);
    Rec[2]=new Rectangle ((startx>endx?startx:endx) -5,y-5,10,10);
    Rec[3]=new Rectangle ((startx>endx?startx:endx) -5,y1-5,10,10);
    Rec[4]=new Rectangle (STARTX&GT;ENDX?STARTX:ENDX) -5, (Starty>endy?starty:endy) -5,10,10); Rec[5]=new rectaNgle (X1-5, (Starty>endy?starty:endy) -5,10,10);
    Rec[6]=new Rectangle (x-5, (Starty>endy?starty:endy) -5,10,10);
  Rec[7]=new Rectangle (x-5,y1-5,10,10); public void mousemoved (MouseEvent me) {if Select.contains (Me.getpoint ()) {This.setcursor (new Cursor (Cursor .
      Move_cursor));
    Current=states.move;
      } else{states[] St=states.values ();
          for (int i=0;i<rec.length;i++) {if (Rec[i].contains (Me.getpoint ())) {Current=st[i];
          This.setcursor (St[i].getcursor ());
        Return
      } this.setcursor (CS);
    Current=states.default; } public void mouseexited (MouseEvent me) {} public void mouseentered (MouseEvent me) {} public void MOUs
    Edragged (mouseevent me) {int x=me.getx ();
    int y=me.gety ();
      if (current==states.move) {startx+= (X-TEMPX);
      starty+= (Y-tempy);
      endx+= (X-TEMPX);
      endy+= (Y-tempy);
      Tempx=x;
    Tempy=y; }else if (current==states.easT) {if (startx>endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
        }}else if (Current==states.north) {if (Starty<endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
        }}else if (current==states.west) {if (startx<endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
        }}else if (Current==states.south) {if (Starty>endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
        } else if (current==states.north_east) {if (startx>endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
        } if (Starty<endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
}}else if (current==states.north_west) {      if (startx<endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
        } if (Starty<endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
        }}else if (current==states.south_east) {if (Starty>endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
        } if (startx>endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
        }}else if (current==states.south_west) {if (Starty>endy) {starty+= (y-tempy);
      Tempy=y;
        }else{endy+= (y-tempy);
      Tempy=y;
        } if (startx<endx) {startx+= (X-TEMPX);
      Tempx=x;
        } else{endx+= (X-TEMPX);
      Tempx=x;
      }} else{startx=tempx;
      Starty=tempy;
      Endx=me.getx ();
    Endy=me.gety (); } This. repaint ();
    public void mousepressed (MouseEvent me) {tempx=me.getx ();
  Tempy=me.gety ();
    public void mousereleased (MouseEvent me) {System.out.println ("mousereleased");
        if (Me.ispopuptrigger ()) {if (current==states.move) {startx=0;
        starty=0;
        endx=0;
        endy=0;
      Repaint ();
      } else{Jf.dispose ();
    }} public void mouseclicked (MouseEvent me) {bufferedimage get; if (Me.getclickcount () ==2) {//rectangle rec=new Rectangle (startx,starty,math.abs), Endx-startx (math.abs
      ));
      Point P=me.getpoint (); if (Select.contains (p)) {if (Select.x+select.width<this.getwidth () &&select.y+select.height<
          This.getheight ()) {get=bi.getsubimage (select.x,select.y,select.width,select.height);
          Jf.dispose ();
          Save.setenabled (TRUE);
        Capturescreenhh.dosave (get);
      }else{int wid=select.width,het=select.height;    if (Select.x+select.width>=this.getwidth ()) {wid=this.getwidth ()-select.x;
          } if (Select.y+select.height>=this.getheight ()) {het=this.getheight ()-select.y;
          } get=bi.getsubimage (Select.x,select.y,wid,het);
          Jf.dispose ();
          Save.setenabled (TRUE);
        Capturescreenhh.dosave (get);

 }
      }
    }
  }
}


 

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.