Gostaria de saber qual a forma correta de se passar como parametro de um método um array bidimensional.
Acredito que da mesma forma que a declara:
[code]
int[][] array = null;
public void metodo(int[][] arrayBiDimensional){;}[/code]
Bom, a minha duvida é quando eu tenho que chamar este metodo.
getMetodo();
Não compreendi. Quando você tem que chamar um método? Geralmente esse “requisito” é de seu domínio.
Vou dar um exemplo
public class Grid
{
public Grid(boolean preenchido[10][10])
{
}
}
Continuo sem entender, escreva mais. Porém vou tentar advinhar, você quer saber como criar esse objeto?
public class Grid{
public static void main(String[] args){
Grid test = new Grid(new boolean[10][10]);
}
// public Grid(boolean preenchido[10][10]){
public Grid(boolean[][] preenchido){
}
}
É isso?
É que eu to tentando montar um jogo e eu me preocupei apenas em criar um array bidimensional para o objeto player
e quando quase terminei o player percebi que tinha que adicionar mais um parametro ao metodo createObj()
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GameWindow extends JFrame
{
private static Container tela;
private static GridPanel grid;
private static JTextField cmd;
private Player player;
private static boolean vGrid[][] = new boolean[30][10];
private static boolean obj[][] = new boolean[3][3];
private static int rows, cols, orient;
private Player ene1, ene2, ene3, ene4, ene5, ene6;
public GameWindow()
{
tela = getContentPane();
setTitle("JTetris");
setSize(350, 525);
setResizable(false);
tela.setLayout(new FlowLayout(FlowLayout.LEFT));
components();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void components()
{
grid = new GridPanel(10, 30);
grid.setPreferredSize(new Dimension(195, 485));
grid.setCorFundoPanel(1, 1, 10, Color.black);
cmd = new JTextField(0);
cmd.addKeyListener(new Commands());
rows = 18;
cols = 4;
player = new Player();
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
tela.add(grid);
tela.add(cmd);
}
public void clearGridPanel()
{
for(int i = 0; i <= 29; i++)
for(int j = 0; j <= 9; j++)
{
vGrid[i][j] = false;
colorGridPanel();
}
}
public void colorGridPanel()
{
for(int i = 0; i <= 29; i++)
for(int j = 0; j <= 9; j++)
{
if(vGrid[i][j])
grid.setCorFundoPanel(i+1, j+1, 10, Color.yellow);
else
grid.setCorFundoPanel(i+1, j+1, 10, Color.gray);
}
}
public void createEnemies()
{
}
public class Player
{
public void createObj(int rows, int cols)
{
for(int i=0; i<=2; i++)
for(int j=0; j<=2; j++)
{
obj[i][j] = false;
}
switch(orient)
{
case 1:{
obj[0][1] = true;
obj[1][0] = true;
obj[1][1] = true;
obj[1][2] = true;
obj[2][0] = true;
obj[2][2] = true;
};break;
case 2:{
obj[0][0] = true;
obj[0][2] = true;
obj[1][0] = true;
obj[1][1] = true;
obj[1][2] = true;
obj[2][1] = true;
};break;
case 3:{
obj[0][1] = true;
obj[0][2] = true;
obj[1][0] = true;
obj[1][1] = true;
obj[2][1] = true;
obj[2][2] = true;
};break;
case 4:{
obj[0][0] = true;
obj[0][1] = true;
obj[1][1] = true;
obj[1][2] = true;
obj[2][0] = true;
obj[2][1] = true;
};break;
default:{
obj[0][1] = true;
obj[1][0] = true;
obj[1][1] = true;
obj[1][2] = true;
obj[2][0] = true;
obj[2][2] = true;
};break;
}
for(int i=0; i<=2; i++)
for(int j=0; j<=2; j++)
{
if(obj[i][j])
{
vGrid[i+rows][j+cols] = true;
colorGridPanel();
}
}
}
}
public class Commands implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==38)
{
if(orient!=1)
{
orient = 1;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
else
{
if(rows <= 0)
rows = 0;
else
{
rows = rows - 1;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
}
}
if(e.getKeyCode()==40)
{
if(orient!=2)
{
orient = 2;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
else
{
if(rows >= 27)
rows = 27;
else
{
rows = rows + 1;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
}
}
if(e.getKeyCode()==37)
{
if(orient!=3)
{
orient = 3;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
else
{
if(cols <= 0)
cols = 0;
else
{
cols = cols - 1;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
}
}
if(e.getKeyCode()==39)
{
if(orient != 4)
{
orient = 4;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
else
{
if(cols >= 7)
cols = 7;
else
{
cols = cols + 1;
clearGridPanel();
player.createObj(rows, cols);
colorGridPanel();
}
}
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
}
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class GridPanel extends JPanel
{
private static ArrayList<JPanel> cellList;
public static JPanel cell;
public GridPanel(int cols, int rows)
{
setLayout(new GridLayout(rows, cols));
setBorder(BorderFactory.createEtchedBorder());
cellList = new ArrayList<JPanel>();
for(int i=0; i<cols; i++)
for(int j=0; j<rows; j++)
{
cell = new JPanel();
cell.setBorder(BorderFactory.createEtchedBorder());
cellList.add(cell);
add(cell);
}
}
public void setCorFundoPanel(int rows, int cols, int columns, Color cor)
{
int nrPanel = (rows - 1) * columns + (cols - 1);
if(nrPanel<cellList.size())
{
cell = cellList.get(nrPanel);
cell.setBackground(cor);
}
}
}