location - Java JLabel.getLocation() always returning 0 -
i'm studying java i'm pretty new. i'm creating simple 'maze' type game using gui layouts, images, labels ect..
to create maze layouts used array of strings;
mazelayout[0] = "wwwwwwwwww"; mazelayout[1] = "wssswwswww"; mazelayout[2] = "wswswwsssw"; mazelayout[3] = "wswswwwwsw"; mazelayout[4] = "wswswwwwsw"; mazelayout[5] = "wswswssssw"; mazelayout[6] = "wswswswwww"; mazelayout[7] = "wswswswwww"; mazelayout[8] = "wswssswwww"; mazelayout[9] = "wwwwwwwwww";
and converted 2d array , placed label in image icon in depending on string being 'w' wall or 's' space. labels array, thoughts behind restricting movement of player can't walk though walls.
int mw = 0; int mf = 0; for(int y = 0; y < 10; y++){ for(int x = 0; x < 10; x++){ mazelayout2d[y][x] = mazelayout[y].substring(x, x+1); if (mazelayout2d[y][x].equals("w")){ lblmazewall[mw] = new jlabel(); mazewall = new imageicon("mazewall.png"); lblmazewall[mw].seticon(mazewall); pcenter.add(lblmazewall[mw]); mw++; pcenter.revalidate(); } if (mazelayout2d[y][x].equals("s")){ lblmazefloor[mf] = new jlabel(); mazefloor = new imageicon("mazefloor.png"); lblmazefloor[mf].seticon(mazefloor); pcenter.add(lblmazefloor[mf]); mf++; pcenter.revalidate(); } } }
my problem when run line
system.out.println(lblmazewall[x].getlocation()); //x being number
i java.awt.point[x=0,y=0]
i know how location of each wall label can check against player movement.
is valid way this? teach me more efficient way?
sorry crude snippets , or bad programming
thankyou niall.
public point getlocation()
due asynchronous nature of native event handling, method can return outdated values (for instance, after several calls of setlocation()
in rapid succession). reason, recommended method of obtaining component's position within java.awt.event.componentlistener.componentmoved()
, called after operating system has finished moving component.
the layout might not have used setlocation()
internally. getlocation() not return value expected.
Comments
Post a Comment