title: Java - How to resize Image to fit window size?
body: <p>I am making a simple program similar to MS Paint. I create a custom Image which is blank as my 'canvas'. However, whenever I enlarge the window, the Image stays the same size. How can I make it so when I resize the window, the image fits?</p>
  
  <p>My code is:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.Cursor;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Image;
  import java.awt.Point;
  import java.awt.RenderingHints;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionAdapter;
  import java.awt.image.BufferedImage;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class ScribblePad extends JFrame {
  
      // Setting GUI variables...
  
      public static int WIDTH = 800;
      public static int HEIGHT = 800;
  
      public static int X = 10;
      public static int Y = 10;
  
      public static String TITLE = "ScribblePad";
  
      // Create GUI components...
  
      PaintPanel paintPanel = new PaintPanel();
  
      JPanel buttonPanel = new JPanel();
  
      Container mainContainer = getContentPane();
  
      JButton clearButton = new JButton("Clear");
  
      JButton buttonBlack = new JButton();
      JButton buttonRed = new JButton();
      JButton buttonBlue = new JButton();
      JButton buttonYellow = new JButton();
      JButton buttonGreen = new JButton();
  
      Cursor crosshairCursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
  
      // Run the entire program...
  
      public static void main(String []args) {
  
          new ScribblePad();
  
      }
  
      // Create ScribblePad constructor...
  
      public ScribblePad() {
  
          // Setting JFrame properties...
  
          super(TITLE);
  
          setVisible(true);
  
          setSize(WIDTH, HEIGHT);
          setLocation(X, Y);      
  
          setDefaultCloseOperation(EXIT_ON_CLOSE);
  
          // Setting properties of other components...
  
          paintPanel.setCursor(crosshairCursor);
  
          buttonPanel.setBackground(Color.WHITE);
  
          buttonPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
  
          // Adding the components...
  
          mainContainer.add(paintPanel, BorderLayout.CENTER);
  
          mainContainer.add(buttonPanel, BorderLayout.NORTH);
  
          // Adding ActionListeners and setting properties of the buttons..
  
          buttonBlack.setBackground(Color.BLACK);
          buttonBlack.setForeground(Color.BLACK);
          buttonBlack.setPreferredSize(new Dimension(32, 32));
  
          buttonRed.setBackground(Color.RED);
          buttonRed.setForeground(Color.RED);
          buttonRed.setPreferredSize(new Dimension(32, 32));
  
          buttonGreen.setBackground(Color.GREEN);
          buttonGreen.setForeground(Color.GREEN);
          buttonGreen.setPreferredSize(new Dimension(32, 32));
  
          buttonBlue.setBackground(Color.BLUE);
          buttonBlue.setForeground(Color.BLUE);
          buttonBlue.setPreferredSize(new Dimension(32, 32));
  
          buttonYellow.setBackground(Color.YELLOW);
          buttonYellow.setForeground(Color.YELLOW);
          buttonYellow.setPreferredSize(new Dimension(32, 32));
  
          clearButton.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.clearCanvas();
  
              }
  
          });
  
          buttonBlack.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.setBlack();
  
              }
  
          });
  
          buttonGreen.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.setGreen();
  
  
              }
  
          });
  
          buttonRed.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.setRed();
  
              }
  
          });
  
          buttonYellow.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.setYellow();
  
              }
  
          });
  
          buttonBlue.addActionListener(new ActionListener(){
  
              public void actionPerformed(ActionEvent e){
  
                  paintPanel.setBlue();
  
              }
  
          });
  
          // Adding buttons to the buttonPanel...
  
          buttonPanel.add(buttonBlack);
          buttonPanel.add(buttonRed);
          buttonPanel.add(buttonGreen);
          buttonPanel.add(buttonBlue);
          buttonPanel.add(buttonYellow);
  
          buttonPanel.add(clearButton);
  
      }
  
      public class PaintPanel extends JComponent {
  
          Image blankCanvas;
  
          Graphics2D g2D;
  
          int mouseX, mouseY, oldX, oldY;
  
          String currentColour = "BLACK";
  
          public PaintPanel() {
  
              setDoubleBuffered(true);
  
              addMouseListener(new MouseAdapter() {
  
                  public void mousePressed(MouseEvent e) {
  
                      oldX = e.getX();
                      oldY = e.getY();
  
                      g2D.drawOval(oldX, oldY, 1, 1);
  
                      System.out.println("[Debug] Point drawn at (" + oldX + ", " + oldY + ").");
  
                      repaint();
  
                  }
  
              });
  
              addMouseMotionListener(new MouseMotionAdapter(){
  
                  public void mouseDragged(MouseEvent e){
  
                      mouseX = e.getX();
                      mouseY = e.getY();
  
                      if(g2D != null)
  
                      g2D.drawLine(oldX, oldY, mouseX, mouseY);
  
                      repaint();
  
                      System.out.println("[Debug] Line drawn from (" + oldX + ", " + oldY + ") to (" + mouseX + ", " + mouseY + ").");
  
                      oldX = mouseX;
                      oldY = mouseY;
  
                  }
  
              });
  
          }
  
          public void paintComponent(Graphics g) {
  
              if (blankCanvas == null) {
  
                  blankCanvas = this.createImage(getWidth(), getHeight());
  
                  g2D = (Graphics2D)blankCanvas.getGraphics();
  
                  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
                  clearCanvas();
  
              }
  
              g.drawImage(blankCanvas, 0, 0, null);   
  
          }
  
          public void clearCanvas() {
  
              g2D.setPaint(Color.WHITE);
              g2D.fillRect(0, 0, getSize().width, getSize().height);
  
              switch(currentColour) {
  
              case "BLACK": g2D.setColor(Color.BLACK); break;
  
              case "RED": g2D.setColor(Color.RED); break;
  
              case "GREEN": g2D.setColor(Color.GREEN); break;
  
              case "BLUE": g2D.setColor(Color.BLUE); break;
  
              case "YELLOW": g2D.setColor(Color.YELLOW); break;
  
              }
  
              System.out.println("[Debug] Canvas cleared.");
  
              repaint();
  
          }
  
          public void setBlack() {
  
              g2D.setColor(Color.BLACK);
  
              System.out.println("[Debug] Colour changed to black.");
  
              currentColour = "BLACK";
  
          }
  
          public void setRed() {
  
              g2D.setColor(Color.RED);
  
              System.out.println("[Debug] Colour changed to red.");
  
              currentColour = "RED";
  
          }
  
          public void setBlue() {
  
              g2D.setColor(Color.BLUE);
  
              System.out.println("[Debug] Colour changed to blue.");
  
              currentColour = "BLUE";
  
          }
  
          public void setYellow() {
  
              g2D.setColor(Color.YELLOW);
  
              System.out.println("[Debug] Colour changed to yellow.");
  
              currentColour = "YELLOW";
  
          }
  
          public void setGreen() {
  
              g2D.setColor(Color.GREEN);
  
              System.out.println("[Debug] Colour changed to green.");
  
              currentColour = "GREEN";
  
          }
  
      }
  
  }
  </code></pre>