簡體   English   中英

getGraphics() 上的空指針異常

[英]Null Pointer Exception on getGraphics()

我的應用程序看起來像這樣,我在 draw() 方法中遇到空指針異常,確切地說是在 g.drawImage(img, 0, 0, null)

package com.ochs.game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable{
private static final long serialVersionUID = 8229934361462702491L;

public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

public boolean isRunning;

private BufferedImage img;
private Graphics2D g2d;

public Game() {
    setFocusable(true);
    requestFocus();
    start();
}

public void start() {
    isRunning = true;
    new Thread(this).start();
}

public void stop() {
    isRunning = false;
}

public void run() {
    long start;
    init();
    while(isRunning) {
        start = System.currentTimeMillis();

        update();
        render();
        draw();

        try {
            Thread.sleep(5 - (System.currentTimeMillis() - start));
        } catch (Exception e) {
        }
    }
}

public void init() {
    img = new BufferedImage(WIDTH*SCALE, HEIGHT*SCALE, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) img.getGraphics();
}

public void update() {

}

public void render() {

}

public void draw() {
    Graphics g = getGraphics();
    g.drawImage(img, 0, 0, null);    // <<<<< getting null pointer here!
}

public static void main(String[] args) {
    Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
    Game gameComponent = new Game();
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(size);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(gameComponent);
}
}

現在我的問題是:為什么在嘗試繪制名為 img 的緩沖圖像時會出現空指針異常? 我也嘗試過使用 drawString() 輸出一些字符串,但這也給自己一個空指針異常。 有沒有人有建議?

您可能會在呈現 JPanel 之前嘗試通過getGraphics()獲取 Graphics 上下文,因此該方法返回 null。 不要這樣做。 在組件上使用getGraphics()獲取 Graphics 上下文存在問題,其中一個是您在上面看到的問題,另一個是獲取的 Graphics 上下文不會持久化。 有時需要這樣做,但通常我們通過paintComponent(...)進行被動繪制。 通常,Swing Timer 可用於動畫循環。

我認為這是因為您試圖使用 getGraphics() 而不是傳統的paintComponent覆蓋來繪制。 你想使用這樣的東西: drawImage 不是在繪圖(請參閱頂部答案)。

如果在該語句之前未呈現 Component,則 getGraphics() 方法將返回 null,因此您將獲得 NullPointerException,如果呈現圖形,則圖形將不穩定,最好使用 PaintComponents...

另請參閱:調用返回 null 的 getGraphics() 的任何替代方法

import java.awt.*;
import java.awt.event.*;

class myframe extends Panel
{
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(10,12,300,150);
    }
    
    public static void main(String args[])
    {
        Frame f=new Frame();
        f.add(new myframe());
        
        
        f.setSize(400,400);
        f.setVisible(true);
        
    }
}

組件必須首先可見,然后在游戲/面板中啟動線程之前嘗試此操作

frame.add(panel) frame.setVisible(true)

然后在游戲/面板中啟動線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM