Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AWTException;
- import java.awt.Color;
- import java.awt.Dialog.ModalExclusionType;
- import java.awt.Font;
- import java.awt.MouseInfo;
- import java.awt.Point;
- import java.awt.PointerInfo;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.Window.Type;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSlider;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- import javax.swing.border.BevelBorder;
- import javax.swing.border.MatteBorder;
- public class MMouse {
- private JFrame frame;
- private final JLabel X = new JLabel("X :");
- private final JLabel Y = new JLabel("Y :");
- private final JLabel Point = new JLabel("(, )");
- private final JPanel panel_1 = new JPanel();
- private Robot robot;
- private final JPanel ColorPanel = new JPanel();
- private final JPanel panel_2 = new JPanel();
- private final JLabel R = new JLabel("R = ");
- private final JLabel G = new JLabel("G = ");
- private final JLabel B = new JLabel("B = ");
- private int x, y = 0;
- private int r,g,b = 0;
- private final JPanel CopyColor = new JPanel();
- private final JPanel panel_3 = new JPanel();
- private JTextField txtRgb;
- private final JPanel panel_4 = new JPanel();
- private final JLabel lblNewLabel = new JLabel("Colors generator");
- private final JPanel GenResult = new JPanel();
- private final JLabel GenLR = new JLabel("R");
- private final JSlider GenR = new JSlider();
- private final JSlider GenG = new JSlider();
- private final JSlider GenB = new JSlider();
- private final JLabel GenLG = new JLabel("G");
- private final JLabel GenLB = new JLabel("B");
- private final JPanel panel_5 = new JPanel();
- private final JButton button = new JButton("Copy");
- public static void main(String[] args) {
- MMouse window = new MMouse();
- window.frame.setVisible(true);
- }
- private boolean isMouseInside(JFrame f, Point p){
- if ( p.x > f.getX() && p.x <= f.getX() + f.getWidth() && p.y > f.getY() && p.y <= f.getY() + f.getHeight() )
- return true;
- return false;
- }
- private void copy(String text){
- StringSelection selection = new StringSelection(text);
- Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- clipboard.setContents(selection, selection);
- txtRgb.setText(text);
- }
- public MMouse() {
- initialize();
- Thread ColorsGen = new Thread(
- new Runnable() {
- public void run() {
- while ( true ){
- int R = GenR.getValue();
- int G = GenG.getValue();
- int B = GenB.getValue();
- GenLR.setText("R " + R);
- GenLG.setText("G " + G);
- GenLB.setText("B " + B);
- GenResult.setBackground(new Color(R, G, B));
- //try { Thread.sleep(500); } catch (InterruptedException e) {}
- }
- }
- }
- );
- Thread Colors = new Thread(
- new Runnable() {
- public void run() {
- PointerInfo inf = MouseInfo.getPointerInfo();
- try {
- robot = new Robot(inf.getDevice());
- } catch (AWTException e) { e.printStackTrace(); }
- while ( true ) {
- //Point p = MouseInfo.getPointerInfo().getLocation();
- if (!isMouseInside(frame, MouseInfo.getPointerInfo().getLocation() )){
- Color c = robot.getPixelColor(x, y);
- r = c.getRed();
- g = c.getGreen();
- b = c.getBlue();
- R.setText("R = " + r);
- G.setText("G = " + g);
- B.setText("B = " + b);
- ColorPanel.setBackground(c);
- try { Thread.sleep(30); } catch (InterruptedException e) {}
- }
- }
- }
- }
- );
- Thread posititons = new Thread(
- new Runnable() {
- public void run() {
- PointerInfo inf = MouseInfo.getPointerInfo();
- while(true){
- inf = MouseInfo.getPointerInfo();
- Point p = inf.getLocation();
- //Color pixelColor = robot.getPixelColor(p.x, p.y);
- if ( !isMouseInside(frame, MouseInfo.getPointerInfo().getLocation()) )
- x = (int)p.getX();
- y = (int)p.getY();
- X.setText("X : " + (int)p.getX());
- Y.setText("Y : " + (int)p.getY());
- Point.setText("( " + (int)p.getX() + ", " + (int)p.getY() + ")");
- }
- }
- }
- );
- posititons.start();
- Colors.start();
- ColorsGen.start();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setTitle("No Name program");
- frame.setType(Type.POPUP);
- frame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
- frame.setBounds(100, 100, 575, 469);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- frame.setResizable(false);
- panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
- panel_1.setBounds(10, 137, 274, 292);
- frame.getContentPane().add(panel_1);
- panel_1.setLayout(null);
- ColorPanel.setBounds(10, 179, 254, 102);
- panel_1.add(ColorPanel);
- panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
- panel_2.setBounds(10, 11, 254, 157);
- panel_1.add(panel_2);
- panel_2.setLayout(null);
- R.setFont(new Font("Tahoma", Font.BOLD, 18));
- R.setBounds(10, 11, 80, 22);
- panel_2.add(R);
- G.setFont(new Font("Tahoma", Font.BOLD, 18));
- G.setBounds(10, 44, 80, 22);
- panel_2.add(G);
- B.setFont(new Font("Tahoma", Font.BOLD, 18));
- B.setBounds(10, 77, 80, 22);
- panel_2.add(B);
- JButton btnNewButton = new JButton("Copy");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- copy("rgb(" + r + ", " + g + ", " + b + ");");
- CopyColor.setBackground(new Color(r,g,b));
- }
- });
- btnNewButton.setBounds(10, 110, 234, 36);
- panel_2.add(btnNewButton);
- panel_3.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
- panel_3.setBounds(294, 11, 265, 164);
- frame.getContentPane().add(panel_3);
- panel_3.setLayout(null);
- CopyColor.setBounds(10, 70, 245, 83);
- panel_3.add(CopyColor);
- txtRgb = new JTextField();
- txtRgb.setEditable(false);
- txtRgb.setFont(new Font("Tahoma", Font.BOLD, 18));
- txtRgb.setHorizontalAlignment(SwingConstants.CENTER);
- txtRgb.setText("rgb(255, 255, 255);");
- txtRgb.setBounds(10, 11, 245, 48);
- panel_3.add(txtRgb);
- txtRgb.setColumns(10);
- panel_4.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
- panel_4.setBounds(294, 211, 265, 218);
- frame.getContentPane().add(panel_4);
- panel_4.setLayout(null);
- GenResult.setBounds(10, 122, 245, 38);
- panel_4.add(GenResult);
- GenLR.setHorizontalAlignment(SwingConstants.CENTER);
- GenLR.setBounds(10, 11, 40, 26);
- panel_4.add(GenLR);
- GenR.setSnapToTicks(true);
- GenR.setPaintLabels(true);
- GenR.setValue(0);
- GenR.setMaximum(255);
- GenR.setBounds(60, 11, 195, 26);
- panel_4.add(GenR);
- GenG.setValue(0);
- GenG.setSnapToTicks(true);
- GenG.setPaintLabels(true);
- GenG.setMaximum(255);
- GenG.setBounds(60, 48, 195, 26);
- panel_4.add(GenG);
- GenB.setValue(0);
- GenB.setSnapToTicks(true);
- GenB.setPaintLabels(true);
- GenB.setMaximum(255);
- GenB.setBounds(60, 85, 195, 26);
- panel_4.add(GenB);
- GenLG.setHorizontalAlignment(SwingConstants.CENTER);
- GenLG.setBounds(10, 48, 40, 26);
- panel_4.add(GenLG);
- GenLB.setHorizontalAlignment(SwingConstants.CENTER);
- GenLB.setBounds(10, 85, 40, 26);
- panel_4.add(GenLB);
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- r = GenR.getValue();
- g = GenG.getValue();
- b = GenB.getValue();
- copy("rgb(" + r + ", " + g + ", " + b + ");");
- CopyColor.setBackground(new Color(r,g,b));
- }
- });
- button.setBounds(10, 171, 245, 36);
- panel_4.add(button);
- lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
- lblNewLabel.setBounds(294, 186, 215, 14);
- frame.getContentPane().add(lblNewLabel);
- panel_5.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
- panel_5.setBounds(10, 11, 274, 115);
- frame.getContentPane().add(panel_5);
- panel_5.setLayout(null);
- JCheckBox AlwaysOnTop = new JCheckBox("Always On Top");
- AlwaysOnTop.setBounds(6, 7, 165, 30);
- panel_5.add(AlwaysOnTop);
- AlwaysOnTop.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if ( AlwaysOnTop.isSelected() ) {
- frame.setAlwaysOnTop(true);
- }else{
- frame.setAlwaysOnTop(false);
- }
- }
- });
- AlwaysOnTop.setFont(new Font("Tahoma", Font.BOLD, 18));
- X.setBounds(6, 44, 80, 30);
- panel_5.add(X);
- X.setFont(new Font("Tahoma", Font.BOLD, 18));
- Y.setBounds(6, 74, 80, 30);
- panel_5.add(Y);
- Y.setFont(new Font("Tahoma", Font.BOLD, 18));
- Point.setBounds(96, 44, 168, 60);
- panel_5.add(Point);
- Point.setHorizontalAlignment(SwingConstants.CENTER);
- Point.setFont(new Font("Tahoma", Font.PLAIN, 30));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement