Java Reference
In-Depth Information
/** A Member Class that contains an off-screen Image that is
* drawn into; this component's paint() copies from there to
* the screen. This avoids having to keep a list of all the
* things that have been drawn.
*/
class
class
PCanvas
PCanvas
extends
extends
Canvas
{
private
private static
static final
final
long
long
serialVersionUID
=
6827371843858633606L
;
Image offScreenImage
;
int
int
width
;
int
int
height
;
Graphics pg
;
PCanvas
(
int
int
w
,
int
int
h
) {
width
=
w
;
height
=
h
;
setBackground
(
Color
.
white
);
setForeground
(
Color
.
red
);
}
public
public
Graphics
getOsGraphics
() {
return
return
pg
;
}
/** This is called by AWT after the native window peer is created,
* and before paint() is called for the first time, so
* is a good time to create images and the like.
*/
public
public
void
void
addNotify
() {
super
super
.
addNotify
();
offScreenImage
=
createImage
(
width
,
height
);
// assert (offScreenImage != null);
pg
=
offScreenImage
.
getGraphics
();
}
public
public
void
void
paint
(
Graphics pg
) {
pg
.
drawImage
(
offScreenImage
,
0
,
0
,
null
null
);
}
public
public
Dimension
getPreferredSize
() {
return
return new
new
Dimension
(
width
,
height
);
}
}
}