How do I create my own drawing component: Subclass JPanel and override paintComponent()

Import javax.swing.*; import java.awt.*; Public Class MyDrawPanel extends JPanel {// This is a very important method. You should never call it yourself. For the system to call public void paintComponent(Graphics g){// You can think of G as a drawing device, telling it to draw with what Color what shape g.setcolor (color.orange); g.fillRect(20, 50, 100, 100); }}Copy the code

Call:

import javax.swing.*; public class getCP { public static void main(String[] args) { JFrame frame=new JFrame(); MyDrawPanel myDrawPanel=new MyDrawPanel(); frame.getContentPane().add(myDrawPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Frame. The setSize (300300); frame.setVisible(true); }}Copy the code

Effect:

Example 1: Display images in paintComponent()

@Override protected void paintComponent(Graphics g) { Image image=new ImageIcon("src/images/cat.jpg").getImage(); G.d rawImage (image, 3, 4, this); }Copy the code

Example 2: Paint random colored circles in paintComponent()

Public class RandomCircle extends JPanel {@override protected void paintComponent(Graphics g) {// Fill with the default color // The first two parameters are the coordinates of the starting point, and the second two parameters are the width and height respectively. // get its own width and height here, so it will fill the panel with g.fillrect (0,0, this.getwidth (), this.getheight ()); int red=(int)(Math.random()*255); int green=(int)(Math.random()*255); int blue=(int)(Math.random()*255); // Pass in 3 parameters to represent the RGB value Color randomColor=new Color(red,green,blue); g.setColor(randomColor); // fill the circular region specified by the parameter g.filval (70,70,100,100); }}Copy the code

Effect:

Example 3: Paint a gradient colored circle in paintComponent()

Public class GradientColor extends JPanel {@override protected void paintComponent(Graphics g) {// It's actually a Graphics2D object Graphics2D graphics2D=(Graphics2D)g; / / starting point the Color of the finish layer of the final Color gradually GradientPaint GradientPaint = new GradientPaint (70; seven Color. Blue, 150150, Color, orange). // Change the virtual brush to gradiics2d.setPaint (gradientPaint); // fill the circle graphics2d.filloval (70,70,100,100) with the current brush Settings; }}Copy the code

Effect:

Example 4: Paint random colored circles in paintComponent()

public class GradientColor extends JPanel { @Override protected void paintComponent(Graphics g) { Graphics2D graphics2D=(Graphics2D)g; int red=(int)(Math.random()*255); int green=(int)(Math.random()*255); int blue=(int)(Math.random()*255); Color startColor=new Color(red,green,blue); red=(int)(Math.random()*255); green=(int)(Math.random()*255); blue=(int)(Math.random()*255); Color endColor=new Color(red,green,blue); GradientPaint GradientPaint = new GradientPaint (70, startColor, 150150, endColor); graphics2D.setPaint(gradientPaint); Graphics2D. FillOval,70,100,100 (70); }}Copy the code