Using two classes, you can create a graphical window and draw shapes and/or text in it.
The two classes are:
- JFrame
- JComponent
The JFrame class creates a window that 'frames' other items.
You can't draw or place text directly on a JFrame instance.
A JComponent is an item that can be added to a JFrame; i.e., inside the 'frame'.
A simple graphical program
- creates a JFrame specifying its size (width and height in pixels) and its location on the screen.
- make sure the program will terminate if the user closes the window.
- Create an element that is of a JComponent type and add it to the JFrame.
- make the JFrame visible.
But, there are some additional things going on.
- When the JFrame is first made visible it will automatically draw all its components (if any)
- If the JFrame is minimized and then restored or if part becomes obscured and then uncovered again, or if the user resizes the window, the JFrame will redraw its components.
- But... the JFrame redraws its components by having each component do the redrawing.
- The element of JComponent type should have a method
public void paintComponent(Graphics g)
that draws the element in the JComponent using the graphics resource g.