?
Solved

Graphics g

Posted on 2003-03-03
12
Medium Priority
?
218 Views
Last Modified: 2010-03-31
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
Comment
Question by:hello1111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058092
Yes. You can pass a Graphics reference around just like any other.
0
 

Author Comment

by:hello1111
ID: 8058183
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058250
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:hello1111
ID: 8058299
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8058863
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058910
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8059063
True, it was only to examplify the involved classes. Thanks for clarifying CEHJ!
0
 

Expert Comment

by:jaquo
ID: 8059199
//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
 

Author Comment

by:hello1111
ID: 8063150
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 8063351
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
 

Author Comment

by:hello1111
ID: 8073525
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
 

Expert Comment

by:jaquo
ID: 8078098
lol, glad to hear you got it worked out hello1111 =D  Cheers!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question