List of usage examples for java.awt Graphics2D setFont
public abstract void setFont(Font font);
From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java
public void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle, boolean multiline) { Graphics2D g2 = (Graphics2D) g; Font font = g2.getFont();//from ww w.j a va2 s .c o m if (angle != 0) { g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle)))); } Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2); Point2D pos = getPoint(bounds, align); double x = pos.getX(); double y = pos.getY() + sSize.getHeight(); switch (align) { case TopCenter: case BottomCenter: case Center: x -= (sSize.getWidth() / 2); break; case TopRight: case MiddleRight: case BottomRight: x -= (sSize.getWidth()); break; case BottomLeft: case MiddleLeft: case TopLeft: break; } if (multiline) { // Create a new LineBreakMeasurer from the paragraph. // It will be cached and re-used. //if (lineMeasurer == null) { AttributedCharacterIterator paragraph = new AttributedString(text).getIterator(); paragraphStart = paragraph.getBeginIndex(); paragraphEnd = paragraph.getEndIndex(); FontRenderContext frc = g2.getFontRenderContext(); lineMeasurer = new LineBreakMeasurer(paragraph, frc); //} // Set break width to width of Component. float breakWidth = (float) bounds.getWidth(); float drawPosY = (float) y; // Set position to the index of the first character in the paragraph. lineMeasurer.setPosition(paragraphStart); // Get lines until the entire paragraph has been displayed. while (lineMeasurer.getPosition() < paragraphEnd) { // Retrieve next layout. A cleverer program would also cache // these layouts until the component is re-sized. TextLayout layout = lineMeasurer.nextLayout(breakWidth); // Compute pen x position. If the paragraph is right-to-left we // will align the TextLayouts to the right edge of the panel. // Note: this won't occur for the English text in this sample. // Note: drawPosX is always where the LEFT of the text is placed. float drawPosX = layout.isLeftToRight() ? (float) x : (float) x + breakWidth - layout.getAdvance(); // Move y-coordinate by the ascent of the layout. drawPosY += layout.getAscent(); // Draw the TextLayout at (drawPosX, drawPosY). layout.draw(g2, drawPosX, drawPosY); // Move y-coordinate in preparation for next layout. drawPosY += layout.getDescent() + layout.getLeading(); } } else { g2.drawString(text, (float) x, (float) y); } g2.setFont(font); }
From source file:org.eclipse.wb.internal.swing.model.property.editor.beans.PropertyEditorWrapper.java
private Image paintValue(GC gc, int width, int height) throws Exception { // create AWT graphics BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = (Graphics2D) image.getGraphics(); // prepare color's Color background = gc.getBackground(); Color foreground = gc.getForeground(); // fill graphics graphics2D.setColor(SwingUtils.getAWTColor(background)); graphics2D.fillRect(0, 0, width, height); // set color//w ww . ja va2 s .co m graphics2D.setBackground(SwingUtils.getAWTColor(background)); graphics2D.setColor(SwingUtils.getAWTColor(foreground)); // set font FontData[] fontData = gc.getFont().getFontData(); String name = fontData.length > 0 ? fontData[0].getName() : "Arial"; graphics2D.setFont(new java.awt.Font(name, java.awt.Font.PLAIN, graphics2D.getFont().getSize() - 1)); // paint image m_propertyEditor.paintValue(graphics2D, new java.awt.Rectangle(0, 0, width, height)); // conversion try { return SwingImageUtils.convertImage_AWT_to_SWT(image); } finally { image.flush(); graphics2D.dispose(); } }
From source file:org.jfree.demo.DrawStringPanel.java
/** * Paints the panel./* www . jav a 2s . c o m*/ * * @param g the graphics device. */ public void paintComponent(final Graphics g) { super.paintComponent(g); final Graphics2D g2 = (Graphics2D) g; final Dimension size = getSize(); final Insets insets = getInsets(); final Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top, size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom); final double x = available.getCenterX(); final double y = available.getCenterY(); final Line2D line1 = new Line2D.Double(x - 2.0, y + 2.0, x + 2.0, y - 2.0); final Line2D line2 = new Line2D.Double(x - 2.0, y - 2.0, x + 2.0, y + 2.0); g2.setPaint(Color.red); g2.draw(line1); g2.draw(line2); g2.setFont(this.font); g2.setPaint(Color.black); if (this.rotate) { TextUtilities.drawRotatedString(this.text, g2, (float) x, (float) y, this.anchor, this.angle, this.rotationAnchor); } else { TextUtilities.drawAlignedString(this.text, g2, (float) x, (float) y, this.anchor); } }
From source file:com.newatlanta.bluedragon.CustomClusteredXYBarRenderer.java
private void drawItemLabel(Graphics2D g2, PlotOrientation orientation, XYDataset dataset, int series, int item, Rectangle2D bar, boolean negative) { XYItemLabelGenerator generator = getItemLabelGenerator(series, item); if (generator == null) return;/*from w w w . j a v a2 s .c o m*/ String label = generator.generateLabel(dataset, series, item); if (label == null) { return; // nothing to do } Font labelFont = getItemLabelFont(series, item); g2.setFont(labelFont); Paint paint = getItemLabelPaint(series, item); g2.setPaint(paint); // find out where to place the label... ItemLabelPosition position = null; if (!negative) { position = getPositiveItemLabelPosition(series, item); } else { position = getNegativeItemLabelPosition(series, item); } // work out the label anchor point... Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar, orientation); TextUtilities.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(), position.getRotationAnchor()); }
From source file:figs.treeVisualization.gui.PhyloDateAxis.java
/** * Draws the axis line, tick marks and tick mark labels. * /*from w w w.j a v a 2 s. c o m*/ * @param g2 the graphics device. * @param cursor the cursor. * @param plotArea the plot area. * @param dataArea the data area. * @param edge the edge that the axis is aligned with. * * * Original method is in <code>org.jfree.chart.axis.ValueAxis</code> */ protected void drawTickMarksAndLabels(Graphics2D g2, double cursor, Rectangle2D dataArea) { //AxisState state = new AxisState(cursor); RectangleEdge edge = RectangleEdge.RIGHT; drawAxisLine(g2, cursor, dataArea); double ol = getTickMarkOutsideLength(); double il = getTickMarkInsideLength(); // Their's miss the first and last label //List ticks = refreshTicks(g2, state, dataArea, edge); List ticks = refreshTicksVertical(g2, dataArea); g2.setFont(getTickLabelFont()); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { ValueTick tick = (ValueTick) iterator.next(); if (isTickLabelsVisible()) { g2.setPaint(getTickLabelPaint()); float[] anchorPoint = calculateAnchorPoint(tick, cursor, dataArea, edge); TextUtilities.drawRotatedString(tick.getText(), g2, anchorPoint[0], anchorPoint[1], tick.getTextAnchor(), tick.getAngle(), tick.getRotationAnchor()); } if (isTickMarksVisible()) { float xx = (float) valueToJava2D(tick.getValue(), dataArea, edge); Line2D mark = null; g2.setStroke(getTickMarkStroke()); g2.setPaint(getTickMarkPaint()); if (edge == RectangleEdge.LEFT) { mark = new Line2D.Double(cursor - ol, xx, cursor + il, xx); } else if (edge == RectangleEdge.RIGHT) { mark = new Line2D.Double(cursor + ol, xx, cursor - il, xx); } else if (edge == RectangleEdge.TOP) { mark = new Line2D.Double(xx, cursor - ol, xx, cursor + il); } else if (edge == RectangleEdge.BOTTOM) { mark = new Line2D.Double(xx, cursor + ol, xx, cursor - il); } g2.draw(mark); } } // end tick list iterator }
From source file:com.celements.photo.image.GenerateThumbnail.java
private void drawWatermark(String watermark, Graphics2D g2d, int width, int height) { //TODO implement 'secure' watermark (i.e. check if this algorithm secure or can it be easily reversed?) g2d.setColor(new Color(255, 255, 150)); AlphaComposite transprency = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.45f); g2d.setComposite(transprency);// ww w. ja va 2s . co m FontMetrics metrics = calcWatermarkFontSize(watermark, width, g2d); g2d.setFont(calcWatermarkFontSize(watermark, width, g2d).getFont()); // rotate around image center double angle = (Math.sin(height / (double) width)); g2d.rotate(-angle, width / 2.0, height / 2.0); g2d.drawString(watermark, (width - metrics.stringWidth(watermark)) / 2, ((height - metrics.getHeight()) / 2) + metrics.getAscent()); // undo rotation for correct copyright positioning g2d.rotate(angle, width / 2.0, height / 2.0); }
From source file:org.jfree.chart.demo.DrawStringPanel.java
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D graphics2d = (Graphics2D) g; Dimension dimension = getSize(); Insets insets = getInsets();/*from w w w. ja va 2 s . c om*/ java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(insets.left, insets.top, dimension.getWidth() - (double) insets.left - (double) insets.right, dimension.getHeight() - (double) insets.top - (double) insets.bottom); double d = double1.getCenterX(); double d1 = double1.getCenterY(); java.awt.geom.Line2D.Double double2 = new java.awt.geom.Line2D.Double(d - 2D, d1 + 2D, d + 2D, d1 - 2D); java.awt.geom.Line2D.Double double3 = new java.awt.geom.Line2D.Double(d - 2D, d1 - 2D, d + 2D, d1 + 2D); graphics2d.setPaint(Color.red); graphics2d.draw(double2); graphics2d.draw(double3); graphics2d.setFont(font); graphics2d.setPaint(Color.black); if (rotate) TextUtilities.drawRotatedString(text, graphics2d, (float) d, (float) d1, anchor, angle, rotationAnchor); else TextUtilities.drawAlignedString(text, graphics2d, (float) d, (float) d1, anchor); }
From source file:com.alibaba.simpleimage.render.FixDrawTextItem.java
@Override public void drawText(Graphics2D graphics, int width, int height) { if (StringUtils.isBlank(text)) { return;//from w w w .ja v a 2s . c om } int x = 0, y = 0; int fontsize = 1; if (position == Position.CENTER) { // ? int textLength = (int) (width * textWidthPercent); // ?? fontsize = textLength / text.length(); // ?.....? if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = defaultFont.deriveFont(fsize); graphics.setFont(font); FontRenderContext context = graphics.getFontRenderContext(); int sw = (int) font.getStringBounds(text, context).getWidth(); // ?? x = (width - sw) / 2; y = height / 2 + fontsize / 2; } else if (position == Position.TOP_LEFT) { fontsize = ((int) (width * textWidthPercent)) / text.length(); if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = defaultFont.deriveFont(fsize); graphics.setFont(font); x = fontsize; y = fontsize * 2; } else if (position == Position.TOP_RIGHT) { fontsize = ((int) (width * textWidthPercent)) / text.length(); if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = defaultFont.deriveFont(fsize); graphics.setFont(font); FontRenderContext context = graphics.getFontRenderContext(); int sw = (int) font.getStringBounds(text, context).getWidth(); x = width - sw - fontsize; y = fontsize * 2; } else if (position == Position.BOTTOM_LEFT) { fontsize = ((int) (width * textWidthPercent)) / text.length(); if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = defaultFont.deriveFont(fsize); graphics.setFont(font); x = fontsize / 2; y = height - fontsize; } else if (position == Position.BOTTOM_RIGHT) { fontsize = ((int) (width * textWidthPercent)) / text.length(); if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = defaultFont.deriveFont(fsize); graphics.setFont(font); FontRenderContext context = graphics.getFontRenderContext(); int sw = (int) font.getStringBounds(text, context).getWidth(); x = width - sw - fontsize; y = height - fontsize; } else { throw new IllegalArgumentException("Unknown position : " + position); } if (x <= 0 || y <= 0) { return; } if (fontShadowColor != null) { graphics.setColor(fontShadowColor); graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize)); } graphics.setColor(fontColor); graphics.drawString(text, x, y); }
From source file:org.openaltimeter.desktopapp.annotations.XYHeightAnnotation.java
@Override public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { PlotOrientation orientation = plot.getOrientation(); RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation); RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation); float anchorX = (float) domainAxis.valueToJava2D(this.getX(), dataArea, domainEdge); float anchorY = (float) rangeAxis.valueToJava2D(this.getY(), dataArea, rangeEdge); if (orientation == PlotOrientation.HORIZONTAL) { float tempAnchor = anchorX; anchorX = anchorY;//from w w w .ja v a 2s . c o m anchorY = tempAnchor; } g2.setFont(getFont()); Shape hotspot = TextUtilities.calculateRotatedStringBounds(getText(), g2, anchorX, anchorY, getTextAnchor(), getRotationAngle(), getRotationAnchor()); if (this.getBackgroundPaint() != null) { g2.setPaint(this.getBackgroundPaint()); g2.fill(hotspot); } g2.setPaint(getPaint()); TextUtilities.drawRotatedString(getText(), g2, anchorX + (3.6f * DOT_SIZE), anchorY + (0.2f * DOT_SIZE), getTextAnchor(), getRotationAngle(), getRotationAnchor()); if (this.isOutlineVisible()) { g2.setStroke(this.getOutlineStroke()); g2.setPaint(this.getOutlinePaint()); g2.draw(hotspot); } // cross drawing // g2.setPaint(getPaint()); // g2.setStroke(new BasicStroke(1.0f)); // g2.drawLine((int) anchorX - CROSS_SIZE / 2, (int) anchorY, (int) anchorX + CROSS_SIZE / 2, (int) anchorY); // g2.drawLine((int) anchorX, (int) anchorY - CROSS_SIZE / 2, (int) anchorX, (int) anchorY + CROSS_SIZE / 2); // dot drawing g2.setPaint(getPaint()); g2.setStroke(new BasicStroke(1.0f)); Ellipse2D e = new Ellipse2D.Double(anchorX - DOT_SIZE / 2, anchorY - DOT_SIZE / 2, DOT_SIZE, DOT_SIZE); g2.fill(e); String toolTip = getToolTipText(); String url = getURL(); if (toolTip != null || url != null) { addEntity(info, hotspot, rendererIndex, toolTip, url); } }
From source file:org.uva.itast.blended.omr.scanners.BarcodeScanner.java
/** * @param campo// w w w.ja v a 2s . c o m */ public void markBarcode(Field campo) { try { //get bbox in pixels Rectangle rect = pageImage.toPixels(campo.getBBox()); // expand the area for some tolerance Rectangle2D expandedArea = getExpandedArea(campo.getBBox(), (float) BARCODE_AREA_PERCENT); Rectangle expandedRect = pageImage.toPixels(expandedArea); Graphics2D g = pageImage.getReportingGraphics(); AffineTransform t = g.getTransform(); g.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1, new float[] { (float) (3 / t.getScaleX()), (float) (6 / t.getScaleY()) }, 0)); if (lastResult != null) g.setColor(Color.BLUE); else g.setColor(Color.RED); g.drawRoundRect(rect.x, rect.y, rect.width, rect.height, 3, 3); g.drawRoundRect(expandedRect.x, expandedRect.y, expandedRect.width, expandedRect.height, 3, 3); g.setFont(new Font("Arial", Font.BOLD, (int) (12 / t.getScaleX()))); String message; if (lastResult != null) message = ((Result) lastResult.getResult()).getBarcodeFormat().toString() + "=" + getParsedCode(lastResult); else message = "UNRECOGNIZED!"; g.drawString(message, rect.x, rect.y); } catch (Exception e) { logger.error("Unexpected errr while logging the image:", e); } }