Java 2D阵列支持网格创建游戏板
我正在尝试创建一个游戏,使用2D数组来显示游戏的布局 我正在努力将int更改为Java 2D阵列支持网格创建游戏板,java,arrays,image,grid,jbutton,Java,Arrays,Image,Grid,Jbutton,我正在尝试创建一个游戏,使用2D数组来显示游戏的布局 我正在努力将int更改为JButton,并根据网格中显示的数字为其分配不同的图像: private void PlayPanel() { try { iconBlank=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("white32x32.jpg"))); } catch (Excepti
JButton
,并根据网格中显示的数字为其分配不同的图像:
private void PlayPanel() {
try
{
iconBlank=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("white32x32.jpg")));
}
catch (Exception e)
{
System.err.println("Blank Icon ImageIcon" +e);
}
try
{
iconSand=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sand.jpg")));
}
catch (Exception e)
{
System.err.println("Sand Icon ImageIcon" +e);
}
try
{
iconBall=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sand60x60.png")));
}
catch (Exception e)
{
System.err.println("Ball Icon ImageIcon" +e);
}
try
{
iconEnd=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sandstone.jpg")));
}
catch (Exception e)
{
System.err.println("End Icon ImageIcon" +e);
}
pPlayScreen =new JPanel();
pPlayScreen.setPreferredSize(new Dimension(550,520));
pPlayScreen.setBorder( BorderFactory.createRaisedBevelBorder() );
pPlayScreen.setBackground(Color.white);
pPlayScreen.setLayout (new GridLayout (13,16));
int[][] playButtons = {
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 3, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
for (int rows = 0 ; rows < 16 ; rows++)
{
for (int cols = 0 ; cols < 13 ; cols++)
{
if(playButtons [rows][cols]==0){
// ???
}
playButton [rows] [cols] = new JButton ();
playButton [rows] [cols].addActionListener (new Play());
pPlayScreen.add (playButton [rows] [cols]);
pMain.add(pPlayScreen);
}
}
}
private void播放面板(){
尝试
{
iconBlank=newImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource(“white32x32.jpg”));
}
捕获(例外e)
{
System.err.println(“空白图标ImageIcon”+e);
}
尝试
{
iconSand=newImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource(“sand.jpg”));
}
捕获(例外e)
{
System.err.println(“沙图标图像图标”+e);
}
尝试
{
iconBall=newImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource(“sand60x60.png”));
}
捕获(例外e)
{
System.err.println(“球形图标ImageIcon”+e);
}
尝试
{
iconEnd=newImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource(“sandstore.jpg”));
}
捕获(例外e)
{
System.err.println(“结束图标ImageIcon”+e);
}
pPlayScreen=newjpanel();
pPlayScreen.setPreferredSize(新尺寸(550520));
pPlayScreen.setboorder(BorderFactory.createRaisedBevelBorder());
pPlayScreen.setBackground(颜色:白色);
pPlayScreen.setLayout(新网格布局(13,16));
int[][]播放按钮={
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 3, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
对于(int行=0;行<16;行++)
{
for(int cols=0;cols<13;cols++)
{
如果(播放按钮[行][cols]==0){
// ???
}
playButton[rows][cols]=newjbutton();
playButton[rows][cols].addActionListener(新播放());
pPlayScreen.add(播放按钮[行][cols]);
pMain.add(pPlayScreen);
}
}
}
不能将JButton分配到int数组中。它需要是一个对象数组,才能同时存储整数和jbutton。我建议采用更面向对象的方法。无法将按钮转换为int
例如,尝试使用MVC(model、view、controller),然后您可以只使用包含2D int数组的某个对象作为模型,并使用一个视图,根据模型显示您喜欢的按钮类型来解释该数组。考虑创建一个自定义类,该类同时具有JButton和int的属性,例如
public class MyButton {
private int n;
private JButton button;
// Constructor
public MyButton() {
}
// Accessors and mutators
private void setN(int n)
this.n =n;
}
private int getN() {
return this.n;
}
private void setButton(JButton button) {
this.button = button;
}
private JButton getButton() {
return this.button;
}
}
ArrayList<MyButton> buttons = new ArrayList<MyButton>();
然后你可以通过创建一个新的MyButton对象来访问它,然后像这样有一个数组列表
public class MyButton {
private int n;
private JButton button;
// Constructor
public MyButton() {
}
// Accessors and mutators
private void setN(int n)
this.n =n;
}
private int getN() {
return this.n;
}
private void setButton(JButton button) {
this.button = button;
}
private JButton getButton() {
return this.button;
}
}
ArrayList<MyButton> buttons = new ArrayList<MyButton>();
ArrayList按钮=新建ArrayList();
然后你可以做你需要的事。这就是OOP和Java类功能的美妙之处。这些天我在做JavaScript时非常想念这个。哈哈。希望这有帮助……:) 你有什么问题?