List of usage examples for java.awt Graphics2D getFont
public abstract Font getFont();
From source file:com.celements.photo.image.GenerateThumbnail.java
private FontMetrics calcCopyrightFontSize(String copyright, int width, Graphics2D g2d) { FontMetrics metrics;/*w ww.j a va 2 s. c om*/ int fontSize = 16; do { metrics = g2d.getFontMetrics(new Font(g2d.getFont().getFontName(), Font.BOLD, fontSize)); fontSize--; } while ((fontSize > 0) && (metrics.stringWidth(copyright) > (width / 3))); return metrics; }
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 ww . j av a 2s .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:org.optaplanner.examples.tsp.swingui.TspWorldPanel.java
public void resetPanel(TravelingSalesmanTour travelingSalesmanTour) { translator = new LatitudeLongitudeTranslator(); for (Location location : travelingSalesmanTour.getLocationList()) { translator.addCoordinates(location.getLatitude(), location.getLongitude()); }//from w ww .j a v a 2 s .co m Dimension size = getSize(); double width = size.getWidth(); double height = size.getHeight(); translator.prepareFor(width, height); Graphics2D g = createCanvas(width, height); String tourName = travelingSalesmanTour.getName(); if (tourName.startsWith("europe")) { g.drawImage(europaBackground.getImage(), 0, 0, translator.getImageWidth(), translator.getImageHeight(), this); } g.setFont(g.getFont().deriveFont((float) LOCATION_NAME_TEXT_SIZE)); g.setColor(TangoColorFactory.PLUM_2); List<Visit> visitList = travelingSalesmanTour.getVisitList(); for (Visit visit : visitList) { Location location = visit.getLocation(); int x = translator.translateLongitudeToX(location.getLongitude()); int y = translator.translateLatitudeToY(location.getLatitude()); g.fillRect(x - 1, y - 1, 3, 3); if (location.getName() != null && visitList.size() <= 500) { g.drawString(StringUtils.abbreviate(location.getName(), 20), x + 3, y - 3); } } g.setColor(TangoColorFactory.ALUMINIUM_4); Domicile domicile = travelingSalesmanTour.getDomicile(); Location domicileLocation = domicile.getLocation(); int domicileX = translator.translateLongitudeToX(domicileLocation.getLongitude()); int domicileY = translator.translateLatitudeToY(domicileLocation.getLatitude()); g.fillRect(domicileX - 2, domicileY - 2, 5, 5); if (domicileLocation.getName() != null && visitList.size() <= 500) { g.drawString(domicileLocation.getName(), domicileX + 3, domicileY - 3); } Set<Visit> needsBackToDomicileLineSet = new HashSet<Visit>(visitList); for (Visit trailingVisit : visitList) { if (trailingVisit.getPreviousStandstill() instanceof Visit) { needsBackToDomicileLineSet.remove(trailingVisit.getPreviousStandstill()); } } g.setColor(TangoColorFactory.CHOCOLATE_1); for (Visit visit : visitList) { if (visit.getPreviousStandstill() != null) { Location previousLocation = visit.getPreviousStandstill().getLocation(); Location location = visit.getLocation(); translator.drawRoute(g, previousLocation.getLongitude(), previousLocation.getLatitude(), location.getLongitude(), location.getLatitude(), location instanceof AirLocation, false); // Back to domicile line if (needsBackToDomicileLineSet.contains(visit)) { translator.drawRoute(g, location.getLongitude(), location.getLatitude(), domicileLocation.getLongitude(), domicileLocation.getLatitude(), location instanceof AirLocation, true); } } } // Drag if (dragSourceStandstill != null) { g.setColor(TangoColorFactory.CHOCOLATE_2); Location sourceLocation = dragSourceStandstill.getLocation(); Location targetLocation = dragTargetStandstill.getLocation(); translator.drawRoute(g, sourceLocation.getLongitude(), sourceLocation.getLatitude(), targetLocation.getLongitude(), targetLocation.getLatitude(), sourceLocation instanceof AirLocation, dragTargetStandstill instanceof Domicile); } // Legend g.setColor(TangoColorFactory.ALUMINIUM_4); g.fillRect(5, (int) height - 15 - TEXT_SIZE, 5, 5); g.drawString("Domicile", 15, (int) height - 10 - TEXT_SIZE); g.setColor(TangoColorFactory.PLUM_2); g.fillRect(6, (int) height - 9, 3, 3); g.drawString("Visit", 15, (int) height - 5); g.setColor(TangoColorFactory.ALUMINIUM_5); String locationsSizeString = travelingSalesmanTour.getLocationList().size() + " locations"; g.drawString(locationsSizeString, ((int) width - g.getFontMetrics().stringWidth(locationsSizeString)) / 2, (int) height - 5); if (travelingSalesmanTour.getDistanceType() == DistanceType.AIR_DISTANCE) { String leftClickString = "Left click and drag between 2 locations to connect them."; g.drawString(leftClickString, (int) width - 5 - g.getFontMetrics().stringWidth(leftClickString), (int) height - 10 - TEXT_SIZE); String rightClickString = "Right click anywhere on the map to add a visit."; g.drawString(rightClickString, (int) width - 5 - g.getFontMetrics().stringWidth(rightClickString), (int) height - 5); } // Show soft score g.setColor(TangoColorFactory.ORANGE_3); SimpleLongScore score = travelingSalesmanTour.getScore(); if (score != null) { String distanceString = travelingSalesmanTour.getDistanceString(NUMBER_FORMAT); g.setFont(g.getFont().deriveFont(Font.BOLD, (float) TEXT_SIZE * 2)); g.drawString(distanceString, (int) width - g.getFontMetrics().stringWidth(distanceString) - 10, (int) height - 15 - 2 * TEXT_SIZE); } repaint(); }
From source file:ru.runa.wfe.graph.image.figure.AbstractFigure.java
private int drawText(Graphics2D graphics, String text, int hOffset) { Rectangle r = getTextBoundsRectangle(); Rectangle2D textBounds = graphics.getFontMetrics().getStringBounds(text, graphics); if (textBounds.getWidth() > r.getWidth() - 4) { int y = coords[1] + hOffset; AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, graphics.getFont()); AttributedCharacterIterator characterIterator = attributedString.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, graphics.getFontRenderContext()); while (measurer.getPosition() < characterIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout((float) r.getWidth() - 4); y += textLayout.getAscent(); float x = (float) (r.getCenterX() + 2 - textLayout.getBounds().getCenterX()); textLayout.draw(graphics, x, y); y += textLayout.getDescent() + textLayout.getLeading(); }//from w w w . j av a 2 s.c o m return y - coords[1]; } else { graphics.drawString(text, (float) (r.getCenterX() + 2 - textBounds.getCenterX()), (float) (coords[1] + textBounds.getHeight() + hOffset)); return (int) (textBounds.getHeight() + hOffset + 3); } }
From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphNode.java
/** * Draw a GraphNode as a rectangle with a name * * @param g The Java2D Graphics//from w w w .j a va 2 s .co m * @param col The color to draw */ public void drawNode(final Graphics2D g, final Color col) { final int x = displayPosition.x; final int y = displayPosition.y; g.setFont(g.getFont().deriveFont(Font.BOLD, 11)); final FontMetrics metrics = g.getFontMetrics(); final String name = node.getId(); final Rectangle2D rect = metrics.getStringBounds(name, g); final int stringWidth = (int) rect.getWidth(); setSize(Math.max(stringWidth, 50) + 10, 25); int step = 4; int alpha = 96; for (int i = 0; i < step; ++i) { g.setColor(new Color(0, 0, 0, alpha - (32 * i))); g.drawLine(x + i + 1, y + nodeHeight + i, x + nodeWidth + i - 1, y + nodeHeight + i); g.drawLine(x + nodeWidth + i, y + i, x + nodeWidth + i, y + nodeHeight + i); } Shape clipShape = new Rectangle(x, y, nodeWidth, nodeHeight); g.setComposite(AlphaComposite.SrcAtop); g.setPaint(new GradientPaint(x, y, col, x + nodeWidth, y + nodeHeight, col.darker())); g.fill(clipShape); g.setColor(Color.blue); g.draw3DRect(x, y, nodeWidth - 1, nodeHeight - 1, true); g.setColor(Color.BLACK); g.drawString(name, x + (nodeWidth - stringWidth) / 2, y + 15); }
From source file:pl.edu.icm.visnow.lib.basic.viewers.Viewer2D.Display2DPanel.java
@Override public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; Font f = g2d.getFont(); Color c = g2d.getColor();/*from w w w . j a v a 2s. c o m*/ Rectangle clear = new Rectangle(0, 0, getWidth(), getHeight()); g2d.setPaint(bgColor); g2d.fill(clear); draw2D(g2d); g2d.setFont(titleFont); g2d.setColor(titleColor); g2d.drawString(titleText, 50, 10 + titleFont.getSize()); g2d.setFont(f); g2d.setColor(c); if (storingFrames && !dontWrite) { if (storingJPEG) { writeImage(controlsFrame.getMovieCreationPanel().getCurrentFrameFileName(), FORMAT_JPEG); } else { writeImage(controlsFrame.getMovieCreationPanel().getGenericFrameFileName(), FORMAT_PNG); } } }
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(); if (angle != 0) { g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle)))); }/*www. j a va 2 s . c om*/ 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.squidy.designer.zoom.impl.VisualizationShape.java
/** * @param paintContext//from w w w. j a va 2 s. c o m */ protected void paintNodeLabels(PPaintContext paintContext) { Graphics2D g = paintContext.getGraphics(); g.setFont(g.getFont().deriveFont(18f)); FontMetrics fm = g.getFontMetrics(); PBounds bounds = getBoundsReference(); double width = bounds.getWidth(); double height = bounds.getHeight(); String inputName = pipeShape.getSource().getTitle(); rotation270.setToRotation(Math.toRadians(270)); paintContext.pushTransform(rotation270); g.setColor(Color.WHITE); g.drawString(inputName, (int) -((height / 2) + (FontUtils.getWidthOfText(fm, inputName) / 2)), -25); paintContext.popTransform(rotation270); String outputName = pipeShape.getTarget().getTitle(); rotation90.setToRotation(Math.toRadians(90)); paintContext.pushTransform(rotation90); g.setColor(Color.WHITE); g.drawString(outputName, (int) ((height / 2) - (FontUtils.getWidthOfText(fm, outputName) / 2)), (int) (-width - 30)); paintContext.popTransform(rotation90); }
From source file:org.csml.tommo.sugar.modules.MappingQuality.java
protected void drawRowHeader(Graphics2D g2, final MappingQualityTableModel model, int imgSize, int width, int xOffset) { for (int r = 0; r < model.getRowCount(); r++) { int i = r / model.getTileNumeration().getCycleSize(); String s = i < MappingQualityMatrix.THRESHOLDS.length ? "MAPQ<" + MappingQualityMatrix.THRESHOLDS[i] : "AVERAGE"; if (model.getCycleID(r) == 0) { int stringHeight = g2.getFont().getSize(); //g2.getFontMetrics().getHeight(); int stringWidth = g2.getFontMetrics().stringWidth(s); g2.drawString(s, 1 + width + (xOffset - 1 - stringWidth) / 2, imgSize * (r + 1) - (imgSize - stringHeight) / 2); }/*from w ww . j a va 2s .co m*/ } }
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 w w . j a v a 2s. c o 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(); } }