Resize drawing to match frame size

Assuming you rename your method that paints for 300x300 as paint300, define a buffered image:

@Override public void paint(Graphics g) {
     Image bufferImage = createImage(300, 300);  // empty image
     paint300(bufferImage.getGraphics());  // fill the image
     g.drawImage(bufferImage, 0, 0, null);  // send the image to graphics device
}

Above is when you want to draw at full size (300x300). If your window is resized:

@Override public void paint(Graphics g) {
     Image bufferImage = createImage(300, 300);  
     paint300(bufferImage.getGraphics());
     int width = getWidth();
     int height = getHeight(); 
     CropImageFilter crop = 
         new CropImageFilter((300 - width)/2, (300 - height)/2 , width, height);
     FilteredImageSource fis = new FilteredImageSource(bufferImage, crop);
     Image croppedImage = createImage(fis);
     g.drawImage(croppedImage, 0, 0, null);
}

The new classes are from from java.awt.image.*.

I didn't test this code. It's just to send you in the right direction.


if you want to painting Custom paint then look for JLabel or JPanel and including Icon/ImageIcon inside, simple example about that

enter image description here enter image description here

enter image description here

import java.awt.*;
import javax.swing.*;

public class MainComponentPaint extends JFrame {

    private static final long serialVersionUID = 1L;

    public MainComponentPaint() {
        setTitle("Customize Preffered Size Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponent());
        pack();
        setMinimumSize(getSize());
        setPreferredSize(getSize());
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            setVisible(true);
        }
    });
    }

    public static void main(String[] args) {
        MainComponentPaint main = new MainComponentPaint();
        main.display();
    }
}

class CustomComponent extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        for (int i = 0; i < Math.max(w, h); i += 20) {
            g.drawLine(i, 0, i, h);
            g.drawLine(0, i, w, i);
        }
    }
}