0

I need to pass the grapics object 'g' to the action method from the paint method. Something like this:

 public boolean action(Event event, Object obj)
 {   

   Graphics g=getGraphics();
   repaint();

   if (event.target == choice) 
   String selection = choice.getSelectedItem();
   if (selection.equals("do something"))
   {
       doSomething(g);
       repaint();
   }
   else if (selection.equals("do something else"))
   {
       Somethingelse(g);
       repaint();
   }

   return(true);
}

      else
          return(false);
   }

I tried to declare g as a global graphics variable but it doesnt work. Is there another way to do this. Any help is appreciated .. Thanks..

Priya
  • 29
  • 4
Isabel Inc
  • 1,871
  • 2
  • 21
  • 28

1 Answers1

0

I assume this is java and you are overriding a Swing component (so getGraphics() is actually available).

Try Graphics g = this.createGraphics(), that might help. Also, be sure to call the repaint() methods from the Event Dispatch Thread. If this action is linked to the UI, that is generally the case. To be sure, wrap the repaints in a Runnable and send it to the end of the EDT:

SwingUtilities.invokeLater(new Runnable() { 
    public void run() {
        repaint();
    }
}
Mike Adler
  • 1,199
  • 9
  • 24