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

时间:2011-05-28 16:34:27

标签: java swing nullpointerexception jpanel

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

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

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

2 个答案:

答案 0 :(得分:1)

不要使用公共变量。

定义用于更改要更改的任何属性的值的方法。

此外,自定义绘制是通过覆盖paintComponent()方法完成的。你不应该重写paint()方法。

答案 1 :(得分:1)

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

YootPanel object = new YootPanel();

但是我用了

JPanel object = new YootPanel();

代替.... 难怪我无法访问我在YootPanel类中创建的方法....

谢谢大家的所有答案!