在尝试使用Graphics2D在JPanel上渲染时,如何修复此NullPointerException?

时间:2016-08-03 10:34:39

标签: java nullpointerexception jpanel graphics2d jwindow

我一直试图为我的游戏开店。 这是不成功的。

我尝试过drawComponent,没有工作。 没有错误,代码执行但没有工作。 现在我想做:

private void render() {
    Graphics2D g = (Graphics2D) graphics.getGraphics();

    /////////////////////
    g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
    /////////////////////
    g.dispose();

    Graphics2D g2d = (Graphics2D) getGraphics();
    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();
}

现在我在g2d上得到一个NullPointerException。 我已经尝试了一切。

`Exception in thread "game" java.lang.NullPointerException
    at com.johnythecarrot.game.Shop$DrawPane.access$2(Shop.java:123)
    at com.johnythecarrot.game.Shop.render(Shop.java:154)
    at com.johnythecarrot.game.Game.render(Game.java:75)
    at com.johnythecarrot.game.Game.run(Game.java:112)
    at java.lang.Thread.run(Unknown Source)`

我的目标是能够拥有可点击的按钮。 它DID工作。但我几乎每次都要重启。因为代码的大部分时间都没有被执行。所以我试着解决它。现在一切都搞砸了。

这是它的代码。 (DoubleInt是我图书馆的一部分,它只不过是x和y。)

public class Shop {

    public BuildWindow window;
    public static JWindow w;

    private int WIDTH = 860, HEIGHT = 440;

    private BufferedImage graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    public DrawPane drawPane;

    public Shop() {
        //window = new BuildWindow().setSize(new DoubleInt(100, 100)).at(wi, he).setTitle("Shop").setOpacity(1).setDragable(false).showEmpty(true);
        w = new JWindow();
        w.setOpacity(1);
        w.setSize(WIDTH, HEIGHT);
        w.setLocation(800, 800);
        w.setVisible(false);
        w.setAlwaysOnTop(true);
        //graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    }

    private void createShop() {
        /***Graphics2D g = (Graphics2D) graphics.getGraphics();
        g.setColor(Color.blue);
        g.drawString("hey", WIDTH-50, HEIGHT-50);
        g.fillRect(0, 0, WIDTH, HEIGHT);*/
    }

    public class DrawPane extends JPanel {

        int width = WIDTH;
        int height = HEIGHT;
        private ArrayList<Shape> buttons;
        private Shape btn1 = new Rectangle2D.Double(20, 60, width/2, height-20);
        private Shape btnClose = new Rectangle2D.Double(width-25, 5, 20, 20);

        Point wCoords;
        Point mCoords;

        public DrawPane() {
            buttons = new ArrayList<>();
            buttons.add(btn1);
            buttons.add(btnClose);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    for(Shape s : buttons) {
                        if(s.contains(e.getPoint())) {
                            System.out.println("Clicked " + s.getBounds());
                            if(s == btnClose) {
                                w.dispose();
                            }
                        }
                    }
                }
                @Override
                public void mousePressed(MouseEvent e) {
                    mCoords = e.getPoint();
                }
                @Override
                public void mouseReleased(MouseEvent arg0) {
                    mCoords = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                wCoords = e.getLocationOnScreen();
                w.setLocation(wCoords.x - mCoords.x, wCoords.y - mCoords.y);
                }
            });
        }

        void repaintThis() {
            repaint();
        }

        BufferedImage img = loadImageFrom.LoadImageFrom(Shop.class, "bar.png");

        Graphics gb;

        /**
         * super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.red);
            //g.fillRect(0, 0, width, 50);
            g.drawImage(img, 0, 0, width, 50, null);
            g.setColor(Color.WHITE);
            g.drawString("SHOP", 15, 30);
            g.drawString("X", width-20, 20);
            for(Shape b : buttons) {
                g2d.draw(b);
            }
            System.out.println("Built");
            gb = g;
         */

        private void render() {
            Graphics2D g = (Graphics2D) graphics.getGraphics();

            /////////////////////
            g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
            /////////////////////
            g.dispose();

            Graphics2D g2d = (Graphics2D) getGraphics();
            g2d.drawImage(img, 0, 0, null);
            g2d.dispose();
        }

        public void Build() {
            Graphics g = gb;
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.red);
            //g.fillRect(0, 0, width, 50);
            g.drawImage(img, 0, 0, width, 50, null);
            g.setColor(Color.WHITE);
            g.drawString("SHOP", 15, 30);
            g.drawString("X", width-20, 20);
            for(Shape b : buttons) {
                g2d.draw(b);
            }
            System.out.println("Built");
        }

    }

    public void render(Graphics2D g) {
            drawPane.render();
    }

    public void addDrawPane() {
        drawPane = new DrawPane();
        w.add(drawPane);
    }
}

如果您需要访问更多代码,请问我。

1 个答案:

答案 0 :(得分:1)

您应该覆盖paintComponent方法,如下所示:

public class DrawPane extends JPanel {

  // all your variables and other things

  @Override
  paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    // Your code goes here, use the g2d

  }

}

然后如果你需要重新绘制你的组件,只需在它上面调用repaint()。