Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Simple tile-based gaming structure...

Thread view: 
Chris Shepherd - 14 Dec 2003 18:40 GMT
Hello,
  I'm looking for any resource sites and such that might give me the
ability to write a simple tile-based game (a la Sim City). I know the
basic gui stuff for writing an application, but I'm unfamiliar at best
with creating clickable interfaces that DON'T have buttons and etc.
  So if anyone could point me to any simple guides, it would be
appreciated.

Thanks,
Signature

Chris Shepherd

Chris Smith - 14 Dec 2003 22:45 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.