Showing posts with label awt. Show all posts
Showing posts with label awt. Show all posts

Thursday, January 7, 2016

Capture screen using java.awt.Robot

Java example to capture screen using java.awt.Robot:

package javarobotcapturescreen;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaRobotCaptureScreen {

    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            
            Toolkit myToolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = myToolkit.getScreenSize();
            
            Rectangle screen = new Rectangle(screenSize);
            
            BufferedImage screenFullImage = robot.createScreenCapture(screen);
            ImageIO.write(screenFullImage, "jpg", new File("screen.jpg"));
            
        } catch (AWTException | IOException ex) {
            Logger.getLogger(JavaRobotCaptureScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}




captured screen:


Get screen size and resolution, with java.awt.Toolkit

Java example to get screen size and resolution, with java.awt.Toolkit:

package javagetscreensize;

import java.awt.Dimension;
import java.awt.Toolkit;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaGetScreenSize {

    public static void main(String[] args) {
        Toolkit myToolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = myToolkit.getScreenSize();
        int screenResolution = myToolkit.getScreenResolution();
        System.out.println("Screen size = " + screenSize.width + " x " + screenSize.height);
        System.out.println("Screen Resolution: " + screenResolution + " dots-per-inch");
    }
    
}



Saturday, December 21, 2013

Example of using KeyListener

Example to implement KeyListener for Swing UI elements.
Example of using KeyListener
Example of using KeyListener

package javamyframe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaMyFrame extends JFrame 
    implements KeyListener{

    JTextArea textArea;
    JButton buttonClear;
    
    String s = "";
    
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JavaMyFrame myFrame = new JavaMyFrame();

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myFrame.prepareUI();

        myFrame.pack();
        myFrame.setVisible(true);
    }
    
    private void prepareUI(){
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.addKeyListener(this);
        JScrollPane panel = new JScrollPane(textArea);
        panel.setPreferredSize(new Dimension(300, 100));
        
        buttonClear = new JButton("Clear");
        buttonClear.addActionListener(anotherActionListener);
        
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(buttonClear, BorderLayout.PAGE_END);
    }
    
    ActionListener anotherActionListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            s = "";
            textArea.setText(s);
        }
        
    };

    @Override
    public void keyTyped(KeyEvent e) {
        s += e.getKeyChar();
        textArea.setText(s);
    }

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}
}


Related: Example of using ActionListener

Example of using ActionListener

Example to implement ActionListener for Swing UI elements.

Example of using ActionListener
Example of using ActionListener

package javamyframe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaMyFrame extends JFrame 
    implements ActionListener{

    JTextArea textArea;
    JButton button1, button2;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JavaMyFrame myFrame = new JavaMyFrame();

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myFrame.prepareUI();

        myFrame.pack();
        myFrame.setVisible(true);
    }
    
    private void prepareUI(){
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane panel = new JScrollPane(textArea);
        panel.setPreferredSize(new Dimension(300, 100));
        
        button1 = new JButton("My Button 1");
        button1.addActionListener(this);
        button2 = new JButton("My Button 2");
        button2.addActionListener(anotherActionListener);
        
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(button1, BorderLayout.PAGE_START);
        getContentPane().add(button2, BorderLayout.PAGE_END);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        textArea.setText(e.getActionCommand());
    }
    
    ActionListener anotherActionListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.setText("anotherActionListener: " 
                    + e.getActionCommand());
        }
        
    };
}


Related: Example of using KeyListener