I am trying to write a java code to obtain mouse current position and want the o
ID: 441584 • Letter: I
Question
I am trying to write a java code to obtain mouse current position and want the object to match the string with one of the case: "up"the next position should be the current row-1/current column."down"the next position should be the current row+1/current column."left"the next position should be the current row/current column-1. "right"the next position should be the current row/current column+1. "Other"any other input will return the current position as the desired position. I have been trying and getting an error if someone would given me an idea. This is the code tht i have written and i m getting error that x and y are not declared. import java.util.Scanner; public class Mouse { / public Coords getMove(Room room, int mi){ / Scanner input = new Scanner(System.in); System.out.println("Move: (up, down, left, right,other)"); String movement = input.nextLine(); if (movement == "up"){ Coords upmove = new Coords(x-1,y); return upmove; } else if (movement == "down"){ Coords downmove = new Coords(x+1,y); return downmove; } else if (movement == "left"){ Coords leftmove = new Coords(x,y-1); return leftmove; } else if (movement == "right"){ Coords rightmove = new Coords(x,y+1); } */ return null; } }Explanation / Answer
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; import javax.swing.border.LineBorder; @SuppressWarnings("serial") public class MousePosition extends JPanel { //the time after which the timer fires = 200 ms private static final int POLL_TIME = 200; private static final Dimension SIZE = new Dimension(100, 30); private JLabel displayLabel; private JPopupMenu popup; private Window parent; //this handles getting the current mouse position private Timer timer = new Timer(POLL_TIME, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { PointerInfo info = MouseInfo.getPointerInfo(); updateDisplay(info.getLocation()); } }); public MousePosition(Window parentWindow) { super(new BorderLayout()); this.parent = parentWindow; setBorder(new LineBorder(Color.BLACK, 2)); setToolTipText("Press to drag. Right click for popup."); createPopUp(); displayLabel = new JLabel(); displayLabel.setHorizontalAlignment(JLabel.CENTER); displayLabel.setForeground(Color.WHITE); displayLabel.setFont(displayLabel.getFont().deriveFont(Font.BOLD, 14f)); add(displayLabel); //this is for handling dragging of window and displaying popup, please note that this is not for //getting mouse position MouseAdapter mouseListener = new MouseAdapter() { boolean pressed; int x, y, pressedX, pressedY; public void mousePressed(MouseEvent e) { if (maybeShowPopup(e)) return; pressed = true; setCursor(new Cursor(Cursor.MOVE_CURSOR)); x = parent.getX(); y = parent.getY(); pressedX = e.getXOnScreen(); pressedY = e.getYOnScreen(); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); if (pressed) { pressed = false; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } } public void mouseDragged(MouseEvent e) { parent.setLocation(x + (e.getXOnScreen() - pressedX), y + (e.getYOnScreen() - pressedY)); } private boolean maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); return true; } return false; } }; addMouseListener(mouseListener); addMouseMotionListener(mouseListener); timer.start(); } private void updateDisplay(Point p) { displayLabel.setText(p.x + " , " + p.y); } @Override public Dimension getPreferredSize() { return SIZE; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); GradientPaint paint = new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), Color.BLACK); ((Graphics2D) g).setPaint(paint); g.fillRect(0, 0, getWidth(), getHeight()); } private void createPopUp() { popup = new JPopupMenu(); popup.add(new AbstractAction("Close") { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } private static void showGUI() { JDialog container = new JDialog(); container.setUndecorated(true); container.setContentPane(new MousePosition(container)); container.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); container.pack(); //for displaying the window in top right corner of screen container.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width - container.getPreferredSize().width - 10, 40); container.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { showGUI(); } }); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.