There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer cpu crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware missing monitor network networking outlook outlook 2003 outlook 2007 outlook express password popups problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Java Question


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Davidov541's Avatar
Member with 72 posts.
 
Join Date: Apr 2004
Location: Cincinnati
28-May-2006, 10:55 PM #1
Java Question
i have seen it in places on the web, but i need someone to help me because i can't find it anymore... i need to figure out a way to find out if a mouse click is within a hexagon which i have already drawn. i have included the code i use to create the hexagon, if anyone could help me, i would be more than appreciative. memory is at a premium, so any memory savers would help as well! thanks!

protected void drawHexagon(int x, int y, Graphics2D page, Color c) {
int xarray[] = {30 + x, 60 + x, 60 + x, 30 + x, x, x};
int yarray[] = {y, 16 + y, 44 + y, 60 + y, 44 + y, 16 + y};
Polygon hexa = new Polygon(xarray, yarray, 6);
page.setColor(c);
page.fill(hexa);
page.setColor(Color.black);
if (c != Color.blue) {
page.drawString(randInt(), 25 + x, 30 + y); //
counter++;
}
page.draw(hexa);
hexXCoordinates[z] = xarray;
hexYCoordinates[z] = yarray;
z++;
}
bpmurray's Avatar
Senior Member with 103 posts.
 
Join Date: Jun 2003
Location: Ireland
Experience: Advanced
29-May-2006, 03:47 PM #2
First, you need to make sure your class implements mouselistener, then you need to add a call to addMouseListener(this). Of course, this now means that you have to have the 5 mouse methods in your class:
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}

You'll want to react to at least one of these, so the trick is to pick up the event and see if this is within your polygon. You can get the position of the click from getPoint (or getX and getY), and you may want to apply translatepoint() to these values.

To then check if the point is within the polygon, you need to check that it contains the point, i.e. use: if (hexa.contains(e.getPoint())) { blah, blah}.
__________________
There are 10 kinds of people: those that understand binary and those that don't.
Davidov541's Avatar
Member with 72 posts.
 
Join Date: Apr 2004
Location: Cincinnati
29-May-2006, 04:39 PM #3
ok thanks alot, i did not know that contains was a method of hexa... as im creating this game, another problem im noticing is that whenever say the msdos window gets put on top of the game play screen, the screen doesn't repaint itself. I know with applets there is a repaint method, is the same true for a swing application, and if it is, how would i use it? thanks alot for putting up with me and my newness to swing.
bpmurray's Avatar
Senior Member with 103 posts.
 
Join Date: Jun 2003
Location: Ireland
Experience: Advanced
29-May-2006, 07:39 PM #4
Hmm - it should really paint itself. FYI, there's a lot of information on Sun's site - see http://java.sun.com/docs/books/tutor...swing/TOC.html.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

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

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:21 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.