Multidimensional arrays: Java. How does this code work? - java

I've been following a tutorial about creating a basic tile-based tower defense game in Java and have encountered a piece of code I cannot wrap my brain around and would like some help. (My main question is at the bottom after the code)
At this point we are iterating through a multidimensional array of 0's and 1's that we pass to a constructor which has a method that assigns a grass tile for 0's and stone tile for 1's and then another method to draw them to the screen creating our game screen. Simple enough, right?
Here is the class:
package data;
import static helpers.Artist.*;
public class TileGrid {
public Tile[][] map;
public TileGrid(int[][] newMap){
map = new Tile[20][15];
for(int i = 0; i < map.length; i++){
for(int j = 0; j <map[i].length; j++){
switch(newMap[j][i]){
case 0:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.GRASS);
break;
case 1:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.STONE);
break;
case 2:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.WATER);
break;
}
}
}
}
public void Draw(){
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[i].length; j++){
Tile t = map[i][j];
DrawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());
}
}
}
}
And here is the array we are passing in:
int[][] map = { //20 tiles wide, 15 tiles high
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
};
So my main question has to do with the switch statement in the constructor for the TileGrid.
Why do the i and the j get switched newMap[j][i] when checking what it equals? I get that this code works, well, because it does and I understand nested for loops to iterate through a multidimensional array.
But why wouldn't newMap[i][j] work?
As well at the very beginning of that same constructor why do we create an array (of type Tile) with the dimension of map = new Tile[20][15] when we are passing in an array with the dimensions of map[15][20]?
I have tried to figure this out and study this on my own and will continue to do so until I understand it but any help would be soooo appreciated!

You are passing to the TileGrid constructor a 2D array of 15 rows and 20 columns, but inside the constructor you create a 2D array of 20 rows and 15 columns. That's why map[i][j] corresponds with newMap[j][i].
If the input int[][] map was also of 20 rows and 15 columns, you wouldn't have to switch the order of the indices inside the constructor.

This would give an index out of bounds error if it was really switched. However, there is a col variable in the second for loop that is not defined anywhere so it's hard to tell what's going on. This code will not compile.
If it is some code pulled off a a tutorial on some web page, I would just assume that it was a typo and continue on with that in mind. OR better yet, contact the author.

Related

For Loop Map Overwrites Previous Value

Essentially I am making a board game using sockets. Each client connected is stored in a map .
When the user makes a move to a coordinate in the 2D Array i.e. "move 2,3", it should put the connectionID in that position.
Currently my issue is, because I have a for loop; when i use the move command, the connection ID is replaced with the last value in the loop.
public void move(int x, int y) {
for (int value : gs.returnClients().values()) {
storeArray[x][y] = value;
}
}
i.e. If i have 2 clients connected: {62029=1, 62032=2} and my board
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Let's say I want to have client 1 move to 0, 3 it should be:
[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
But instead I get:
[[0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Since it's overwriting the last value of the map in the for loop.
How would I make it move dependent on its client "id" from the map?
Edit
In my server i have clients.put(socket.getPort(), connectionID); and methods to return the map and id. I have a class GameService that executes command which is
switch (request.type) {
case MOVE:
int clientID = Integer.parseInt(request.params[0]);
int getX = Integer.parseInt(request.params[1]);
int getY = Integer.parseInt(request.params[2]);
game.move(clientID,getX, getY);
return game.returnBoard();
In a file Request
String[] items = line.trim().split("\\s+");
switch (items[0].toUpperCase()) {
case "MOVE":
return new Request(RequestType.MOVE, items[1], items[2], items[3]);
As far as I understand your code, you need to do your move the following way:
public void move(int client, int x, int y) {
storeArray[x][y] = client;
}
where client is one (!) of the numbers within the gs.returnClients().values()-list. If you need to take it out of that map, you need to deliver the appropriate key for that, e.g.:
public void move(int clientId, int x, int y) {
storeArray[x][y] = gs.returnClients().get(clientId);
}

Dijkstra's Algorithm and trouble with A* algorithm

So I have been searching these two algorithms on the Internet and was able to, after days of working on it, get Dijkstra's Algorithm working. From what I have read the two Algorithms are not that different. So I am going to post what I did for Dijkstra's and I was hoping that you can show me or point me into the direction on how to modify it to the A* algorithm.
public Integer processGraph(int graph[][], int algorithm, int s, int d) {
if(algorithm == 1) {
// shortestDist[index] will hold the distance from s to the index
int shortestDist[] = new int[graph.length];
// added[i] is true if vertex index is included / the shortest distance from the source to the index is finalized
boolean added[] = new boolean[graph.length];
// establish all distances as Integer.Max_Value and added[] as false
for (int vertexIndex = 0; vertexIndex < graph.length; vertexIndex++) {
shortestDist[vertexIndex] = Integer.MAX_VALUE;
added[vertexIndex] = false;
}
// Distance of the source from itself is always 0
shortestDist[s] = 0;
// Parent array to store shortest the shortest path -- example: [num, num, num]
int[] parents = new int[graph.length];
// Find shortest path for all vertices
for (int i = 1; i < graph.length; i++) {
// Pick the minimum distance vertex
// from the set of vertices not yet
// processed. nearestVertex is
// always equal to the source in
// first iteration.
int u = minDistance(shortestDist, added);
added[u] = true;
for (int vertexIndex = 0; vertexIndex < graph.length; vertexIndex++) {
if (!added[vertexIndex] && graph[u][vertexIndex] != 0 && shortestDist[u] != Integer.MAX_VALUE && shortestDist[u] + graph[u][vertexIndex] < shortestDist[vertexIndex]) {
shortestDist[vertexIndex] = shortestDist[u] + graph[u][vertexIndex];
parents[vertexIndex] = u;
}
}
}
if(shortestDist[d] == Integer.MAX_VALUE) {
System.out.println(" ");
System.out.println("ERROR MESSAGE -- No path present from " + s + " to " + d);
System.out.println("ERROR MESSAGE --no path present from " + s + " to " + d);
System.out.println(" ");
return null;
}
//pathPrint(s, shortestDist, d); this only works for the first test
return shortestDist[d];
} // end algorithm 1
TO add more context to this, I have a driver class in which with several graphs upon which this finds the shortest path.
Example of graph[][]:
int regValid[][] = {{0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0}};
Again, how can I modify what I have to work with the A* algorithm instead of Dijkstras. Thank you and hopefully this makes sense.
You have full information about all nodes, edges and destination node, so Dijkstra is the best solution for your problem. A* would be useful if you hadn't known complete graph (most often for huge node space) and were forced to prioritize decisions according to some heuristics.
If you really want to simulate A* on your graph, you have to think out some artificial criterion (heuristics) which describes the estimated distance between given node and target node (in other words, pretend you don't know the path). Since your data set doesn't contain such additional information (and thinking out any satisfying admissibility condition is not trivial), your question doesn't make much sense to me.

Java adding a 2D arraylist to a list of 2D arraylists

I am creating a map system for my game which requires me at a few stages to replace the old 2D array map with another. What I wanted to do is add all the 2D arraylist maps into a List of these 2D arraylists. However, what I found difficult was that when I wanted to retrieve said map, the .get method would not give me the correct data, as it you would try to pull the first item of that arraylist which in this case would be null. Thus the program threw me nullpointerexceptions.
private int[][] mapArray;
private List<int[][]> maps = new ArrayList<>();
mapArray = new int[][]{
{0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 9, 1, 1, 7, 7, 7, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{7, 7, 7, 7, 5, 8, 8, 8, 4, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 6, 7, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 4, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
maps.add(mapArray);
When I sout maps.get(0) I get the following:
[[I#58838f3f
If I have two items, it will show up like this:
[[[I#58838f3f, [[I#59a60155]
Ideally I want it to be added like this:
[I#58838f3f, I#58838f3f]
I am just unsure how to do this. Any help would be appreciated!
I might have overlooked something obvious, probably is the case!
Much obliged
You seem to be confusing the output of converting your map (or the list of maps) to string (which is what happens when you try to print them) - with what actual value there are there.
Because ArrayList like most Java container formats have nice toString() implementations that get automatically called when trying to print them, you'd get something that "looks like an array" in the output. So for example when printing an array list containing two strings, it might look like this: [a, b].
Arrays don't have such nice formatters, because they aren't really objects (they are "primitive types") and so print them gets you their address and some Java byte code that describes their type (if you can understand Java byte code), so an array of integers will look like this: [I#<address> while an two dimensional array of bytes will look like this: [[B#<address>, and so on.
In regards to your original question, the code seems to work well for me. Here is an example program:
import java.util.ArrayList;
public class Test2D {
public static void main(String... args) {
ArrayList<int[][]> maps = new ArrayList<>();
maps.add(new int[][] {
{1,2,3},
{4,5,6},
{7,8,9},
});
maps.add(new int[][] {
{10,20,30},
{40,50,60},
});
int[][] map = maps.get(0);
System.out.println(map[0][0]);
}
}
The output will print correctly 1 , showing that there is no null pointer exception.

How do I set specific index in two dimension arrayList

I have the following
ArrayList<ArrayList<Integer>> row = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> column = new ArrayList<Integer>();
for(int i = 0; i < 12; i++) {
column.add(0);
}
for(int j=0; j < 12 ; j++) {
row.add(column);
}
which it gave me
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
what I want is to replace a specific index in the arrayList. let's say 2nd arrayList of 3rd index with a 9.
row.get(2).set(3, 9);
but above replaces entire column with 9 instead. how should I set the arrayList to be specific without affecting other rows.
You currently add the same exact column 12 times, but you should add 12 different ones, all shaped in the same way initially:
ArrayList<ArrayList<Integer>> row = new ArrayList<ArrayList<Integer>>();
for(int j=0; j < 12 ; j++) {
ArrayList<Integer> column = new ArrayList<Integer>();
for(int i = 0; i < 12; i++) {
column.add(0);
}
row.add(column);
}
This is because you add the same instance of ArrayList as many times as you have columns, you need to create a copy first as next otherwise indeed all your columns will be affected by a modification:
for(int j=0; j < 12 ; j++) {
row.add(new ArrayList<>(column));
}
It doesn't replace all the values, it only replaces the one value. Your problem is that all 12 elements of the row list is referencing the same column list.
You need to create a new column list for each row.
Also, indexes are zero-based, so to set the 2nd arrayList of 3rd index, you need to specify indexes 1 and 2, not 2 and 3.
If you know the exact size of the matrix, you should use regular arrays, not ArrayList.

Colision detecting in java(Tiled map)

Here i have a code with a picture moving around the map.
How to make this picture colide when interacte with map tiles ? I'm a beginer at java so i have no ideas. Any Help or advise is very welcome.
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.Timer;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.omg.CORBA.portable.InputStream;
import java.io.Reader;
public class NewTest extends JFrame implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image TestImage;
private BufferedImage bf;
private BufferedImage bufferedImage;
private int cordX = 100;
private int cordY = 100;
public int mapy=25;
public int mapx=mapy;
public int size= 20;
private boolean down, up, left, right;
private Image wall = Toolkit.getDefaultToolkit().getImage("image/Koala.jpg");
private Image no = Toolkit.getDefaultToolkit().getImage("image/house.jpg");
public static int[][]
map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
{0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 4, 0, 5, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
{0, 12, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 13}};
public NewTest() throws IOException {
setTitle("Testing....");
setSize(mapy*size+50,mapx*size+50);
imageLoader();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void imageLoader() throws IOException {
TestImage = Toolkit.getDefaultToolkit().getImage("image/Koala.jpg");
addKeyListener(this);
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
bf = new BufferedImage( this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);
try{
animation(bf.getGraphics());
g.drawImage(bf,0,0,null);
}catch(Exception ex){
}
}
public void animation(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
for (int y = 0; y <= mapy; y++){
for (int x = 0; x <= mapx; x++){
int L = x * size;
int U = y * size;
int R = size;
int D = size;
/// g2d.rotate(Math.toRadians(-90));
if (map[y][x] == 1){
//no black wall
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
}else if (map[y][x] == 2){
//on left
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L-size/2 +2, U, 8, size);
}else if (map[y][x] == 3){
//on right
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L+size, U, 8, size);
}else if (map[y][x] == 4){
//on top
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U-size/2 + 2, size, 8);
}else if (map[y][x] == 5){
//on bottom
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U+size, size, 8);
}else if (map[y][x] == 10){
//on bottom
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U-size/2 + 2, size, 8);
g2d.fillRect(L-size/2 +2, U, 8, size );
}else if (map[y][x] == 11){
//on bottom
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U+size, size, 8);
}else if (map[y][x] == 12){
//on bottom
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U+size, size, 8);
}else if (map[y][x] == 13){
//on bottom
g2d.setColor(Color.green);
g2d.fillRect(L, U, R, D);
g2d.setColor(Color.black);
g2d.fillRect(L, U+size, size, 8);
}
}
}
g.drawImage(TestImage, cordX, cordY,20 , 20, this);
}
public static void main(String[] args) {
try {
new NewTest();
} catch (IOException e) {
e.printStackTrace();
}
}
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
}
updateState();
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
}
updateState();
}
protected void updateState() {
if (right) {
cordX += 5;
} else if (left) {
cordX -= 5;
}
if (down) {
cordY += 5;
} else if (up) {
cordY -= 3;
}
if (down && up) {
cordY += 0;
cordX += 0;
}
if (right && left) {
cordX += 0;
cordY += 0;
}
System.out.printf("X:");System.out.println(cordX);
System.out.printf("Y:");System.out.println(cordY);
repaint();
}
public void keyTyped(KeyEvent ke) {
}
}
Thank you for help!
Good approach is to seperate visual and reality model. It means deep in the program, you have your "physics rules" and all objects and based on that model, you draw it.
You can start with creating class like this
public class ObjectInMyGame{
private int posx;
private int posy;
private int width;
private int height;
private Image image;
private int movingX;
private int movingY;
//Note that it is good to use Point or something like that, instead of posx, posy.
}
Then it is good to have some kind of "game controller", which is running your whole world.
public class GameController{
List<ObjectInMyGame> objects;
public updateGame(){
for (ObjectInMyGame object : objects){
object.doSomething(); //sometimes it is good idea to let objects itself do something
}
}
}
So for example, when you move object, you can check, if he collide or not and then decide what to do.

Categories