Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 1304 times)
0 Members and 1 Guest are viewing this topic.
Offline hopper85

Innocent Bystander





« 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.

 

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

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!