Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AWTException;
- import java.awt.BorderLayout;
- import java.awt.Panel;
- 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.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- //l'class té5ou kol chay m'JFRAME (pre-definis)
- //JFrame f java ya3ni "fenetre" fiha l'5orm mta3 l'fenetre lkol
- //(kaber, sa8er, sakker, taille, position, visibilité, etc...)
- public class ScreenCapture extends JFrame {
- // ta3mel button
- JButton button = new JButton("Save screen");
- // bech t7othom fi JPanel fih BorderLayout (layouy ya3ni l'organisation
- // visuel, imin, isar, fou9, ta7t, etc..)
- JPanel p = new JPanel(new BorderLayout());
- // Panneau principal mté3ék bech t7ottou fel JFrame (l'class mté3ék aslan
- // JFrame "extends)
- Panel panel = new Panel();
- public ScreenCapture() {
- // Class pre-definis tésta3mélha bech l'program ywalli ycapti les events
- // ex : click, , tsa8ar l'fenetre, tkabarha, t5abbiha, etc.. tajjém fi
- // kol 7kéya tsir t9ollou a3mél 7aja
- // ex: howa y5abbi l'program w énti traj3ou w t9ollou "l'3ASBA :D "
- ListenForEvents lfe = new ListenForEvents();
- // el variables mta3 toul w el 3ordh mta3 l"fenetre mté3ék
- int width = 200;
- int height = 100;
- // 7otho fel fentere, this héya l'class, fiha "setSize" 5aterha
- // mé5dhétha m'JFrame
- this.setSize(width, height);
- // el variable mta3 el position
- // Toolkit class 9ad rab rassék, googelha sinon ab3atha tnayék
- // lahné st7a99itha béch né5ou taille mta3 l'écran béch n7ot el fenetre
- // par defaut tét7al fel wost w louta chway (dabbér fi zebbi)
- int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
- int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
- // X = largeurEcran/2 - largeurFenetre/2
- // Y = HauteurEcran/2 - HauteurFenetre/2
- this.setLocation(screenW / 2 - width / 2, screenH / 2 + height * 2);
- // parametre mta3 Jframe (ahawkom wadh7in)
- this.setAlwaysOnTop(true);
- this.setResizable(false);
- this.setVisible(true);
- // kén ma t7ottéch hédhi, ki ténzél 3al X l'7amra l'program yo93éd
- // yé5dém fel background
- // tajjém troddou yétsakker b'9ofla o5ra mén 3andék sinon 7ot l'X par
- // defaut
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // ajouti l'class mta3 l'events lel bouton, sinon el bouton ma ya3mel
- // chay
- button.addActionListener(lfe);
- // ajouti l'bouton l'JPanel, el position mté3ou mén fou9 (NORTH)
- p.add(button, BorderLayout.NORTH);
- // ajouti lkol lel panneau principal fel wost (CENTER)
- panel.add(p, BorderLayout.CENTER);
- // ajouti l'paneau principal lel class (JFRAME)
- this.add(panel);
- // yup tajjém t7othom toul mén 8ir taksir rass, normalement hné ma
- // tést7a9héch 5atrou program s8ir éma norlament anyék program béch t7ot
- // fih +5 panneau
- }
- // l'class mta3 l'event, té5ou kol chay mel ActionListener (googlih)
- private class ListenForEvents implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- // 3andék kén source wa7da fel program hédha, el button
- // el button yraja3 true wa9t t'clicki 3lih
- // donc ki t'clicki 3ayét lel method shot() li ta3mél l'capture
- if (e.getSource() == button) {
- shot();
- }
- }
- }
- // méthod capture ecran
- private void shot() {
- // 7ot kol chay fi block Try Catch 5ater té5dém bel les fichier
- // try-Catch = ki l'code mté3ék fih 8alta walla utilisateur y7ot chemin
- // 8alét
- // el program ma ya9éfch, ykammél yé5dém éma fi 3oudh ma y3ayyét lel
- // bloc elli fi wost try {}
- // ywalli y3ayét lel bloc li fel catch{}
- // généralement fel catch béch t7ot message d'erreur
- try {
- Robot robot = new Robot();
- // Create image (capture)
- BufferedImage bi = robot.createScreenCapture(new Rectangle(Toolkit
- .getDefaultToolkit().getScreenSize()));
- // location mta3 l'enregistrement
- // rodha entrer mén 3and l'utilisateur (zid JTextArea w tsarref fél
- // bé9i)
- ImageIO.write(bi, "png", new File("C:/shot.png"));
- JOptionPane.showMessageDialog(null, "Enregistrer", "Titre",
- JOptionPane.INFORMATION_MESSAGE);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- new ScreenCapture();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement