codice:
public class MyCanvas extends ImageCanvas implements MouseListener, MouseMotionListener{
protected MyImagePlus imp;
public int imageWidth, imageHeight;
private int dstWidth, dstHeight;
private double magnification;
private Point startPoint, endPoint, halfPoint, clickPoint, mouse;
private Image drawing;
private Graphics2D drawGraphics;
protected Rectangle srcRect;
Line l;
//punto da modificare 0=startPoint, 1=endPoint, 2=halfPoint
int mod;
boolean drag;
public MyCanvas(MyImagePlus imp) {
super(imp);
this.imp = imp;
int width = imp.getWidth();
int height = imp.getHeight();
imageWidth = width;
imageHeight = height;
srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
dstWidth = width;
dstHeight = height;
setSize(width, height);
magnification = 1.0;
addMouseListener(this);
addMouseMotionListener(this);
}
//-----------------------------------------
//------------> EVENTI MOUSE <------------
//-----------------------------------------
public void mousePressed(MouseEvent evt) {
System.out.println("mousePressed");
if(ImageTools.getToolSel()==0){
drag=false;
Line roi = (Line) imp.getRoi();
//se è su uno dei quadratini
if(getCursor().equals(handCursor)){
clickPoint = evt.getPoint();
}
//devo disegnare la nuova linea
else{
startPoint = null;
endPoint = null;
if (SwingUtilities.isLeftMouseButton(evt)) {
startPoint = evt.getPoint();
repaint();
}
}
}
else if(ImageTools.getToolSel()==1){ System.out.println("selezionato zoomin");
int x = evt.getX();
int y = evt.getY();
zoomIn(x, y);
}
else if(ImageTools.getToolSel()==2){
int x = evt.getX();
int y = evt.getY();
zoomOut(x, y);
}
}
public void mouseDragged(MouseEvent evt) {
if(ImageTools.getToolSel()==0){
drag=true;
Line roi = (Line) imp.getRoi();
//se è su uno dei quadratini
if(getCursor().equals(handCursor)){
//cambio il punto che ho cliccato
if(mod==0)
startPoint = evt.getPoint();
if(mod==1)
endPoint = evt.getPoint();
if(mod==2){
halfPoint = evt.getPoint();
//calcolo di quanto si è spostato il punto
int diffPointX1=halfPoint.x-clickPoint.x;
int diffPointY1=halfPoint.y-clickPoint.y;
//il nuovo clickPoint diventa il drag
clickPoint=halfPoint;
//aggiorno lo start e end
startPoint.x=startPoint.x+diffPointX1;
startPoint.y=startPoint.y+diffPointY1;
endPoint.x=endPoint.x+diffPointX1;
endPoint.y=endPoint.y+diffPointY1;
}
}
//è il drag di una nuova linea
else{
endPoint=evt.getPoint();
}
//info line
if(roi!=null)
ImageTools.updateInfoLine(startPoint.x,startPoint.y,(int)roi.getLength(),endPoint.x,endPoint.y);
repaint();
}
}
public void mouseReleased(MouseEvent evt) {
System.out.println("Mouse released");
if(ImageTools.getToolSel()==0 ){
repaint();
if(drag==true){
drag=false;
//aggiorno il punto centrale della linea (l'endPoint si è aggiornato con il drag)
//non potevo metterlo nel drag perchè all'inizio la roi non c'è
Line roi = (Line) imp.getRoi();
int halfPointX=(int)(roi.getBounds().x+(roi.getBounds().width)/2);
int halfPointY=(int)(roi.getBounds().y+(roi.getBounds().height)/2);
halfPoint=new Point(halfPointX,halfPointY);
}
}
}
@Override
public void mouseClicked(MouseEvent evt) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
if(ImageTools.getToolSel()==0){
int mx = e.getX();
int my = e.getY();
mouse=new Point(mx,my);
setCursor(mx, my);
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g) {
final int drawWidth = (int)(imageWidth*magnification);
final int drawHeight = (int)(imageHeight*magnification);
if(drawing==null){
drawing=createImage(drawWidth, drawHeight);
drawGraphics=(Graphics2D)drawing.getGraphics();
}
//per contrasto
if (imageUpdated) {
imageUpdated = false;
imp.updateImage();
}
Image img = imp.getImage();
if (img!=null)
drawGraphics.drawImage(img, 0, 0, drawWidth, drawHeight,
srcRect.x, srcRect.y, srcRect.x+srcRect.width, srcRect.y+srcRect.height, null);
if(startPoint!=null && endPoint!=null){
drawGraphics.setPaint((Color.YELLOW));
drawGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
l=new Line(startPoint.x,startPoint.y,endPoint.x,endPoint.y);
//devo settare immagine altrimenti non funziona plot
l.setImage(imp);
l.setWidth(l.getWidth());
l.draw(drawGraphics);
//aggiungo roi a MyImagePlus
imp.setRoi(l);
}
g.drawImage(drawing, 0, 0, null);
}
//--------------------------
public void zoomIn(int x, int y) {
if (magnification>=32)
return;
double newMag = magnification*2;
if (newMag==1.0) {
return;
}
int newWidth = dstWidth*2;
int newHeight = dstHeight*2;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
ImageView iv = WindowManagerCCD.getCurrentWindow();
Point loc = iv.getLocationOnScreen();
if ((loc.x+50+newWidth)<screen.width && (loc.y+50+newHeight)<screen.height) {
setDrawingSize(newWidth, newHeight);
iv.pack();
}
else {
int w = (int)Math.round(dstWidth/newMag);
if (w*newMag<dstWidth) w++;
int h = (int)Math.round(dstHeight/newMag);
if (h*newMag<dstHeight) h++;
x = offScreenX(x);
y = offScreenY(y);
Rectangle r = new Rectangle(x-w/2, y-h/2, w, h);
if (r.x<0) r.x = 0;
if (r.y<0) r.y = 0;
if (r.x+w>imageWidth) r.x = imageWidth-w;
if (r.y+h>imageHeight) r.y = imageHeight-h;
srcRect = r;
}
//imposto nuova magnification
this.magnification = newMag;
repaint();
}