import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class JPanelDrawLinesShapesText extends JFrame { JFrame frame = new JFrame(); public static void main(String[] args) { new JPanelDrawLinesShapesText(); } public JPanelDrawLinesShapesText() { // Set up the JFrame frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Demo: Drawing On JPanel"); // Create a JPanel and a method for "painting" it JPanel panel = new JPanel() { @Override public void paint(Graphics g) { // Call the "paint" method in the parent class super.paint(g); // Call a method (below) to do the drawing DrawStuff(g); } }; // Set the background color of the JPanel panel.setBackground(Color.WHITE); // Add the JPanel to the JFrame frame.add(panel); frame.setVisible(true); } public void DrawStuff(Graphics g) { // Draw a blue line g.setColor(Color.BLUE); g.drawLine(45, 25, 445, 25); // Draw a red rectangle g.setColor(Color.RED); g.drawRect(45, 50, 400, 285); // Draw some green text g.setColor(Color.GREEN); g.setFont(new Font("Arial", Font.BOLD, 30)); g.drawString("Hello, World!", 155, 120); // Draw a filled-in magenta circle g.setColor(Color.MAGENTA); g.fillOval(200, 180, 100, 100); } }