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 / October 2004

Tip: Looking for answers? Try searching our database.

Painting a board game

Thread view: 
Frank - 07 Oct 2004 16:51 GMT
I'm about to start out on a Java based board game, somewhat similar to
monopoly or whatever. - The player rolls a dice, moves his piece around
on the board, then solves some kind of task depending on where he lands.

I have a little experience with graphics programming from earlier, and a
decent handle on Swing, but never did anything like this in Java before.

All the graphics are completed; there's a background image, the 'tiles'
you'll be moving around on, as well as any other graphic element.

The only interactive pieces will be a few buttons, some dialogs
(JInternalFrame's display ok in an applet?), and the tiles you move
across. These tiles can be highlited (whenever it's a legal move), and
need to generate some kind of event when clicked. I have different
background images for both states, as well as a non-changing symbol i
need to blend on top.

I have no idea what would be the best way to do layout. Just go for
absolute positioning? The applet will be fixed-size anyway, so..
Could be nice if the thing would scale to different resolutions, though.
And maybe fairly easily skinnable?

Thoughts on how to render this thing? Anything from the mile-high view,
all the way down to implementing the tiles, is appreciated.

-Frank
Frank - 07 Oct 2004 16:58 GMT
> Thoughts on how to render this thing? Anything from the mile-high view,
> all the way down to implementing the tiles, is appreciated.
>
> -Frank

Forgot to mention my system requirements. Thankfully, every user can be
expected to run JRE1.4.2 or later.

-Frank
Paul Lutus - 07 Oct 2004 17:13 GMT
/ ...

> I have no idea what would be the best way to do layout. Just go for
> absolute positioning? The applet will be fixed-size anyway, so..
> Could be nice if the thing would scale to different resolutions, though.
> And maybe fairly easily skinnable?

Yes, all that is feasible. When I need to do this, I use the mouse to move
the pieces around the board, in which case you will need to create your own
layout manager to keep the pieces entirely on the board (detect when the
pieces begin to overlap the board edges).

And scaling the entire display is not difficult. Java has ways of presenting
a background graphic in any convenient scale, and the player pieces can be
scaled easily also.

> Thoughts on how to render this thing? Anything from the mile-high view,
> all the way down to implementing the tiles, is appreciated.

A board game like this is not unlike other layouts that may change over
time. The only difference is the pieces are moved by the mouse, and after
being moved, assume a new position in the layout manager's scheme.

You can even save the entire game to be resumed later, if your program is an
application or a signed applet.

Just to get you started, here is a very simple program that moves some
instances of JButton around a "board". There are some approaches in this
example that are dated -- it is rather old -- but it will serve to give you
some ideas:

******************************************************************

// MouseDragger.java

import javax.swing.*;
import java.awt.*;

public class MouseDragger extends javax.swing.JFrame {
 
  String[] words = {"Rearrange" ,"these","player","pieces"};
 
  public MouseDragger() {
     initComponents ();
     setSize(600,300);
     setLocationRelativeTo(null);
     for(int i = 0; i < words.length;i++) {
        MyJButton b = new MyJButton(words[i]);
        jPanel1.add(b);
        b.setLocation(50+i*120,150);
     }
     setVisible(true);
  }
 
 
  private void initComponents() {
     jPanel1 = new javax.swing.JPanel();
     addWindowListener(new java.awt.event.WindowAdapter() {
           public void windowClosing(java.awt.event.WindowEvent evt) {
              exitForm(evt);
           }
        }
     );
     
     jPanel1.setLayout(null);
     jPanel1.setBackground(java.awt.Color.white);
     
     getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
  }
 
  private void exitForm(java.awt.event.WindowEvent evt) {
     System.exit (0);
  }
 
  public static void main (String args[]) {
     new MouseDragger ().show ();
  }
 
  private javax.swing.JPanel jPanel1;
 
}

class MyJButton extends JButton {
  int dx,dy;
  MyJButton(String name) {
     super(name);
     setSize(getPreferredSize());
     addMouseListener(
        new java.awt.event.MouseAdapter() {
           public void mousePressed(java.awt.event.MouseEvent evt) {
              mouseIsPressed(evt);
           }
        }
     );
     addMouseMotionListener(
        new java.awt.event.MouseMotionAdapter() {
           public void mouseDragged(java.awt.event.MouseEvent evt) {
              mouseIsDragged(evt);
           }
        }
     );
  }
 
  public void mouseIsPressed(java.awt.event.MouseEvent evt)
  {
     dx = evt.getX();
     dy = evt.getY();
  }
 
  public void mouseIsDragged(java.awt.event.MouseEvent evt)
  {
     Point p = getLocation();
     p.x += evt.getX()-dx;
     p.y += evt.getY()-dy;
     setLocation(p.x,p.y);
  }
}

Signature

Paul Lutus
http://www.arachnoid.com

Thomas Weidenfeller - 07 Oct 2004 17:25 GMT
> I have no idea what would be the best way to do layout. Just go for
> absolute positioning? The applet will be fixed-size anyway, so..

It seems to be very popular to do all the screen painting in games in
the program, and to not use any layout managers.

So, in your case I would most likely use a fixed-size Canvas/JPanel and
do all the painting and image rendering in the paint()/paintComponent()
method.

Whenever you have processed some user input, and something on the game
board needs to be changed, you change the corresponding values in your
internal data model, and then call repaint() on the Canvas/JPanel. This
triggers a call to paint()/paintComponent(). And in this method you read
the data from your data model and paint the corresponding tiles where
they belong.

> Could be nice if the thing would scale to different resolutions, though.

I don't know about your graphics, but the usual scaling algorithms for
images are not perfect. Depending on the contents of your image and the
scale factor, the results might be ok, or you might get horrible
results. Therefore, if you need to provide your game in different
sizes/resolutions, you best provide a set of graphics for all supported
resolutions and flip between the sets when another resolution is desired.

> And maybe fairly easily skinnable?

Well, if you really paint everything in your game yourself, including
buttons and the like, then some simple skinning should be easy by
providing a different set of images. If you want to use and skin
existing Swing components, then you can try your hands on a Skin PLAF or
hack the whole thing yourself. However, I would not add any extra code
or a library or PLAF for skinning, since you are talking about an
applet. And your users would have to load all the extra skinning code
over the network.

BTW: Unless any 1.2+ Java features are really needed, I would at least
consider to write the whole applet for 1.1.x, especially if you paint
everything yourself and don't use any Swing components.

/Thomas
John Bokma - 07 Oct 2004 19:31 GMT
> I don't know about your graphics, but the usual scaling algorithms for
> images are not perfect. Depending on the contents of your image and
[quoted text clipped - 3 lines]
> supported resolutions and flip between the sets when another
> resolution is desired.

Or use vector graphics, or a set of prerendered graphics combined with
(down)scaling.

Signature

John                               MexIT: http://johnbokma.com/mexit/
                          personal page:       http://johnbokma.com/
       Experienced programmer available:     http://castleamber.com/
           Happy Customers: http://castleamber.com/testimonials.html

Jacob - 07 Oct 2004 18:30 GMT
> Thoughts on how to render this thing? Anything from the mile-high view,
> all the way down to implementing the tiles, is appreciated.

There is a 2D graphics library at http://geosoft.no/graphics/
with examples from similar applications.


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



©2008 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.