if (viewRect.width < imgRect.width) width = viewRect.width; if (viewRect.height < imgRect.height) height = viewRect.height;
width = img.getWidth(); height = img.getHeight(); if (x+viewRect.width < imgRect.width) width = x+viewRect.width; if (y+viewRect.height < imgRect.height) height = y+viewRect.height;
    public void paintComponent(Graphics g)    {        super.paintComponent(g);        BufferedImage img = model.getDisplayedImage();        if (img == null) return;        Graphics2D g2D = (Graphics2D) g;        ImagePaintingFactory.setGraphicRenderingSettings(g2D);        g2D.drawImage(img, null, 0, 0);         if (model.isUnitBar()) {            String value = model.getUnitBarValue();             if (value != null) {                int size = (int) (model.getUnitBarSize());                int width, height;                width = img.getWidth();                height = img.getHeight();                                Rectangle imgRect = new Rectangle(0, 0, width, height);                Rectangle viewRect = view.getViewport().getBounds();                if (imgRect.contains(viewRect)) {                    JViewport port = view.getViewport();                    width = (int) port.getViewPosition().getX()+port.getWidth();                    height = (int) port.getViewPosition().getY()+                            port.getHeight();                } else {                	if (viewRect.width < imgRect.width) width = viewRect.width;                	if (viewRect.height < imgRect.height)                		height = viewRect.height;                }                ImagePaintingFactory.paintScaleBar(g2D, width-size-10,                 										height-10, size, value);            }        }    }