Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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.
0
hello1111
Asked:
hello1111
  • 4
  • 4
  • 2
  • +1
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now