Why doesn't Java use Graphics to draw pictures?

the Graphics below has content, but why can"t I draw a graph?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawPanel extends JFrame{
    public static void main(String[] args) {
        new DrawPanel();
    }

    public DrawPanel() {
        setTitle("");
        setSize(600,600);
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        createDP();
    }
    
    private JPanel panel;
    private Point prevPoint;
    private Color color;
    private int Thickness=10;
    private void createDP(){
        JPanel tools=new JPanel();
        Container cont=getContentPane();
        cont.setLayout(null);
        panel=new JPanel();
        panel.setBackground(Color.BLUE);
        panel.setBounds(10, 100, 400, 400);
        //
        final Graphics2D g=(Graphics2D)createImage(300,300).getGraphics();
        //
//        BufferedImage buffImg=new BufferedImage(300,300,BufferedImage.TYPE_INT_ARGB);
//        final Graphics2D g=(Graphics2D)buffImg.createGraphics();
        panel.addMouseListener(new MouseAdapter() {
            
            @Override
            public void mousePressed(MouseEvent e) {
                prevPoint=e.getPoint();
                g.setColor(Color.RED);
                g.fillOval(prevPoint.x, prevPoint.y, Thickness, Thickness);
                
            }
        });
        panel.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e){
                
            }
        });
        
        cont.add(tools,BorderLayout.NORTH);
        cont.add(panel);
    }
}
Mar.18,2021

are you sure your code has been compiled? There is obviously something wrong with new DrawPanel () in main. DrawPanel is function, not class.


since you need to draw on panel , you should get a panel brush for each drawing:

panel.addMouseListener(new MouseAdapter() {

    @Override
    public void mousePressed(MouseEvent e) {
        prevPoint = e.getPoint();

        //  panel  Graphics2D 
        Graphics g = panel.getGraphics(); 
        
        g.setColor(Color.RED); 
        g.fillOval(prevPoint.x, prevPoint.y, Thickness, Thickness);
    }
});
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4c83c-44eb5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4c83c-44eb5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?