Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Using java.awt.Graphics outside of paintComponent(){}

$
0
0
I am trying to use the graphics module outside of paintComponent to draw things under certain conditions, but what keeps happening is a NullPointerException.
Here is my TitleState code (It is a State, which is a JPanel, so paint component can be used with it)
import java.awt.*;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
public class TitleState extends StateMachine{
	thingy thing;
	public TitleState(thingy t){
		thing = t;
		Listener inp = new Listener();
		thing.addMouseListener(inp);
	}

	public void render(Graphics g) {
		g.drawString("Hello", 50, 25);
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		this.setBackground(Color.green);	
	}
	public void update() {
		this.repaint();
		render(thing.g);
	}

	public void handleInput(MouseEvent ev) {
	}
}




I would like to know whether or not it is allowed to use "Graphics g" outside of paintComponent(){}

-Thanks

I figured out that I could send "Graphics g" from paintComponent, which is useful, but still not what I am aiming for.
So this if this is the only solution, tell me.

Viewing all articles
Browse latest Browse all 51036

Trending Articles