Scaling an image

You can use apply an AffineTransform object when drawing the image, or you can create a BufferedImage and perform an scaling operation on it.

Apply AffineTransform when drawing image

Main.java:

import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   private Image image;
   private double scalex = 1.0;
   private double scaley = 1.0;
   private JButton button1;
   private JButton button2;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      image = Toolkit.getDefaultToolkit().getImage("c:\djkrush.jpg");
      waitForImage(image);
 
      getContentPane().setLayout(new BorderLayout());
      
      button1 = new JButton("scale up by 5");
      button1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            scalex += .1;
            scaley += .1;
            repaint();
         }
      });
 
      button2 = new JButton("scale down by 5");
      button2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            scalex -= .1;
            scaley -= .1;
            repaint();
         }
      });
 
      JPanel panel = new JPanel(new GridLayout(1, 2));
      panel.add(button1);
      panel.add(button2);
      getContentPane().add(BorderLayout.SOUTH, panel);
   }
 
   public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
 
      AffineTransform at = new AffineTransform();
      at.scale(scalex, scaley);
      g2d.drawImage(image, at, this);
 
      button1.repaint();
      button2.repaint();
   }
 
   public void waitForImage(Image image) {
      MediaTracker mt = new MediaTracker(this);
 
      int id = 0;
      mt.addImage(image, id);
 
      try {
         mt.waitForID(id);
      }
      catch(InterruptedException e) {
         System.out.println("Image loading interrupted : " + e);
      }
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 400);
      main.setVisible(true);
   }
}

Creating BufferedImage and scaling afterwards

Main.java:

import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   private Image image;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      image = Toolkit.getDefaultToolkit().getImage("c:\djkrush.jpg");
      waitForImage(image);
      image = scaleImage(image, 2.0, 2.0);
   }
 
   public void paint(Graphics g) {
      g.drawImage(image, 0, 0, this);
   }
 
   public void waitForImage(Image image) {
      MediaTracker mt = new MediaTracker(this);
 
      int id = 0;
      mt.addImage(image, id);
 
      try {
         mt.waitForID(id);
      }
      catch(InterruptedException e) {
         System.out.println("Image loading interrupted : " + e);
      }
   }
 
   public Image scaleImage(Image image, double scalex, double scaley) {
      BufferedImage bi = toBufferedImage(image);
 
      AffineTransformOp op = new AffineTransformOp(
                   AffineTransform.getScaleInstance(scalex, scaley), null); 
 
      return op.filter(bi, null); 
   }
 
   public BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
                                           BufferedImage.TYPE_INT_RGB); 

      // copy the original image
      Graphics g = bi.createGraphics();
    
      g.drawImage(image, 0, 0, null);
      g.dispose();
 
      return bi;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 400);
      main.setVisible(true);
   }
}

Image used: