Advertisement
Mouamle

Mouse Stuff code

May 26th, 2016
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.04 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.Color;
  3. import java.awt.Dialog.ModalExclusionType;
  4. import java.awt.Font;
  5. import java.awt.MouseInfo;
  6. import java.awt.Point;
  7. import java.awt.PointerInfo;
  8. import java.awt.Robot;
  9. import java.awt.Toolkit;
  10. import java.awt.Window.Type;
  11. import java.awt.datatransfer.Clipboard;
  12. import java.awt.datatransfer.StringSelection;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15.  
  16. import javax.swing.JButton;
  17. import javax.swing.JCheckBox;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JSlider;
  22. import javax.swing.JTextField;
  23. import javax.swing.SwingConstants;
  24. import javax.swing.border.BevelBorder;
  25. import javax.swing.border.MatteBorder;
  26.  
  27.  
  28. public class MMouse {
  29.  
  30.     private JFrame frame;
  31.     private final JLabel X = new JLabel("X :");
  32.     private final JLabel Y = new JLabel("Y :");
  33.     private final JLabel Point = new JLabel("(, )");
  34.     private final JPanel panel_1 = new JPanel();
  35.     private Robot robot;
  36.     private final JPanel ColorPanel = new JPanel();
  37.     private final JPanel panel_2 = new JPanel();
  38.     private final JLabel R = new JLabel("R = ");
  39.     private final JLabel G = new JLabel("G = ");
  40.     private final JLabel B = new JLabel("B = ");
  41.     private int x, y = 0;  
  42.     private int r,g,b = 0;
  43.     private final JPanel CopyColor = new JPanel();
  44.     private final JPanel panel_3 = new JPanel();
  45.     private JTextField txtRgb;
  46.     private final JPanel panel_4 = new JPanel();
  47.     private final JLabel lblNewLabel = new JLabel("Colors generator");
  48.     private final JPanel GenResult = new JPanel();
  49.     private final JLabel GenLR = new JLabel("R");
  50.     private final JSlider GenR = new JSlider();
  51.     private final JSlider GenG = new JSlider();
  52.     private final JSlider GenB = new JSlider();
  53.     private final JLabel GenLG = new JLabel("G");
  54.     private final JLabel GenLB = new JLabel("B");
  55.     private final JPanel panel_5 = new JPanel();
  56.     private final JButton button = new JButton("Copy");
  57.    
  58.  
  59.     public static void main(String[] args) {
  60.         MMouse window = new MMouse();
  61.         window.frame.setVisible(true);
  62.     }
  63.    
  64.     private boolean isMouseInside(JFrame f, Point p){
  65.         if ( p.x > f.getX() && p.x <= f.getX() + f.getWidth() && p.y > f.getY() && p.y <= f.getY() + f.getHeight() )
  66.             return true;
  67.         return false;
  68.     }
  69.    
  70.     private void copy(String text){
  71.         StringSelection selection = new StringSelection(text);
  72.         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  73.         clipboard.setContents(selection, selection);
  74.         txtRgb.setText(text);
  75.     }
  76.  
  77.     public MMouse() {
  78.         initialize();
  79.        
  80.         Thread ColorsGen = new Thread(
  81.             new Runnable() {
  82.                 public void run() {
  83.                     while ( true ){
  84.                         int R = GenR.getValue();
  85.                         int G = GenG.getValue();
  86.                         int B = GenB.getValue();
  87.                         GenLR.setText("R " + R);
  88.                         GenLG.setText("G " + G);
  89.                         GenLB.setText("B " + B);
  90.                        
  91.                         GenResult.setBackground(new Color(R, G, B));
  92.                         //try { Thread.sleep(500); } catch (InterruptedException e) {}
  93.                     }
  94.                 }
  95.             }
  96.         );
  97.        
  98.         Thread Colors = new Thread(
  99.             new Runnable() {
  100.                 public void run() {
  101.                     PointerInfo inf = MouseInfo.getPointerInfo();
  102.                     try {
  103.                         robot = new Robot(inf.getDevice());
  104.                     } catch (AWTException e) { e.printStackTrace(); }
  105.                     while ( true ) {
  106.                         //Point p = MouseInfo.getPointerInfo().getLocation();
  107.                         if (!isMouseInside(frame, MouseInfo.getPointerInfo().getLocation() )){
  108.                             Color c = robot.getPixelColor(x, y);
  109.                             r = c.getRed();
  110.                             g = c.getGreen();
  111.                             b = c.getBlue();
  112.                             R.setText("R = " + r);
  113.                             G.setText("G = " + g);
  114.                             B.setText("B = " + b);
  115.                             ColorPanel.setBackground(c);
  116.                             try { Thread.sleep(30); } catch (InterruptedException e) {}
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.         );
  122.            
  123.         Thread posititons = new Thread(
  124.                 new Runnable() {
  125.                     public void run() {
  126.                         PointerInfo inf = MouseInfo.getPointerInfo();
  127.                         while(true){
  128.                             inf = MouseInfo.getPointerInfo();
  129.                             Point p = inf.getLocation();
  130.                             //Color pixelColor = robot.getPixelColor(p.x, p.y);
  131.                             if ( !isMouseInside(frame, MouseInfo.getPointerInfo().getLocation()) )
  132.                                 x = (int)p.getX();
  133.                                 y = (int)p.getY();
  134.                                 X.setText("X : " + (int)p.getX());
  135.                                 Y.setText("Y : " + (int)p.getY());
  136.                                 Point.setText("( " + (int)p.getX() + ", " + (int)p.getY() + ")");
  137.                         }
  138.                     }
  139.                 }
  140.             );
  141.         posititons.start();
  142.         Colors.start();
  143.         ColorsGen.start();
  144.     }
  145.  
  146.     private void initialize() {
  147.         frame = new JFrame();
  148.         frame.setTitle("No Name program");
  149.         frame.setType(Type.POPUP);
  150.         frame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
  151.         frame.setBounds(100, 100, 575, 469);
  152.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  153.         frame.getContentPane().setLayout(null);
  154.         frame.setResizable(false);
  155.        
  156.        
  157.         panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
  158.         panel_1.setBounds(10, 137, 274, 292);
  159.        
  160.         frame.getContentPane().add(panel_1);
  161.         panel_1.setLayout(null);
  162.         ColorPanel.setBounds(10, 179, 254, 102);
  163.        
  164.         panel_1.add(ColorPanel);
  165.         panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
  166.         panel_2.setBounds(10, 11, 254, 157);
  167.        
  168.         panel_1.add(panel_2);
  169.         panel_2.setLayout(null);
  170.         R.setFont(new Font("Tahoma", Font.BOLD, 18));
  171.         R.setBounds(10, 11, 80, 22);
  172.        
  173.         panel_2.add(R);
  174.         G.setFont(new Font("Tahoma", Font.BOLD, 18));
  175.         G.setBounds(10, 44, 80, 22);
  176.        
  177.         panel_2.add(G);
  178.         B.setFont(new Font("Tahoma", Font.BOLD, 18));
  179.         B.setBounds(10, 77, 80, 22);
  180.        
  181.         panel_2.add(B);
  182.        
  183.         JButton btnNewButton = new JButton("Copy");
  184.         btnNewButton.addActionListener(new ActionListener() {
  185.             public void actionPerformed(ActionEvent e) {
  186.                 copy("rgb(" + r + ", " + g + ", " + b + ");");
  187.                 CopyColor.setBackground(new Color(r,g,b));
  188.             }
  189.         });
  190.         btnNewButton.setBounds(10, 110, 234, 36);
  191.         panel_2.add(btnNewButton);
  192.         panel_3.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
  193.         panel_3.setBounds(294, 11, 265, 164);
  194.        
  195.         frame.getContentPane().add(panel_3);
  196.         panel_3.setLayout(null);
  197.         CopyColor.setBounds(10, 70, 245, 83);
  198.         panel_3.add(CopyColor);
  199.        
  200.         txtRgb = new JTextField();
  201.         txtRgb.setEditable(false);
  202.         txtRgb.setFont(new Font("Tahoma", Font.BOLD, 18));
  203.         txtRgb.setHorizontalAlignment(SwingConstants.CENTER);
  204.         txtRgb.setText("rgb(255, 255, 255);");
  205.         txtRgb.setBounds(10, 11, 245, 48);
  206.         panel_3.add(txtRgb);
  207.         txtRgb.setColumns(10);
  208.         panel_4.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
  209.         panel_4.setBounds(294, 211, 265, 218);
  210.        
  211.         frame.getContentPane().add(panel_4);
  212.         panel_4.setLayout(null);
  213.         GenResult.setBounds(10, 122, 245, 38);
  214.        
  215.         panel_4.add(GenResult);
  216.         GenLR.setHorizontalAlignment(SwingConstants.CENTER);
  217.         GenLR.setBounds(10, 11, 40, 26);
  218.        
  219.         panel_4.add(GenLR);
  220.        
  221.         GenR.setSnapToTicks(true);
  222.         GenR.setPaintLabels(true);
  223.         GenR.setValue(0);
  224.         GenR.setMaximum(255);
  225.         GenR.setBounds(60, 11, 195, 26);
  226.         panel_4.add(GenR);
  227.         GenG.setValue(0);
  228.         GenG.setSnapToTicks(true);
  229.         GenG.setPaintLabels(true);
  230.         GenG.setMaximum(255);
  231.         GenG.setBounds(60, 48, 195, 26);
  232.        
  233.         panel_4.add(GenG);
  234.         GenB.setValue(0);
  235.         GenB.setSnapToTicks(true);
  236.         GenB.setPaintLabels(true);
  237.         GenB.setMaximum(255);
  238.         GenB.setBounds(60, 85, 195, 26);
  239.        
  240.         panel_4.add(GenB);
  241.         GenLG.setHorizontalAlignment(SwingConstants.CENTER);
  242.         GenLG.setBounds(10, 48, 40, 26);
  243.        
  244.         panel_4.add(GenLG);
  245.         GenLB.setHorizontalAlignment(SwingConstants.CENTER);
  246.         GenLB.setBounds(10, 85, 40, 26);
  247.        
  248.         panel_4.add(GenLB);
  249.         button.addActionListener(new ActionListener() {
  250.             public void actionPerformed(ActionEvent e) {
  251.                 r = GenR.getValue();
  252.                 g = GenG.getValue();
  253.                 b = GenB.getValue();
  254.                 copy("rgb(" + r + ", " + g + ", " + b + ");");
  255.                 CopyColor.setBackground(new Color(r,g,b));
  256.             }
  257.         });
  258.         button.setBounds(10, 171, 245, 36);
  259.        
  260.         panel_4.add(button);
  261.         lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  262.         lblNewLabel.setBounds(294, 186, 215, 14);
  263.        
  264.         frame.getContentPane().add(lblNewLabel);
  265.         panel_5.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
  266.         panel_5.setBounds(10, 11, 274, 115);
  267.        
  268.         frame.getContentPane().add(panel_5);
  269.         panel_5.setLayout(null);
  270.        
  271.         JCheckBox AlwaysOnTop = new JCheckBox("Always On Top");
  272.         AlwaysOnTop.setBounds(6, 7, 165, 30);
  273.         panel_5.add(AlwaysOnTop);
  274.         AlwaysOnTop.addActionListener(new ActionListener() {
  275.             public void actionPerformed(ActionEvent e) {
  276.                 if ( AlwaysOnTop.isSelected() ) {
  277.                     frame.setAlwaysOnTop(true);
  278.                 }else{
  279.                     frame.setAlwaysOnTop(false);
  280.                 }
  281.             }
  282.         });
  283.         AlwaysOnTop.setFont(new Font("Tahoma", Font.BOLD, 18));
  284.         X.setBounds(6, 44, 80, 30);
  285.         panel_5.add(X);
  286.        
  287.         X.setFont(new Font("Tahoma", Font.BOLD, 18));
  288.         Y.setBounds(6, 74, 80, 30);
  289.         panel_5.add(Y);
  290.        
  291.         Y.setFont(new Font("Tahoma", Font.BOLD, 18));
  292.         Point.setBounds(96, 44, 168, 60);
  293.         panel_5.add(Point);
  294.        
  295.         Point.setHorizontalAlignment(SwingConstants.CENTER);
  296.         Point.setFont(new Font("Tahoma", Font.PLAIN, 30));
  297.        
  298.        
  299.     }
  300.    
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement