Advertisement
alaslipknot

ScreenCapture

Nov 1st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.BorderLayout;
  3. import java.awt.Panel;
  4. import java.awt.Rectangle;
  5. import java.awt.Robot;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.IOException;
  12.  
  13. import javax.imageio.ImageIO;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18.  
  19. //l'class té5ou kol chay m'JFRAME (pre-definis)
  20. //JFrame f java ya3ni "fenetre" fiha l'5orm mta3 l'fenetre lkol
  21. //(kaber, sa8er, sakker, taille, position, visibilité, etc...)
  22. public class ScreenCapture extends JFrame {
  23.  
  24.     // ta3mel button
  25.     JButton button = new JButton("Save screen");
  26.     // bech t7othom fi JPanel fih BorderLayout (layouy ya3ni l'organisation
  27.     // visuel, imin, isar, fou9, ta7t, etc..)
  28.     JPanel p = new JPanel(new BorderLayout());
  29.     // Panneau principal mté3ék bech t7ottou fel JFrame (l'class mté3ék aslan
  30.     // JFrame "extends)
  31.     Panel panel = new Panel();
  32.  
  33.     public ScreenCapture() {
  34.  
  35.         // Class pre-definis tésta3mélha bech l'program ywalli ycapti les events
  36.         // ex : click, , tsa8ar l'fenetre, tkabarha, t5abbiha, etc.. tajjém fi
  37.         // kol 7kéya tsir t9ollou a3mél 7aja
  38.         // ex: howa y5abbi l'program w énti traj3ou w t9ollou "l'3ASBA :D "
  39.         ListenForEvents lfe = new ListenForEvents();
  40.  
  41.         // el variables mta3 toul w el 3ordh mta3 l"fenetre mté3ék
  42.         int width = 200;
  43.         int height = 100;
  44.         // 7otho fel fentere, this héya l'class, fiha "setSize" 5aterha
  45.         // mé5dhétha m'JFrame
  46.         this.setSize(width, height);
  47.  
  48.         // el variable mta3 el position
  49.         // Toolkit class 9ad rab rassék, googelha sinon ab3atha tnayék
  50.         // lahné st7a99itha béch né5ou taille mta3 l'écran béch n7ot el fenetre
  51.         // par defaut tét7al fel wost w louta chway (dabbér fi zebbi)
  52.         int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
  53.         int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
  54.         // X = largeurEcran/2 - largeurFenetre/2
  55.         // Y = HauteurEcran/2 - HauteurFenetre/2
  56.         this.setLocation(screenW / 2 - width / 2, screenH / 2 + height * 2);
  57.         // parametre mta3 Jframe (ahawkom wadh7in)
  58.         this.setAlwaysOnTop(true);
  59.         this.setResizable(false);
  60.         this.setVisible(true);
  61.         // kén ma t7ottéch hédhi, ki ténzél 3al X l'7amra l'program yo93éd
  62.         // yé5dém fel background
  63.         // tajjém troddou yétsakker b'9ofla o5ra mén 3andék sinon 7ot l'X par
  64.         // defaut
  65.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.  
  67.         // ajouti l'class mta3 l'events lel bouton, sinon el bouton ma ya3mel
  68.         // chay
  69.         button.addActionListener(lfe);
  70.         // ajouti l'bouton l'JPanel, el position mté3ou mén fou9 (NORTH)
  71.         p.add(button, BorderLayout.NORTH);
  72.         // ajouti lkol lel panneau principal fel wost (CENTER)
  73.         panel.add(p, BorderLayout.CENTER);
  74.         // ajouti l'paneau principal lel class (JFRAME)
  75.         this.add(panel);
  76.         // yup tajjém t7othom toul mén 8ir taksir rass, normalement hné ma
  77.         // tést7a9héch 5atrou program s8ir éma norlament anyék program béch t7ot
  78.         // fih +5 panneau
  79.  
  80.     }
  81.  
  82.     // l'class mta3 l'event, té5ou kol chay mel ActionListener (googlih)
  83.     private class ListenForEvents implements ActionListener {
  84.         public void actionPerformed(ActionEvent e) {
  85.             // 3andék kén source wa7da fel program hédha, el button
  86.             // el button yraja3 true wa9t t'clicki 3lih
  87.             // donc ki t'clicki 3ayét lel method shot() li ta3mél l'capture
  88.             if (e.getSource() == button) {
  89.                 shot();
  90.             }
  91.         }
  92.  
  93.     }
  94.  
  95.     // méthod capture ecran
  96.     private void shot() {
  97.  
  98.         // 7ot kol chay fi block Try Catch 5ater té5dém bel les fichier
  99.         // try-Catch = ki l'code mté3ék fih 8alta walla utilisateur y7ot chemin
  100.         // 8alét
  101.         // el program ma ya9éfch, ykammél yé5dém éma fi 3oudh ma y3ayyét lel
  102.         // bloc elli fi wost try {}
  103.         // ywalli y3ayét lel bloc li fel catch{}
  104.         // généralement fel catch béch t7ot message d'erreur
  105.         try {
  106.  
  107.             Robot robot = new Robot();
  108.             // Create image (capture)
  109.             BufferedImage bi = robot.createScreenCapture(new Rectangle(Toolkit
  110.                     .getDefaultToolkit().getScreenSize()));
  111.             // location mta3 l'enregistrement
  112.             // rodha entrer mén 3and l'utilisateur (zid JTextArea w tsarref fél
  113.             // bé9i)
  114.             ImageIO.write(bi, "png", new File("C:/shot.png"));
  115.             JOptionPane.showMessageDialog(null, "Enregistrer", "Titre",
  116.                     JOptionPane.INFORMATION_MESSAGE);
  117.         } catch (IOException e) {
  118.             e.printStackTrace();
  119.         } catch (AWTException e) {
  120.  
  121.             e.printStackTrace();
  122.         }
  123.     }
  124.  
  125.    
  126.    
  127.     public static void main(String[] args) {
  128.         new ScreenCapture();
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement