Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2d arrays.  (Read 1138 times)
0 Members and 1 Guest are viewing this topic.
Offline hopper85

JGO Visitor




« Posted 2006-03-15 21:38:52 »

Hi,
im having a problem when trying to draw an image multiple times to different screen co-ordinates i have saved in a 2d array.
(The 2d arrays holds 15 x 2 co-ordinates, this is for a battleship mobile game.)

This is the snippet of code im using to draw the image(s):

   for (int i = 0; i < 15; i++)
      for (int j = 0; j < 2; j++)
   g.drawImage(HitImg, CursorHitForClient[j], CursorHitForClient[j], Graphics.HCENTER | Graphics.BOTTOM);

Anyone know how to do this???

Thanks,
Hopper
Offline cborders

Junior Member





« Reply #1 - Posted 2006-03-15 23:42:44 »

Lets say that you have a 3x2 array instead, for simplicity's sake, that looks like this
1  
2  
3  
0  0
0 10
0 20

meanig that you want the pictures to be drawn down the left hand side like this
1  
2  
3  
[ ]
[ ]
[ ]

Ok, now lets look at your code
1  
2  
3  
4  
5  
6  
7  
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 2; j++)
    {
        g.drawImage(HitImg, CursorHitForClient[i][j], CursorHitForClient[i][j], Graphics.HCENTER | Graphics.BOTTOM);
    }
}


I added the brackets for clairity.  Ok on the first loop through it gets [0,0] which is 0, so it draws at (0,0), then it hits the inner loop again and gets [0,1] which is 0 again so it draws another one at (0,0).  The it hits the outer loop again and it it gets [1,0] which is 0, so it draws a third image at (0,0), then it gets [1,1] whic is 10, so it draws the image at (10, 10)!!! Then it will draw another one at (0,0) then at (20,20), then at (0,0) then at (30,30).

The trouble is that you are using the same value for both the X and Y coordinates of the drawImage().  Your code should look like this instead.
1  
2  
3  
4  
for (int i = 0; i < 3; i++)
{
    g.drawImage(HitImg, CursorHitForClient[i][0], CursorHitForClient[i][1], Graphics.HCENTER | Graphics.BOTTOM);
}
Offline g666

Junior Member





« Reply #2 - Posted 2006-03-16 20:06:43 »

i think u need to change this line:

g.drawImage(HitImg, CursorHitForClient[j], CursorHitForClient[j], Graphics.HCENTER | Graphics.BOTTOM);

to

g.drawImage(HitImg, CursorHitForClient
  • , CursorHitForClient[1], Graphics.HCENTER | Graphics.BOTTOM);

EDIT: i really should read properly b4 posting, cborders just explained this. Roll Eyes

desperately seeking sanity
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (87 views)
2013-05-17 21:29:12

alaslipknot (95 views)
2013-05-16 21:24:48

gouessej (127 views)
2013-05-16 00:53:38

gouessej (121 views)
2013-05-16 00:17:58

theagentd (129 views)
2013-05-15 15:01:13

theagentd (117 views)
2013-05-15 15:00:54

StreetDoggy (160 views)
2013-05-14 15:56:26

kutucuk (183 views)
2013-05-12 17:10:36

kutucuk (184 views)
2013-05-12 15:36:09

UnluckyDevil (190 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.092 seconds with 20 queries.