This is a way to override the paint method using a factory and an anonymous class. This is opposed to creating a separate, named class that extends JFrame.
import javax.swing.*; import java.awt.*; public class Test { public static void main(String[] args) { new Test().run(); } public void run() { JFrame frame = FrameFactory.getInstance(); frame.setBounds(50, 50, 300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class FrameFactory { private FrameFactory () {} public static JFrame getInstance() { return new JFrame() { public void paint(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; Dimension size = getSize(); g.clearRect(0, 0, (int)size.getWidth(), (int)size.getHeight()); g.drawLine(0, 0, (int)size.getWidth(), (int)size.getHeight()); } }; } }