Claudiu C
Claudiu C

Reputation: 105

Using Graphics outside the paintComponent

Let's say we have the following situation:

JPanel panelp=new JPanel();
paintSomething(panelp.getGraphics();

and somewhere else in a different object, the method:

void paintSomething(Graphics g){ /*code*/ }

I don't want to override paintComponent method of panelp. How can I paint something to panelp from the method paintSomething using the Graphics of panelp?

Upvotes: 0

Views: 2035

Answers (2)

mKorbel
mKorbel

Reputation: 109813

whatever.getGraphics() is snapshot is the snapshot that will go away when

  • after first repaint

  • JComponets are repainted internally from Mouse or Key Events, these events are implemented in the concrete JComponets API

  • simple example for usage of whatever.getGraphics() is printing to the printer or saving current GUI as printscreen to the e.g. JPEG or PGN File

basic stuff is described in the 2D Graphics

Upvotes: 4

GETah
GETah

Reputation: 21419

You could draw your stuff in the paintSomething into a BufferedImage which you can then draw to the panel by overriding paintComponent

Upvotes: 3

Related Questions