Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

DrawingPanel.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 /*
00024  * Created on Nov 17, 2006
00025  *
00026  */
00027 package com.meapsoft.visualizer;
00028 
00029 import java.awt.Dimension;
00030 import java.awt.Graphics;
00031 import java.awt.event.ActionListener;
00032 import java.awt.image.BufferedImage;
00033 
00034 import javax.swing.JPanel;
00035 import javax.swing.JScrollPane;
00036 
00040 public class DrawingPanel extends JPanel
00041 {
00042         ActionListener actionListener;
00043         Renderer renderer;
00044         private BufferedImage image;
00045         private JScrollPane scroller;
00046         
00047         private int origW = -1;
00048         private int origH = -1;
00049         
00050         private double hZoom = 1.0;
00051         private double wZoom = 1.0;
00052 
00053     private double zoomIncrement = 0.25;
00054         
00055         boolean iJustZoomed = false;
00056         
00057         public DrawingPanel(Renderer renderer, ActionListener aL)
00058         {
00059                 //setSize(800, 600);
00060                 //setPreferredSize(new Dimension(800, 600));
00061                 this.actionListener = aL;
00062                 this.renderer = renderer;
00063         }
00064 
00065         void setScroller(JScrollPane scroller)
00066         {
00067                 this.scroller = scroller;
00068         }
00069         
00070         void setRenderer(Renderer renderer)
00071         {
00072                 this.renderer = renderer;
00073                 //System.out.println("using: " + this.visualizer.name);
00074                 repaint();
00075         }
00076 
00077         public void setOrigWH(int w, int h)
00078         {
00079                 origW = w;
00080                 origH = h;      
00081                 image = (BufferedImage)createImage(w, h);
00082         }
00083         
00084         public void zoomIn()
00085         {
00086                 iJustZoomed = true;
00087 
00088         // only zoom horizontally...
00089                 //hZoom += zoomIncrement;
00090                 wZoom += zoomIncrement;
00091 
00092         // zoom out vertically so we don't need a vertical scrollbar
00093         hZoom = 1 - scroller.getHorizontalScrollBar().getPreferredSize().getHeight()/origH;
00094         
00095                 int w = (int)Math.round(origW * wZoom);
00096                 int h = (int)Math.round(origH * hZoom);
00097         
00098                 setSize(w, h);
00099                 setPreferredSize(new Dimension(w, h));
00100                 image = (BufferedImage)createImage(w, h);
00101                 scroller.revalidate();
00102                 
00103                 //System.out.println("hZoom: " + hZoom + " wZoom: " + wZoom);
00104         }
00105         
00106         public void zoomOut()
00107         {
00108                 if (hZoom == 1.0 || wZoom == 1.0)
00109                         return;
00110 
00111                 iJustZoomed = true;
00112                 
00113         // only zoom horizontally...
00114                 //hZoom -= zoomIncrement;
00115                 wZoom -= zoomIncrement;
00116                 
00117         // no scrollbar necessary anymore
00118         if(wZoom == 1.0)
00119             hZoom = 1.0;
00120         
00121                 int w = (int)Math.round(origW * wZoom);
00122                 int h = (int)Math.round(origH * hZoom);
00123                 
00124                 setSize(w, h);
00125                 setPreferredSize(new Dimension(w, h));
00126                 image = (BufferedImage)createImage(w, h);
00127                 scroller.revalidate();
00128                 
00129                 //System.out.println("hZoom: " + hZoom + " wZoom: " + wZoom);
00130         }
00131         
00132         public void resetZoom()
00133         {
00134                 iJustZoomed = true;
00135                 
00136                 hZoom = 1.0;
00137                 wZoom = 1.0;
00138 
00139                 int w = (int)Math.round(origW * wZoom);
00140                 int h = (int)Math.round(origH * hZoom);
00141                 
00142                 setSize(w, h);
00143                 setPreferredSize(new Dimension(w, h));
00144                 image = (BufferedImage)createImage(w, h);
00145                 scroller.revalidate();
00146                 
00147                 //System.out.println("w: " + w + " h: " + h);
00148         }
00149         
00150         public void paintComponent(Graphics g)
00151         {               
00152                 //System.out.println("pC...");
00153                 //first time
00154                 if (origW == -1)
00155                 {
00156                         origW = getWidth();
00157                         origH = getHeight();
00158                 }
00159                 
00160                 int w = (int)Math.round(origW * wZoom);
00161                 int h = (int)Math.round(origH * hZoom);
00162                 
00163                 if (image == null)
00164                 {
00165                         //System.out.println("making new image!");
00166                         image = (BufferedImage)createImage(w, h);
00167                 }
00168                 
00169                 //System.out.println("gW: " + getWidth() + " gH: " + getHeight() +
00170                          //" w: " + w + " h: " + h +
00171                          //" origW: " + origW + " origH: " + origH); 
00172                 renderer.draw(image, w, h);
00173                 g.drawImage(image, 0, 0, this);
00174         }
00175 }

Generated on Tue Feb 6 19:02:25 2007 for MEAPsoft by doxygen1.2.18