我无法从其他类的数组中提取值 [英] I cannot pull values from an array in a different class

查看:49
本文介绍了我无法从其他类的数组中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lifegrid2数组的打印输出仅给出全零。我在MyPanel中测试了lifegrid数组,并且该数组正在填充,但这些值一直为零。我通过在lifegrid2中放置一些值来检查它们是否被擦除,因此它是从MyPanel获取的,但是只有零。没有错误报告。

The printout of the array lifegrid2 just gives all zeros. I tested the lifegrid array in MyPanel and that is populating, but the values are pulling through as zeros. I checked by putting some values in lifegrid2 and they are being wiped, so it is pulling through from MyPanel, but only zeros. There are no errors being reported.

我制作了一个带有2d数组的小型测试程序,该程序确实可以读取值。

I have made a small test program with a 2d array which does pull values through.

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
   import javax.swing.BorderFactory;
import javax.swing.JButton;

  import java.awt.*; 
import java.awt.event.*; 

public class SwingPaintDemo3 {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}

private static void createAndShowGUI() {
    JFrame f = new JFrame("Swing Paint Demo");
    f.setPreferredSize(new Dimension(1280,800));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);
    JPanel subPanel = new JPanel();
    JButton start = new JButton("START");
    subPanel.add(start);
    start.setPreferredSize(new Dimension(100,50));
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae1) {
            System.out.println("START");
            int [][][] lifegrid2 = new int [12][12][2];
            int temp;
            
            for (int i=0; i<12; i++) {
                for (int j=0; j<12; j++) {
            **MyPanel obj = new MyPanel();
            temp = obj.lifegrid [i][j][0];
                lifegrid2 [i][j][0] = temp;**
                if (j<11) 
                    {System.out.print(lifegrid2 [j] [i] [0]);}
                else 
                    {System.out.println(lifegrid2 [j] [i] [0]);}}}
            
            
            
                                        }   
    });
    f.add(subPanel, BorderLayout.EAST);
    
} 

}




import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

导入javax.swing.JPanel;

import javax.swing.JPanel;

class MyPanel extends JPanel {

public int [][][] lifegrid = new int [12][12][2];

private int squareX = 1280;
private int squareY = 800;
private int gridX, gridY ;

public MyPanel() {

           
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            squareX = e.getX();
            squareY = e.getY();
                            if ((squareX>50 & squareX <550) & (squareY>50 & squareY <550) ){
                gridX =(squareX-50)/50+1;
                gridY =(squareY-50)/50+1;
                
                squareX = (squareX -50)/50 * 50 + 50;
                squareY = (squareY -50)/50 * 50 + 50;
                System.out.println(gridX + " " + gridY);
                lifegrid [gridX] [gridY] [0] = 1;
                
                repaint(squareX,squareY,50,50);}
            else {
                            }
        }
        });
     
                             }







protected void paintComponent(Graphics g) {
    super.paintComponent(g);       
    g.drawString("This is my custom Panel!",10,20);
    g.setColor(Color.RED);
    g.fillRect(squareX,squareY,48,48);
    g.setColor(Color.BLACK);
    g.drawRect(squareX,squareY,48,48);
}  
}


推荐答案


我制作了一个带有2d数组的小型测试程序

I have made a small test program with a 2d array


int [][][] lifegrid2 = new int [12][12][2];

这是一个3D数组。

一个2D数组定义为:

int [][] lifegrid2 = new int [12][12];

初始化数组的逻辑属于您的类的构造函数:

The logic to initialize the array belongs in the constructor of your class:

MyPanel()
{
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 12; j++) {
            lifegrid [i][j] = 0)
        }
    }
}

然后,您的paintComponent()方法需要遍历2D网格并仅绘制值为1的网格。

Then your paintComponent() method needs to iterate through the 2D grid and only paint the grids that have a value of 1.

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
 
   for (int i = 0; i < 12; i++) {
       for (int j = 0; j < 12; j++) {
            if (lifegrid[i][j] = 1)
               // paint the grid
            }
       }
    }
}

编辑:

据我所知,您正在尝试创建一个基于网格的游戏。当您单击网格中的一个单元格时,该单元格将变为红色。有两种常见的解决方法:

As best as I can tell you are trying to create a grid based game. When you click on a cell in the grid the cell turns red. The are two common ways to approach this:


  1. 使用组件。有一个使用 GridLayout 的父面板,然后向网格中的每个单元格添加一个子面板。您还可以将MouseListener添加到每个子面板。当您单击子面板时,会将面板的背景更改为红色。

  1. Use components. In this approach you have a parent panel using a GridLayout and you add a child panel to each cell in the grid. You would also add your MouseListener to each child panel. When you click on the child panel you change the background of the panel to red.

执行自定义绘画。在这种方法中,您只有一个JPanel。此类为状态保留2D数组。网格中每个单元的数量。您将单个MouseListener添加到面板。当您在面板上单击时,您将确定单击了哪个单元格,然后更新该单元格的状态。在paintComponent()方法中,您可以遍历2D数组并绘制状态已更改的每个单元。

Do custom painting. In this approach you have a single JPanel. This class keep a 2D Array for the "state" of each cell in the grid. You add a single MouseListener to the panel. When you click on the panel you determine which cell was clicked and then you update the state of that cell. In the paintComponent() method you iterate through the 2D Array and paint each cell where that state has changed.

您的这种方法似乎是两者之间的某种混合,并且不起作用。

Your approach seems to be some kind of hybrid between the two and is not working.

这篇关于我无法从其他类的数组中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆