samer@0: /* samer@0: * MatEditor.java 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.units; samer@0: samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: import java.util.*; samer@0: import samer.core.*; samer@0: import samer.core.types.*; samer@0: import samer.core.util.*; samer@0: samer@0: /** samer@0: This is a mouse event handler that interprets mouse clicks samer@0: to edit a matrix. It assumes that it is handling mouse samer@0: events for a component the contains some representation of the samer@0: matrix that is arranged in rows and columns, just like the samer@0: matrix itself (it doesn't have to be an image). The window samer@0: relative coordinates are simply mapped to a matrix element samer@0: address. samer@0: */ samer@0: samer@0: public class Clicker extends MouseAdapter samer@0: implements MouseMotionListener // , Agent samer@0: { samer@0: Component canvas; samer@0: int w, h; samer@0: VInteger x, y; samer@0: samer@0: public Clicker( Component c, int w, int h) samer@0: { samer@0: canvas=c; samer@0: canvas.addMouseListener(this); samer@0: canvas.addMouseMotionListener(this); samer@0: this.w=w; samer@0: this.h=h; samer@0: x=y=null; samer@0: // vwr.exposeCommands(this); samer@0: } samer@0: samer@0: public void setXReceiver(VInteger x) { this.x=x; } samer@0: public void setYReceiver(VInteger y) { this.y=y; } samer@0: samer@0: /* samer@0: public void getCommands(Agent.Registry r) { r.add("add",add); } samer@0: public void execute(String cmd, Environment env) { samer@0: add=X._bool(env.datum(),!add); samer@0: } samer@0: */ samer@0: private void handle(MouseEvent e) samer@0: { samer@0: // get position in matrix samer@0: int i = (h*e.getY())/canvas.getHeight(); samer@0: int j = (w*e.getX())/canvas.getWidth(); samer@0: // Shell.print("click: "+i+","+j); samer@0: samer@0: // check bounds samer@0: if (i<0 || i>=h) return; samer@0: if (j<0 || j>=w) return; samer@0: samer@0: // correct for vertical flip samer@0: i=h-i-1; samer@0: samer@0: if (x!=null) x.set(j); samer@0: if (y!=null) y.set(i); samer@0: } samer@0: samer@0: public void mouseMoved(MouseEvent e) {} samer@0: public void mouseDragged(MouseEvent e) { handle(e); e.consume(); } samer@0: public void mousePressed(MouseEvent e) { handle(e); e.consume(); } samer@0: samer@0: // public void detach() { setto.dispose(); } samer@0: }