@Override
 public void sequenceChanged(SequenceEvent sequenceEvent) {
   if (sequenceEvent.getSource() == SequenceEvent.SequenceEventSourceType.SEQUENCE_DATA) {
     cancelCompute = true;
     while (computing)
       try {
         Thread.sleep(1);
       } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     (new Thread(new computeRunnable())).start();
   }
   if (sequenceEvent.getSourceType() == SequenceEventSourceType.SEQUENCE_OVERLAY
       && sequenceEvent.getSource() == this
       && sequenceEvent.getType() == SequenceEventType.REMOVED)
     sequence.removeROI(displayRectangle);
 }
  @Override
  public void paint(Graphics2D g, Sequence sequence, IcyCanvas canv) {
    if (canv != canvas) {
      canvas = canv;
      canvas.getViewer().addListener(this);
      canvas.getViewer().addKeyListener(this);
    }
    if (lastPoint == null) lastPoint = new Point(0, 0);
    // create a graphics object so that we can then dispose it at the end of the paint to clean
    // all change performed in the paint,
    // like transform, color change, stroke (...).
    HashMap<ROI2D, IntensityPaint> roiPairTemp = new HashMap<ROI2D, IntensityPaint>();
    Graphics2D g2 = (Graphics2D) g.create();
    for (ROI2D roi : sequence.getROI2Ds()) {
      if (roi.getName().startsWith("(") && roi.getName().endsWith(")")) continue;
      {
        IntensityPaint ip;

        if (roiPairDict.containsKey(roi)) {
          ip = roiPairDict.get(roi);
          //        			rect.displayRectangle.setName("["+roi.getName()+"]");
          //        			Rectangle2D box2 = rect.displayRectangle.getBounds2D();
          //       //        			if(!sequence.getROI2Ds().contains(ip.displayRectangle)){//
          //		sequence.removeROI(rect.displayRectangle);//
          //	rect.displayRectangle.remove();//            			rect.displayRectangle new
          // ROI2DRectangle(lastPoint.getX(),lastPoint.getY(),Math.min(800,sequence.getWidth()),0);;;//            			rect.displayRectangle.setColor(roi.getColor());//            			sequence.addROI(rect.displayRectangle);//        			}
          if (ip != null) {
            if (!sequence.getROI2Ds().contains(ip.displayRectangle)) {
              ip.dispose();
            } else roiPairTemp.put(roi, ip);
          }
        } else {
          ip = null;
          if (enableAddRoi) {
            roi.setName("" + Integer.toString(nameIndex) + "#");
            nameIndex += 1;
            roi.setColor(getRandomColor());
            // roi.setSelectedColor(getRandomColor());
            ip = new IntensityPaint(roi, sequence, canvas);
            lastPoint.setLocation(lastPoint.getX() + 10, lastPoint.getY() + 10);
            if (lastPoint.getX() > sequence.getHeight() || lastPoint.getY() > sequence.getWidth())
              lastPoint.setLocation(0, 0);
            sequence.addROI(ip.displayRectangle);
          }
          roiPairTemp.put(roi, ip);
        }
        if (ip != null && !skipSelect && roi instanceof ROI2DShape) {
          if (roi.isSelected()) ip.displayRectangle.setSelected(roi.isSelected());
          else if (ip.displayRectangle.isSelected())
            roi.setSelected(ip.displayRectangle.isSelected());
        }

        try {
          if (ip != null) drawHisto(roi, g2, sequence, canvas);
        } catch (Exception e2) {

        }
      }
    }
    for (ROI2D roi : roiPairDict.keySet()) {
      if (!roiPairTemp.containsKey(roi)) {
        if (roiPairDict.get(roi) != null) {
          roiPairDict.get(roi).dispose();
          sequence.removeROI(roiPairDict.get(roi).displayRectangle);
        }
      }
    }
    roiPairDict.clear();
    roiPairDict = roiPairTemp;
    g2.dispose();
  }
 @Override
 public void sequenceClosed(Sequence sequence) {
   sequence.removeROI(displayRectangle);
 }