2D array of ImageIcon's displays inconsistently and only partially in JPanel - java

I'm trying to create a screen full of 16x16 floor tiles using ImageIcons, JLabels, and a JPanel. My issue is that whenever I try to display these tiles using a 2D array, - even when looping the drawing method repeatedly - they don't display entirely. Rather, they only display a few tiles as opposed to the full arrays worth. Furthermore, the number of displayed tiles changes each time I run the program!
Here's the code I'm using:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrameTest {
static BufferedImage buf;
static JPanel panel;
public static void main(String[] args) {
createWindow();
loadImage();
showImage();
}
public static void createWindow() {
JFrame frame = new JFrame();
panel = new JPanel();
frame.setSize(1000, 1000);
frame.setTitle("Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.setVisible(true);
panel.setLayout(null);
}
public static void loadImage() {
try {
buf = ImageIO.read(new File("res/test.png"));
} catch (IOException ex) {
System.out.println("NOT FOUND");
}
}
public static void showImage() {
final int[][] MAP =
{
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}
};
ImageIcon test = new ImageIcon(buf);
panel.setBackground(Color.WHITE);
System.out.println(test.getIconWidth());
System.out.println(test.getIconHeight());
JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setSize(test.getIconWidth(), test.getIconWidth());
labelGrid[r][c].setLocation(test.getIconWidth() * r, test.getIconHeight() * c);
labelGrid[r][c].setIcon(test);
panel.add(labelGrid[r][c]);
}
}
panel.revalidate();
}
}
Here about what's normally printed:
It seems that the program just gives up in displaying the images. However, note that the number of displayed images changes every time! Some times only five images may appear, other times 22.
When I try using a smaller map such as:,
final int[][] MAP =
{
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}
};
the entire 3x3 grid of images will appear about half the time, only one or two images being displayed the other half.
What work around can be implemented to solve this problem?
Computer specs:
Windows 10
i8-8700
1080 ti
16gb ram

Your basic problem is one of misunderstanding ... and welcome to my world 🙄
Using panel.setLayout(null); means you are taking full responsibility for the layout, which is fine, when you know what you are doing.
Compound that with calling setVisible before you've established the UI and using revalidate which is used to run a layout pass and you're setting yourself up for trouble.
Instead of revalidate, you should be using repaint, to trigger a new paint pass (note, in the normal course of events, you should use both when updating the the UI, but since you're no longer using a layout manager, you don't need revalidate)
Possibly a better solution is to use a custom painting route, for example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
protected static final int[][] MAP
= {
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}
};
protected static Dimension GRID_SIZE = new Dimension(20, 20);
private Rectangle buf; // This represents your image
private Dimension preferredSize;
public TestPane() {
buf = new Rectangle(0, 0, GRID_SIZE.width, GRID_SIZE.height);
int max = 0;
for (int row = 0; row < MAP.length; row++) {
max = Math.max(max, MAP[row].length);
}
preferredSize = new Dimension(GRID_SIZE.width * MAP.length, GRID_SIZE.height * max);
}
#Override
public Dimension getPreferredSize() {
return preferredSize;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (int row = 0; row < MAP.length; row++) {
for (int col = 0; col < MAP[row].length; col++) {
int x = GRID_SIZE.width * row;
int y = GRID_SIZE.height * col;
Graphics2D translated = (Graphics2D) g2d.create();
translated.translate(x, y);
translated.draw(buf);
translated.dispose();
}
}
g2d.dispose();
}
}
}
GridLayout
Another option is to use GridLayout
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
protected static final int[][] MAP
= {
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}
};
protected static Dimension GRID_SIZE = new Dimension(20, 20);
private BufferedImage buf; // This represents your image
public TestPane() {
int max = 0;
for (int row = 0; row < MAP.length; row++) {
max = Math.max(max, MAP[row].length);
}
setLayout(new GridLayout(MAP.length, max));
buf = makeBuffer();
for (int row = 0; row < MAP.length; row++) {
for (int col = 0; col < MAP[row].length; col++) {
add(new JLabel(new ImageIcon(buf)));
}
}
}
protected BufferedImage makeBuffer() {
BufferedImage img = new BufferedImage(GRID_SIZE.width, GRID_SIZE.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
g2d.dispose();
return img;
}
}
}
but this will resize the cells, which might not be desirable in which you could use a ...
GridBagLayout
This will honour the preferred size of the components better and, based on the way it's currently configured, won't resize the components when the window size changes
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
protected static final int[][] MAP
= {
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}
};
protected static Dimension GRID_SIZE = new Dimension(20, 20);
private BufferedImage buf; // This represents your image
public TestPane() {
int max = 0;
for (int row = 0; row < MAP.length; row++) {
max = Math.max(max, MAP[row].length);
}
setLayout(new GridBagLayout());
buf = makeBuffer();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
for (int row = 0; row < MAP.length; row++) {
gbc.gridx = 0;
for (int col = 0; col < MAP[row].length; col++) {
add(new JLabel(new ImageIcon(buf)), gbc);
gbc.gridx++;
}
gbc.gridy++;
}
}
protected BufferedImage makeBuffer() {
BufferedImage img = new BufferedImage(GRID_SIZE.width, GRID_SIZE.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
g2d.dispose();
return img;
}
}
}

Related

Breadth First Search from scratch in Java

I wrote a java-code to find the path in an 2d array. The array is filled with 0=valid node and 1=obstacle. The code runs perfetcly with small 2d arrays for example with the size of int [][] obstacleMap=new int[10][11];
When I increase the size of the array (e.g. int[][] obstacleMap =[600][300];) now the code doesn't work anymore, I get not an Error. I have tested a lot of times my code, I think the problem is the while loop within the findpath function (code below)
My code has following functions:
main: to test the other functions
2.findPath: find path with given map from start position to target position
3.addNeighbour: add neighbour
Following classes:
Node : x and y coordinates and distance to start
pathCoordinates: x and y values for path
The Code is here:
package strategyRobot;
//import java.awt.datatransfer.SystemFlavorMap;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.LinkedList;
import java.util.List;
public class strategyRobotClass {
static Set<String> visitedNodes = new HashSet<>();
public static void main(String[] args) {
int[][] randomMap={
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 1, 0 ,1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
int value = randomMap[2][6];
System.out.printf("value = %d", value);
System.out.println(" ");
LinkedList<pathCoordinates> pathMap= findPath(randomMap,0,0,3,6);
for(int i = 0; i<pathMap.size();i++) {
System.out.printf("(%d,%d) %n",pathMap.get(i).x,pathMap.get(i).y);
}
}
static public LinkedList<pathCoordinates> findPath(int map[][], int xStartPos, int yStartPos, int xTargetPos, int yTargetPos) {
LinkedList<pathCoordinates> pathMap = new LinkedList<pathCoordinates>();
Node source = new Node(xStartPos, yStartPos,0);
Queue<Node> queue= new LinkedList<Node>();
queue.add(source);
while(!queue.isEmpty()) { // IS THE BUG HERE ????
Node poped = queue.poll();
if(poped.x == xTargetPos && poped.y ==yTargetPos) {
return pathMap;
}
else {
map[poped.x][poped.y] = 1 ;
pathCoordinates coordinates = new pathCoordinates(poped.x,poped.y);
pathMap.add(coordinates);
List<Node> neighbourList = addNeighbours(poped,map);
queue.addAll(neighbourList);
}
}
return null;
}
static public List addNeighbours(Node poped, int[][] map) {
List<Node> list=new LinkedList<Node>();
if((poped.x-1 >0 && poped.x-1<map.length) && (map[poped.x-1][poped.y]== 0)) {
list.add(new Node(poped.x-1, poped.y, poped.distanceFromStart+1));
}
if((poped.x+1 >0 && poped.x+1<map.length) && (map[poped.x+1][poped.y]== 0)) {
list.add(new Node(poped.x+1, poped.y, poped.distanceFromStart+1));
}
if((poped.y-1 >0 && poped.y-1<map.length) && (map[poped.x][poped.y-1]== 0)) {
list.add(new Node(poped.x-1, poped.y, poped.distanceFromStart+1));
}
if((poped.y+1 >0 && poped.y+1<map.length) && (map[poped.x][poped.y+1]== 0)) {
list.add(new Node(poped.x, poped.y+1, poped.distanceFromStart+1));
}
return list;
}
static public class Node{
int x, y; //coordinates in a cell
int distanceFromStart;
Node parent;
Node(int x, int y, int distanceFromStart){
this.x=x;
this.y=y;
this.distanceFromStart=distanceFromStart;
}
}
static public class pathCoordinates {
int x,y;
pathCoordinates(int x, int y){
this.x=x;
this.y=y;
}
}

Efficient way to trim 2d array in Java

I have a following 2 dimensional array:
int[][] array = new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};
and I would like to trim all the surrounding zeroes, so my output would be like this (removing "zeros" outside and preserving the zeroes that are surrounded by "ones"):
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 1},
{0, 1, 1, 1, 1},
{0, 0, 0, 1, 0}
I'm looking for an efficient way of doing this.
Possible solution (dunno if it is the most efficient way):
public static int[][] trim(int[][] mtx, int rmin, int rmax, int cmin, int cmax) {
int[][] result = new int[rmax-rmin+1][];
for (int r = rmin, i = 0; r <= rmax; r++, i++) {
result[i] = Arrays.copyOfRange(mtx[r], cmin, cmax+1);
}
return result;
}
public static int[][] trim(int[][] mtx, int trimmed) {
int cmin = mtx[0].length;
int rmin = mtx.length;
int cmax = -1;
int rmax = -1;
for (int r = 0; r < mtx.length; r++)
for (int c = 0; c < mtx[0].length; c++)
if (mtx[r][c] != trimmed) {
if (cmin > c) cmin = c;
if (cmax < c) cmax = c;
if (rmin > r) rmin = r;
if (rmax < r) rmax = r;
}
return trim(mtx, rmin, rmax, cmin, cmax);
}
public static void main (String[] args) {
int[][] array = new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};
int[][] trim = trim(array, 0);
System.out.println(Arrays.deepToString(trim));
}

Checking for number repetition in a line or column in a grid

I have to make a Sudoku board and have the grid but am confused as how to check for any repetitions across a line or column. The code for my grid is.
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
grid[i][j] = new JTextField();
BoardPanel.add(grid[i][j]);
So basically i want to check for repetition across i and then down j
Here is the code with an example to show it.
This program will tell if there are two equal numbers in vertical and horizontal bars.
public static void main(String args[]) {
// store your puzzle in puzzle variable.
int puzzle[][] = {
{0, 9, 0, 0, 0, 0, 0, 0, 8},
{0, 0, 3, 2, 0, 7, 0, 9, 0},
{0, 6, 0, 0, 0, 0, 7, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 6},
{0, 0, 5, 4, 3, 2, 1, 0, 0},
{4, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 7, 0, 0, 0, 0, 3, 0},
{0, 2, 0, 9, 0, 8, 6, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 4, 0}
};
testHorizontal(puzzle);
testVertical(puzzle);
}
public static void testHorizontal(int[][] puzzle) {
for (int[] arr : puzzle) {
test(arr);
}
}
public static void testVertical(int[][] puzzle) {
int[] cols = new int[puzzle.length];
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle.length; j++) {
cols[j] = puzzle[i][j];
test(cols);
}
}
}
public static boolean test(int arr[]) {
boolean flag = false;
for (int a : arr) {
for (int b : arr) {
if (a == b) {
flag = true;
System.out.println("equal numbers found.");
break;
}
}
}
return flag;
}

Adding ActionListeners to buttons added straight into a container

im trying to create a custom JPanel which i add into a card layout in my GUI set up. in this custom layout i use a gridlayout and add buttons directly into a container. i now have a need to add actionlisteners to these buttons and dont know how. any help would be appreciated and please find the custom class below.
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PuzzleSudokuPanel extends JPanel{
int[][] grid =
{{0, 0, 0, 5, 0, 0, 8, 3, 1},
{0, 5, 3, 0, 0, 8, 7, 0, 0},
{7, 0, 0, 0, 4, 2, 0, 6, 0},
{0, 0, 9, 0, 3, 0, 0, 7, 8},
{0, 0, 2, 4, 0, 6, 1, 0, 0},
{6, 3, 0, 0, 9, 0, 2, 0, 0},
{0, 1, 0, 7, 6, 0, 0, 0, 9},
{0, 0, 4, 9, 0, 0, 5, 8, 0},
{2, 9, 7, 0, 0, 3, 0, 0, 0}};
Container myGrid = new Container();
public PuzzleSudokuPanel ()throws NullPointerException{
try{
for(int i = 0; i<9; i++){
for(int j = 0; j<9; j++){
String appropriateNumber = convertSimple(grid[i][j]);
myGrid.add(new JButton(appropriateNumber));
}
}
}
catch(NullPointerException e){
}
myGrid.setLayout(new GridLayout(9, 9));
myGrid.setPreferredSize (new Dimension(400, 400));
add(myGrid);
}
public static String convertSimple(int a){
return ("" + a);
}
}
[edit]
calkuro code:
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PuzzleCalkuroPanel extends JPanel{
String[][] grid =
{{"3/", "3/", "3-", "8x"},
{"9+", "9+", "3-", "8x"},
{"9+", "9+2", "9+2", "8x"},
{"1", "9+2", "1-", "1-"}};
Container myGrid = new Container();
public PuzzleCalkuroPanel ()throws NullPointerException{
try{
for(int i = 0; i<4; i++){
for(int j = 0; j<4; j++){
JButton button = new JButton(grid[i][j]);
button.addActionListener(new calkuroListener());
myGrid.add(new JButton(grid[i][j]));
}
}
}
catch(NullPointerException e){
}
myGrid.setLayout(new GridLayout(4, 4));
myGrid.setPreferredSize (new Dimension(400, 400));
add(myGrid);
}
private class calkuroListener implements ActionListener{
public void actionPerformed (ActionEvent event){
String numStr = JOptionPane.showInputDialog("Enter a number to change to: ");
JOptionPane.showMessageDialog(null, numStr);
}
}
}
Instead of...
myGrid.add(new JButton(appropriateNumber));
Do...
JButton button = new JButton(appropriateNumber);
button.addActionListener(...);
myGrid.add(button);
UPDATED
This is what you're doing...
JButton button = new JButton(grid[i][j]); // Create button here, good...
button.addActionListener(new calkuroListener()); // Add listener here, good...
myGrid.add(new JButton(grid[i][j])); // Create a new button here, bad...
This is what you should be doing
JButton button = new JButton(grid[i][j]); // Create button here, good...
button.addActionListener(new calkuroListener()); // Add listener here, good...
myGrid.add(button); // Add button here, good...

Android java assigning 2d array to 3d array

I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.
Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it.
This is how I have done it and was wondering if that is the correct way of applying it:
private int[][][] masterArray;
private int[][] childArray1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
{1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:
masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
Would that work?
In my application things aren't working so I picking out code that I am not 100% sure on.
It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.
From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)
It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:
masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];
then you can use:
master[currentLevel][x][y] = childArray[x][y];
In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint.
In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.
masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
}
If you ever work with massive arrays and need speed then you could look at System.arrayCopy();
String[] arr1D;
String[][] arr2D;
String[][][] arr3D;
arr1D = new String[] { "1", "2", "3" };
//////////////////////////////////////////////////////////////////////////
//assign 1D array to element of 2D array
//////////////////////////////////////////////////////////////////////////
arr2D = new String[][] {
arr1D ,
arr1D ,
arr1D
};
/*
// OR
arr2D = new String[3][];
arr2D[0] = arr1D;
arr2D[1] = arr1D;
arr2D[2] = arr1D;
// OR
arr2D = new String[][] {
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" }
};
*/
//////////////////////////////////////////////////////////////////////////
//assign 2D array to element of 3D array
//////////////////////////////////////////////////////////////////////////
arr3D = new String[][][] {
arr2D ,
arr2D ,
arr2D
};
/*
// OR
arr3D = new String[3][][];
arr3D[0] = arr2D;
arr3D[1] = arr2D;
arr3D[2] = arr2D;
*/

Categories