0

I am trying to scale an image with AffineTransform. However, the image does not appear correctly when the Frame appears initially.

enter image description here

But after the frame is resized, the image shows perfectly.

enter image description here

Here is the code.

public class MainFrame extends javax.swing.JFrame {

  Line line;
  BufferedImage bi;

  public MainFrame() {
    initComponents();

    try {
      bi = ImageIO.read(new File("map.png"));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    AffineTransform old = g2.getTransform();
    g2.scale(0.25, 0.25);
    g2.drawImage(bi, 0, 0, null);
    g2.setTransform(old);
  }

  ...
}
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
wannik
  • 12,212
  • 11
  • 46
  • 58
  • Could be that your `Graphics` is already clipped to the bounds of the component. If scaling, you might also need to scale the clip bounds. – Harald K Apr 15 '14 at 09:02
  • 4
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). 3) `g2.drawImage(bi, 0, 0, null);` should be `g2.drawImage(bi, 0, 0, this);` and the first thing the overridden paint should do is call the `super` method. – Andrew Thompson Apr 15 '14 at 09:07
  • 1
    this could be proper way for ImageObserver, then JFrame isn't proper container for any Graphics Whatever, use JPanel with paintComponent, no idea without your SSCCE / MCVE / MCTRE – mKorbel Apr 15 '14 at 09:48

0 Answers0