gpt4 book ai didi

java - 无法访问 JPanel 的公共(public)方法/变量,并且无法从 Jpanel 访问其他公共(public)方法/变量

转载 作者:行者123 更新时间:2023-12-02 08:17:07 25 4
gpt4 key购买 nike

package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class YootPanel extends JPanel
{

private ImageIcon bgIcon;
public JTextArea textBox;
public ClientUI clientUI;
public static final int HORSE_HEIGHT = 25;
public static final int HORSE_WIDTH = 40;
public int r1,r2,r3,r4;
public int b1,b2,b3,b4;
YootPanelPlot plot;

public YootPanel(ClientUI clientUI)
{
super();
init_rest();
init_bg();
plot = = new YootPanelPlot();
this.setVisible(true);
this.clientUI = clientUI;
}

private void init_bg()
{
java.net.URL imgUrl = ClientUI.class.getResource("../images/images/background.png");
bgIcon = new ImageIcon(imgUrl);
}

protected void paintComponent(Graphics g)
{
try
{
g.drawImage(bgIcon.getImage(), -20, 0, null);
super.paintComponent(g);
} catch (Exception e)
{


set_Loc();


g.setColor(Color.RED);
g.fillOval(plot.plot[0][r1], plot.plot[1][r1], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][r2], plot.plot[1][r2], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][r3], plot.plot[1][r3], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][r4], plot.plot[1][r4], HORSE_WIDTH, HORSE_HEIGHT);
g.setColor(Color.BLUE);
g.fillOval(plot.plot[0][b1], plot.plot[1][b1], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][b2], plot.plot[1][b2], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][b3], plot.plot[1][b3], HORSE_WIDTH, HORSE_HEIGHT);
g.fillOval(plot.plot[0][b4], plot.plot[1][b4], HORSE_WIDTH, HORSE_HEIGHT);
System.err.println("Paint");
}
}

public void paint(Graphics g)
{
super.paint(g);

}

private void init_rest()
{
this.setBorder(javax.swing.BorderFactory.createTitledBorder("Yoot Map"));
this.setFocusable(false);
this.setMinimumSize(new java.awt.Dimension(670, 400));
this.setName("map"); // NOI18N
this.setOpaque(false);
this.setPreferredSize(new java.awt.Dimension(630, 400));
this.setSize(new java.awt.Dimension(630, 400));
this.setVerifyInputWhenFocusTarget(false);

org.jdesktop.layout.GroupLayout yootMapLayout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(yootMapLayout);
yootMapLayout.setHorizontalGroup(
yootMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(0, 658, Short.MAX_VALUE));
yootMapLayout.setVerticalGroup(
yootMapLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(0, 442, Short.MAX_VALUE));
}

public void set_Loc()
{
try
{
System.err.println("......"+clientUI.client.c2.blueHorseLocation[0]);
r1 = clientUI.client.c2.getRed(0);
r2 = clientUI.client.c2.getRed(1);
r3 = clientUI.client.c2.getRed(2);
r4 = clientUI.client.c2.getRed(3);
b1 = clientUI.client.c2.blueHorseLocation[0];
b2 = clientUI.client.c2.blueHorseLocation[1];
b3 = clientUI.client.c2.blueHorseLocation[2];
b4 = clientUI.client.c2.blueHorseLocation[3];
} catch (NullPointerException e)
{
e.printStackTrace();
}
}
}

我想做的是获取在哪里绘制椭圆的更新。然而最后一部分的 r1 = clientUI.client.c2.getRed(1);抛出 NullPointerException

所以我尝试访问这个类公共(public)变量 r1、r2 ...等,但是我也无法访问这些公共(public)变量......

有没有办法可以修改这些变量,以便在获得新的坐标更新时可以移动椭圆形?

最佳答案

不要使用公共(public)变量。

定义方法来更新您想要更改的任何属性的值。

此外,自定义绘制是通过重写paintComponent()方法来完成的。您不应该重写 Paint() 方法。

关于java - 无法访问 JPanel 的公共(public)方法/变量,并且无法从 Jpanel 访问其他公共(public)方法/变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6162925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com