go to previous page   go to home page   go to next page

Answer:

Yes.


Graphics

The constructor

public DemoPanel()
{
   setPreferredSize( new Dimension(400, 300) );
   setBackground( Color.WHITE );    
}  

First sets preferred size of the panel to 400 by 300 and then sets the background to white. This is our white sheet of paper which we will draw on.

The setPreferredSize() method is called with a Dimension object. The parameters to its constructor are width then height. The preferred size of the JPanel is the size it will start out at. When the user changes the size of the frame, the size of the panel will change to fit the new size.

HINT: In graphics, dimensions are always X then Y (or WIDTH then HEIGHT). Casual speech reverses the order sometimes.

The drawing area is represented by a Graphics object. A Graphics object has methods for doing graphical things. It is somewhat like the paint application of Windows operating systems. It has methods to draw lines, draw circles, write text, and so on.

The paintComponent(Graphics g) method is called when it needs to be drawn. The Graphics object contains methods to draw on that part of the screen. The Graphics parameter is supplied to our paintComponent() method by the system when the system needs to redraw the frame and its contents.

The program does not construct a Graphics object. A Graphics object is constructed by the run-time system Once main() sets things up, the system keeps refreshing the JFrame and its contents by calling paintComponent() with a Graphics object as needed. This is a different style of program behavior than our previous programs.


QUESTION 11:

If the program stays active, even after main() has finished, how can the program be shut down?