无法访问JPanel的公共方法/变量,并且无法从Jpanel访问其他公共方法/变量

发布于 2024-11-10 09:47:41 字数 3735 浏览 5 评论 0原文

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

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

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

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();
        }
    }
}

What I'm trying to do is get updates for where to put draw the ovals.
However the last part's r1 = clientUI.client.c2.getRed(1); throws an NullPointerException

So I tried accessing this Classes public variables r1, r2 ... etc however, I couldn't reach these public variables either....

Is there a way I can modify these variables so that I cam move the ovals around as I get new coordinate updates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2024-11-17 09:47:41

不要使用公共变量。

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

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

Don't use public variables.

Define methods to udate the values of any property that you want to change.

Also, custom painting is done by overriding the paintComponent() method. You should not be overriding the paint() method.

小耗子 2024-11-17 09:47:41

好吧,我很愚蠢,在创建对象时做了错误的工作......
我应该用的

YootPanel object = new YootPanel();

,但我

JPanel object = new YootPanel();

却用了......
难怪我无法访问我在 YootPanel 类中创建的方法......

谢谢大家的回答!

Okay, I was being stupid and did a wrong job when creating the object...
I should have used

YootPanel object = new YootPanel();

but I used

JPanel object = new YootPanel();

instead....
No wonder I couldn't access the methods that I created in the YootPanel class....

Thank you all for all your Answers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文