Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Draw images in grid sqares, swing or awt? 1

Status
Not open for further replies.

jstreich

Programmer
Apr 20, 2002
1,067
US
I've done a bit with Swing and I've done tons of commandline programs -- but what I'm not that familiar with the AWT.

What I am working on is a game, and I have no idea how to do the graphical side of it (I've done applications with controls (i.e. Swing), but nothing heavy in AWT). I know that I want an (multidemntinal ??) array of objects that have images, they will probably implement or extend a class that has the draw() method. I want to have them print in a grid (think Zelda map or scrable board type grid), where each grid square has a background image (ground) and optionally a token (player/object) on top of it. When the user presses an arrow I will catch the event with a listener (that's not an issue) and force the screen to refresh (which may be an issue).

Long and short I can either:
Do some sort of math to figgure out the placement of the upper left corner of each token using AWT -- or I can make each token a sqare on the board and use in Swing's GridLayout...

Which do you is easier to code?
Which is faster?

Thoughts, suggestions and comments greatly appreciated.
 
I recently wrote a graphical game and I had each image as 20px by 20px. My [tt]paint()[/tt] method looked something like this:
Code:
public void paint(Graphics g)
{
  super.paint(g);
  for (int i=0;i<width;i++) // width is global width of grid
  {
    for (int j=0;j<height;j++) // height is global height
    {
      g.drawImage( imgs.get( game.getImageIndex( i,j )), (i*20)+4, (j*20)+50, this);
      // game is backbone of the game and all images are stored
      // there as an integer key, imgs is a HashMap that
      // stores integer=>image pairs for easy access.
    }
  }
}

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
use Swing and GridLayout is easier(JDK 1.4). It is easier to refresh well but you should know that GridLayout means it looks like a chess box. Draw images on the gridbox you like.

Awt and using Graphics2D need more tuning in code to increase speed and refresh well. You can draw images and line at any points.
 
Code:
public void paint(Graphics g)
{
  super.paint(g);
  for (int i=0;i<width;i++) // width is global width of grid
  {
    for (int j=0;j<height;j++) // height is global height
    {
      g.drawImage( imgs.get( game.getImageIndex( i,j )), (i*20)+4, (j*20)+50, this);
      // game is backbone of the game and all images are stored
      // there as an integer key, imgs is a HashMap that
      // stores integer=>image pairs for easy access.
    }
  }
}
I was thinking it would look something like that, but the hard part is height... I want to have player tokens on squares... I'll also have to figure out that my real starting and end int are as the whole map won't be visable, but that will just be a couple of caclulations along the lines of:
Code:
[blue]
//posx = position of player's token (x axis)
//posy = position of player's token (y axis)
//cx = some constant number of squares between
//     the center square and the right/left edges
//cy = some constant number of squares between
//     the center square and the top/bottom edges[/blue]
for(int i = [red]posx - cx[/red]; [red]posx + cx[/red] > i; ++i)
{
  for(int j = [red]posy - cy[/red]; [red]posy + cy[/red] > i; ++i)
  {
     g.drawImage(ground[i][j], [green]...[/green] );
     g.grawImage(objs[i][j], [green]...[/green] );
     [b][green]... other layers ...[/green][/b]
  }
}
[red]Red[/red] highlights changes for displaying only in range part of map.
[green]Green[/green] is just stuff that will be the same over and over for any layers I have.

This paint method is inherited from java.awt.Container... So if I use Swing and make this class extend JPanel I would want to put this in paintComponents?

Am I starting to head in the right direction?
 
Code:
[b]...[/b]
//posx = position of player's token (x axis)
//posy = position of player's token (y axis)
//cx = some constant number of squares between
//     the center square and the right/left edges
//cy = some constant number of squares between
//     the center square and the top/bottom edges
for(int i = posx - cx; posx + cx > i; ++i)
{
  for(int j = posy - cy; posy + cy > i; ++i)
  {
     g.drawImage(ground[i][j], [b]...[/b] );
  }
}

for(int i = 0; players.size() > i; ++i)
{
  Player p = ((Player)players.get(i));
  g.drawImage( p.getImage(), p.getX()* 20, p.getY()*20, this);
}
[b]...[/b]

Everything becomes a bit more clear... Not every square will have a player or object.
 
Quite.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakesperean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top