I'm pretty new to Java, I have studied a couple of modules on it at University (even though I'm studyin Networking :confused:) and I like what I see, so plan to continue expanding my knowledge on it :cool:.
I am developping a small bat and ball game which initially, I thought would be relatively straight forward... How wrong could I be?
I am having problems repainting the bat.
Initially my ball moves around the screen and my bat is painted. I have attached a mouselistener to my main applet which gets the x co-ordinate that the mouse is at, which is then passed to a constructor inside my bat class.
Although, for some reason my bat remains stationary.
I have added some S.O.P testing and I have found where the issue is, although I can't find a way round it. Any help will be much appreciated :cool:.
S.O.P produces this...
public class Bat
public int x;
public void setx (int x)
this.x = x;
System.out.println("X1 = "+this.x);
public void paint (Graphics h)
System.out.println("X2 = "+x);
h.fillRect(x, 490, 50, 10);
System.out.println("X3 = "+x);
Why is the x value being reset to 0?:confused:
X1 = 224
X2 = 0
Thanks in advance
Weird. Are you sure you're calling paint() after you call setx()?
Without seeing an SSCCE, we're just going to be guessing.
Thank you for your response
Yeah I'm fairly sure because the "X2 = " and "Painted" is produced everytime the mouse is moved
Okay. Like I said, without an SSCCE, we're just guessing.
Originally Posted by ivlatt
That doesn't mean paste your whole program here- it would be best if you could simplify things as much as possible, take out anything that doesn't directly relate to the problem. One class containing one mouseListener and one paintComponent() method, added to a JFrame, should be enough.
Wait.. is "X1= " printed every time the mouse is moved?
Yeah all 3 of the SOP comments were
I finally got it working somehow, even though I'm pretty sure it's the same code just rejigged around.
Too bad I have no hair left now
Thanks alot guys, I will be sticking around here so look forward to speaking to you in future :)