I'm having a null pointer exception when setting an alphacomposite. This is my code for my panel:
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);
}
}
}
The null pointer exception is at line 55:
g.setComposite(createComposite(game.powerhandler.power, 100));
I do not know where the null pointer is exactly happening, it only says its on line 55 and then a bunch of java classes. This is the exception:
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)
I do not think that I need to show all of my classes, this is the only one I have problems with. Also I'm trying to make a transparent image using AlphaComposite, but thats the thing that doesn't work.