5
\$\begingroup\$

When you press CTRL you activate the "Register" mode. In this mode, when you click, the program stores the X and Y of the mouse. To turn it off, you press CTRL again. When you turn "Register" mode off, a message option box pops up asking "Perform actions?". If you respond "YES", the program will click on the registered coordinates.

I'm new to programming and Java. This code is a mess and I could use some feedback.

public class Gui extends JFrame {
    private JPanel mousePanel;

    private JLabel statusBar;
    private JLabel keyBar;

    public boolean ctrl;

    List<Integer> xList = new ArrayList<Integer>();
    List<Integer> yList = new ArrayList<Integer>();

    public int[] x;
    public int[] y;

    public Gui() {
        super("Program");
        mousePanel = new JPanel();
        mousePanel.setBackground(Color.WHITE);
        add(mousePanel, BorderLayout.CENTER);
        statusBar = new JLabel("No events");
        keyBar = new JLabel("No key events");
        add(keyBar, BorderLayout.NORTH);;
        add(statusBar, BorderLayout.SOUTH);

        HandlerClass handler = new HandlerClass();
        mousePanel.addMouseListener(handler);
        mousePanel.addMouseMotionListener(handler);
        this.addKeyListener(handler);
    }


    public void Click(int x, int y) throws AWTException {
        Robot bot = new Robot();
        bot.mouseMove(x, y);
        bot.mousePress(InputEvent.BUTTON1_MASK);
        bot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    private class HandlerClass implements MouseListener, MouseMotionListener, KeyListener {

        //Mouse Listener
        public void mouseClicked(MouseEvent event) {
            statusBar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
            if(ctrl) {
                xList.add(MouseInfo.getPointerInfo().getLocation().x);
                yList.add(MouseInfo.getPointerInfo().getLocation().y);
            }
        }

        public void mousePressed(MouseEvent event) {
            statusBar.setText(String.format("You are pressing the mouse at %d, %d", event.getX(), event.getY()));
        }

        public void mouseReleased(MouseEvent event) {
            statusBar.setText(String.format("Released at %d, %d", event.getX(), event.getY()));
        }

        public void mouseEntered(MouseEvent event) {
            statusBar.setText(String.format("Mouse entered at %d, %d", event.getX(), event.getY()));
            mousePanel.setBackground(Color.RED);
        }

        public void mouseExited(MouseEvent event) {
            statusBar.setText(String.format("Mouse exited at %d, %d", event.getX(), event.getY()));
            mousePanel.setBackground(Color.WHITE);
        }

        //Mouse Motion
        public void mouseDragged(MouseEvent event) {
            statusBar.setText(String.format("Dragging mouse at %d, %d", event.getX(), event.getY()));
        }

        public void mouseMoved(MouseEvent event) {
            statusBar.setText(String.format("Moving mouse at %d, %d", event.getX(), event.getY()));
        }

        //Key Listener
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == e.VK_CONTROL && !(ctrl)){
                keyBar.setText("CTRL ON");
                ctrl = true;
            }
            else if(e.getKeyCode() == e.VK_CONTROL && ctrl) {
                keyBar.setText("CTRL OFF");
                ctrl = false;
                 if(JOptionPane.showOptionDialog(null, "Perform actions?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
                     int index = 0;
                     for(int actionX : xList) {
                         try {
                            Click(actionX, yList.get(index));
                        } catch (AWTException e1) {
                            e1.printStackTrace();
                        }
                         index++;
                         try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                     }
                 }
            }
        }

        public void keyReleased(KeyEvent e) {

        }

        public void keyTyped(KeyEvent e) {

        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Question: Where are your import statements? \$\endgroup\$ – TheCoffeeCup Dec 19 '14 at 2:56
7
\$\begingroup\$

The biggest problem I found with your code is that IT DOES NOT DISPLAY ANYTHING. The JFrame(String title) constructor, according to Java SE 8 Documentation:

Creates a new, initially invisible Frame with the specified title.

So you have to put setVisible(true) somewhere in your constructor.

Also, when it shows, is so small you could see nothing. You should also set the size you want it to be and if you want it to be resizable or not in the constructor, like this:

setSize(500, 500);
setResizable(true);

Also, here:

List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();

I suggest you use LinkedList instead, because ArrayList uses arrays to store variables, and since arrays have a limited space, when you run out, it will create a new array and move everything from the old one to the new one. This takes a lot of time, and LinkedList doesn't do that.

You should also add a serial version ID, because that will then save the time the compiler requires to generate one for you, like this:

private static final long serialVersionUID = 1L;

Other than that, your code is good.

Final Code:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        Gui gui = new Gui();
    }
}

class Gui extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel mousePanel;

    private JLabel statusBar;
    private JLabel keyBar;

    public boolean ctrl;

    List<Integer> xList = new LinkedList<Integer>();
    List<Integer> yList = new LinkedList<Integer>();

    public int[] x;
    public int[] y;

    public Gui() {
        super("Program");
        setVisible(true);
        setSize(500, 500);
        setResizable(true);
        mousePanel = new JPanel();
        mousePanel.setBackground(Color.WHITE);
        add(mousePanel, BorderLayout.CENTER);
        statusBar = new JLabel("No events");
        keyBar = new JLabel("No key events");
        add(keyBar, BorderLayout.NORTH);
        add(statusBar, BorderLayout.SOUTH);

        HandlerClass handler = new HandlerClass();
        mousePanel.addMouseListener(handler);
        mousePanel.addMouseMotionListener(handler);
        this.addKeyListener(handler);
    }

    public void Click(int x, int y) throws AWTException {
        Robot bot = new Robot();
        bot.mouseMove(x, y);
        bot.mousePress(InputEvent.BUTTON1_MASK);
        bot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    private class HandlerClass implements MouseListener, MouseMotionListener,
            KeyListener {

        public void mouseClicked(MouseEvent event) {
            statusBar.setText(String.format("Clicked at %d, %d", event.getX(),
                    event.getY()));
            if (ctrl) {
                xList.add(MouseInfo.getPointerInfo().getLocation().x);
                yList.add(MouseInfo.getPointerInfo().getLocation().y);
            }
        }

        public void mousePressed(MouseEvent event) {
            statusBar.setText(String.format(
                    "You are pressing the mouse at %d, %d", event.getX(),
                    event.getY()));
        }

        public void mouseReleased(MouseEvent event) {
            statusBar.setText(String.format("Released at %d, %d", event.getX(),
                    event.getY()));
        }

        public void mouseEntered(MouseEvent event) {
            statusBar.setText(String.format("Mouse entered at %d, %d",
                    event.getX(), event.getY()));
            mousePanel.setBackground(Color.RED);
        }

        public void mouseExited(MouseEvent event) {
            statusBar.setText(String.format("Mouse exited at %d, %d",
                    event.getX(), event.getY()));
            mousePanel.setBackground(Color.WHITE);
        }

        public void mouseDragged(MouseEvent event) {
            statusBar.setText(String.format("Dragging mouse at %d, %d",
                    event.getX(), event.getY()));
        }

        public void mouseMoved(MouseEvent event) {
            statusBar.setText(String.format("Moving mouse at %d, %d",
                    event.getX(), event.getY()));
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == e.VK_CONTROL && !(ctrl)) {
                keyBar.setText("CTRL ON");
                ctrl = true;
            } else if (e.getKeyCode() == e.VK_CONTROL && ctrl) {
                keyBar.setText("CTRL OFF");
                ctrl = false;
                if (JOptionPane.showOptionDialog(null, "Perform actions?", "",
                        JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
                        null, null, null) == JOptionPane.YES_OPTION) {
                    int index = 0;
                    for (int actionX : xList) {
                        try {
                            Click(actionX, yList.get(index));
                        } catch (AWTException exc) {
                            exc.printStackTrace();
                        }
                        index++;
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException exc) {
                            exc.printStackTrace();
                        }
                    }
                }
            }
        }

        public void keyReleased(KeyEvent e) {

        }

        public void keyTyped(KeyEvent e) {

        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.