ZoomPanel.java

package ac.essex.ooechs.imaging.commons.apps.display; 
 
import javax.swing.*; 
import java.awt.image.BufferedImage; 
import java.awt.*; 
 
/** 
 * <p/> 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version, 
 * provided that any use properly credits the author. 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details at http://www.gnu.org 
 * </p> 
 * 
 * @author Olly Oechsle, University of Essex, Date: 11-Dec-2006 
 * @version 1.0 
 */ 
public class ZoomPanel extends JPanel { 
 
        protected int windowWidth; 
        protected int windowHeight; 
        protected BufferedImage img = null; 
        protected int x = 0; 
        protected int y = 0; 
 
        public ZoomPanel(int windowWidth, int windowHeight) { 
            setSize(windowWidth, windowHeight); 
        } 
 
        public void setSize(int windowWidth, int windowHeight) { 
            this.windowWidth = windowWidth / 2; 
            this.windowHeight = windowHeight / 2; 
        } 
 
        public void update(int x, int y) { 
            this.x = x; 
            this.y = y; 
            repaint(); 
        } 
 
        public BufferedImage getImage() { 
            return img; 
        } 
 
        public void setImage(BufferedImage img) { 
            this.img = img; 
        } 
 
        public void paintComponent(Graphics g) { 
            super.paintComponent(g); 
            if (img != null) { 
                g.drawImage(img, 0, 0, getWidth(), getHeight(), x - windowWidth, y - windowHeight, x + windowWidth, y + windowHeight, this); 
                g.setColor(Color.WHITE); 
                g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); 
                g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); 
           } else { 
                g.setColor(Color.BLACK); 
                g.fillRect(0, 0, getWidth(), getHeight()); 
           } 
        } 
 
    }