Click on the close button. That is what setDefaultCloseOperation()
is about.
Our paintComponent()
overrides the method of the same name in the super class of JPanel
.
Call the super class method super.paintComponent( gr )
before doing custom painting.
If you forget to do this the background color will be the default color.
class DemoPanel extends JPanel
{
. . .
public void paintComponent ( Graphics gr )
{
int width = getWidth();
int height = getHeight();
super.paintComponent( gr );
gr.setColor( Color.BLUE );
gr.drawLine( 0, 0, width-1, height-1 );
}
}
To draw a diagonal line, the method needs the width and height of the panel. This does not include the width and height of the borders and menu bar of the frame.
getWidth() — the current width in pixels of this component getHeight() — the current height in pixels of this component
The method then sets the pen color to BLUE and draws a diagonal line.
The setColor()
method of a Graphics
object changes the color that is used in drawing.
This color will be used for all drawing until is it changed again.
(Review: ) Which pixel is the one at coordinates (0,0)
?