|
-
Aug 16th, 2005, 02:37 PM
#1
Thread Starter
Lively Member
Question about getGraphics() method.
Hey guys.
this thing is the this will work:
public void mouseReleased(MouseEvent evt) {
Graphics g = getGraphics();
g.drawLine(...);
}
but this wont:
Graphics g = getGraphics();
public void mouseReleased(MouseEvent evt) {
g.drawLine(...);
}
the question is why?
it is the same with the paint methode too, the graphics has to be created inside? why wont the outside work?
thanks.
-
Aug 16th, 2005, 03:14 PM
#2
Member
Re: Question about getGraphics() method.
You could try
public Graphics g = getGraphics();
If that doesn't work, post the class that your mouse listener is in and I'll see if I can figure out what else might be going wrong.
-
Aug 16th, 2005, 03:37 PM
#3
Thread Starter
Lively Member
Re: Question about getGraphics() method.
okey, if i write public inside the mouseReleased method, i get an illigal start of expression, and id i write it outside themethod its the same thing like without public.
here is the class:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Dot extends Applet implements MouseListener {
public void init() {
addMouseListener(this);
setBackground(Color.blue);
}
public void mousePressed(MouseEvent evt) {
Graphics g = getGraphics();
g.fillRect(evt.getX(),evt.getY(), 10, 10);
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
-
Aug 16th, 2005, 03:52 PM
#4
Member
Re: Question about getGraphics() method.
Well, I think I've figured out why it doesn't work when you put the Graphics g = getGraphics(); outside of the mouse listener.
You're implementing MouseListener, which doesn't have a Graphics object created to begin with. That problem is solved when you create an object inside of the mousePressed method, because it's affecting MouseListener. However, when you create the object outside of that method, it's actually creating the object for the Dot class, not MouseListener.
-
Aug 16th, 2005, 03:55 PM
#5
Thread Starter
Lively Member
Re: Question about getGraphics() method.
i see, but how about when i create a new int variable, i will be able to use that in the methodes, ti wont be just for the class?
Last edited by Sebouh; Aug 16th, 2005 at 03:58 PM.
-
Aug 16th, 2005, 04:16 PM
#6
Member
Re: Question about getGraphics() method.
Yeah, you would think they would work the same, but I've noticed that Graphics is real picky about how and where you set up the objects, especially when you start using the getGraphics() method. I do know that if you create a variable outside of any method, but inside the class and put the word 'public' in front of the variable declaration, you should be able to use that variable anywhere within the class. I'm guessing the difference is because Graphics is an object, while int or String or something is a variable, but I'd have to research a little bit more to confirm that.
-
Aug 16th, 2005, 05:01 PM
#7
Frenzied Member
Re: Question about getGraphics() method.
I wouldn't use it like that. Create some flag variables and call a repaint in an if statement or something. Much easier to read, and to understand.
-
Aug 16th, 2005, 05:05 PM
#8
Frenzied Member
Re: Question about getGraphics() method.
By the way:
references to Graphics that are passed to methods, such as paint() and update(), are only valid during the execution of the method that they are passed to. That is, they are a copy of the original Graphics object. Once the method returns, the reference is no longer valid because its caller is responsible for disposing of it.
-
Aug 17th, 2005, 04:03 AM
#9
Thread Starter
Lively Member
Re: Question about getGraphics() method.
well i am just learning and i am experimenting with stuff. the thing is that every time i learn something i ask a million questions on it and try each one to see what happens, which is very annoying.
thanks for the help guys.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|