List of usage examples for java.awt Font getStringBounds
public Rectangle2D getStringBounds(String str, FontRenderContext frc)
From source file:com.alibaba.simpleimage.render.CornerDrawTextItem.java
@Override public void drawText(Graphics2D graphics, int width, int height) { if (StringUtils.isBlank(text)) { return;// www . j a v a 2 s . co m } int x = 0, y = 0; // ? int textLength = (int) (width * textWidthPercent); // ?? int 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(); // ?? if (width > height) { y = height / 4; } else { y = width / 4; } int halflen = sw / 2; if (halflen <= (y - fontsize)) { x = y - halflen; } else { x = fontsize; } 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); if (width > height) { y = height - (height / 4); } else { y = height - (width / 4); } halflen = sw / 2; if (halflen <= (height - y - fontsize)) { x = width - (height - y) - halflen; } else { x = width - sw - fontsize; } 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:com.alibaba.simpleimage.render.FootnoteDrawTextItem.java
@Override public void drawText(Graphics2D graphics, int width, int height) { if (StringUtils.isBlank(text) || StringUtils.isBlank(domainName)) { return;/* ww w . j av a 2 s .co m*/ } int x = 0, y = 0; int fontsize = ((int) (width * textWidthPercent)) / domainName.length(); if (fontsize < minFontSize) { return; } float fsize = (float) fontsize; Font font = domainFont.deriveFont(fsize); graphics.setFont(font); FontRenderContext context = graphics.getFontRenderContext(); int sw = (int) font.getStringBounds(domainName, context).getWidth(); x = width - sw - fontsize; y = height - fontsize; if (x <= 0 || y <= 0) { return; } if (fontShadowColor != null) { graphics.setColor(fontShadowColor); graphics.drawString(domainName, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize)); } graphics.setColor(fontColor); graphics.drawString(domainName, x, y); //draw company name fsize = (float) fontsize; font = defaultFont.deriveFont(fsize); graphics.setFont(font); context = graphics.getFontRenderContext(); sw = (int) font.getStringBounds(text, context).getWidth(); x = width - sw - fontsize; y = height - (int) (fontsize * 2.5); 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:FontShow.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Dialog", Font.PLAIN, 96); g2.setFont(font);//from w w w . j a v a 2s .c o m int width = getSize().width; int height = getSize().height; String message = "Java2s"; FontRenderContext frc = g2.getFontRenderContext(); LineMetrics metrics = font.getLineMetrics(message, frc); float messageWidth = (float) font.getStringBounds(message, frc).getWidth(); // center text float ascent = metrics.getAscent(); float descent = metrics.getDescent(); float x = (width - messageWidth) / 2; float y = (height + metrics.getHeight()) / 2 - descent; int PAD = 25; g2.setPaint(getBackground()); g2.fillRect(0, 0, width, height); g2.setPaint(getForeground()); g2.drawString(message, x, y); g2.setPaint(Color.white); // Base lines drawLine(g2, x - PAD, y, x + messageWidth + PAD, y); drawLine(g2, x, y + PAD, x, y - ascent - PAD); g2.setPaint(Color.green); // Ascent line drawLine(g2, x - PAD, y - ascent, x + messageWidth + PAD, y - ascent); g2.setPaint(Color.red); // Descent line drawLine(g2, x - PAD, y + descent, x + messageWidth + PAD, y + descent); }
From source file:BookTest.java
/** * Gets the page count of this section.// w w w . j a va2 s . co m * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
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 ww w . j ava 2 s. com*/ } 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:Chart.java
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // compute the minimum and maximum values if (values == null) return; double minValue = 0; double maxValue = 0; for (double v : values) {//from w w w . j av a2 s.c o m if (minValue > v) minValue = v; if (maxValue < v) maxValue = v; } if (maxValue == minValue) return; int panelWidth = getWidth(); int panelHeight = getHeight(); Font titleFont = new Font("SansSerif", Font.BOLD, 20); Font labelFont = new Font("SansSerif", Font.PLAIN, 10); // compute the extent of the title FontRenderContext context = g2.getFontRenderContext(); Rectangle2D titleBounds = titleFont.getStringBounds(title, context); double titleWidth = titleBounds.getWidth(); double top = titleBounds.getHeight(); // draw the title double y = -titleBounds.getY(); // ascent double x = (panelWidth - titleWidth) / 2; g2.setFont(titleFont); g2.drawString(title, (float) x, (float) y); // compute the extent of the bar labels LineMetrics labelMetrics = labelFont.getLineMetrics("", context); double bottom = labelMetrics.getHeight(); y = panelHeight - labelMetrics.getDescent(); g2.setFont(labelFont); // get the scale factor and width for the bars double scale = (panelHeight - top - bottom) / (maxValue - minValue); int barWidth = panelWidth / values.length; // draw the bars for (int i = 0; i < values.length; i++) { // get the coordinates of the bar rectangle double x1 = i * barWidth + 1; double y1 = top; double height = values[i] * scale; if (values[i] >= 0) y1 += (maxValue - values[i]) * scale; else { y1 += maxValue * scale; height = -height; } // fill the bar and draw the bar outline Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height); g2.setPaint(Color.RED); g2.fill(rect); g2.setPaint(Color.BLACK); g2.draw(rect); // draw the centered label below the bar Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context); double labelWidth = labelBounds.getWidth(); x = x1 + (barWidth - labelWidth) / 2; g2.drawString(names[i], (float) x, (float) y); } }
From source file:WeatherWizard.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension size = getSize();//w w w .java2s . c om Composite origComposite; setupWeatherReport(); origComposite = g2.getComposite(); if (alpha0 != null) g2.setComposite(alpha0); g2.drawImage(img0, 0, 0, size.width, size.height, 0, 0, img0.getWidth(null), img0.getHeight(null), null); if (img1 != null) { if (alpha1 != null) g2.setComposite(alpha1); g2.drawImage(img1, 0, 0, size.width, size.height, 0, 0, img1.getWidth(null), img1.getHeight(null), null); } g2.setComposite(origComposite); // Freezing, Cold, Cool, Warm, Hot, // Blue, Green, Yellow, Orange, Red Font font = new Font("Serif", Font.PLAIN, 36); g.setFont(font); String tempString = feels + " " + temperature + "F"; FontRenderContext frc = ((Graphics2D) g).getFontRenderContext(); Rectangle2D boundsTemp = font.getStringBounds(tempString, frc); Rectangle2D boundsCond = font.getStringBounds(condStr, frc); int wText = Math.max((int) boundsTemp.getWidth(), (int) boundsCond.getWidth()); int hText = (int) boundsTemp.getHeight() + (int) boundsCond.getHeight(); int rX = (size.width - wText) / 2; int rY = (size.height - hText) / 2; g.setColor(Color.LIGHT_GRAY); g2.fillRect(rX, rY, wText, hText); g.setColor(textColor); int xTextTemp = rX - (int) boundsTemp.getX(); int yTextTemp = rY - (int) boundsTemp.getY(); g.drawString(tempString, xTextTemp, yTextTemp); int xTextCond = rX - (int) boundsCond.getX(); int yTextCond = rY - (int) boundsCond.getY() + (int) boundsTemp.getHeight(); g.drawString(condStr, xTextCond, yTextCond); }
From source file:Bouncer.java
protected void setClip(Graphics2D g2) { if (mClip == false) return;//from ww w . j av a2 s . co m if (mClipShape == null) { Dimension d = getSize(); FontRenderContext frc = g2.getFontRenderContext(); Font font = new Font("Serif", Font.PLAIN, 144); String s = "Java Source and Support!"; GlyphVector gv = font.createGlyphVector(frc, s); Rectangle2D bounds = font.getStringBounds(s, frc); mClipShape = gv.getOutline((d.width - (float) bounds.getWidth()) / 2, (d.height + (float) bounds.getHeight()) / 2); } g2.clip(mClipShape); }
From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java
private void renderLegend(Graphics2D g2) { g2.setFont(NORMALFONT.deriveFont(32f).deriveFont(Font.BOLD)); // erst die Breite der Zahlen fuer die Einrueckung berechnen Font font = g2.getFont(); FontRenderContext fontRenderContext = g2.getFontRenderContext(); Map<Segment, Double> numberWidths = new HashMap<>(); double maxNumberWidth = 0; for (Segment seg : segments) { double numberWidth = font .getStringBounds(legendPortionFormat.format(seg.getPortion()), fontRenderContext).getWidth(); numberWidths.put(seg, numberWidth); maxNumberWidth = Math.max(maxNumberWidth, numberWidth); }//from w w w . ja v a2 s .co m if (showTotalInLegend) { double numberWidth = font.getStringBounds(legendPortionFormat.format(total), fontRenderContext) .getWidth(); numberWidths.put(null, numberWidth); maxNumberWidth = Math.max(maxNumberWidth, numberWidth); } // jetzt die Zeilen in die Legende malen int verticalOffset = 0; for (int row = 0; row < segments.size(); ++row) { Segment seg = getLegendSegment(row); // Zahlen rechtsbuendig double indentation = maxNumberWidth - numberWidths.get(seg); String segmentPortionNumber = legendPortionFormat.format(seg.getPortion()); String segmentText = seg.getText(); Color segmentColor = seg.getColor(); String subSegmentText = seg.getSubSegmentText(); String subNumberText = legendPortionFormat.format(seg.subPortion); drawLegendLine(g2, verticalOffset, indentation, segmentColor, segmentText, segmentPortionNumber, seg.getPortion() != 1d, seg.getSubPortion() > 0, subSegmentText, subNumberText, seg.getSubPortion() != 1d); verticalOffset += 85; } if (showTotalInLegend) { double indentation = maxNumberWidth - numberWidths.get(null); String subNumberText = legendPortionFormat.format(subTotal); drawLegendLine(g2, verticalOffset, indentation, null, totalText, legendPortionFormat.format(total), total != 1, subTotalTextOrNull != null, subTotalTextOrNull, subNumberText, subTotal != 1); } }
From source file:de.unisb.cs.st.javaslicer.jung.ShowJungGraph.java
public void displayGraph(DirectedGraph<VertexType, SliceEdge<VertexType>> graph) { JFrame mainFrame = new JFrame("slice graph display"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Layout<VertexType, SliceEdge<VertexType>> layout = new KKLayout<VertexType, SliceEdge<VertexType>>(graph); final VisualizationViewer<VertexType, SliceEdge<VertexType>> viewer = new VisualizationViewer<VertexType, SliceEdge<VertexType>>( layout);/*from w w w . ja v a 2s .c o m*/ viewer.getRenderContext().setVertexLabelTransformer(this.vertexLabelTransformer); viewer.setVertexToolTipTransformer(this.vertexTooltipTransformer); viewer.getRenderContext().setEdgeLabelTransformer(new Transformer<SliceEdge<VertexType>, String>() { @Override public String transform(SliceEdge<VertexType> edge) { Variable var = edge.getVariable(); if (var instanceof ArrayElement) { ArrayElement elem = (ArrayElement) var; return "arr" + elem.getArrayId() + "[" + elem.getArrayIndex() + "]"; } else if (var instanceof LocalVariable) { LocalVariable localVar = (LocalVariable) var; if (localVar.getVarName() != null) return localVar.getVarName(); return "local" + localVar.getVarIndex(); } else if (var instanceof StackEntry) { return "stack"; } else if (var instanceof ObjectField) { ObjectField objField = (ObjectField) var; return "obj" + objField.getObjectId() + "." + objField.getFieldName(); } else if (var instanceof StaticField) { StaticField objField = (StaticField) var; String ownerInternalClassName = objField.getOwnerInternalClassName(); String simpleClassName = getSimpleClassName(ownerInternalClassName); return simpleClassName + "." + objField.getFieldName(); } else { assert var == null; return ""; } } }); viewer.setEdgeToolTipTransformer(new Transformer<SliceEdge<VertexType>, String>() { @Override public String transform(SliceEdge<VertexType> edge) { Variable var = edge.getVariable(); if (var instanceof ArrayElement) { ArrayElement elem = (ArrayElement) var; return "Array element " + elem.getArrayIndex() + " of array #" + elem.getArrayId(); } else if (var instanceof LocalVariable) { LocalVariable localVar = (LocalVariable) var; if (localVar.getVarName() != null) return "Local variable \"" + localVar.getVarName() + "\""; return "Local variable #" + localVar.getVarIndex(); } else if (var instanceof StackEntry) { return "Dependency over the operand stack"; } else if (var instanceof ObjectField) { ObjectField objField = (ObjectField) var; return "Field \"" + objField.getFieldName() + "\" of object #" + objField.getObjectId(); } else if (var instanceof StaticField) { StaticField objField = (StaticField) var; String ownerInternalClassName = objField.getOwnerInternalClassName(); return "Static field \"" + objField.getFieldName() + "\" of class \"" + Type.getType(ownerInternalClassName).getClassName(); } else { assert var == null; return "Control dependency"; } } }); viewer.getRenderContext().setEdgeDrawPaintTransformer(new Transformer<SliceEdge<VertexType>, Paint>() { @Override public Paint transform(SliceEdge<VertexType> edge) { return edge.getVariable() == null ? Color.red : Color.black; } }); viewer.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR); viewer.getRenderContext().setVertexShapeTransformer(new Transformer<VertexType, Shape>() { @Override public Shape transform(VertexType inst) { Font font = viewer.getFont(); String text = viewer.getRenderContext().getVertexLabelTransformer().transform(inst); FontRenderContext fontRenderContext = ((Graphics2D) viewer.getGraphics()).getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(text, fontRenderContext); return new RoundRectangle2D.Double(-.6 * bounds.getWidth(), -.6 * bounds.getHeight(), 1.2 * bounds.getWidth(), 1.2 * bounds.getHeight(), 8, 8); } }); viewer.setBackground(Color.white); DefaultModalGraphMouse<InstructionInstance, SliceEdge<VertexType>> mouse = new DefaultModalGraphMouse<InstructionInstance, SliceEdge<VertexType>>(); mouse.setMode(Mode.ANNOTATING); viewer.setGraphMouse(mouse); mainFrame.getContentPane().add(viewer); mainFrame.pack(); mainFrame.setVisible(true); }