> Hello,
> I'm looking for any resource sites and such that might give me the
[quoted text clipped - 3 lines]
> So if anyone could point me to any simple guides, it would be
> appreciated.
It's really not difficult enough to warrant a published guide of some
kind. All you do is override paintComponent (or paint if you're using
non-Swing components) to draw the grid, and then add a mouseListener
that determines the target of the click and fires an appropriate event.
Simple division can be used for hit-testing in this case; if the hit-
testing were more difficult (such as for a hexagonal board) then Polygon
contains the code to do it.
Here's a piece of code I found sitting around my system (from a game
programming class I taught to some kids recently) that does the basic
stuff -- only this uses processMouseEvent instead of a MouseListener...
either would work:
package net.twu.cdsmith.trollsiege;
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.util.EventListener;
import javax.swing.JPanel;
import javax.swing.event.EventListenerList;
public class SiegeBoard extends JPanel
{
private EventListenerList listeners = new EventListenerList();
private Siege siege;
private SiegeObject selection = null;
public SiegeBoard(Siege s)
{
this.siege = s;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Siege getSiege()
{
return siege;
}
public SiegeObject getSelection()
{
return selection;
}
public Dimension getPreferredSize()
{
final int SQUARE_HEIGHT_PREF = 32;
final int SQUARE_WIDTH_PREF = 32;
return new Dimension(
siege.getWidth() * SQUARE_WIDTH_PREF,
siege.getHeight() * SQUARE_HEIGHT_PREF);
}
private int getSquareWidth()
{
int width = getWidth();
Insets in = getInsets();
width = width - in.left - in.right;
return width / siege.getWidth();
}
private int getSquareHeight()
{
int height = getHeight();
Insets in = getInsets();
height = height - in.top - in.bottom;
return height / siege.getHeight();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Insets in = getInsets();
for (int y = 0; y < siege.getHeight(); y++)
{
for (int x = 0; x < siege.getWidth(); x++)
{
SiegeObject obj = siege.getSiegeObject(x, y);
if (obj != null)
{
g.drawOval(
in.left + (x * getSquareWidth()),
in.top + (y * getSquareHeight()),
getSquareWidth(), getSquareHeight());
}
}
}
}
protected void processMouseEvent(MouseEvent e)
{
if (e.getID() == MouseEvent.MOUSE_CLICKED)
{
SiegeObject obj = hitTest(e.getX(), e.getY());
if ((obj != null) && (obj != selection))
{
SiegeObject oldSelection = selection;
selection = obj;
fireSelectionEvent(oldSelection, selection);
repaint();
return;
}
}
super.processMouseEvent(e);
}
private SiegeObject hitTest(int x, int y)
{
Insets in = getInsets();
if (x < in.left) return null;
if (y < in.top) return null;
int realX = (x - in.left) / getSquareWidth();
int realY = (y - in.top) / getSquareHeight();
if ((realX < siege.getWidth()) && (realY < siege.getHeight()))
{
return siege.getSiegeObject(realX, realY);
}
else
{
return null;
}
}
public void addSelectionListener(SiegeSelectionListener listener)
{
listeners.add(SiegeSelectionListener.class, listener);
}
public void removeSelectionListener(SiegeSelectionListener listener)
{
listeners.remove(SiegeSelectionListener.class, listener);
}
private void fireSelectionEvent(SiegeObject oldSel, SiegeObject
newSel)
{
SiegeSelectionEvent event = new SiegeSelectionEvent(
this, oldSel, newSel);
EventListener[] arr = listeners.getListeners(
SiegeSelectionListener.class);
for (int i = 0; i < arr.length; i++)
{
((SiegeSelectionListener) arr[i]).selectionChanged(event);
}
}
}

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Shepherd - 15 Dec 2003 04:27 GMT
> It's really not difficult enough to warrant a published guide of some
> kind. All you do is override paintComponent (or paint if you're using
[quoted text clipped - 3 lines]
> testing were more difficult (such as for a hexagonal board) then Polygon
> contains the code to do it.
Interesting, seems very simple actually. Thanks for the info!
> Here's a piece of code I found sitting around my system (from a game
> programming class I taught to some kids recently) that does the basic
> stuff -- only this uses processMouseEvent instead of a MouseListener...
> either would work:
[...]
Thanks a load!

Signature
Chris Shepherd