List of usage examples for java.awt Graphics2D drawRect
public void drawRect(int x, int y, int width, int height)
From source file:components.SizeDisplayer.java
protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); //copy g Dimension minSize = getMinimumSize(); Dimension prefSize = getPreferredSize(); Dimension size = getSize();/*from w w w .ja va 2 s . c o m*/ int prefX = 0, prefY = 0; //Set hints so text looks nice. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); //Draw the maximum size rectangle if we're opaque. if (isOpaque()) { g2d.setColor(getBackground()); g2d.fillRect(0, 0, size.width, size.height); } //Draw the icon. if (icon != null) { Composite oldComposite = g2d.getComposite(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f)); icon.paintIcon(this, g2d, (size.width - icon.getIconWidth()) / 2, (size.height - icon.getIconHeight()) / 2); g2d.setComposite(oldComposite); } //Draw the preferred size rectangle. prefX = (size.width - prefSize.width) / 2; prefY = (size.height - prefSize.height) / 2; g2d.setColor(Color.RED); g2d.drawRect(prefX, prefY, prefSize.width - 1, prefSize.height - 1); //Draw the minimum size rectangle. if (minSize.width != prefSize.width || minSize.height != prefSize.height) { int minX = (size.width - minSize.width) / 2; int minY = (size.height - minSize.height) / 2; g2d.setColor(Color.CYAN); g2d.drawRect(minX, minY, minSize.width - 1, minSize.height - 1); } //Draw the text. if (text != null) { Dimension textSize = getTextSize(g2d); g2d.setColor(getForeground()); g2d.drawString(text, (size.width - textSize.width) / 2, (size.height - textSize.height) / 2 + g2d.getFontMetrics().getAscent()); } g2d.dispose(); }
From source file:io.github.karols.hocr4j.PageRenderer.java
/** * Renders this page on the given image. * The image is modified, not copied.//from w w w . j av a 2s .c o m * * @param page page to render */ public void renderOnTop(@Nonnull Page page, @Nonnull BufferedImage img) { Graphics2D g = (Graphics2D) img.getGraphics(); g.setColor(Color.RED); for (Area a : page) { for (Paragraph p : a) { for (Line l : p) { for (Word w : l.words) { if (w.isBold()) { if (w.isItalic()) { g.setFont(boldItalicFont); } else { g.setFont(boldFont); } } else if (w.isItalic()) { g.setFont(italicFont); } else { g.setFont(plainFont); } Bounds b = w.getBounds().scale(scale); g.drawString(w.getText(), b.getLeft(), b.getBottom()); } } } } g.setStroke(new BasicStroke(strokeWidth)); g.setColor(defaultRectangleColor); for (Bounds rect : rectanglesToDraw) { if (rect != null) { Bounds b = rect.scale(scale); g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight()); } } for (Pair<Color, Bounds> rect : coloredRectanglesToDraw) { if (rect != null) { g.setColor(rect.getLeft()); Bounds b = rect.getRight().scale(scale); g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight()); } } }
From source file:org.uva.itast.blended.omr.scanners.SolidSquareMarkScanner.java
/** * @param pageImage/*from w w w .j a v a 2 s . c o m*/ */ public void putEmphasisMarkOnImage(PageImage pageImage, Color color) { Graphics2D g = pageImage.getReportingGraphics(); // int centerColor=imagen.getRGB(maxsimX, maxsimY); // g.setXORMode(new Color(centerColor)); // g.setColor(Color.RED); // g.fillOval(maxsimX - markWidth/2, maxsimY - markHeight/2, markWidth, // markHeight); // g.setPaintMode(); Dimension2D markDimsPx = pageImage.sizeInPixels(new Size(markWidth, markHeight)); int markWidth = (int) markDimsPx.getWidth(); int markHeight = (int) markDimsPx.getHeight(); g.setColor(color); AffineTransform t = g.getTransform(); g.drawLine(maxsimX, maxsimY - markHeight / 2 - 1, maxsimX, maxsimY - markHeight / 2 - (int) (20 / t.getScaleY())); Polygon arrowHead = new Polygon(); arrowHead.addPoint(maxsimX, (int) (maxsimY - markHeight / 2 - 1 / t.getScaleY())); arrowHead.addPoint((int) (maxsimX - 6 / t.getScaleX()), (int) (maxsimY - markHeight / 2 - 6 / t.getScaleY())); arrowHead.addPoint((int) (maxsimX + 6 / t.getScaleX()), (int) (maxsimY - markHeight / 2 - 6 / t.getScaleY())); g.fillPolygon(arrowHead); g.setStroke(new BasicStroke(2, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1, new float[] { (float) (3 / t.getScaleX()), (float) (3 / t.getScaleY()) }, 0)); g.drawRect(maxsimX - markWidth / 2 - 1, maxsimY - markHeight / 2 - 1, markWidth + 1, markHeight + 1); }
From source file:de.fhg.igd.iva.explorer.main.JStatBar.java
@Override protected void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; int width = getWidth() - 1; int height = getHeight() - 1; int whiskerSize = 6; // fill background rect g.setColor(Color.WHITE);// w ww. j a v a2 s.c om g.fillRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY); int q10X = mapXToScreen(stats.getPercentile(10.0), width); int q25X = mapXToScreen(stats.getPercentile(25.0), width); int q50X = mapXToScreen(stats.getPercentile(50.0), width); int q75X = mapXToScreen(stats.getPercentile(75.0), width); int q90X = mapXToScreen(stats.getPercentile(90.0), width); g.setColor(Color.PINK); int leftX = Math.min(q25X, q75X); int rightX = Math.max(q25X, q75X); g.fillRect(insetX + leftX, insetY + 1, rightX - leftX, height - 2 * insetY - 1); g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2); g.drawLine(insetX + q10X, (height - whiskerSize) / 2, insetX + q10X, (height + whiskerSize) / 2); g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2); g.drawLine(insetX + q90X, (height - whiskerSize) / 2, insetX + q90X, (height + whiskerSize) / 2); g.setColor(Color.RED); g.drawLine(insetX + q50X, insetY + 1, insetX + q50X, height - insetY - 1); // draw outline border rect g.setColor(Color.BLACK); g.drawRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY); int buttonX = mapXToScreen(quality, width); drawButton(g, buttonX, height); }
From source file:vteaexploration.plottools.panels.XYExplorationPanel.java
public void makeOverlayImage(ArrayList gates, int x, int y, int xAxis, int yAxis) { //convert gate to chart x,y path Gate gate;//ww w.j a v a2 s . c o m ListIterator<Gate> gate_itr = gates.listIterator(); //.get int total = 0; int gated = 0; int selected = 0; int gatedSelected = 0; int gatecount = gates.size(); while (gate_itr.hasNext()) { gate = gate_itr.next(); if (gate.getSelected()) { Path2D path = gate.createPath2DInChartSpace(); ArrayList<MicroObject> result = new ArrayList<MicroObject>(); ArrayList<MicroObject> volumes = (ArrayList) this.plotvalues.get(1); MicroObjectModel volume; double xValue = 0; double yValue = 0; ListIterator<MicroObject> it = volumes.listIterator(); try { while (it.hasNext()) { volume = it.next(); if (volume != null) { xValue = ((Number) processPosition(xAxis, (MicroObject) volume)).doubleValue(); yValue = ((Number) processPosition(yAxis, (MicroObject) volume)).doubleValue(); if (path.contains(xValue, yValue)) { result.add((MicroObject) volume); } } } } catch (NullPointerException e) { } ; Overlay overlay = new Overlay(); int count = 0; BufferedImage placeholder = new BufferedImage(impoverlay.getWidth(), impoverlay.getHeight(), BufferedImage.TYPE_INT_ARGB); ImageStack gateOverlay = new ImageStack(impoverlay.getWidth(), impoverlay.getHeight()); selected = getSelectedObjects(); total = volumes.size(); gated = getGatedObjects(impoverlay); gatedSelected = getGatedSelected(impoverlay); for (int i = 0; i <= impoverlay.getNSlices(); i++) { BufferedImage selections = new BufferedImage(impoverlay.getWidth(), impoverlay.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = selections.createGraphics(); ImageRoi ir = new ImageRoi(0, 0, placeholder); ListIterator<MicroObject> vitr = result.listIterator(); while (vitr.hasNext()) { try { MicroObject vol = (MicroObject) vitr.next(); int[] x_pixels = vol.getXPixelsInRegion(i); int[] y_pixels = vol.getYPixelsInRegion(i); for (int c = 0; c < x_pixels.length; c++) { g2.setColor(gate.getColor()); g2.drawRect(x_pixels[c], y_pixels[c], 1, 1); } ir = new ImageRoi(0, 0, selections); count++; } catch (NullPointerException e) { } } ir.setPosition(i); ir.setOpacity(0.4); overlay.add(ir); gateOverlay.addSlice(ir.getProcessor()); java.awt.Font f = new Font("Arial", Font.BOLD, 12); BigDecimal percentage = new BigDecimal(selected); BigDecimal totalBD = new BigDecimal(total); percentage = percentage.divide(totalBD, 4, BigDecimal.ROUND_UP); BigDecimal percentageGated = new BigDecimal(gated); BigDecimal totalGatedBD = new BigDecimal(total); percentageGated = percentageGated.divide(totalGatedBD, 4, BigDecimal.ROUND_UP); BigDecimal percentageGatedSelected = new BigDecimal(gatedSelected); BigDecimal totalGatedSelectedBD = new BigDecimal(total); percentageGatedSelected = percentageGatedSelected.divide(totalGatedSelectedBD, 4, BigDecimal.ROUND_UP); // System.out.println("PROFILING: gate fraction: " + percentage.toString()); if (impoverlay.getWidth() > 256) { TextRoi textTotal = new TextRoi(5, 10, selected + "/" + total + " gated (" + 100 * percentage.doubleValue() + "%)"); if (gated > 0) { textTotal = new TextRoi(5, 10, selected + "/" + total + " total (" + 100 * percentage.doubleValue() + "%)" + "; " + gated + "/" + total + " roi (" + 100 * percentageGated.doubleValue() + "%)" + "; " + gatedSelected + "/" + total + " overlap (" + 100 * percentageGatedSelected.doubleValue() + "%)", f); } //TextRoi textImageGated = new TextRoi(5, 18, selected + "/" + total + " gated objects (" + 100 * percentage.doubleValue() + "%)", f); textTotal.setPosition(i); //textImageGated.setPosition(i); overlay.add(textTotal); } else { f = new Font("Arial", Font.PLAIN, 10); TextRoi line1 = new TextRoi(5, 5, selected + "/" + total + " gated" + "(" + 100 * percentage.doubleValue() + "%)", f); overlay.add(line1); if (gated > 0) { f = new Font("Arial", Font.PLAIN, 10); TextRoi line2 = new TextRoi(5, 18, gated + "/" + total + " roi (" + 100 * percentageGated.doubleValue() + "%)", f); overlay.add(line2); TextRoi line3 = new TextRoi(5, 31, gatedSelected + "/" + total + " overlap (" + 100 * percentageGatedSelected.doubleValue() + "%)", f); overlay.add(line3); } line1.setPosition(i); } } impoverlay.setOverlay(overlay); //ImagePlus gateMaskImage = new ImagePlus("gates", gateOverlay); //gateMaskImage.show(); gate.setGateOverlayStack(gateOverlay); } impoverlay.draw(); impoverlay.setTitle(this.getTitle()); if (impoverlay.getDisplayMode() != IJ.COMPOSITE) { impoverlay.setDisplayMode(IJ.COMPOSITE); } if (impoverlay.getSlice() == 1) { impoverlay.setZ(Math.round(impoverlay.getNSlices() / 2)); } else { impoverlay.setSlice(impoverlay.getSlice()); } impoverlay.show(); } }
From source file:org.broad.igv.variant.VariantTrack.java
/** * Render the features in the supplied rectangle. * * @param context/*ww w . ja va 2 s . c om*/ * @param trackRectangle * @param packedFeatures */ @Override protected void renderFeatureImpl(RenderContext context, Rectangle trackRectangle, PackedFeatures packedFeatures) { Graphics2D g2D = context.getGraphics(); top = trackRectangle.y; Rectangle visibleRectangle = context.getVisibleRect(); // A disposable rect -- note this gets modified all over the place, bad practice Rectangle tmpRect = new Rectangle(trackRectangle); tmpRect.height = getGenotypeBandHeight(); tmpRect.y = trackRectangle.y; Rectangle bandRect = new Rectangle(tmpRect); bandRect.y += getVariantsHeight(); drawBackground(g2D, bandRect, visibleRectangle, BackgroundType.DATA); List<PackedFeatures.FeatureRow> rows = packedFeatures.getRows(); int overallFeatureRectHeight = getVariantsHeight(); int overallSampleRectHeight = trackRectangle.height - overallFeatureRectHeight; Rectangle overallSampleRect = new Rectangle(trackRectangle.x, top + overallFeatureRectHeight, trackRectangle.width, overallSampleRectHeight); int curRowTop = top; if (rows.size() > 0) { final double locScale = context.getScale(); final double origin = context.getOrigin(); final double pXMin = tmpRect.getMinX(); final double pXMax = tmpRect.getMaxX(); tmpRect.height = variantBandHeight; int lastEndX = -1; int minSpacing = 3; for (PackedFeatures.FeatureRow row : rows) { List<Feature> features = row.getFeatures(); for (Feature feature : features) { Variant variant = (Variant) feature; if (hideFiltered && variant.isFiltered()) { continue; } int start = variant.getStart(); int end = variant.getEnd(); int pX = (int) ((start - origin) / locScale); int dX = (int) Math.max(2, (end - start) / locScale); if (pX + dX < pXMin) { continue; } if (pX > pXMax) { break; } int w = dX; int x = pX; if (w < 3) { w = 3; x--; } //Make sure we have some whitespace between this //feature and the previous one, but only if they don't //actually overlap and the current size is reasonably large int spacing = x - lastEndX; if (spacing > 0 && spacing < minSpacing && w > 2 * minSpacing) { x += minSpacing - spacing; } tmpRect.y = curRowTop; if (tmpRect.intersects(visibleRectangle)) { renderer.renderSiteBand(variant, tmpRect, x, w, context); lastEndX = x + w - 1; } renderSamples(g2D, visibleRectangle, variant, context, overallSampleRect, x, w); boolean isSelected = selectedVariant != null && selectedVariant == variant; if (isSelected) { Graphics2D selectionGraphics = context.getGraphic2DForColor(Color.black); selectionGraphics.drawRect(x, curRowTop, w, getHeight()); } } if (areFeaturesStacked()) { curRowTop += variantBandHeight; lastEndX = -1; } } } else { tmpRect.height = variantBandHeight; tmpRect.y = trackRectangle.y; g2D.setColor(Color.gray); GraphicUtils.drawCenteredText("No Variants Found", trackRectangle, g2D); } renderBoundaryLines(g2D, trackRectangle, visibleRectangle); }
From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java
private byte[] generateNoDataChart(int width, int height) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.setBackground(parseColor(M_sm.getChartBackgroundColor())); g2d.clearRect(0, 0, width - 1, height - 1); g2d.setColor(parseColor("#cccccc")); g2d.drawRect(0, 0, width - 1, height - 1); Font f = new Font("SansSerif", Font.PLAIN, 12); g2d.setFont(f);/*from w w w .j a v a2s.c o m*/ FontMetrics fm = g2d.getFontMetrics(f); String noData = msgs.getString("no_data"); int noDataWidth = fm.stringWidth(noData); int noDataHeight = fm.getHeight(); g2d.setColor(parseColor("#555555")); g2d.drawString(noData, width / 2 - noDataWidth / 2, height / 2 - noDataHeight / 2 + 2); final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(img, "png", out); } catch (IOException e) { LOG.warn("Error occurred while generating SiteStats chart image data", e); } return out.toByteArray(); }
From source file:org.sakaiproject.sitestats.impl.ServerWideReportManagerImpl.java
private byte[] generateNoDataChart(int width, int height) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.setBackground(parseColor(statsManager.getChartBackgroundColor())); g2d.clearRect(0, 0, width - 1, height - 1); g2d.setColor(parseColor("#cccccc")); g2d.drawRect(0, 0, width - 1, height - 1); Font f = new Font("SansSerif", Font.PLAIN, 12); g2d.setFont(f);/*from w w w . j a v a 2 s .c om*/ FontMetrics fm = g2d.getFontMetrics(f); String noData = msgs.getString("no_data"); int noDataWidth = fm.stringWidth(noData); int noDataHeight = fm.getHeight(); g2d.setColor(parseColor("#555555")); g2d.drawString(noData, width / 2 - noDataWidth / 2, height / 2 - noDataHeight / 2 + 2); final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(img, "png", out); } catch (IOException e) { log.warn("Error occurred while generating SiteStats chart image data", e); } return out.toByteArray(); }
From source file:edu.umn.cs.spatialHadoop.operations.Plot.java
public static void drawShape(Graphics2D graphics, Shape s, Rectangle fileMbr, int imageWidth, int imageHeight, double scale) { if (s instanceof NASAPoint) { final int MinValue = 7500; final int MaxValue = 16000; NASAPoint pt = (NASAPoint) s;/* w w w . j a v a2s. co m*/ int x = (int) ((pt.x - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int y = (int) ((pt.y - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); int value = pt.value; if (value < min_value && value > 1000) min_value = value; if (value > max_value) max_value = value; if (value > 0 && x >= 0 && x < imageWidth && y >= 0 && y < imageHeight) { Color color; if (value < MinValue) { color = Color.BLACK; } else if (value < MaxValue) { float ratio = 0.78f - 0.78f * (value - MinValue) / (MaxValue - MinValue); color = Color.getHSBColor(ratio, 0.5f, 1.0f); } else { color = Color.WHITE; } graphics.setColor(color); graphics.fillRect(x, y, 1, 1); } } else if (s instanceof Point) { Point pt = (Point) s; int x = (int) ((pt.x - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int y = (int) ((pt.y - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); if (x >= 0 && x < imageWidth && y >= 0 && y < imageHeight) graphics.fillRect(x, y, 1, 1); } else if (s instanceof Rectangle) { Rectangle r = (Rectangle) s; int s_x1 = (int) ((r.x1 - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int s_y1 = (int) ((r.y1 - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); int s_x2 = (int) (((r.x2) - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int s_y2 = (int) (((r.y2) - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); graphics.drawRect(s_x1, s_y1, s_x2 - s_x1 + 1, s_y2 - s_y1 + 1); } else if (s instanceof OGCShape) { OGCShape ogc_shape = (OGCShape) s; OGCGeometry geom = ogc_shape.geom; Color shape_color = graphics.getColor(); if (geom instanceof OGCGeometryCollection) { OGCGeometryCollection geom_coll = (OGCGeometryCollection) geom; for (int i = 0; i < geom_coll.numGeometries(); i++) { OGCGeometry sub_geom = geom_coll.geometryN(i); // Recursive call to draw each geometry drawShape(graphics, new OGCShape(sub_geom), fileMbr, imageWidth, imageHeight, scale); } } else if (geom.getEsriGeometry() instanceof MultiPath) { MultiPath path = (MultiPath) geom.getEsriGeometry(); double sub_geom_alpha = path.calculateLength2D() * scale; int color_alpha = sub_geom_alpha > 1.0 ? 255 : (int) Math.round(sub_geom_alpha * 255); if (color_alpha == 0) return; int[] xpoints = new int[path.getPointCount()]; int[] ypoints = new int[path.getPointCount()]; for (int i = 0; i < path.getPointCount(); i++) { double px = path.getPoint(i).getX(); double py = path.getPoint(i).getY(); // Transform a point in the polygon to image coordinates xpoints[i] = (int) Math.round((px - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); ypoints[i] = (int) Math.round((py - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); } // Draw the polygon graphics.setColor(new Color((shape_color.getRGB() & 0x00FFFFFF) | (color_alpha << 24), true)); if (path instanceof Polygon) graphics.drawPolygon(xpoints, ypoints, path.getPointCount()); else if (path instanceof Polyline) graphics.drawPolyline(xpoints, ypoints, path.getPointCount()); } } else if (s instanceof JTSShape) { JTSShape jts_shape = (JTSShape) s; Geometry geom = jts_shape.geom; Color shape_color = graphics.getColor(); drawJTSShape(graphics, geom, fileMbr, imageWidth, imageHeight, scale, shape_color); } else { LOG.warn("Cannot draw a shape of type: " + s.getClass()); Rectangle r = s.getMBR(); int s_x1 = (int) ((r.x1 - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int s_y1 = (int) ((r.y1 - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); int s_x2 = (int) (((r.x2) - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1)); int s_y2 = (int) (((r.y2) - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1)); if (s_x1 >= 0 && s_x1 < imageWidth && s_y1 >= 0 && s_y1 < imageHeight) graphics.drawRect(s_x1, s_y1, s_x2 - s_x1 + 1, s_y2 - s_y1 + 1); } }
From source file:org.kalypso.kalypso1d2d.pjt.map.HydrographManagementWidget.java
private void paintHydrographInMap(final Graphics g) { final Graphics2D g2 = (Graphics2D) g; final GM_Point point = (GM_Point) m_selectedHydrograph.getLocation(); final IMapPanel mapPanel = getMapPanel(); if (mapPanel == null) return;/*from w w w. j ava 2 s . c o m*/ final GeoTransform projection = mapPanel.getProjection(); if (projection == null || point == null) return; final int x = (int) projection.getDestX(point.getX()); final int y = (int) projection.getDestY(point.getY()); final int sizeOuter = 16; final Color defaultColor = g2.getColor(); final Color color = new Color(255, 30, 30); g2.setColor(color); final Stroke defaultStroke = g2.getStroke(); final BasicStroke stroke = new BasicStroke(3); g2.setStroke(stroke); g2.drawRect(x - sizeOuter / 2, y - sizeOuter / 2, sizeOuter, sizeOuter); g2.setColor(defaultColor); g2.setStroke(defaultStroke); }