CS 235 - Week 9 Lab - 2015-10-21
* Graphics and Swing
* FIRST: it is preferred that you draw/paint on
a JPanel (rather than, say, a JFrame or
other container)
* SECOND: to draw/paint on a JPanel,
you override its inherited
paintComponent method in your JPanel subclass
public void paintComponent(Graphics g)
{
// parent JPanel's paintComponent knows to fill
// a JPanel with its background color --
// here we are calling parent JPanel's version,
// then, to do this for us...
super.paintComponent(g);
...// this JPanel subclass' addition painting/drawing
}
* note: you do not directly call
paintComponent -- it is called
AUTOMATICALLY whenever a part of your
application needs to be redrawn
(because a window is resized or
moved, because something happens, etc.)
...and you SHOULD NOT interfere with
this automatic process;
* THIRD: if you want say, essentially,
PLEASE repaint/redraw my application,
how then can you do it?
call:
repaint();
...this causes paintComponent to be called
for all components, each with a properly-
configured Graphics object;
* note that 0,0 is the top-left of your JPanel subclass instance,
and drawing/painting coordinates are given in
pixels (picture elements, one dot on the screen)
* a few quick'n'sleazy Graphics methods:
* setFont - sets the font for subsequent
painted strings
* setColor - sets the color for subsequent things drawn
* drawString(String stringToDraw,
int pixelLocXOfBottomLeft,
int pixelLocYOfBottomLeft)