I am trying to write a java code to obtain mouse current position and want the o
ID: 441582 • 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.Explanation / Answer
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Mouse3 extends Applet implements MouseListener, MouseMotionListener { int width, height; // We need a place to store a list of mouse positions. // Rather than use an array, we'll use a Vector, because // it allows elements to be easily appended and deleted. // (Technically, it would probably be more appropriate to // use a LinkedList, but they're only supported by Java 1.2) Vector listOfPositions; public void init() { width = getSize().width; height = getSize().height; setBackground( Color.black ); listOfPositions = new Vector(); addMouseListener( this ); addMouseMotionListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } public void mouseMoved( MouseEvent e ) { if ( listOfPositions.size() >= 50 ) { // delete the first element in the list listOfPositions.removeElementAt( 0 ); } // add the new position to the end of the list listOfPositions.addElement( new Point( e.getX(), e.getY() ) ); repaint(); e.consume(); } public void mouseDragged( MouseEvent e ) { } public void paint( Graphics g ) { g.setColor( Color.white ); for ( int j = 1; jRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.