List of usage examples for java.awt Graphics2D setColor
public abstract void setColor(Color c);
From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java
@Override protected void paintComponent(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; Rectangle rect = getInternalBounds(); int width = rect.width; int height = rect.height; if (useBGImage) { // Create a translucent intermediate image in which we can perform // the soft clipping GraphicsConfiguration gc = g.getDeviceConfiguration(); if (img == null || img.getWidth() != width || img.getHeight() != height) { img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); }/*from ww w . j a v a 2 s. com*/ Graphics2D g2 = img.createGraphics(); // Clear the image so all pixels have zero alpha g2.setComposite(AlphaComposite.Clear); g2.fillRect(0, 0, width, height); g2.setComposite(AlphaComposite.Src); g2.setColor(new Color(0, 0, 0, 85)); g2.fillRect(0, 0, width, height); if (delegateRenderer != null) { delegateRenderer.render(g, g2, img); } g2.dispose(); // Copy our intermediate image to the screen g.drawImage(img, rect.x, rect.y, null); } super.paintComponent(graphics); if (StringUtils.isNotEmpty(text)) { Graphics2D g2 = (Graphics2D) graphics; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(fillColor); g2.fillRect(margin.left, margin.top, rect.width, rect.height); g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize)); FontMetrics fm = g2.getFontMetrics(); int tw = fm.stringWidth(text); int th = fm.getHeight(); int tx = (rect.width - tw) / 2; int ty = (rect.height - th) / 2; if (yPos != null) { ty = yPos; } int expand = 20; int arc = expand * 2; g2.setColor(new Color(0, 0, 0, 50)); int x = margin.left + tx - (expand / 2); int y = margin.top + ty - fm.getAscent() - (expand / 2); drawBGContainer(g2, true, x + 4, y + 6, tw + expand, th + expand, arc, arc); g2.setColor(new Color(255, 255, 255, 220)); drawBGContainer(g2, true, x, y, tw + expand, th + expand, arc, arc); g2.setColor(Color.DARK_GRAY); drawBGContainer(g2, false, x, y, tw + expand, th + expand, arc, arc); g2.setColor(textColor == null ? Color.BLACK : textColor); g2.drawString(text, tx, ty); } }
From source file:FontPaint.java
public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white);//from w w w . j av a 2s .co m int width = getSize().width; int height = getSize().height; Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica", 1, 60); String s = new String("Java Source and Support."); TextLayout textTl = new TextLayout(s, f, frc); AffineTransform transform = new AffineTransform(); Shape outline = textTl.getOutline(null); Rectangle outlineBounds = outline.getBounds(); transform = g2.getTransform(); transform.translate(width / 2 - (outlineBounds.width / 2), height / 2 + (outlineBounds.height / 2)); g2.transform(transform); g2.setColor(Color.blue); g2.draw(outline); g2.setClip(outline); }
From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java
/** * Draws the visual representation of a single wind arrow. *//* w w w. ja v a 2 s .c o m*/ @Override public void drawItem(Graphics2D g2d, XYItemRendererState state, Rectangle2D plotArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { resizeArrowByPlotHeight((int) plotArea.getHeight()); // Needs a new graphics object to use translate() and rotate() Graphics2D g2 = (Graphics2D) g2d.create(); g2.setRenderingHints(renderHints); RectangleEdge domainAxisLocation = plot.getDomainAxisEdge(); double middleY = plotArea.getCenterY(); WindDataset windData = (WindDataset) dataset; Number x = windData.getX(series, item); Number windDir = windData.getWindDirection(series, item); Number wforce = windData.getWindForce(series, item); double middleX = domainAxis.valueToJava2D(x.doubleValue(), plotArea, domainAxisLocation); g2.translate((int) middleX, (int) middleY); g2.setColor(Color.BLACK); if (wforce.doubleValue() <= zeroWindLimit) { drawCircle(g2); } else { g2.rotate(Math.toRadians(windDir.doubleValue() - 180)); drawArrow(g2, wforce.doubleValue()); if (useArrowHead) { g2.fill(getPolygonHead(arrowHeadSize, arrowHeight)); } else { g2.draw(getCircleHead(arrowHeadSize, arrowHeight)); } } }
From source file:com.simiacryptus.mindseye.applications.ArtistryUtil.java
/** * Paint circles./*from w w w . j a v a 2 s.c o m*/ * * @param canvas the canvas * @param scale the scale */ public static void paint_Circles(final Tensor canvas, final int scale) { BufferedImage originalImage = canvas.toImage(); BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = (Graphics2D) newImage.getGraphics(); IntStream.range(0, 10000).forEach(i -> { Random random = new Random(); int positionX = random.nextInt(originalImage.getWidth()); int positionY = random.nextInt(originalImage.getHeight()); int width = 1 + random.nextInt(2 * scale); int height = 1 + random.nextInt(2 * scale); DoubleStatistics[] stats = { new DoubleStatistics(), new DoubleStatistics(), new DoubleStatistics() }; canvas.coordStream(false).filter(c -> { int[] coords = c.getCoords(); int x = coords[0]; int y = coords[1]; double relX = Math.pow(1 - 2 * ((double) (x - positionX) / width), 2); double relY = Math.pow(1 - 2 * ((double) (y - positionY) / height), 2); return relX + relY < 1.0; }).forEach(c -> stats[c.getCoords()[2]].accept(canvas.get(c))); graphics.setStroke(new Stroke() { @Override public Shape createStrokedShape(final Shape p) { return null; } }); graphics.setColor(new Color((int) stats[0].getAverage(), (int) stats[1].getAverage(), (int) stats[2].getAverage())); graphics.fillOval(positionX, positionY, width, height); }); canvas.set(Tensor.fromRGB(newImage)); }
From source file:ucar.unidata.idv.control.chart.WayPoint.java
/** * Draws the annotation.// w w w . ja v a 2s .c o m * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param rendererIndex the renderer index. * @param info an optional info object that will be populated with * entity information. */ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { super.setGraphicsState(g2); if (!getPlotWrapper().okToDraw(this)) { return; } g2.setStroke(new BasicStroke()); if (false && getSelected()) { g2.setColor(COLOR_SELECTED); } else { g2.setColor(getColor()); } x = getXFromValue(dataArea, domainAxis); int width2 = (int) (ANNOTATION_WIDTH / 2); int bottom = (int) (dataArea.getY() + dataArea.getHeight()); y = bottom; int[] xs = { x - width2, x + width2, x, x - width2 }; int[] ys = { bottom - ANNOTATION_WIDTH, bottom - ANNOTATION_WIDTH, bottom, bottom - ANNOTATION_WIDTH }; g2.fillPolygon(xs, ys, xs.length); if ((getName() != null) && !isForAnimation) { FontMetrics fm = g2.getFontMetrics(); int width = fm.stringWidth(getName()); int textLeft = x - width / 2; g2.drawString(getName(), textLeft, bottom - ANNOTATION_WIDTH - 2); } if (getSelected()) { g2.setColor(COLOR_SELECTED); g2.drawPolygon(xs, ys, xs.length); } if (getPropertyListeners().hasListeners(PROP_WAYPOINTVALUE) || isForAnimation) { g2.setColor(Color.gray); g2.drawLine(x, y - ANNOTATION_WIDTH, x, (int) dataArea.getY()); } boolean playSound = canPlaySound(); if (isForAnimation) { if (clockImage == null) { clockImage = GuiUtils.getImage("/auxdata/ui/icons/clock.gif"); } if (playSound) { g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null); } else { g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null); } } if (canPlaySound()) { if (noteImage == null) { noteImage = GuiUtils.getImage("/auxdata/ui/icons/note.gif"); } if (isForAnimation) { g2.drawImage(noteImage, x + 8, (int) dataArea.getY() + 1, null); } else { g2.drawImage(noteImage, x, (int) dataArea.getY() + 1, null); } } if (minutesSpan > 0.0) { int left = (int) domainAxis.valueToJava2D(domainValue - (minutesSpan * 60000) / 2, dataArea, RectangleEdge.BOTTOM); int right = (int) domainAxis.valueToJava2D(domainValue + (minutesSpan * 60000) / 2, dataArea, RectangleEdge.BOTTOM); g2.setPaint(Color.black); g2.setStroke(new BasicStroke(2.0f)); g2.drawLine(left, y, right, y); } }
From source file:business.ImageManager.java
public void doDrawCircle(Graphics2D big, int x, int y, Color color) { //setup para os rastros big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); big.setStroke(new BasicStroke()); big.setColor(color); //draw on graph big.drawOval(x, x, y, y);//w w w .j ava 2s. com }
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;/* ww w .j a v a 2 s .c om*/ 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:au.org.ala.biocache.web.MapController.java
@Deprecated @RequestMapping(value = "/occurrences/wms", method = RequestMethod.GET) public void pointsWmsImage(SpatialSearchRequestParams requestParams, @RequestParam(value = "colourby", required = false, defaultValue = "0") Integer colourby, @RequestParam(value = "width", required = false, defaultValue = "256") Integer widthObj, @RequestParam(value = "height", required = false, defaultValue = "256") Integer heightObj, @RequestParam(value = "zoom", required = false, defaultValue = "0") Integer zoomLevel, @RequestParam(value = "symsize", required = false, defaultValue = "4") Integer symsize, @RequestParam(value = "symbol", required = false, defaultValue = "circle") String symbol, @RequestParam(value = "bbox", required = false, defaultValue = "110,-45,157,-9") String bboxString, @RequestParam(value = "type", required = false, defaultValue = "normal") String type, @RequestParam(value = "outline", required = true, defaultValue = "false") boolean outlinePoints, @RequestParam(value = "outlineColour", required = true, defaultValue = "0x000000") String outlineColour, HttpServletResponse response) throws Exception { // size of the circles int size = symsize.intValue(); int width = widthObj.intValue(); int height = heightObj.intValue(); requestParams.setStart(0);/*w w w.j a va 2 s.co m*/ requestParams.setPageSize(Integer.MAX_VALUE); String query = requestParams.getQ(); String[] filterQuery = requestParams.getFq(); if (StringUtils.isBlank(query) && StringUtils.isBlank(requestParams.getFormattedQuery())) { displayBlankImage(width, height, false, response); return; } // let's force it to PNG's for now response.setContentType("image/png"); // Convert array to list so we append more values onto it ArrayList<String> fqList = null; if (filterQuery != null) { fqList = new ArrayList<String>(Arrays.asList(filterQuery)); } else { fqList = new ArrayList<String>(); } // the bounding box double[] bbox = new double[4]; int i; i = 0; for (String s : bboxString.split(",")) { try { bbox[i] = Double.parseDouble(s); i++; } catch (Exception e) { logger.error(e.getMessage(), e); } } double pixelWidth = (bbox[2] - bbox[0]) / width; double pixelHeight = (bbox[3] - bbox[1]) / height; bbox[0] += pixelWidth / 2; bbox[2] -= pixelWidth / 2; bbox[1] += pixelHeight / 2; bbox[3] -= pixelHeight / 2; //offset for points bounding box by size double xoffset = (bbox[2] - bbox[0]) / (double) width * (size * 2); double yoffset = (bbox[3] - bbox[1]) / (double) height * (size * 2); //adjust offset for pixel height/width xoffset += pixelWidth; yoffset += pixelHeight; double[] bbox2 = new double[4]; bbox2[0] = convertMetersToLng(bbox[0] - xoffset); bbox2[1] = convertMetersToLat(bbox[1] - yoffset); bbox2[2] = convertMetersToLng(bbox[2] + xoffset); bbox2[3] = convertMetersToLat(bbox[3] + yoffset); bbox[0] = convertMetersToLng(bbox[0]); bbox[1] = convertMetersToLat(bbox[1]); bbox[2] = convertMetersToLng(bbox[2]); bbox[3] = convertMetersToLat(bbox[3]); double[] pbbox = new double[4]; //pixel bounding box pbbox[0] = convertLngToPixel(bbox[0]); pbbox[1] = convertLatToPixel(bbox[1]); pbbox[2] = convertLngToPixel(bbox[2]); pbbox[3] = convertLatToPixel(bbox[3]); String bboxString2 = bbox2[0] + "," + bbox2[1] + "," + bbox2[2] + "," + bbox2[3]; bboxToQuery(bboxString2, fqList); PointType pointType = getPointTypeForZoomLevel(zoomLevel); String[] newFilterQuery = (String[]) fqList.toArray(new String[fqList.size()]); // convert back to array requestParams.setFq(newFilterQuery); List<OccurrencePoint> points = searchDAO.getFacetPoints(requestParams, pointType); logger.debug("Points search for " + pointType.getLabel() + " - found: " + points.size()); if (points.size() == 0) { displayBlankImage(width, height, false, response); return; } BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) img.getGraphics(); g.setColor(Color.RED); int x, y; int pointWidth = size * 2; double width_mult = (width / (pbbox[2] - pbbox[0])); double height_mult = (height / (pbbox[1] - pbbox[3])); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Color oColour = Color.decode(outlineColour); for (i = 0; i < points.size(); i++) { OccurrencePoint pt = points.get(i); float lng = pt.getCoordinates().get(0).floatValue(); float lat = pt.getCoordinates().get(1).floatValue(); x = (int) ((convertLngToPixel(lng) - pbbox[0]) * width_mult); y = (int) ((convertLatToPixel(lat) - pbbox[3]) * height_mult); if (colourby != null) { int colour = 0xFF000000 | colourby.intValue(); Color c = new Color(colour); g.setPaint(c); } else { g.setPaint(Color.blue); } // g.fillOval(x - (size / 2), y - (size / 2), pointWidth, pointWidth); Shape shp = getShape(symbol, x - (size / 2), y - (size / 2), pointWidth, pointWidth); g.draw(shp); g.fill(shp); if (outlinePoints) { g.setPaint(oColour); g.drawOval(x - (size / 2), y - (size / 2), pointWidth, pointWidth); } } g.dispose(); try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(img, "png", outputStream); ServletOutputStream outStream = response.getOutputStream(); outStream.write(outputStream.toByteArray()); outStream.flush(); outStream.close(); } catch (Exception e) { logger.error("Unable to write image", e); } }
From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java
@Override public void paintComponent(Graphics g) { super.paintComponent(g); if (mTimeFrame == null) { renderNoInfoView(g);/*from w ww. ja v a 2 s.c o m*/ } else { updateSize(); LongRange startRange = mTimeFrame.getStartRange(); LongRange arriveRange = mTimeFrame.getArriveRange(); HashMap<LongRange, TimeSpan> startRanges = mTimeFrame .startTimespansToRangesMap(AnyTribe.getSingleton()); HashMap<LongRange, TimeSpan> arriveRanges = mTimeFrame.arriveTimespansToRangesMap(null); long minValue = startRange.getMinimumLong(); long maxValue = arriveRange.getMaximumLong(); Graphics2D g2d = (Graphics2D) g; g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3))); g2d.fillRect(0, 0, getWidth(), getHeight()); //draw frame around the entire range renderRange(new LongRange(startRange.getMinimumLong(), arriveRange.getMaximumLong()), startRange, arriveRange, false, false, g2d, null, popupInfo); g2d.setColor(Constants.DS_BACK); popupInfo.clear(); //fill start range renderRange(startRange, startRange, arriveRange, true, false, g2d, null, popupInfo); //fill arrive range renderRange(arriveRange, startRange, arriveRange, false, true, g2d, null, popupInfo); Paint p = g2d.getPaint(); Iterator<LongRange> rangeKeys = startRanges.keySet().iterator(); while (rangeKeys.hasNext()) { LongRange currentRange = rangeKeys.next(); TimeSpan spanForRange = startRanges.get(currentRange); if (spanForRange != null) { if (spanForRange.isValidAtEveryDay()) { g2d.setPaint(new TexturePaint(DAILY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3))); } else if (spanForRange.isValidAtExactTime()) { g2d.setPaint(new TexturePaint(EXACT_START_FRAME_FILL, new Rectangle(0, 0, 3, 3))); } else { g2d.setPaint(new TexturePaint(ONE_DAY_START_FRAME_FILL, new Rectangle(0, 0, 3, 3))); } } renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo); } Composite c = g2d.getComposite(); rangeKeys = arriveRanges.keySet().iterator(); while (rangeKeys.hasNext()) { LongRange currentRange = rangeKeys.next(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); g2d.setPaint(new TexturePaint(ARRIVE_FRAME_FILL, new Rectangle(0, 0, 3, 3))); TimeSpan spanForRange = arriveRanges.get(currentRange); renderRange(currentRange, startRange, arriveRange, false, false, g2d, spanForRange, popupInfo); } g2d.setComposite(c); g2d.setPaint(p); renderDayMarkers(minValue, maxValue, g2d); renderPopup(popupInfo, g2d); } }
From source file:Starry.java
public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white);/*from w w w. j a v a 2 s . com*/ w = getSize().width; h = getSize().height; Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica", 1, w / 10); String s = new String("The Starry Night"); TextLayout textTl = new TextLayout(s, f, frc); AffineTransform transform = new AffineTransform(); Shape outline = textTl.getOutline(null); Rectangle r = outline.getBounds(); transform = g2.getTransform(); transform.translate(w / 2 - (r.width / 2), h / 2 + (r.height / 2)); g2.transform(transform); g2.setColor(Color.blue); g2.draw(outline); g2.setClip(outline); g2.drawImage(img, r.x, r.y, r.width, r.height, this); }