package Lect40CustomWidget; import java.awt.*; import java.awt.geom.*; import javax.swing.*; // This is about as simple a custom widget can be // It just paints a red square at a fixed place within the widget public class RedSquareWidget extends JComponent { public void paintComponent(Graphics g) { // We are promised we really get a Graphics2D, a subclass of Graphics. The API // uses the older Graphics object for backwards compatibility. Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.WHITE); g2.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight())); g2.setColor(Color.RED); g2.fill(new Rectangle2D.Double(50, 50, 50, 50)); } }