简体   繁体   English

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

[英]Can't Access JPanel's Public method/variable, and can't access from Jpanel other public method/variable

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);然而最后一部分的 r1 = clientUI.client.c2.getRed(1); throws an NullPointerException抛出 NullPointerException

So I tried accessing this Classes public variables r1, r2... etc however, I couldn't reach these public variables either....因此,我尝试访问此类公共变量 r1、r2... 等,但是,我也无法访问这些公共变量...。

Is there a way I can modify these variables so that I cam move the ovals around as I get new coordinate updates?有没有办法可以修改这些变量,以便在获得新的坐标更新时移动椭圆?

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.此外,自定义绘画是通过覆盖 paintComponent() 方法来完成的。 You should not be overriding the paint() method.您不应该覆盖paint() 方法。

Okay, I was being stupid and did a wrong job when creating the object... I should have used好的,在创建 object 时,我很愚蠢并且做错了工作......我应该使用

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....相反......难怪我无法访问我在 YootPanel class 中创建的方法......

Thank you all for all your Answers!谢谢大家的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM