First Drawing Application

Our first drawing application simply draws two lines. Class DrawPanel (Fig. 4.18) performs the actual drawing, while class DrawPanelTest (Fig. 4.19) creates a window to display the drawing. In class DrawPanel, the import statements in lines 3–4 allow us to use class Graphics (from package java.awt), which provides various methods for drawing text and shapes onto the screen, and class JPanel (from package javax.swing), which provides an area on which we can draw.

 1   // Fig. 4.18: DrawPanel.java 2   // Using drawLine to connect the corners of a panel. 3   import java.awt.Graphics;  4   import javax.swing.JPanel; 5  6   public class DrawPanel extends JPanel 7   { 8      // draws an X from ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.