Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GUI mouse movement

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI mouse movement

    I started out by creating an applet with the size of 400x400 pixels. Draw three horizontal lines and three vertical lines that evenly divide the area into 16 cells. Based on mouse movement over the boxes, a box has been created in cell one, (this was just a guessing place to start for me), as the mouse is moved over each of the sixteen squares the white box created should move to the box the mouse is pointing in (and disappear from the previous box the mouse was in.)

    I am now stuck and don't know where to go next. I have read all of the mouse event and etc. but just don't get the next move. Any advice would be appreciated.

    Here is my code.

     
     import java.awt.*;
     import java.awt.event.*;
     import java.applet.Applet;
     
    /*
    <applet code="Assign10" width=400 height=400>
    </applet>
    */
      public class Assign10 extends Applet
      	implements MouseListener, MouseMotionListener {
      		public void init() {
    		addMouseListener(this);
    		addMouseMotionListener(this);
      		}
     
    	public void paint(Graphics g) {
     
    		g.setColor(Color.blue);
    		setBackground(Color.blue);
     
    		//draw vertical lines
    		for (int x=100; x<=300; x+=100) {
     
    			g.setColor(Color.red);
    			g.drawLine (x,0, x,400);
    		}
     
    		//draw horizontal lines
    		for (int y=100; y<=300; y+=100) {
     
    			g.setColor(Color.red);
    			g.drawLine (0,y, 400,y);
    		}
     
    		//draw square
    		g.setColor(Color.white);
    		g.drawRect(10, 10, 80, 80);
     
     
    		setSize(400,400);
    		}
     
    	//Handle mouse moved.
    	public void mouseMoved(MouseEvent me) {
    	}
     
    	//Handle mouse entered.
    	public void mouseEntered(MouseEvent me) {
    	}
     
    	//Handle mouse exited.
    	public void mouseExited(MouseEvent me){
    	}
     
    	//Handle mouse click.
    	public void mouseClicked(MouseEvent me) {
    	}
     
    	//Handle button pressed.
    	public void mousePressed(MouseEvent me) {
    	}
     
    	//Handle button released.
    	public void mouseReleased(MouseEvent me) {
    	}
     
    	//Handle mouse dragged.
    	public void mouseDragged(MouseEvent me) {
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: GUI mouse movement

    I'm really not sure what your question is, and I don't see any logic in the MouseEvent handlers. You're going to have to add code that reacts to the mouse actions and updates the position of the white box appropriately. This is an assignment, so we can't just offer you code without you trying something first.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI mouse movement

    Thanks for your response. I am not looking for someone to do my homework just looking for a starting point. When reading about mouse events I thought it said all of those handlers had to be in code even if they weren't being used (null). I have tried several things but wanted "clean" code posted here instead of junk.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: GUI mouse movement

    Recommended reading: How to Write a Mouse Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    I'd suggest you start simply by putting print statements in each method, exploring the MouseEvent API for useful functions, and printing out those values.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. WASD Movement! Need Help!
    By Alex555 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 19th, 2012, 12:59 PM
  2. How to control gain between mouse movement and cursor movement ?
    By DrPete in forum Java Theory & Questions
    Replies: 3
    Last Post: March 12th, 2012, 07:27 AM
  3. Resizing on mouse movement
    By java_novice in forum Java Theory & Questions
    Replies: 9
    Last Post: February 3rd, 2012, 07:53 AM
  4. 3D camera movement
    By macko in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2011, 07:53 AM
  5. movement
    By mlan in forum Java Theory & Questions
    Replies: 4
    Last Post: February 15th, 2010, 10:57 PM