0

i want to rotate an image and in next level i want to resize it plese help me. i create a class that extended from JPanel and override paintComponent() method for drawing image.

public class NewJPanel extends javax.swing.JPanel {

/** Creates new form NewJPanel */
public NewJPanel() {
    initComponents();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 20, 20, this);
}
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117

3 Answers3

3

Here is some code I use. You can modify it to fit your needs.

Resize Image:

/**
 * Resizes the image
 *
 * @param filePath File path to the image to resize
 * @param w        Width of the image
 * @param h        Height of the image
 * @return A resized image
 */
public ImageIcon resizeImage(String filePath, int w, int h) {
    String data = filePath;
    BufferedImage bsrc, bdest;
    ImageIcon theIcon;
    //scale the image
    try {
        if (dataSource == DataTypeEnum.file) {
            bsrc = ImageIO.read(new File(data));
        } else {
            bsrc = ImageIO.read(new URL(filePath));
        }
        bdest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bdest.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(
                (double) w / bsrc.getWidth(), (double) h / bsrc.getHeight());
        g.drawRenderedImage(bsrc, at);
        //add the scaled image
        theIcon = new ImageIcon(bdest);
        return theIcon;
    } catch (Exception e) {
        Window.getLogger().warning("This image can not be resized. " +
                "Please check the path and type of file.");
        //restore the old background
        return null;
    }
}

Rotate Image:
NOTE: The angle is in radians

public static BufferedImage rotate(BufferedImage image, double angle) {
    double sin = Math.abs(Math.sin(angle)),
           cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(),
        h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin),
        newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage result =
            gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);
    g.dispose();
    return result;
}
Community
  • 1
  • 1
user489041
  • 27,916
  • 55
  • 135
  • 204
0

Without giving away a full solution

Graphics2D has rotate and scale functions.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
brian_d
  • 11,190
  • 5
  • 47
  • 72
-1

Use the BufferedImage class

BufferedImage newImg = new BufferedImage(newWidth, newHeight, imageType); newImg.createGraphics().drawImage(oldImg,0,0,newWidth, newHeight,0,0,oldWidth,oldHeight, null);

then just repaint using newImg instead of the old image, should work, I'm not near a compiler at the moment to test.

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver%29

awestover89
  • 1,713
  • 4
  • 21
  • 37