Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JAVA 2D, Graphics Object

  Asked By: Geeske    Date: Feb 15    Category: Java    Views: 905
  

I am trying to implement a swing program, with JAVA 2D elements. As the method
void paint (Graphics g) is always executed automatically when the frame is
shown, I decided to call it something else ( say, void drawgraphic (Graphics g).
I don't want that it will be executed automatically. I connected it with an
action listener..

My problem, is how should I give the arguments Graphic g to this manually
implemented method. Should I instantiate any object of typ graphics and then
give it as arguments . And I don't how to do that..

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Freddie Evans     Answered On: Feb 15

use the repaint() method............

 
Answer #2    Answered By: Jonah Brown     Answered On: Feb 15

I'm kind of curious as to why you don't want to use the paint method. If your
intention is to only paint a certain object  at a certain time, then that
component should only be added to your pane when necessary. If it's not a
component, then why can't you have some kind of flag inside your class that
indicates when it should be drawn and when it should not be.

Creating a new graphics  object will not accomplish anything. You'll be drawing
graphics to nowhere. One thing I've done in the past is create a new
BufferedImage and got a Graphics object that references that. Using that I had
a double buffer that I could then flip to the displayed pane. But that's only
because I needed double buffering. Anyways, if I were you, I'd have the paint
method call  the drawgraphic method  when a flag is set and pass the Graphics
object.

 
Answer #3    Answered By: Boyce Fischer     Answered On: Feb 15

Thank you all for your help. I think, I'll try to do as you said. I will use the
paint() method. My Problem was, that I didn't want that this will be executed
automatically. As some of the frriends already said, I will but a boolean value,
that will manage this.

 
Answer #4    Answered By: Stacy Cunningham     Answered On: Feb 15

U can get the graphics  handler using the getGraphics factory method
and u can use it anywhere in ur application

Graphics g=getGraphics();

U can use this handler anywhere within ur application.preferred that scope of
g should be there.

 
Answer #5    Answered By: Jimmie Ramirez     Answered On: Feb 15

Like some other people have said, I'd still try to do this in your
paint(Graphics g) method. paint automatically  gets called when the
window gets resized or programs are switched between. If you really
don't want to do it in the paint method, then in your drawGraphic()
method don't use any arguments, but instead make the first line

Graphics g=getGraphics();

This will make it so g draws to the right place. I hope this helps.

 
Didn't find what you were looking for? Find more on JAVA 2D, Graphics Object Or get search suggestion and latest updates.




Tagged: