Drawing Graphics - Using paint() on Frame or Component

This section provides a tutorial example on how to override the paint() method in the javax.awt.Frame class to draw graphics (a rectangle) on the frame window, or override the paint() method in the java.awt.Component class to draw graphics and add the component to the frame window.

Problem: I want to draw some graphics in a frame.

Solution 1: You can do this very easily by extending Frame class to create your own frame class so that you can override the paint() method. The paint() method provides you a Graphics object, which will give you utility methods to draw various types of graphics. The following sample code, FramePaint1.java, shows you how to do this.

/* FramePaint1.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.Frame;
import java.awt.Graphics;
public class FramePaint1 {
   public static void main(String[] a) {
      MyFrame f = new MyFrame();
      f.setTitle("Drawing Graphics in a Frame Directly");
      f.setBounds(100,50,500,300);
      f.setVisible(true);
   }
   static class MyFrame extends Frame {
      public void paint(Graphics g) {
         g.drawRect(20,10,100,60);
      }
   }
}

If you run this example, you will get:

Using paint() on Frame
Using paint() on Frame

Note 1: The paint() method is inherited by Frame class from the Component class. It will be called whenever this component should be painted.

Note 2: If you look at the rectangle displayed on the frame, you will see that the origin of the drawing coordinates is located at the top left corner of the entire frame, including the title bar.

Note 3: By default, the paint() method provides transparent background, in JDK 1.4, 1.5 and 1.6. This is why you see a Web page showing up in the picture.

Note 4: In JDK 7 and higher, the paint() method does not provides transparent background.

This solution is not recommended. We don't want to draw graphics in the frame bar area.

Solution 2: Obviously, solution 1 is not so ideal, because we are drawing on the UI element of the frame itself. To draw graphics only in the content area of the frame, we can create a new component with its own paint() method and add it to the frame. The following sample code, FramePaint2.java, shows you how to do this.

/* FramePaint2.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Component;
public class FramePaint2 {
   public static void main(String[] a) {
      Frame f = new Frame();
      f.setTitle("Drawing Graphics in a Frame"
         +" by Adding a Component");
      f.setBounds(100,50,500,300);
      f.add(new MyComponent());
      f.setVisible(true);
   }
   static class MyComponent extends Component {
      public void paint(Graphics g) {
         g.drawRect(20,10,100,60);
      }
   }
}

If you run this example, you will get:

Using paint() on Component
Using paint() on Component

Note 1: Since no size and location is given to the new added component, it takes the entire area of the content pane, which is the entire area of the frame without the title bar area.

Note 2: Now the origin of the drawing coordinates is at the top left corner of the content pane, much better than solution 1.

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

AWT (Abstract Windows Toolkit)

 What Is AWT (Abstract Windows Toolkit)

 HelloAWT.java - My First AWT Program

 Closing AWT Frame and Terminating Application

Drawing Graphics - Using paint() on Frame or Component

 Creating Labels with java.awt.Label Class

 Creating Buttons with java.awt.Button Class

 AWT Button Action Handler at the Component Level

 AWT Button Action Handler at the Frame Level

 AWT Button Mouse Click Handler at the Frame Level

 AWT TextField and ActionListener

 MenuBarTest.java - AWT Menu Bar Test Program

 MenuTest.java - AWT Menu Test Program

 MenuItemTest.java - AWT Menu Item Test Program

 MenuItemActionListenerTest.java - AWT Menu Item Action Listener

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB