Results 1 to 9 of 9

Thread: Question about getGraphics() method.

  1. #1

    Thread Starter
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    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.

  2. #2
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    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.

  3. #3

    Thread Starter
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    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) { }


    }

  4. #4
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    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.

  5. #5

    Thread Starter
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    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.

  6. #6
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    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.

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  9. #9

    Thread Starter
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    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
  •  



Click Here to Expand Forum to Full Width