samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.tools; samer@0: samer@0: import java.awt.*; samer@0: import java.awt.image.*; samer@0: import java.util.*; samer@0: import samer.core.*; samer@0: import samer.core.util.*; samer@0: samer@0: /** samer@0: A base class for displaying images of real values. samer@0: All that is needed is an ImageSource that implements samer@0: ImageSourceBase. The ImageViewer provides a VMap samer@0: for the IMap in the ImageSourceBase, and exposes the samer@0: VMaps commands in a popup menu. samer@0: */ samer@0: samer@0: public class ImageViewer extends samer.core.util.heavy.VCanvas implements Observer, Agent samer@0: { samer@0: protected ImageSourceBase ip; samer@0: protected Image img; samer@0: protected VMap map; samer@0: protected Observable obs; samer@0: protected int cx=1, cy=1, iw, ih; samer@0: private boolean autoscale=false; samer@0: samer@0: public ImageViewer( ImageSourceBase source, Observable o) samer@0: { samer@0: map = new VMap(source.getMap()); samer@0: source.setMap(map.getMap()); samer@0: source.setColorModel( samer@0: (IndexColorModel)Shell.datum("colormap") samer@0: .get(ColorModelCodec,ImageSourceBase.GREY)); samer@0: samer@0: ip = source; obs = o; samer@0: img = createImage(ip); samer@0: iw=img.getWidth(null); samer@0: ih=img.getHeight(null); samer@0: samer@0: int cz=Shell.getInt("cell.size",4); samer@0: cx=Shell.getInt("cell.width",cz); samer@0: cy=Shell.getInt("cell.height",cz); samer@0: samer@0: exposeCommands( this); samer@0: exposeCommands( map); samer@0: map.addObserver(this); samer@0: } samer@0: samer@0: public void scale() {} samer@0: public void update(Observable o, Object s) samer@0: { samer@0: if (s==this) return; samer@0: else if (s==VMap.NEW_MAP) ip.setMap(map.getMap()); samer@0: else if (s==Viewable.DISPOSING) { samer@0: if (o==obs) Shell.releaseViewer(this); return; samer@0: } samer@0: samer@0: if (autoscale) scale(); samer@0: ip.sendPixels(); samer@0: if (s instanceof Point) { samer@0: Point p=(Point)s; samer@0: int x=(2*p.x*width+iw)/(2*iw); samer@0: int y=(2*p.y*height+ih)/(2*ih); samer@0: int w=(width+iw-1)/iw; samer@0: int h=(height+ih-1)/ih; samer@0: y=height-y-h; samer@0: repaint(0,x,y,w,h); samer@0: } else repaint(0,0,0,width,height); samer@0: } samer@0: samer@0: public void attach() { super.attach(); if (obs!=null) obs.addObserver(this); } samer@0: public void detach() { if (obs!=null) obs.deleteObserver(this); super.detach(); } samer@0: samer@0: // .............. Agent bits .............................. samer@0: samer@0: public void getCommands(Registry r) { samer@0: r.add("scale").add("autoscale",autoscale).add("update"); samer@0: r.group(); r.add("publish"); samer@0: } samer@0: samer@0: public void execute(String c, Environment env) throws Exception samer@0: { samer@0: if (c.equals("scale")) { scale(); update(null,null); } samer@0: else if (c.equals("autoscale")) { samer@0: autoscale = X._bool(env.datum(),!autoscale); samer@0: if (autoscale) update(null,null); samer@0: } else if (c.equals("update")) { update(null,null); } samer@0: else if (c.equals("publish")) samer@0: Shell.put(X.string(env.datum(),"image"),this); samer@0: } samer@0: samer@0: // .............. The rest ............................... samer@0: samer@0: public VMap getVMap() { return map; } samer@0: public IMap getMap() { return ip.getMap(); } samer@0: public void setMap(IMap map) { ip.setMap(map); } samer@0: public void setColorModel( IndexColorModel cm) { samer@0: ip.setColorModel(cm); samer@0: ip.sendPixels(); samer@0: repaint(); samer@0: } samer@0: samer@0: public void update(Graphics g) { samer@0: g.drawImage( img, 0, 0, width, height, null); samer@0: } samer@0: samer@0: public void paint( Graphics g) { samer@0: g.drawImage( img, 0, 0, width, height, null); samer@0: } samer@0: samer@0: public void setCellSize( int w, int h) { cx=w; cy=h; } samer@0: public Dimension getPreferredSize() { samer@0: return new Dimension(cx*ip.width,cy*ip.height); samer@0: } samer@0: samer@0: public static Environment.Codec ColorModelCodec = new Environment.Codec() samer@0: { samer@0: public Class targetClass() { return IndexColorModel.class; } samer@0: samer@0: public String string(Object o) { return o.toString(); } // ?? samer@0: public Object object(Object o) { return o; } samer@0: public Object decode(Object o) { samer@0: if (o instanceof IndexColorModel) return o; samer@0: return Shell.datum(o.toString()).get(this,null); samer@0: } samer@0: }; samer@0: }