我无法从不同类中的数组中提取值。

huangapple go评论86阅读模式
英文:

I cannot pull values from an array in a different class

问题

以下是你提供的代码的翻译:

数组lifegrid2的输出只显示全部为零我在MyPanel中测试了lifegrid数组它是有数据的但这些值都变成了零我尝试了在lifegrid2中放入一些值但它们都被清除了所以它确实是从MyPanel传递过来的但都是零没有报告任何错误

我编写了一个带有二维数组的小测试程序可以成功传递值

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绘图演示");
        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("开始");
        subPanel.add(start);
        start.setPreferredSize(new Dimension(100, 50));
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae1) {
                System.out.println("开始");
                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;

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("这是我的自定义面板!", 10, 20);
        g.setColor(Color.RED);
        g.fillRect(squareX, squareY, 48, 48);
        g.setColor(Color.BLACK);
        g.drawRect(squareX, squareY, 48, 48);
    }
}

请注意,我只提供了代码的翻译部分,不包含问题回答等其他内容。如果您需要进一步的帮助,请随时提问。

英文:

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.

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(&quot;Swing Paint Demo&quot;);
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(&quot;START&quot;);
subPanel.add(start);
start.setPreferredSize(new Dimension(100,50));
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae1) {
System.out.println(&quot;START&quot;);
int [][][] lifegrid2 = new int [12][12][2];
int temp;
for (int i=0; i&lt;12; i++) {
for (int j=0; j&lt;12; j++) {
**MyPanel obj = new MyPanel();
temp = obj.lifegrid [i][j][0];
lifegrid2 [i][j][0] = temp;**
if (j&lt;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;

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&gt;50 &amp; squareX &lt;550) &amp; (squareY&gt;50 &amp; squareY &lt;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 + &quot; &quot; + gridY);
lifegrid [gridX] [gridY] [0] = 1;
repaint(squareX,squareY,50,50);}
else {
}
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);       
g.drawString(&quot;This is my custom Panel!&quot;,10,20);
g.setColor(Color.RED);
g.fillRect(squareX,squareY,48,48);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,48,48);
}  
}

答案1

得分: 1

> 我已经创建了一个带有2D数组的小测试程序。

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

这是一个3D数组。

一个2D数组的定义如下:

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

初始化数组的逻辑应该放在类的构造函数中:

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

然后你的 paintComponent() 方法需要遍历这个2D网格,并且只绘制值为1的格子。

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int i = 0; i &lt; 12; i++) {
for (int j = 0; j &lt; 12; j++) {
if (lifegrid[i][j] = 1)
// 绘制格子
}
}
}
}

编辑:

我认为你正在尝试创建一个基于网格的游戏。当你点击网格中的一个单元格时,单元格会变为红色。有两种常见的方法可以解决这个问题:

  1. 使用组件。在这种方法中,你有一个父面板使用 GridLayout,并在网格的每个单元格中添加一个子面板。你还会为每个子面板添加一个 MouseListener。当你点击子面板时,你会将面板的背景更改为红色。

  2. 进行自定义绘制。在这种方法中,你只有一个 JPanel。该类保留一个用于网格中每个单元格“状态”的2D数组。你只需将一个 MouseListener 添加到面板中。当你点击面板时,你确定点击了哪个单元格,然后更新该单元格的状态。在 paintComponent() 方法中,你会遍历2D数组,并绘制状态发生变化的每个单元格。

你的方法似乎是这两种方法之间的一种混合,但并不起作用。

英文:

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

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

That is a 3D array.

A 2D array is defined as:

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 &lt; 12; i++) {
for (int j = 0; j &lt; 12; j++) {
lifegrid [i][j] = 0)
}
}
}

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 &lt; 12; i++) {
for (int j = 0; j &lt; 12; j++) {
if (lifegrid[i][j] = 1)
// paint the grid
}
}
}
}

Edit:

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. 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.

  2. 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.

huangapple
  • 本文由 发表于 2020年9月13日 23:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63872583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定