Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (688)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple Mouse Question  (Read 1628 times)
0 Members and 1 Guest are viewing this topic.
Offline jeffchuck

Innocent Bystander

Java games rock!

« Posted 2005-01-05 05:28:11 »

Hello Everybody,

I know Java pretty well, and I am familiar with the basics of Swing and Graphics/Graphics2D, but I have no experience with games.  I am writing a Risk style game that will have many oddly shaped regions.  The part I am having trouble with is knowing when the mouse is over a particular region of the map.  Should I use something like a MouseMotionListener?  How would I figure out which region the mouse is moved into?
Offline Serethos

Junior Devvie

Java games rock!

« Reply #1 - Posted 2005-01-05 07:11:38 »

the easiest way would be to design a "Polygon" object in shape of you region. each time you click on the screen you can use the polygon's internal contains() methods for checking if its a specified region .
Offline KONI

Junior Devvie

Java games rock!

« Reply #2 - Posted 2005-01-05 11:24:22 »

I don't know if this is just plain stupid but what came in my mind would be to have a lookup array of int[][] where you can check the x,y coords and get the id back of the region you clicked on.

This would look like the following:

1 1 1 2 2 3
1 1 1 1 2 3
1 1 1 2 3 3
1 1 2 2 3 4

so when you click on 0,0 it would give you zone 1 p.ex.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #3 - Posted 2005-01-05 17:57:26 »

Boths ways would work. Serethos' would be a bit slow and KONI's would eat some ram Smiley

I would use polys as Serethos suggested, but I would use a two tiered check. First determine which bounding rects you hit then check those polys and as soon as you got a positive hit for the contains method stop checking.

The benefit of that method is that it's easy to do and it's also pretty fast because you can exclude the vast majority of the polys for the indepth check.

So basically... create an array with all Polys and create an array with bounding rectangles (same size/same order - just use getBounds()). If a click occours, step through the rectangle array and do the contains check... if positive do the same with the poly (same index)... if positive return the current index and if nothing was hit return -1.

弾幕 ☆ @mahonnaiseblog
Offline Serethos

Junior Devvie

Java games rock!

« Reply #4 - Posted 2005-01-06 08:12:05 »

good that you point to the performance question. in many game articles and books i read that for 2D games-collision detection java-shapes are used.
a year ago i compared the speed of the contains() method
to manually checking each side of the shape. my result was that the manual check was much faster (...for simple shapes).
do you think its a (good) common practise to use java-shapes for collision detection (especially pointing to action games) ?
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #5 - Posted 2005-01-06 08:53:07 »

>do you think its a (good) common practise to use
>java-shapes for collision detection (especially pointing to
>action games) ?

If you're using only circles and rectangles you're pretty save with those methods from the AWT. The poly checks are pretty slow, because it's one size fits all algo for concave and convex polys (convex polys are cheaper to check). Well, usually polys aren't needed anyways.

As a rule of thumb... just write it plain and simple. If it's too slow for your taste profile and take care about the time consuming spots.


I just checked the Polygon source. They actually already do that bounds check before doing the in depth test (the bounding rectangle gets cached). So even the simple way of checking (just poly.contains) should be fast enough. You can also get everything cached first by requesting the bounding rectangle for each poly once at the beginning.

So basically like Serethos said. Create the polys... call getBounds once for each poly (without storing the result) and then just loop through em, do a poly.contains() and if it's positive stop.

弾幕 ☆ @mahonnaiseblog
Offline 69_Jeremy_69

Senior Newbie

Java games rock!

« Reply #6 - Posted 2005-01-06 19:23:04 »

Hi Jeff, I created a Risk style game as well.   I used the following method to define maps.  I currently have 4 different maps.  It takes some time to set up.  But there is not large amount of RAM used or long access times.  I currently get 54 fps.;action=display;num=1105044939

But since I assume its turn based, as another dev mentioned to me in another post.  You may want to look at re-active renderring rather then active renderring for a risk game.  So far I'm using active, but I may change that soon, but so far so good my renderring seems ok.

Actually I would really like to port the project over to LWJGL.  And try the same thing with maybe a 3D map.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (73 views)
2015-11-07 13:29:08

Rems19 (81 views)
2015-10-31 01:36:56

Rems19 (79 views)
2015-10-31 01:32:37

williamwoles (107 views)
2015-10-23 10:42:59

williamwoles (93 views)
2015-10-23 10:42:45

Jervac_ (111 views)
2015-10-18 23:29:12

DarkCart (137 views)
2015-10-16 00:58:11

KaiHH (118 views)
2015-10-11 14:10:14

KaiHH (158 views)
2015-10-11 13:26:18

BurntPizza (173 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!