void drawRoiLabel(Graphics g, int index, Roi roi) {
   Rectangle r = roi.getBounds();
   int x = screenX(r.x);
   int y = screenY(r.y);
   double mag = getMagnification();
   int width = (int) (r.width * mag);
   int height = (int) (r.height * mag);
   int size = width > 40 && height > 40 ? 12 : 9;
   if (font != null) {
     g.setFont(font);
     size = font.getSize();
   } else if (size == 12) g.setFont(largeFont);
   else g.setFont(smallFont);
   boolean drawingList = index >= LIST_OFFSET;
   if (drawingList) index -= LIST_OFFSET;
   String label = "" + (index + 1);
   if (drawNames && roi.getName() != null) label = roi.getName();
   FontMetrics metrics = g.getFontMetrics();
   int w = metrics.stringWidth(label);
   x = x + width / 2 - w / 2;
   y = y + height / 2 + Math.max(size / 2, 6);
   int h = metrics.getAscent() + metrics.getDescent();
   if (bgColor != null) {
     g.setColor(bgColor);
     g.fillRoundRect(x - 1, y - h + 2, w + 1, h - 3, 5, 5);
   }
   if (!drawingList && labelRects != null && index < labelRects.length)
     labelRects[index] = new Rectangle(x - 1, y - h + 2, w + 1, h);
   g.setColor(labelColor);
   g.drawString(label, x, y - 2);
   g.setColor(defaultColor);
 }
 void showFrameRate(Graphics g) {
   frames++;
   if (System.currentTimeMillis() > firstFrame + 1000) {
     firstFrame = System.currentTimeMillis();
     fps = frames;
     frames = 0;
   }
   g.setColor(Color.white);
   g.fillRect(10, 12, 50, 15);
   g.setColor(Color.black);
   g.drawString((int) (fps + 0.5) + " fps", 10, 25);
 }
 void drawRoi(Graphics g, Roi roi, int index) {
   int type = roi.getType();
   ImagePlus imp2 = roi.getImage();
   roi.setImage(imp);
   Color saveColor = roi.getStrokeColor();
   if (saveColor == null) roi.setStrokeColor(defaultColor);
   if (roi instanceof TextRoi) ((TextRoi) roi).drawText(g);
   else roi.drawOverlay(g);
   roi.setStrokeColor(saveColor);
   if (index >= 0) {
     if (roi == currentRoi) g.setColor(Roi.getColor());
     else g.setColor(defaultColor);
     drawRoiLabel(g, index, roi);
   }
   if (imp2 != null) roi.setImage(imp2);
   else roi.setImage(null);
 }
 // Use double buffer to reduce flicker when drawing complex ROIs.
 // Author: Erik Meijering
 void paintDoubleBuffered(Graphics g) {
   final int srcRectWidthMag = (int) (srcRect.width * magnification);
   final int srcRectHeightMag = (int) (srcRect.height * magnification);
   if (offScreenImage == null
       || offScreenWidth != srcRectWidthMag
       || offScreenHeight != srcRectHeightMag) {
     offScreenImage = createImage(srcRectWidthMag, srcRectHeightMag);
     offScreenWidth = srcRectWidthMag;
     offScreenHeight = srcRectHeightMag;
   }
   Roi roi = imp.getRoi();
   try {
     if (imageUpdated) {
       imageUpdated = false;
       imp.updateImage();
     }
     Graphics offScreenGraphics = offScreenImage.getGraphics();
     Java2.setBilinearInterpolation(offScreenGraphics, Prefs.interpolateScaledImages);
     Image img = imp.getImage();
     if (img != null)
       offScreenGraphics.drawImage(
           img,
           0,
           0,
           srcRectWidthMag,
           srcRectHeightMag,
           srcRect.x,
           srcRect.y,
           srcRect.x + srcRect.width,
           srcRect.y + srcRect.height,
           null);
     if (overlay != null) drawOverlay(offScreenGraphics);
     if (showAllROIs) drawAllROIs(offScreenGraphics);
     if (roi != null) drawRoi(roi, offScreenGraphics);
     if (srcRect.width < imageWidth || srcRect.height < imageHeight)
       drawZoomIndicator(offScreenGraphics);
     if (IJ.debugMode) showFrameRate(offScreenGraphics);
     g.drawImage(offScreenImage, 0, 0, null);
   } catch (OutOfMemoryError e) {
     IJ.outOfMemory("Paint");
   }
 }
 void drawZoomIndicator(Graphics g) {
   int x1 = 10;
   int y1 = 10;
   double aspectRatio = (double) imageHeight / imageWidth;
   int w1 = 64;
   if (aspectRatio > 1.0) w1 = (int) (w1 / aspectRatio);
   int h1 = (int) (w1 * aspectRatio);
   if (w1 < 4) w1 = 4;
   if (h1 < 4) h1 = 4;
   int w2 = (int) (w1 * ((double) srcRect.width / imageWidth));
   int h2 = (int) (h1 * ((double) srcRect.height / imageHeight));
   if (w2 < 1) w2 = 1;
   if (h2 < 1) h2 = 1;
   int x2 = (int) (w1 * ((double) srcRect.x / imageWidth));
   int y2 = (int) (h1 * ((double) srcRect.y / imageHeight));
   if (zoomIndicatorColor == null) zoomIndicatorColor = new Color(128, 128, 255);
   g.setColor(zoomIndicatorColor);
   ((Graphics2D) g).setStroke(Roi.onePixelWide);
   g.drawRect(x1, y1, w1, h1);
   if (w2 * h2 <= 200 || w2 < 10 || h2 < 10) g.fillRect(x1 + x2, y1 + y2, w2, h2);
   else g.drawRect(x1 + x2, y1 + y2, w2, h2);
 }
Example #6
0
 public void draw(Graphics g) {
   Color color = strokeColor != null ? strokeColor : ROIColor;
   if (fillColor != null) color = fillColor;
   g.setColor(color);
   mag = getMagnification();
   int sw = (int) (width * mag);
   int sh = (int) (height * mag);
   int sw2 = (int) (0.14645 * width * mag);
   int sh2 = (int) (0.14645 * height * mag);
   int sx1 = screenX(x);
   int sy1 = screenY(y);
   int sx2 = sx1 + sw / 2;
   int sy2 = sy1 + sh / 2;
   int sx3 = sx1 + sw;
   int sy3 = sy1 + sh;
   Graphics2D g2d = (Graphics2D) g;
   if (stroke != null) g2d.setStroke(getScaledStroke());
   if (fillColor != null) g.fillOval(sx1, sy1, sw, sh);
   else g.drawOval(sx1, sy1, sw, sh);
   if (state != CONSTRUCTING && clipboard == null && !overlay) {
     int size2 = HANDLE_SIZE / 2;
     drawHandle(g, sx1 + sw2 - size2, sy1 + sh2 - size2);
     drawHandle(g, sx3 - sw2 - size2, sy1 + sh2 - size2);
     drawHandle(g, sx3 - sw2 - size2, sy3 - sh2 - size2);
     drawHandle(g, sx1 + sw2 - size2, sy3 - sh2 - size2);
     drawHandle(g, sx2 - size2, sy1 - size2);
     drawHandle(g, sx3 - size2, sy2 - size2);
     drawHandle(g, sx2 - size2, sy3 - size2);
     drawHandle(g, sx1 - size2, sy2 - size2);
   }
   drawPreviousRoi(g);
   if (updateFullWindow) {
     updateFullWindow = false;
     imp.draw();
   }
   if (state != NORMAL) showStatus();
 }
 void initGraphics(Graphics g, Color textColor, Color defaultColor) {
   if (smallFont == null) {
     smallFont = new Font("SansSerif", Font.PLAIN, 9);
     largeFont = new Font("SansSerif", Font.PLAIN, 12);
   }
   if (textColor != null) {
     labelColor = textColor;
     if (overlay != null && overlay.getDrawBackgrounds())
       bgColor =
           new Color(
               255 - labelColor.getRed(), 255 - labelColor.getGreen(), 255 - labelColor.getBlue());
     else bgColor = null;
   } else {
     int red = defaultColor.getRed();
     int green = defaultColor.getGreen();
     int blue = defaultColor.getBlue();
     if ((red + green + blue) / 3 < 128) labelColor = Color.white;
     else labelColor = Color.black;
     bgColor = defaultColor;
   }
   this.defaultColor = defaultColor;
   g.setColor(defaultColor);
 }
 public void paint(Graphics g) {
   Roi roi = imp.getRoi();
   if (roi != null || showAllROIs || overlay != null) {
     if (roi != null) roi.updatePaste();
     if (!IJ.isMacOSX() && imageWidth != 0) {
       paintDoubleBuffered(g);
       return;
     }
   }
   try {
     if (imageUpdated) {
       imageUpdated = false;
       imp.updateImage();
     }
     Java2.setBilinearInterpolation(g, Prefs.interpolateScaledImages);
     Image img = imp.getImage();
     if (img != null)
       g.drawImage(
           img,
           0,
           0,
           (int) (srcRect.width * magnification),
           (int) (srcRect.height * magnification),
           srcRect.x,
           srcRect.y,
           srcRect.x + srcRect.width,
           srcRect.y + srcRect.height,
           null);
     if (overlay != null) drawOverlay(g);
     if (showAllROIs) drawAllROIs(g);
     if (roi != null) drawRoi(roi, g);
     if (srcRect.width < imageWidth || srcRect.height < imageHeight) drawZoomIndicator(g);
     if (IJ.debugMode) showFrameRate(g);
   } catch (OutOfMemoryError e) {
     IJ.outOfMemory("Paint");
   }
 }
Example #9
0
 public void draw(Graphics g) {
   updatePolygon();
   Color color = strokeColor != null ? strokeColor : ROIColor;
   boolean hasHandles = xSpline != null || type == POLYGON || type == POLYLINE || type == ANGLE;
   boolean isActiveOverlayRoi = !overlay && isActiveOverlayRoi();
   if (isActiveOverlayRoi && !hasHandles) {
     if (color == Color.cyan) color = Color.magenta;
     else color = Color.cyan;
   }
   boolean fill = false;
   mag = getMagnification();
   if (fillColor != null && !isLine() && state != CONSTRUCTING) {
     color = fillColor;
     fill = true;
   }
   g.setColor(color);
   Graphics2D g2d = (Graphics2D) g;
   if (stroke != null && !isActiveOverlayRoi) g2d.setStroke(getScaledStroke());
   if (xSpline != null) {
     if (type == POLYLINE || type == FREELINE) {
       drawSpline(g, xSpline, ySpline, splinePoints, false, fill, isActiveOverlayRoi);
       if (wideLine && !overlay) {
         g2d.setStroke(onePixelWide);
         g.setColor(getColor());
         drawSpline(g, xSpline, ySpline, splinePoints, false, fill, isActiveOverlayRoi);
       }
     } else drawSpline(g, xSpline, ySpline, splinePoints, true, fill, isActiveOverlayRoi);
   } else {
     if (type == POLYLINE || type == FREELINE || type == ANGLE || state == CONSTRUCTING) {
       g.drawPolyline(xp2, yp2, nPoints);
       if (wideLine && !overlay) {
         g2d.setStroke(onePixelWide);
         g.setColor(getColor());
         g.drawPolyline(xp2, yp2, nPoints);
       }
     } else {
       if (fill) {
         if (isActiveOverlayRoi) {
           g.setColor(Color.cyan);
           g.drawPolygon(xp2, yp2, nPoints);
         } else g.fillPolygon(xp2, yp2, nPoints);
       } else g.drawPolygon(xp2, yp2, nPoints);
     }
     if (state == CONSTRUCTING && type != FREEROI && type != FREELINE) drawStartBox(g);
   }
   if (hasHandles && state != CONSTRUCTING && clipboard == null && !overlay) {
     int size2 = HANDLE_SIZE / 2;
     if (activeHandle > 0)
       drawHandle(g, xp2[activeHandle - 1] - size2, yp2[activeHandle - 1] - size2);
     if (activeHandle < nPoints - 1)
       drawHandle(g, xp2[activeHandle + 1] - size2, yp2[activeHandle + 1] - size2);
     handleColor = strokeColor != null ? strokeColor : ROIColor;
     drawHandle(g, xp2[0] - size2, yp2[0] - size2);
     handleColor = Color.white;
     for (int i = 1; i < nPoints; i++) drawHandle(g, xp2[i] - size2, yp2[i] - size2);
   }
   drawPreviousRoi(g);
   if (!(state == MOVING_HANDLE || state == CONSTRUCTING || state == NORMAL)) showStatus();
   if (updateFullWindow) {
     updateFullWindow = false;
     imp.draw();
   }
 }
Example #10
0
 private void drawStartBox(Graphics g) {
   if (type != ANGLE) {
     double offset = getOffset(0.5);
     g.drawRect(ic.screenXD(startXD + offset) - 4, ic.screenYD(startYD + offset) - 4, 8, 8);
   }
 }
Example #11
0
 public void paint(Graphics g) {
   if (histogram != null) {
     if (os == null) {
       os = createImage(WIDTH, HEIGHT);
       osg = os.getGraphics();
       osg.setColor(Color.white);
       osg.fillRect(0, 0, WIDTH, HEIGHT);
       osg.setColor(Color.gray);
       for (int i = 0; i < WIDTH; i++) {
         if (hColors != null) osg.setColor(hColors[i]);
         osg.drawLine(i, HEIGHT, i, HEIGHT - ((int) (HEIGHT * histogram[i]) / hmax) - 4);
       }
       osg.dispose();
     }
     g.drawImage(os, 0, 0, this);
   } else {
     g.setColor(Color.white);
     g.fillRect(0, 0, WIDTH, HEIGHT);
   }
   g.setColor(Color.black);
   g.drawLine(0, HEIGHT - 4, 256, HEIGHT - 4);
   g.drawRect(0, 0, WIDTH, HEIGHT);
   g.drawRect((int) minHue, 1, (int) (maxHue - minHue), HEIGHT - 5);
 }