5 Replies - 879 Views - Last Post: 06 August 2012 - 05:12 AM Rate Topic: -----

#1 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Java MouseClicked Image problem

Posted 05 August 2012 - 08:26 AM

Alright,

I am making a strategy game and to move a unit I need to select it and then click somewhere else to move it there.
I want to "spawn" a flag where I move my unit.
Everything runs fine until I click after clicked an unit to move it, no flag appears and I get a nullpointexception error in console. It also points to this part of my code:
	public void mouseClicked(MouseEvent e) 
	{
		addFlag();
	}


addFlag code:

	public void addFlag() {

Graphics g = getGraphics();		
g.drawImage(Ships.flagImage(),mouseX, mouseY, null);
		
	}


There's something Ive missed...

btw, im using the BufferedImage method to load images.

Is This A Good Question/Topic? 0
  • +

Replies To: Java MouseClicked Image problem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,829
  • Joined: 10-September 10

Re: Java MouseClicked Image problem

Posted 05 August 2012 - 08:34 AM

Post the error message/stack trace, copied and pasted from exactly as it appears at your end, and post the code it points to. If you're not sure what code it points to, post the whole class you think it points to. If you're still not sure, we'll tell you what to post after reading the error message and stack trace.
Was This Post Helpful? 0
  • +
  • -

#3 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: Java MouseClicked Image problem

Posted 05 August 2012 - 08:41 AM

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at game.starwars.StarWars.drawFlag(StarWars.java:69)
	at game.starwars.StarWars.mouseClicked(StarWars.java:37)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.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)


points to the MouseClicked thingy I wrote above and
	public void drawFlag() {
		Graphics g = getGraphics();
		g.drawImage(Ships.flagImage(), 1, 1, null);
		
	}

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,829
  • Joined: 10-September 10

Re: Java MouseClicked Image problem

Posted 05 August 2012 - 08:57 AM

So I'm guessing line 69 corresponds to the drawFlag() lines 2 or 3 that you posted. In which case, either getGraphics() or Ships.flagImage() is returning a null. Look at those and see if you can figure out which (or both?) is the guilty party. Once you've figured out which, determine why and fix it.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: Java MouseClicked Image problem

Posted 05 August 2012 - 08:09 PM

never good idea to

Graphics g = getGraphics();

in your mouseClicked(MouseEvent e)

set a flag in your instance variables and call repaint()
in your paint() method act accordingly based to that instance variable set using the Graphics object received as parameter

public void mouseClicked(MouseEvent e) {
   boolean mouseHasBeenClicked = true;
   repaint();
}

public void paint(Graphics g) {
   super.paint(g);
   if(mouseHasBeenClicked) {
      .....
   }


Was This Post Helpful? 1
  • +
  • -

#6 RookieCookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 05-August 12

Re: Java MouseClicked Image problem

Posted 06 August 2012 - 05:12 AM

View Postpbl, on 05 August 2012 - 08:09 PM, said:

never good idea to

Graphics g = getGraphics();

in your mouseClicked(MouseEvent e)

set a flag in your instance variables and call repaint()
in your paint() method act accordingly based to that instance variable set using the Graphics object received as parameter

public void mouseClicked(MouseEvent e) {
   boolean mouseHasBeenClicked = true;
   repaint();
}

public void paint(Graphics g) {
   super.paint(g);
   if(mouseHasBeenClicked) {
      .....
   }


Thanks alot! Well explained +1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1