Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2007, 07:41 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
Use the mouse position
I hope someone can enlighten me as I have no idea how to do what im trying to do.

I have an applet which has a grid of 225 squares (15x15). The grid starts at 200, 0. The squares are 40x40. I have added a mouselistener and have coded it to execute a method on mousedown. What I am trying to do is make a method that finds the square that the mouse was clicked in, without coding 225 if(mouse is here) do something statements.

What I am asking is if there is a way to get the x, y values of the corner of the square in which the user clicked. Example: the user clicks at 212, 5. The method will return 200, 0 (the x,y position of the corner of the topleft square in the grid).

Code:
public void paintGrid(Graphics g) { g.setColor(Color.gray); for(int x = 200; x < 800; x += 40) { for(int y = 0; y < 600; y += 40) { g.drawRect(x, y, 40, 40); } } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-29-2007, 01:10 AM
Member
 
Join Date: Jul 2007
Posts: 23
marco is on a distinguished road
use this applet template i made
all variables are 'private' variables, and this one responds to the mouse (x and y variables) as well as clicks and drags!! ^^ - you can also use the keyboard with this one (input in ASCII form tho)

Code:
//TestApplet import java.lang.*; import java.awt.*; import java.applet.*; public class TestNewApplet extends Applet implements Runnable { private int x, y, key; //Mouse X and Y private Thread pgm; // program loop private boolean click, drag; // True when pressed. //All variables go here, having a 'private' infront of them public void init () //Sets bg and game start { pgm = new Thread (this); pgm.start (); // Start game thread. } public void run () //Calls the loop to run { mainProgram (); // Runs the game } private void mainProgram () { Graphics c = getGraphics (); //Game graphics for (;;) { //all code goes here if (click) //Check click to disallow multiple clicks on one mouse down click = false; delay (10); //Refresh rate } } //Ends game loop method //Mouse methods for the program public boolean mouseMove (Event e, int mx, int my) { x = mx; y = my; click = false; drag = false; return true; } public boolean mouseDown (Event e, int mx, int my) { x = mx; y = my; click = true; drag = false; return true; } public boolean mouseUp (Event e, int mx, int my) { click = false; drag = false; x = mx; y = my; return true; } public boolean mouseDrag (Event e, int mx, int my) { click = false; drag = true; x = mx; y = my; return true; } public boolean keyDown (Event evt, int keyPressed) { key = keyPressed; return true; } // keyDown method public void delay (int tme) //sets the delays within the game { try { Thread.sleep (tme); } catch (Exception err) { } } } //Ends main applet

Last edited by levent : 07-29-2007 at 01:51 AM. Reason: Code is placed inside [code] tags!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to make a JDialog scrolling from one position to the other. Eranga AWT / Swing 1 03-28-2008 12:26 PM
BorderFactory to set position? aleplgr AWT / Swing 2 01-30-2008 02:46 PM
How to get the position of character in TextArea loganathan.lakshmanan JavaServer Pages (JSP) and JSTL 1 01-19-2008 02:06 AM
The mouse and the cheese Don Quixote Java 2D 4 08-16-2007 12:55 AM
Help with position in java fernando New To Java 1 07-31-2007 09:54 PM


All times are GMT +3. The time now is 07:29 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org