List of usage examples for java.awt FontMetrics stringWidth
public int stringWidth(String str)
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); }// w w w.j a va 2 s .co m 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:org.tinymediamanager.ui.components.ImageLabel.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (scaledImage != null) { int originalWidth = scaledImage.getWidth(null); int originalHeight = scaledImage.getHeight(null); // calculate new height/width int newWidth = 0; int newHeight = 0; int offsetX = 0; int offsetY = 0; if (drawBorder && !drawFullWidth) { Point size = ImageCache.calculateSize(this.getWidth() - 8, this.getHeight() - 8, originalWidth, originalHeight, true); // calculate offsets if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) { offsetX = this.getWidth() - size.x - 8; }// w ww. java 2 s . co m if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) { offsetY = this.getHeight() - size.y - 8; } if (position == Position.CENTER) { offsetX = (this.getWidth() - size.x - 8) / 2; offsetY = (this.getHeight() - size.y - 8) / 2; } newWidth = size.x; newHeight = size.y; // when the image size differs too much - reload and rescale the original image recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight); g.setColor(Color.BLACK); g.drawRect(offsetX, offsetY, size.x + 7, size.y + 7); g.setColor(Color.WHITE); g.fillRect(offsetX + 1, offsetY + 1, size.x + 6, size.y + 6); // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX + 4, offsetY + 4, newWidth, newHeight, this); g.drawImage(scaledImage, offsetX + 4, offsetY + 4, newWidth, newHeight, this); } else { Point size = null; if (drawFullWidth) { size = new Point(this.getWidth(), this.getWidth() * originalHeight / originalWidth); } else { size = ImageCache.calculateSize(this.getWidth(), this.getHeight(), originalWidth, originalHeight, true); } // calculate offsets if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) { offsetX = this.getWidth() - size.x; } if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) { offsetY = this.getHeight() - size.y; } if (position == Position.CENTER) { offsetX = (this.getWidth() - size.x) / 2; offsetY = (this.getHeight() - size.y) / 2; } newWidth = size.x; newHeight = size.y; // when the image size differs too much - reload and rescale the original image recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight); // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX, offsetY, newWidth, newHeight, this); g.drawImage(scaledImage, offsetX, offsetY, newWidth, newHeight, this); } } else { // draw border and background if (drawBorder) { g.setColor(Color.BLACK); g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); if (getParent().isOpaque()) { g.setColor(getParent().getBackground()); g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2); } } // calculate diagonal int diagonalSize = (int) Math .sqrt(this.getWidth() * this.getWidth() + this.getHeight() * this.getHeight()); // draw text String text = ""; if (alternativeText != null) { text = alternativeText; } else { text = BUNDLE.getString("image.nonefound"); //$NON-NLS-1$ } if (!getParent().isOpaque()) { text = ""; } Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); AffineTransform orig = g2.getTransform(); AffineTransform at = new AffineTransform(orig); at.translate(0, this.getHeight()); at.rotate(this.getWidth(), -this.getHeight()); g2.setTransform(at); g2.setColor(Color.BLACK); g2.setFont(FONT); FontMetrics fm = g2.getFontMetrics(); int x = (diagonalSize - fm.stringWidth(text)) / 2; int y = (fm.getAscent() - fm.getDescent()) / 2; g2.drawString(text, x, y); // g2.drawLine(0, 0, diagonalSize, 0); at.translate(0, -this.getHeight()); g2.setTransform(orig); } }
From source file:edu.ku.brc.stats.StatGroupTable.java
/** * Overrides paint to draw in name at top with separator AND the Label *//*from w w w .j a va2 s . co m*/ public void paint(Graphics g) { super.paint(g); if (!useSeparator) { Dimension dim = getSize(); FontMetrics fm = g.getFontMetrics(); int strW = fm.stringWidth(name); int x = (dim.width - strW) / 2; Insets ins = getBorder().getBorderInsets(this); int y = 2 + fm.getAscent(); int lineW = dim.width - ins.left - ins.right; g.setColor(Color.BLUE.darker()); g.drawString(name, x, y); x = ins.left; y += fm.getDescent() + fm.getLeading(); g.setColor(Color.LIGHT_GRAY.brighter()); g.drawLine(x, y, x + lineW, y); y++; x++; g.setColor(Color.LIGHT_GRAY); g.drawLine(x, y, x + lineW, y); } }
From source file:nz.ac.massey.cs.gql4jung.browser.queryviews.GraphBasedQueryView.java
private void configureRenderer( RenderContext<nz.ac.massey.cs.gql4jung.browser.queryviews.GraphBasedQueryView.VisualVertex, nz.ac.massey.cs.gql4jung.browser.queryviews.GraphBasedQueryView.VisualEdge> context) { context.setVertexLabelTransformer(new Transformer<VisualVertex, String>() { @Override//from w ww. ja va2 s . c o m public String transform(VisualVertex v) { if (v instanceof TypeVertex) { TypeVertex tv = (TypeVertex) v; return tv.role; } else if (v instanceof ConstraintVertex) { ConstraintVertex cv = (ConstraintVertex) v; return cv.constraint; } else if (v instanceof GroupByVertex) { GroupByVertex cv = (GroupByVertex) v; return "group by " + cv.def; } else return ""; } }); context.setEdgeLabelTransformer(new Transformer<VisualEdge, String>() { @Override public String transform(VisualEdge e) { if (e instanceof DepEdge) { DepEdge de = (DepEdge) e; StringBuffer b = new StringBuffer(); if (de.minLength == de.maxLength) { b.append(de.maxLength); } else { b.append(de.minLength).append("-").append(de.maxLength == -1 ? "many" : de.maxLength); } return b.toString(); } else return null; } }); context.setEdgeStrokeTransformer(new Transformer<VisualEdge, Stroke>() { @Override public Stroke transform(nz.ac.massey.cs.gql4jung.browser.queryviews.GraphBasedQueryView.VisualEdge e) { if (e instanceof DepEdge) { DepEdge de = (DepEdge) e; return de.maxLength == 1 ? EDGE_STROKE : PATH_STROKE; } else if (e instanceof ConstraintEdge) { return CONSTRAINT_STROKE; } else if (e instanceof GroupByEdge) { return CONSTRAINT_STROKE; } else return EDGE_STROKE; } }); context.setVertexStrokeTransformer(new Transformer<VisualVertex, Stroke>() { @Override public Stroke transform( nz.ac.massey.cs.gql4jung.browser.queryviews.GraphBasedQueryView.VisualVertex v) { return v instanceof TypeVertex ? TYPE_VERTEX_STROKE : CONSTRAINT_VERTEX_STROKE; } }); context.setVertexShapeTransformer(new Transformer<VisualVertex, Shape>() { @Override public Shape transform(VisualVertex v) { Font f = v instanceof TypeVertex ? settings.getRoleFont() : settings.getConstraintFont(); FontMetrics FM = GraphBasedQueryView.this.getGraphics().getFontMetrics(f); int W = Math.max(settings.getMinBoxWidth(), FM.stringWidth(getLongLabel(v)) + 10); int H = v instanceof TypeVertex ? 30 : 22; if (v instanceof TypeVertex) return new RoundRectangle2D.Float(-W / 2, -H / 2, W, H, 5, 5); else return new Ellipse2D.Float(-W / 2, -H / 2, W, H); } }); context.setVertexFillPaintTransformer(new Transformer<VisualVertex, Paint>() { @Override public Paint transform(VisualVertex v) { if (v instanceof TypeVertex) return makeTransparent(settings.getRoleFillColor()); else if (v instanceof ConstraintVertex) return makeTransparent(settings.getConstraintFillColor()); else if (v instanceof GroupByVertex) return makeTransparent(settings.getGroupByFillColor()); return Color.WHITE; } }); context.setVertexDrawPaintTransformer(new Transformer<VisualVertex, Paint>() { @Override public Paint transform(VisualVertex v) { if (v instanceof TypeVertex) return settings.getRoleFrameColor(); else if (v instanceof ConstraintVertex) return settings.getConstraintFrameColor(); else if (v instanceof GroupByVertex) return settings.getGroupByFrameColor(); else return Color.BLACK; } }); context.setEdgeDrawPaintTransformer(new Transformer<VisualEdge, Paint>() { @Override public Paint transform(VisualEdge v) { if (v instanceof DepEdge) return settings.getRoleFrameColor(); else if (v instanceof ConstraintEdge) return settings.getConstraintFrameColor(); else if (v instanceof GroupByEdge) return settings.getGroupByFrameColor(); else return Color.BLACK; } }); context.setVertexFontTransformer(new Transformer<VisualVertex, Font>() { @Override public Font transform(VisualVertex v) { return v instanceof TypeVertex ? settings.getRoleFont() : settings.getConstraintFont(); } }); context.setEdgeFontTransformer(new Transformer<VisualEdge, Font>() { @Override public Font transform(VisualEdge e) { return settings.getEdgeFont(); } }); context.setArrowDrawPaintTransformer(new Transformer<VisualEdge, Paint>() { @Override public Paint transform(VisualEdge v) { return v instanceof DepEdge ? settings.getRoleFrameColor() : makeTransparent(settings.getBackground(), 255); } }); context.setArrowFillPaintTransformer(new Transformer<VisualEdge, Paint>() { @Override public Paint transform(VisualEdge v) { return v instanceof DepEdge ? settings.getRoleFrameColor() : makeTransparent(settings.getBackground(), 255); } }); }
From source file:model.DrawTopologyDiagram.java
@Override public void init() { //create a graph Graph<VertexTopology, Number> ig = Graphs.<VertexTopology, Number>synchronizedDirectedGraph( new DirectedSparseMultigraph<VertexTopology, Number>()); ObservableGraph<VertexTopology, Number> og = new ObservableGraph<VertexTopology, Number>(ig); og.addGraphEventListener(new GraphEventListener<VertexTopology, Number>() { public void handleGraphEvent(GraphEvent<VertexTopology, Number> evt) { System.err.println("got " + evt); }/*w w w.jav a2 s . co m*/ }); this.g = og; //layouts //create a graphdraw // layout = new FRLayout2<String,Number>(g); // layout = new SpringLayout<String,Number>(g); // ((FRLayout)layout).setMaxIterations(200); layout = new KKLayout<VertexTopology, Number>(g); vv = new VisualizationViewer<VertexTopology, Number>(layout, new Dimension(600, 600)); createGraph(); Container content = getContentPane(); JPanel totalCasesPanel = new JPanel(); final JPanel scaleGrids = new JPanel(new GridLayout(0, 2)); scaleGrids.add(new JLabel(" Test Cases ")); scaleGrids.add(new JLabel(" ")); totalCasesPanel.add(scaleGrids); JPanel filteredCasesPanel = new JPanel(); final JPanel filteredGrids = new JPanel(new GridLayout(0, 2)); filteredGrids.add(new JLabel(" Filtered Cases ")); filteredGrids.add(new JLabel(" ")); filteredCasesPanel.add(filteredGrids); JRootPane rp = this.getRootPane(); rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); content.setLayout(new BorderLayout()); content.setBackground(java.awt.Color.lightGray); content.setFont(new Font("Serif", Font.PLAIN, 12)); vv.getModel().getRelaxer().setSleepTime(500); vv.setGraphMouse(new DefaultModalGraphMouse<VertexTopology, Number>()); vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR); vv.setForeground(Color.white); FontMetrics fm = vv.getFontMetrics(vv.getFont()); int width = fm.stringWidth(g.toString()); Transformer<VertexTopology, Shape> vertexSize = new Transformer<VertexTopology, Shape>() { public Shape transform(VertexTopology i) { Ellipse2D circle = new Ellipse2D.Double(-20, -20, 40, 40); // in this case, the vertex is twice as large return circle; } }; vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller() { @Override public String transform(Object v) { return ((VertexTopology) v).getScreenName(); } }); vv.getRenderContext().setVertexShapeTransformer(vertexSize); //Get picked states final PickedState<VertexTopology> pickedState = vv.getPickedVertexState(); pickedState.addItemListener(new ItemListener() { ArrayList<TestCase> outputTestCase = new ArrayList<TestCase>(); final Map<String, JButton> createdBtns = new HashMap<String, JButton>(); Map<String, Integer> deviceSelected = new HashMap<String, Integer>(); // not useful ArrayList<String> endPointList = new ArrayList<String>(); @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub Object subject = e.getItem(); if (e.getStateChange() != 1) { scaleGrids.removeAll(); filteredGrids.removeAll(); endPointList.remove(getScreenName(subject)); outputTestCase.clear(); filteredGrids.repaint(); filteredGrids.add(new JLabel(" Filtered Cases ")); filteredGrids.add(new JLabel(" ")); scaleGrids.repaint(); scaleGrids.add(new JLabel(" Test Cases ")); scaleGrids.add(new JLabel(" ")); deviceSelected.clear(); } if (e.getStateChange() == 1) { for (TestCase testCase : outputTestCase) { scaleGrids.removeAll(); scaleGrids.add(new JLabel(" Test Cases ")); scaleGrids.add(new JLabel(" ")); filteredGrids.removeAll(); filteredGrids.add(new JLabel(" Filtered Cases ")); filteredGrids.add(new JLabel(" ")); } if (subject instanceof VertexTopology) { final VertexTopology edgePicked = (VertexTopology) subject; if (pickedState.isPicked(edgePicked)) { for (TestCase testCase : edgePicked.getTestCaseList()) { if (!outputTestCase.contains(testCase)) outputTestCase.add(testCase); System.out.println("The size for reference is " + testCase.inputReferenceMap.size() + testCase.getName()); System.out.println("The size for target is " + testCase.inputTargetMap.size() + testCase.getName()); } if (deviceSelected.get(edgePicked.getScreenName()) != null) deviceSelected.put(edgePicked.getScreenName(), deviceSelected.get(edgePicked.getScreenName()) + 1); else deviceSelected.put(edgePicked.getScreenName(), 1); endPointList.add(edgePicked.getScreenName()); } } } for (TestCase testCase : outputTestCase) { JButton btnCase = new JButton(testCase.getName()); scaleGrids.add(btnCase); if (testCase.getInputDeviceList().size() <= endPointList.size()) if (testCaseSelected(testCase, endPointList)) { JButton btnCaseFiltered = new JButton(testCase.getName()); filteredGrids.add(btnCaseFiltered); } ; } scaleGrids.revalidate(); scaleGrids.setVisible(true); } }); final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); graphMouse.setMode(ModalGraphMouse.Mode.PICKING); content.setPreferredSize(new Dimension(1400, 900)); content.add(vv); switchLayout = new JButton("Switch to SpringLayout"); // switchLayout.addActionListener(new ActionListener() { // // @SuppressWarnings("unchecked") // public void actionPerformed(ActionEvent ae) { // Dimension d = new Dimension(600,600); // if (switchLayout.getText().indexOf("Spring") > 0) { // switchLayout.setText("Switch to FRLayout"); // layout = new SpringLayout<String,Number>(g, // new ConstantTransformer(EDGE_LENGTH)); // layout.setSize(d); // vv.getModel().setGraphLayout(layout, d); // } else { // switchLayout.setText("Switch to SpringLayout"); // layout = new FRLayout<String,Number>(g, d); // vv.getModel().setGraphLayout(layout, d); // } // } // }); JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, totalCasesPanel, filteredCasesPanel); jSplitPane.setResizeWeight(.5d); content.add(switchLayout, BorderLayout.SOUTH); content.add(jSplitPane, BorderLayout.EAST); }
From source file:com.celements.photo.image.GenerateThumbnail.java
private FontMetrics calcWatermarkFontSize(String watermark, int width, Graphics2D g2d) { FontMetrics metrics; int fontSize = 1; do {//from w ww . j a v a 2s . c o m metrics = g2d.getFontMetrics(new Font(g2d.getFont().getFontName(), Font.BOLD, fontSize)); fontSize++; } while (metrics.stringWidth(watermark) < (0.8 * width)); return metrics; }
From source file:org.jas.dnd.DragTooltipDialog.java
private DynamicPanel getDynamicPanel(IconType type, List<?> list) { if (list == null || list.isEmpty()) { return null; }/*from ww w . jav a 2 s . co m*/ JPanel dynamicPanel = null; JLabel dynamicText = null; String text = list.isEmpty() ? null : (list.size()) + " " + (type == null ? "ERROR" : type.getText()); if (list.size() == 1) { text = list.get(0) instanceof File ? ((File) list.get(0)).getName() : list.get(0).toString(); } dynamicText = new JLabel(text); dynamicText.setForeground(Color.WHITE); FontMetrics fontMetrics = dynamicText.getFontMetrics(dynamicText.getFont()); int width = fontMetrics.stringWidth(dynamicText.getText()) + DEFAULT_MIN_FONT_WIDTH; int realHeight = ROW_HEIGHT; String longestText = ""; while (text.contains("<br>")) { text = text.substring(text.indexOf("<br>") + DEFAULT_MIN_FONT_WIDTH); if (text.length() > longestText.length()) { longestText = text; } realHeight += ROW_HEIGHT; } if (!longestText.isEmpty()) { width = fontMetrics.stringWidth(longestText) + DEFAULT_MIN_FONT_WIDTH; } dynamicPanel = new JPanel(); dynamicPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); dynamicPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, realHeight)); dynamicPanel.setMinimumSize(new Dimension(0, realHeight)); dynamicPanel.setPreferredSize(new Dimension(100, realHeight)); dynamicPanel.setSize(new Dimension(100, realHeight)); if (type != null) { dynamicPanel.add(getDynamicIcon(type), null); JPanel spacer = new JPanel(); Dimension d = new Dimension(SPACER_WIDTH, SPACER_WIDTH); width += SPACER_WIDTH; spacer.setSize(d); spacer.setMinimumSize(d); spacer.setMaximumSize(d); spacer.setPreferredSize(d); dynamicPanel.add(spacer); width += type.width; } dynamicPanel.add(dynamicText); return new DynamicPanel(dynamicPanel, width, realHeight); }
From source file:com.ctsim.dmi.MainFrame.java
private void drawTargetDestance() { String str = df.format(App.targetDistance) + " m."; int strWidth; double barHeight; g2.drawImage(targetDestination, 30, 90, this); g2.setColor(Color.GREEN);/*from w w w .ja v a2 s . com*/ g2.setFont(new Font("Loma", Font.PLAIN, 18)); FontMetrics metrics = g2.getFontMetrics(); strWidth = metrics.stringWidth(str); g2.drawString(str, 40 - (strWidth / 2), 70); barHeight = (App.targetDistanceActual / App.targetDistance) * 360; g2.fillRect(52, (int) (450 - barHeight), 14, (int) barHeight); }
From source file:com.ctsim.dmi.MainFrame.java
private void drawCeillingSpeed() { int strWidth; String speedShow = df.format(App.ceilingSpeed); g2.setColor(Color.RED);/*w w w . j a va2 s .c om*/ g2.setFont(new Font("Loma", Font.BOLD, 50)); FontMetrics metrics = g2.getFontMetrics(); strWidth = metrics.stringWidth(speedShow); g2.drawString(String.valueOf(speedShow), 306 - strWidth / 2, 535); g2.setColor(Color.GREEN); g2.setStroke(new BasicStroke(15)); int arc = (int) (App.ceilingSpeed * 2.76); g2.drawArc(90, 40, 430, 430, 228 - arc, arc); }
From source file:edu.ku.brc.af.core.NavBox.java
@Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); FontMetrics fm = this.getFontMetrics(getFont()); int width = fm.stringWidth(name); Insets insets = getBorder().getBorderInsets(this); width += insets.left + insets.right; size.width = Math.max(size.width, width); // if we're putting the items in a scrollpane, return 180 as the maximum preferred height if (scrollable) { if (size.height > MAX_HEIGHT) { size.height = MAX_HEIGHT;/* w w w .j a v a 2 s. c o m*/ } } if (collapsed) { size.height = minHeight; } //log.debug("NavBox preferred size: " + size); return size; }