从另一个类调用paintComponent() - NullPointerException

时间:2014-06-06 20:30:25

标签: java swing paintcomponent

您好,我目前正在开发超级六边形克隆,但不幸的是我在java上非常糟糕。我一般都明白这一点,但我从来没有教过大部分细节,也没有时间适当地进入编程......

实际游戏正在运行的第一个程序告诉班级" Wall"产生什么样的墙,从而为绘制所有内容的程序提供什么信息。

这是Frame.java:

package madhex;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Frame2 extends JFrame{
    public Screen screen;

    int w = 1280, h = 800, r= 50;
    int playery[] = {(h/2)+r+20, ((h/2)+r+20)-8, ((h/2)+r+20)-8};
    int playerx[] = {w/2, (w/2)-8, (w/2)+8};


    public Frame2()
        {
            super("Hexagon");
            screen = new Screen();
            screen.setBounds(0, 0, w, h);
            add(screen);
            addKeyListener(new KeyHandler());
        }




    public void repaintScreen(){
        screen.repaint();
    }

    public class Screen extends JLabel
    {
        @Override
        public void paintComponent(Graphics g)
        {

        super.paintComponent(g);
        Graphics g2 = g;
        AffineTransform at = new AffineTransform();

        //generelle Drehung
        at.setToRotation(dreiecktest.getRotation(), w/2, h/2);
        ((Graphics2D) g2).setTransform(at);

        //Hexagon
        g2.setColor(Color.BLACK);
        g2.drawPolygon(hexagonx, hexagony, 6);

        //A
        g2.setColor(Color.GRAY);
        g2.fillPolygon(A1x, A1y, 4);
        g2.fillPolygon(A2x, A2y, 4);
        g2.fillPolygon(A3x, A3y, 4);

        //B
        g2.setColor(Color.LIGHT_GRAY);
        g2.fillPolygon(B1x, B1y, 4);
        g2.fillPolygon(B2x, B2y, 4);
        g2.fillPolygon(B3x, B3y, 4);

        g2.setColor(Color.BLACK);
        g2.fillPolygon(testx, testy, 12);

        //Player
        at.setToRotation(dreiecktest.getGrad()+dreiecktest.getRotation(), w/2, h/2);
        System.out.println(getPlayerRotation());
        System.out.println(getPlayerSector());
        ((Graphics2D) g2).setTransform(at);

        g2.setColor(Color.BLACK);
        g2.fillPolygon(playerx, playery, 3);



        }

        public void draw(Graphics g, int Ax[],int Ay[])
        {
            Graphics g3 = (Graphics)g;
            super.paintComponent(g3);

            g3.fillPolygon(Ax, Ay, 12);
        }

    }

}

Method draw(Graphics g,int ArrayForXcoordinates,int ArrayForYcoordinates)应该从Wall.java获取数组:

public class Wall
{
Frame2 f = new Frame2();
int openSector= 0;
int d = 50;
int w=1280, h =800, r=50;
Graphics g = f.screen.getGraphics();

Wall (int openSector) {
    this.openSector = openSector;

}

public void display() 
{
    if (openSector == 1)
    {
        int Ax [] = {_values}
        int Ay [] = {_values}

        f.screen.draw(g, Ax, Ay);

    }
    if (openSector== 2)
    {

            //I left out the arrays, because they were just taking up too much space        

        f.screen.draw(g, Ax, Ay);
    }


    f.screen.draw(g, Ax, Ay);
}

public void tick () 
{
    d--;
    this.display();
}
}

但是我在f.screen.draw(g,Ax,Ay)的super.paintComponent(g3)上得到一个NullPointerException;在方法draw()和实际运行时程序中的点上,运行Wall的方法tick()。

我全都没有帮助,虽然我会理解,如果有人向我解释,我完全放弃了通过谷歌搜索我自己找到它......

感谢提前!

0 个答案:

没有答案