package edu.cmu.cs.diamond.rsna2007;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import edu.cmu.cs.diamond.opendiamond.Util;

public class OneView extends JComponent {
    final private BufferedImage img;

    protected BufferedImage scaledImg;

    private int drawPosY;

    private int drawPosX;

    private double scale = 1.0;

    final private String viewName;

    public OneView(BufferedImage img, String viewName) {
        super();

        setBackground(null);

        this.img = img;

        this.viewName = viewName;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (scaledImg == null) {
            // System.out.println("drawing scaled image");
            drawScaledImg();
            // System.out.println("done");
        }

        g.drawImage(scaledImg, drawPosX, drawPosY, null);
    }

    public void setScale(double scale) {
        this.scale = scale;

        scaledImg = null;

        Dimension d = new Dimension((int) (scale * img.getWidth()),
                (int) (scale * img.getHeight()));
        setMinimumSize(d);
        setPreferredSize(d);
        revalidate();
    }

    private void drawScaledImg() {
        Insets in = getInsets();
        final int w = getWidth() - in.left - in.right;

        scaledImg = getGraphicsConfiguration()
                .createCompatibleImage((int) (img.getWidth() * scale),
                        (int) (img.getHeight() * scale));

        Util.scaleImage(img, scaledImg, false);

        // center in X
        drawPosX = (w - scaledImg.getWidth()) / 2;
    }

    public BufferedImage getImage() {
        return img;
    }

    public Point getImagePoint(Point p) {
        return new Point((int) ((p.x - drawPosX) / scale),
                (int) ((p.y - drawPosY) / scale));
    }

    public String getViewName() {
        return viewName;
    }
}

@@ -367,16 +367,10 @@
 cale
- = 1.0
 ;%0A
+
 %0A   
@@ -946,24 +946,169 @@
 le scale) %7B%0A
+        if (scale %3C= 0) %7B%0A            throw new IllegalArgumentException(%22Scale (%22 + scale%0A                    + %22) cannot be %3C= 0%22);%0A        %7D%0A%0A
         this
@@ -1273,35 +1273,8 @@
 ));%0A
-        setMinimumSize(d);%0A
     
@@ -1463,86 +1463,23 @@
     
-scaledImg = getGraphicsConfiguration()%0A                .createCompatibleImage(
+final int ww = 
 (int
@@ -1500,25 +1500,25 @@
 h() * scale)
-,
+;
 %0A           
@@ -1514,31 +1514,30 @@
         
-               
+final int hh =
  (int) (
@@ -1560,16 +1560,94 @@
 * scale)
+;%0A%0A        scaledImg = getGraphicsConfiguration().createCompatibleImage(ww, hh
 );%0A%0A    

make things stricter