Link to home
Start Free TrialLog in
Avatar of hello1111
hello1111

asked on

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes. You can pass a Graphics reference around just like any other.
Avatar of hello1111
hello1111

ASKER

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?

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.
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?
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);

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.
True, it was only to examplify the involved classes. Thanks for clarifying CEHJ!
//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
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!




}


ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!!
lol, glad to hear you got it worked out hello1111 =D  Cheers!