Java Reference
In-Depth Information
The Graphics2D Class
Everything in Java2D begins with the Graphics2D class in the java.awt package, which
represents a graphics context , an environment in which something can be drawn. A
Graphics2D object can represent a component on a graphical user interface, printer, or
another display device.
Graphics2D is a subclass of the Graphics class that includes extended features required
by Java2D.
NOTE
Early versions of Java included rudimentary support for graphics in
the Graphics class. These methods have been supplanted by more
sophisticated and efficient alternatives in Java2D.
Before you can start using the Graphics2D class, you need something on which to draw.
Several user interface components can act as a canvas for graphical operations such as
panels and windows.
After you have an interface component to use as a canvas, you can draw text, lines, ovals,
circles, arcs, rectangles, and other polygons on that object.
One component that's suitable for this purpose is JPanel in the javax.swing package.
This class represents panels in a graphical user interface that can be empty or contain
other components.
The following example creates a frame and a panel and then adds the panel to the frame:
JFrame main = new JFrame(“Main Menu”);
JPanel pane = new JPanel();
Container content = main.getContentPane();
content.add(pane);
The frame's getContentPane() method returns a Container object representing the por-
tion of the frame that can contain other components. The container's add() method is
called to add the panel to the frame.
Like many other user interface components in Java, JPanel objects have a
paintComponent( Graphics ) method that is called automatically whenever the compo-
nent needs to be redisplayed.
 
Search WWH ::




Custom Search