001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ImageView.java,v $ 023 Revision 1.9 2004/05/14 00:09:53 markl 024 use JToolBar for buttons 025 026 Revision 1.8 2004/03/16 06:43:39 markl 027 LocaleManager method change 028 029 Revision 1.7 2003/01/19 09:46:48 markl 030 replaced JScrollPane instances with KScrollPane. 031 032 Revision 1.6 2001/03/20 00:54:52 markl 033 Fixed deprecated calls. 034 035 Revision 1.5 2001/03/12 09:56:54 markl 036 KLabel/KLabelArea changes. 037 038 Revision 1.4 2001/03/12 09:27:56 markl 039 Source code and Javadoc cleanup. 040 041 Revision 1.3 1999/04/19 05:59:27 markl 042 I18N changes. 043 044 Revision 1.2 1999/01/10 02:04:05 markl 045 fixed to do a smooth resize 046 ---------------------------------------------------------------------------- 047*/ 048 049package kiwi.ui; 050 051import java.awt.*; 052import java.awt.event.*; 053import javax.swing.*; 054import javax.swing.border.*; 055 056import kiwi.util.*; 057 058/** A general-purpose image viewing component. It displays an image in a scroll 059 * pane and provides zooming buttons. 060 * 061 * <p><center> 062 * <img src="snapshot/ImageView.gif"><br> 063 * <i>An example ImageView.</i> 064 * </center> 065 * 066 * @see java.awt.Image 067 * 068 * @author Mark Lindner 069 */ 070 071public class ImageView extends KPanel 072 { 073 private KButton b_zoomin, b_zoomout, b_zoomreset; 074 private KLabel viewport; 075 private int origw, origh; 076 private float scale = 1; 077 private Image image, curImage = null; 078 private KScrollPane scroll; 079 private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR); 080 private Cursor defaultCursor; 081 082 /** Construct a new <code>ImageView</code>. 083 * 084 * @param comment A comment to display above the image. 085 * @param image The image to display. 086 */ 087 088 public ImageView(String comment, Image image) 089 { 090 origw = image.getWidth(this); 091 origh = image.getHeight(this); 092 this.image = image; 093 094 LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs"); 095 096 ActionListener actionListener = new ActionListener() 097 { 098 public void actionPerformed(ActionEvent evt) 099 { 100 Object o = evt.getSource(); 101 102 if(o == b_zoomin) 103 scaleImage(2.0f); 104 else if(o == b_zoomout) 105 scaleImage(0.5f); 106 else if(o == b_zoomreset) 107 scaleImage(1.0f); 108 } 109 }; 110 111 defaultCursor = getCursor(); 112 113 ResourceManager rm = KiwiUtils.getResourceManager(); 114 115 GridBagConstraints gbc = new GridBagConstraints(); 116 117 setBorder(KiwiUtils.defaultBorder); 118 setLayout(new BorderLayout(5, 5)); 119 120 KPanel p1 = new KPanel(); 121 p1.setLayout(new BorderLayout(5, 5)); 122 123 KLabel label = new KLabel(comment); 124 label.setFont(new Font("Serif", Font.BOLD, 14)); 125 126 p1.add("Center", label); 127 128 JToolBar toolBar = new JToolBar(); 129 toolBar.setFloatable(false); 130 131 b_zoomin = new KButton(rm.getIcon("zoom-in.gif")); 132 b_zoomin.setToolTipText(loc.getMessage("kiwi.tooltip.zoom_in")); 133 b_zoomin.addActionListener(actionListener); 134 toolBar.add(b_zoomin); 135 136 b_zoomout = new KButton(rm.getIcon("zoom-out.gif")); 137 b_zoomout.setToolTipText(loc.getMessage("kiwi.tooltip.zoom_out")); 138 b_zoomout.addActionListener(actionListener); 139 toolBar.add(b_zoomout); 140 141 b_zoomreset = new KButton(rm.getIcon("zoom-reset.gif")); 142 b_zoomreset.setToolTipText(loc.getMessage("kiwi.tooltip.actual_size")); 143 b_zoomreset.addActionListener(actionListener); 144 toolBar.add(b_zoomreset); 145 146 p1.add("East", toolBar); 147 148 add("North", p1); 149 150 viewport = new KLabel(); 151 viewport.setIcon(new ImageIcon(image)); 152 viewport.setVerticalAlignment(SwingConstants.CENTER); 153 viewport.setHorizontalAlignment(SwingConstants.CENTER); 154 155 scroll = new KScrollPane(viewport); 156 scroll.setBackground(Color.white); 157 add("Center", scroll); 158 } 159 160 /** Scale the image by a given scale factor. 161 * 162 * @param scaleFactor The scale factor. 163 */ 164 165 public void scaleImage(float scaleFactor) 166 { 167 if(scaleFactor != 1) 168 scale *= scaleFactor; 169 else 170 scale = 1; 171 172 KiwiUtils.busyOn(this); 173 if(curImage != null) curImage.flush(); 174 curImage = image.getScaledInstance((int)((float)origw * scale), 175 (int)((float)origh * scale), 176 Image.SCALE_SMOOTH); 177 viewport.setIcon(new ImageIcon(curImage)); 178 viewport.invalidate(); 179 validate(); 180 KiwiUtils.busyOff(this); 181 } 182 183 } 184 185/* end of source file */