Graphic Programming
Graphic Programming
PROGRAMMING
Graphic programming
■ This chapter shows you how you can paint your own custom drawing because
you cannot find standard GUI components that meets your requirements.
Java.awt.graphics
■ A graphics context provides the capabilities of drawing on the screen. The graphics
context maintains states such as the color and font used in drawing, as well as
interacting with the underlying operating system to perform the drawing. In Java,
custom painting is done via the java.awt.Graphics class, which manages a graphics
context, and provides a set of device-independent methods for drawing texts, figures
and images on the screen on different platforms.
Graphics Class' Drawing Methods
■ The Graphics class provides methods for drawing three types of graphical objects:
■ Text strings: via the drawString() method. Take note that System.out.println() prints to
the system console, not to the graphics screen.
■ Vector-graphic primitives and shapes: via methods drawXxx() and fillXxx(), where Xxx
could be Line, Rect, Oval, Arc, PolyLine, RoundRect, or 3DRect.
■ Bitmap images: via the drawImage() method.
Draw String
■ // Drawing lines:
drawLine(int x1, int y1, int x2, int y2);
drawPolyline(int[] xPoints, int[] yPoints, int numPoint);
Drawing primitive shape
■ drawImage(Image img, int xTopLeft, int yTopLeft, ImageObserver obs); // draw image
with its size
■ drawImage(Image img, int xTopLeft, int yTopLeft, int width, int height, ImageObserver
o); // resize image on screen