Drawing an object using getGraphics() without extending JFrame
问题 How can I draw an object without a class (which extends JFrame )? I found getGraphics method but it doesnt draw the object. import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel(); frame.add(panel); Graphics g = panel.getGraphics(); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }