Graphics g

Is it possible to declare Graphics g in a method that is not paint or draw???

I have a method and want to use another draw method which needs to take in Graphics g.   But I dont want to have to go through the draw or paint methods.
hello1111Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Yes. You can pass a Graphics reference around just like any other.
0
hello1111Author Commented:
So with a method such as this:

public void static reDrawArrows(Graphics g){
        g.setColor(Color.yellow);
        for (int i = 0; i < xy.length;){
            drawArrows(g, xy[i*2][0], xy[i*2][1], xy[i*2+1][0], xy[i*2+1][1], index);
        }
    }


I thought I could call it with something like this :

public static Graphics qwerty;

Road.reDrawArrows(qwerty);

but it is giving me all sorts of errors.   What am I doing wrong?

0
CEHJCommented:
Because it won't be a valid graphics context if you are calling it like this. You have to use a valid reference to a Graphics object. If you have a JPanel that has been properly initialized, for instance, you can call getGraphics on it.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

hello1111Author Commented:
Sorry I dont understand what u mean.....

in my draw(Graphics g) method I can call drawArrow with for example:

drawArrows(g, int, int, int, int, int);

why cant I do it that way in another method, or is that what you are talking about?
0
Tommy BraasCommented:
What CEHJ is saying is the following:

You can retrieve a Graphics object from any descendent of java.awt.Component. It depends on which component you wish to draw the arrows.

JPanel aPanel = new JPanel();         // a descendent of Component, has a Graphics context associated with it
Graphics g = aPanel.getGraphics();    // get the graphics context associated with aPanel

You can now use 'g' to draw your arrows, the arrows will draw on the 'aPanel' instance:

Road.reDrawArrows(g);

0
CEHJCommented:
That's right, although you should make sure that the context is valid. In orangehead's example, i think the context may not have been fully initialized as the panel has not yet painted itself.
0
Tommy BraasCommented:
True, it was only to examplify the involved classes. Thanks for clarifying CEHJ!
0
jaquoCommented:
//Is this sort of what you mean?
import java.awt.*;
import java.applet.*;
public class SomeApplet extends Applet {
     public void paint(Graphics graphics) {
          graphics.drawString("This is a sample statement.", 10, 50);
          this.someOtherMethod(graphics);
     }
     public void someOtherMethod(Graphics graphics) {
          graphics.drawString("this is another sample statement.", 10, 60);
     }
}
//just pass the graphics arguement to different methods that you want to be able to handle graphics
//I think this is what you were asking?  Sorry if this isn't what you meant.
//jaquo
0
hello1111Author Commented:
Thanks jaquo, but that's not exacly what I meant....this would be more what I meant =>

import java.awt.*;
import java.applet.*;
public class SomeApplet extends Applet {
    public void paint(Graphics graphics) {
         graphics.drawString("This is a sample statement.", 10, 50);
         <somevariable>.draw(graphics);
    }
    public draw{
         someOtherMethod(graphics, int, int)
    }
    public void someOtherMethod(Graphics graphics, int, int) {
         graphics.drawString("this is another sample statement.", 10, 60);
    }

    // this is the bit I am not sure about
    public void anotherMethod(Graphics g){
         someOtherMethod(graphics, int, int)
    }

I want to be able to call anotherMethod from anywhere in the program.   (anotherMethod will also have different code in it than draw, which is why I am not calling draw again).   I am not sure how to do this, though it would be as simple as just calling anotherMethod(graphics g), but apparently it is not, as I want to call it from a mousePressed method so cant pass in Graphics g into the default mousePressed method.   I hope I explained that clearly!
Also even when I just include the anotherMethod, without calling it from anywhere I am getting errors ("identifier expected", and "cannot resolve symbol"...which actually refers to the line in the draw method.
I cant understand why all that is happening!




}


0
CEHJCommented:
hello1111, there is no real mystery here. Just think of the reference to the Graphics objects as just like any other object reference. If you want to use the reference inside a function, you have several options:

a. Pass it into the function as a parameter

or, when you can't, as in your mousePressed scenario

b. Use a reference saved previously elsewhere in the program

or if you're in the right scope, call something like

c. Graphics g = this.getGraphics();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hello1111Author Commented:
Thanks for all your help.
Figured out where my problem was...had void static in my method instead of static void!! D'oh.   Couldn't understand why it wasn't working!!!!
0
jaquoCommented:
lol, glad to hear you got it worked out hello1111 =D  Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.