Graphics2D.setComposite空指针异常

时间:2014-09-28 09:48:15

标签: java swing nullpointerexception jpanel paint

设置alphacomposite时我有一个空指针异常。 这是我的面板代码:

package org.speedygoverment;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Panel extends JPanel {

    private static final long serialVersionUID = 1L;
    public ImageIcon menulogoimage = new ImageIcon(
            Game.class.getResource("/res/logo.png"));
    public int menulogoy = 0 - (menulogoimage.getIconHeight() * 2);
    private Game game;
    public int startbtnx = -100;
    public int startbtny = getHeight() / 2 - 36;

    public Panel(Game game) {
        this.game = game;
    }

    public AlphaComposite createComposite(float f1, float f2) {
        float percentage = (f1 * 100f) / f2;
        return AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                percentage / 100f);
    }

    @Override
    public void paint(Graphics gg) {
        super.paint(gg);
        Graphics2D g = (Graphics2D) gg;
        if (game.state == State.MENU) {
            Image img = new ImageIcon(Game.class.getResource("/res/city.png"))
                    .getImage();
            g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
            g.drawImage(menulogoimage.getImage(), 32, menulogoy, null);
            g.setFont(new Font(game.font.getFontName(), Font.PLAIN, getWidth() / 128));
            g.setColor(Color.white);
            g.drawString(
                    "Made by BadBoy6767, lvivtotoro, Midnightas, Darkastronaut",
                    32, getHeight() - 48);
        } else if (game.state == State.GAME) {
            Composite original = g.getComposite();
            Image imgoff = new ImageIcon(
                    Game.class.getResource("/res/city_off.png")).getImage();
            Image img = new ImageIcon(Game.class.getResource("/res/city.png"))
                    .getImage();
            g.drawImage(imgoff, 0, 0, getWidth(), getHeight(), null);
            g.setComposite(createComposite(game.powerhandler.power, 100));
            g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
            g.setComposite(original);
        }
    }

}

空指针异常位于第55行:

g.setComposite(createComposite(game.powerhandler.power, 100));

我不知道空指针究竟发生在哪里,它只是在第55行然后是一堆java类。这是例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.speedygoverment.Panel.paint(Panel.java:55)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我认为我不需要显示所有我的课程,这是我遇到的唯一问题。 我也尝试使用AlphaComposite制作透明图像,但那是不起作用的。

2 个答案:

答案 0 :(得分:0)

你会检查game.powerhandler是否为空?

答案 1 :(得分:-1)

在第55行的org.speedygoverment.Panel类中有一个例外,它有一个空指针引用。我怀疑game.powerhandler为空。