Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coloronmouse;
- import java.awt.AWTException;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.GridLayout;
- import java.awt.MouseInfo;
- import java.awt.Point;
- import java.awt.Robot;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashSet;
- import java.util.Set;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.Timer;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- import javax.swing.filechooser.FileNameExtensionFilter;
- /**
- * This class checks the position every #DELAY milliseconds and
- * informs all registered MouseMotionListeners about position updates.
- */
- public class NewClass {
- /* the resolution of the mouse motion */
- private static final int DELAY = 100;
- private static JTextField red,blue,green,mousex,mousey,desc;
- private static JButton save;
- private static String filechosen;
- private Component component;
- private Timer timer;
- private Set<MouseMotionListener> mouseMotionListeners;
- protected NewClass(Component component) {
- if (component == null) {
- throw new IllegalArgumentException("Null component not allowed.");
- }
- this.component = component;
- /* poll mouse coordinates at the given rate */
- timer = new Timer(DELAY, new ActionListener() {
- private Point lastPoint = MouseInfo.getPointerInfo().getLocation();
- /* called every DELAY milliseconds to fetch the
- * current mouse coordinates */
- public synchronized void actionPerformed(ActionEvent e) {
- Point point = MouseInfo.getPointerInfo().getLocation();
- if (!point.equals(lastPoint)) {
- fireMouseMotionEvent(point);
- }
- lastPoint = point;
- }
- });
- mouseMotionListeners = new HashSet<MouseMotionListener>();
- }
- public Component getComponent() {
- return component;
- }
- public void start() {
- timer.start();
- }
- public static void lookAndFeel()
- {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
- }
- }
- public void stop() {
- timer.stop();
- }
- public void addMouseMotionListener(MouseMotionListener listener) {
- synchronized (mouseMotionListeners) {
- mouseMotionListeners.add(listener);
- }
- }
- public void removeMouseMotionListener(MouseMotionListener listener) {
- synchronized (mouseMotionListeners) {
- mouseMotionListeners.remove(listener);
- }
- }
- protected void fireMouseMotionEvent(Point point) {
- synchronized (mouseMotionListeners) {
- for (final MouseMotionListener listener : mouseMotionListeners) {
- final MouseEvent event =
- new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
- 0, point.x, point.y, 0, false);
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- listener.mouseMoved(event);
- }
- });
- }
- }
- }
- public static File openFile(){
- File file = null;
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Select a .txt file to save to");
- FileNameExtensionFilter filter = new FileNameExtensionFilter(
- ".txt", "txt");
- fileChooser.setFileFilter(filter);
- int returnValue = fileChooser.showOpenDialog(null);
- if(returnValue == JFileChooser.APPROVE_OPTION) {
- file = fileChooser.getSelectedFile();
- filechosen = fileChooser.getSelectedFile().getAbsolutePath();
- }
- return file;
- }
- public static void saveOutput()
- {
- try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filechosen, true)))) {
- out.println(desc.getText());
- out.println("X: "+mousex.getText()+" Y: "+mousey.getText()+" Red: "
- +red.getText()+" Green: "+green.getText()+
- " Blue"+blue.getText()+"\n");
- }catch (IOException e) {
- }
- }
- /**
- * adds east panel
- * @return
- */
- public static JPanel addEast()
- {
- JPanel p = new JPanel();
- p.setLayout(new GridLayout(3,1));
- JPanel p1 = new JPanel();
- save = new JButton("save");
- p1.add(save);
- class ExitListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- saveOutput();
- }
- }
- save.addActionListener(new ExitListener());
- desc = new JTextField(10);
- JPanel p2 = new JPanel();
- JPanel space = new JPanel();
- p2.add(desc);
- p.add(space);
- p.add(p2);
- p.add(p1);
- return p;
- }
- /**
- * adds the fields
- * @return
- */
- public static JPanel addFields()
- {
- JPanel p = new JPanel();
- p.setLayout(new GridLayout(4,1));
- JPanel p1 = new JPanel();
- JLabel l1 = new JLabel("red");
- red = new JTextField(3);
- p1.add(l1);
- p1.add(red);
- p.add(p1);
- JPanel p2 = new JPanel();
- JLabel l2 = new JLabel("green");
- green = new JTextField(3);
- p2.add(l2);
- p2.add(green);
- p.add(p2);
- JPanel p3 = new JPanel();
- JLabel l3 = new JLabel("blue");
- blue = new JTextField(3);
- p3.add(l3);
- p3.add(blue);
- p.add(p3);
- JPanel p4 = new JPanel();
- p4.setLayout(new GridLayout(2,1));
- JPanel p5 = new JPanel();
- JPanel p6 = new JPanel();
- JLabel l4 = new JLabel("mouse location");
- mousex = new JTextField(4);
- mousey = new JTextField(4);
- p5.add(l4);
- p6.add(mousex);
- p6.add(mousey);
- p4.add(p5);
- p4.add(p6);
- p.add(p4);
- return p;
- }
- /* Testing the ovserver */
- public static void main(String[] args) throws AWTException {
- NewClass.lookAndFeel();
- JFrame main = new JFrame("Color Finder");
- main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- main.setSize(275,300);
- main.setVisible(true);
- main.setLayout(new BorderLayout());
- main.add(addFields(),BorderLayout.WEST);
- main.add(addEast(),BorderLayout.EAST);
- main.getRootPane().setDefaultButton(save);
- NewClass.openFile();
- main.setVisible(true);
- Robot robot = new Robot();
- NewClass mo = new NewClass(main);
- mo.addMouseMotionListener(new MouseMotionListener() {
- public void mouseMoved(MouseEvent e) {
- mousex.setText(new Integer(e.getX()).toString());
- mousey.setText(new Integer(e.getY()).toString());
- Color c = robot.getPixelColor(new Integer(e.getX()),
- new Integer(e.getY()));
- red.setText(new Integer(c.getRed()).toString());
- blue.setText(new Integer(c.getBlue()).toString());
- green.setText(new Integer(c.getGreen()).toString());
- }
- public void mouseDragged(MouseEvent e) {
- System.out.println("mouse dragged: " + e.getPoint());
- }
- });
- mo.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement