List of usage examples for java.awt Graphics2D getFontMetrics
public FontMetrics getFontMetrics()
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and pie chart from summary data * /*w w w . j a v a 2 s . c o m*/ * @param width the width of the resulting image * @param height the height of the resulting image * @param values the values for the chart * @param freqs the corresponding frequencies * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedPieAndHistogramFromSummaryData(int width, int height, List<String> values, List<Double> freqs, List<String> additionalArgs) throws Exception { String plotTitle = "Combined Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; List<String> opts = new ArrayList<String>(); opts.add("-title=distribution"); BufferedImage pie = renderPieChartFromSummaryData(width / 2, height, values, freqs, false, true, opts); opts.clear(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, values, freqs, opts); BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2d.setFont(new Font("SansSerif", Font.BOLD, 12)); g2d.setColor(Color.lightGray); g2d.fillRect(0, 0, width, height + 20); g2d.setColor(Color.black); FontMetrics fm = g2d.getFontMetrics(); int fh = fm.getHeight(); int sw = fm.stringWidth(plotTitle); g2d.drawImage(pie, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and box plot chart from summary data * /*from w w w. j a v a2 s .c o m*/ * @param width the width of the resulting image * @param height the height of the resulting image * @param bins the values for the chart * @param freqs the corresponding frequencies * @param summary the summary stats for the box plot * @param outliers an optional list of outliers for the box plot * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedBoxPlotAndHistogramFromSummaryData(int width, int height, List<String> bins, List<Double> freqs, List<Double> summary, List<Double> outliers, List<String> additionalArgs) throws Exception { String plotTitle = "Combined Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; List<String> opts = new ArrayList<String>(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, bins, freqs, opts); opts.clear(); opts.add("-title=box plot"); BufferedImage box = null; try { box = renderBoxPlotFromSummaryData(width / 2, height, summary, outliers, opts); } catch (Exception ex) { // if we can't generate the box plot (i.e. probably because // data is 100% missing) then just return the histogram } if (box == null) { width /= 2; } BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2d.setFont(new Font("SansSerif", Font.BOLD, 12)); g2d.setColor(Color.lightGray); g2d.fillRect(0, 0, width, height + 20); g2d.setColor(Color.black); FontMetrics fm = g2d.getFontMetrics(); int fh = fm.getHeight(); int sw = fm.stringWidth(plotTitle); if (box != null) { g2d.drawImage(box, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); } else { g2d.drawImage(hist, 0, 20, null); } g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:BasicDraw.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; FontMetrics fontMetrics = g2d.getFontMetrics(); int width = fontMetrics.stringWidth("aString"); int height = fontMetrics.getHeight(); System.out.println(width);//from ww w .j a v a 2 s .co m System.out.println(height); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); String text = "I don't see the problem"; FontMetrics fm = g2d.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent(); g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2)); g2d.drawString(text, x, y);//from www .j a v a 2 s.c o m g2d.dispose(); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); String text = getWidth() + "x" + getHeight(); FontMetrics fm = g2d.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.drawString(text, x, y);/* w ww. ja v a2s .c om*/ g2d.dispose(); }
From source file:FontSizeAnimation.java
public void paint(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; Font font = new Font("Dialog", Font.PLAIN, x); g2d.setFont(font);//from w w w . ja v a2 s . c om FontMetrics fm = g2d.getFontMetrics(); String s = "Java"; int w = (int) getSize().getWidth(); int h = (int) getSize().getHeight(); int stringWidth = fm.stringWidth(s); g2d.drawString(s, (w - stringWidth) / 2, h / 2); }
From source file:org.evors.rs.ui.sandpit.TrialViewer.java
public void drawText(Graphics2D g2, String text) { FontMetrics fm = g2.getFontMetrics(); List<String> strings = Splitter.on("\n").splitToList(text); float x1 = 40, y = 40; int dx = 0;//from w ww. ja v a 2 s . c o m for (String s : strings) { int width = fm.stringWidth(s); if (width > dx) { dx = width; } } g2.setColor(new Color(0.6f, 0.6f, 0.6f, 0.6f)); g2.fillRect((int) x1, (int) y - fm.getHeight(), dx + 5, fm.getHeight() * strings.size() + 5); g2.setColor(Color.red); for (String s : strings) { g2.drawString(s, 40, y); y += fm.getHeight(); } }
From source file:com.alibaba.webx.tutorial.app1.module.screen.simple.SayHiImage.java
private void writeImage(OutputStream out) throws IOException { BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.setPaint(Color.red);//from w w w . jav a 2 s . co m g2d.setFont(new Font("Serif", Font.BOLD, 36)); g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight()); g2d.dispose(); ImageIO.write(img, "jpg", out); }
From source file:$.SayHiImage.java
private void writeImage(OutputStream out) throws IOException { BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.setPaint(Color.red);//from w w w .ja v a2 s .c om g2d.setFont(new Font("Serif", Font.BOLD, 36)); g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight()); g2d.dispose(); ImageIO.write(img, "jpg", out); }
From source file:Main.java
public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE);//from w w w. ja v a 2s . co m Font font = new Font("Verdana", Font.PLAIN, 20); g2d.setFont(font); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = g2d.getFontMetrics(); String option = "This is a test"; int x = (getWidth() - fm.stringWidth(option)) / 2; int y = ((getHeight() - fm.getHeight()) / 2); g2d.drawString(option, x, y + fm.getAscent()); g2d.drawRect((int) x - 20, (int) y - 10, (int) fm.stringWidth(option) + 40, (int) fm.getHeight() + 20); }