Java AWT Graphics fill rectangle

Introduction

To fill a rectangle using Java AWT Graphics.

g.fillRect(10, 10, 60, 50);

Full source


// Demonstrate the key event handlers.
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

class SimpleKey extends JLabel {

  public void paint(Graphics g) {  
    // Draw lines.  
    g.fillRect(10, 10, 60, 50);//from  w w  w  . ja  v  a 2 s.  co  m
  }
}

public class Main {
  public static void main(String args[]) {
    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // Create a new JFrame container.
        JFrame jfrm = new JFrame("java2s.com");

        // Give the frame an initial size.
        jfrm.setSize(220, 200);

        // Terminate the program when the user closes the application.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Add the label to the content pane.
        jfrm.add(new SimpleKey());

        // Display the frame.
        jfrm.setVisible(true);

      }
    });
  }
}



PreviousNext

Related