List of usage examples for java.awt Graphics setFont
public abstract void setFont(Font font);
From source file:GraphicsUtil.java
static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign, int valign) { if (g == null) return new Rectangle(x, y, 0, 0); Font oldfont = g.getFont();/* ww w. j av a 2 s. c om*/ if (font != null) g.setFont(font); Rectangle ret = getTextBounds(g, text, x, y, halign, valign); if (font != null) g.setFont(oldfont); return ret; }
From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java
/** * //ww w .ja v a2s . co m * @param request * @param response * @param cacheManagerForResource */ public static void getRandcode(HttpServletRequest request, HttpServletResponse response, CacheManagerForResource cacheManagerForResource) { // BufferedImageImage,Image???? BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.getGraphics(); // ImageGraphics,????? g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18)); g.setColor(getRandColor(110, 133)); // for (int i = 0; i <= lineSize; i++) { drowLine(g); } // ? String randomString = ""; for (int i = 1; i <= stringNum; i++) { randomString = drowString(g, randomString, i); } String key = null; if (request.getCookies() != null) { for (Cookie tmp : request.getCookies()) { if (tmp.getName().equals(Constants.RANDOMCODEKEY)) { key = tmp.getName(); cacheManagerForResource.removeFromCache(key); break; } } } if (StringUtils.isEmpty(key)) { key = String.valueOf(System.nanoTime()); } cacheManagerForResource.setToCache(key, randomString); final Cookie cookie = new Cookie(Constants.RANDOMCODEKEY, key); cookie.setPath(Constants.COOKIE_PATH); response.addCookie(cookie); g.dispose(); try { ImageIO.write(image, "JPEG", response.getOutputStream()); // ?? } catch (Exception e) { LOG.info(e.getMessage()); } }
From source file:Main.java
/** * Prints a <code>Document</code> using a monospaced font, word wrapping on * the characters ' ', '\t', '\n', ',', '.', and ';'. This method is * expected to be called from Printable 'print(Graphics g)' functions. * * @param g The graphics context to write to. * @param doc The <code>javax.swing.text.Document</code> to print. * @param fontSize the point size to use for the monospaced font. * @param pageIndex The page number to print. * @param pageFormat The format to print the page with. * @param tabSize The number of spaces to expand tabs to. * * @see #printDocumentMonospaced/*from w ww . j a v a 2 s . c o m*/ */ public static int printDocumentMonospacedWordWrap(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize) { g.setColor(Color.BLACK); g.setFont(new Font("Monospaced", Font.PLAIN, fontSize)); // Initialize our static variables (these are used by our tab expander below). tabSizeInSpaces = tabSize; fm = g.getFontMetrics(); // Create our tab expander. //RPrintTabExpander tabExpander = new RPrintTabExpander(); // Get width and height of characters in this monospaced font. int fontWidth = fm.charWidth('w'); // Any character will do here, since font is monospaced. int fontHeight = fm.getHeight(); int MAX_CHARS_PER_LINE = (int) pageFormat.getImageableWidth() / fontWidth; int MAX_LINES_PER_PAGE = (int) pageFormat.getImageableHeight() / fontHeight; final int STARTING_LINE_NUMBER = MAX_LINES_PER_PAGE * pageIndex; // The (x,y) coordinate to print at (in pixels, not characters). // Since y is the baseline of where we'll start printing (not the top-left // corner), we offset it by the font's ascent ( + 1 just for good measure). xOffset = (int) pageFormat.getImageableX(); int y = (int) pageFormat.getImageableY() + fm.getAscent() + 1; // A counter to keep track of the number of lines that WOULD HAVE been // printed if we were printing all lines. int numPrintedLines = 0; // Keep going while there are more lines in the document. currentDocLineNumber = 0; // The line number of the document we're currently on. rootElement = doc.getDefaultRootElement(); // To shorten accesses in our loop. numDocLines = rootElement.getElementCount(); // The number of lines in our document. while (currentDocLineNumber < numDocLines) { // Get the line we are going to print. String curLineString; Element currentLine = rootElement.getElement(currentDocLineNumber); int startOffs = currentLine.getStartOffset(); try { curLineString = doc.getText(startOffs, currentLine.getEndOffset() - startOffs); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); return Printable.NO_SUCH_PAGE; } // Remove newlines, because they end up as boxes if you don't; this is a monospaced font. curLineString = curLineString.replaceAll("\n", ""); // Replace tabs with how many spaces they should be. if (tabSizeInSpaces == 0) { curLineString = curLineString.replaceAll("\t", ""); } else { int tabIndex = curLineString.indexOf('\t'); while (tabIndex > -1) { int spacesNeeded = tabSizeInSpaces - (tabIndex % tabSizeInSpaces); String replacementString = ""; for (int i = 0; i < spacesNeeded; i++) replacementString += ' '; // Note that "\t" is actually a regex for this method. curLineString = curLineString.replaceFirst("\t", replacementString); tabIndex = curLineString.indexOf('\t'); } } // If this document line is too long to fit on one printed line on the page, // break it up into multpile lines. while (curLineString.length() > MAX_CHARS_PER_LINE) { int breakPoint = getLineBreakPoint(curLineString, MAX_CHARS_PER_LINE) + 1; numPrintedLines++; if (numPrintedLines > STARTING_LINE_NUMBER) { g.drawString(curLineString.substring(0, breakPoint), xOffset, y); y += fontHeight; if (numPrintedLines == STARTING_LINE_NUMBER + MAX_LINES_PER_PAGE) return Printable.PAGE_EXISTS; } curLineString = curLineString.substring(breakPoint, curLineString.length()); } currentDocLineNumber += 1; // We have printed one more line from the document. numPrintedLines++; if (numPrintedLines > STARTING_LINE_NUMBER) { g.drawString(curLineString, xOffset, y); y += fontHeight; if (numPrintedLines == STARTING_LINE_NUMBER + MAX_LINES_PER_PAGE) return Printable.PAGE_EXISTS; } } // Now, the whole document has been "printed." Decide if this page had any text on it or not. if (numPrintedLines > STARTING_LINE_NUMBER) return Printable.PAGE_EXISTS; return Printable.NO_SUCH_PAGE; }
From source file:com.klwork.common.utils.WebUtils.java
/** * ???/*from w w w .j a va2 s . c o m*/ * @param request * @param response */ public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ? Graphics g = image.getGraphics(); // ?? Random random = new Random(); // g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // ?155????? g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789"; // ????(4?) String sRand = ""; for (int i = 0; i < 4; i++) { int start = random.nextInt(base.length()); String rand = base.substring(start, start + 1); sRand = sRand.concat(rand); // ?? g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 13 * i + 6, 16); } // ??SESSION request.getSession().setAttribute("entrymrand", sRand); // g.dispose(); OutputStream out = response.getOutputStream(); // ? ImageIO.write(image, "JPEG", out); out.flush(); out.close(); }
From source file:ala.soils2sat.DrawingUtils.java
public static Rectangle drawString(Graphics g, Font font, String text, int x, int y, int width, int height, int align, boolean wrap) { g.setFont(font); FontMetrics fm = g.getFontMetrics(font); setPreferredAliasingMode(g);//from w w w .j a va2 s. co m Rectangle ret = new Rectangle(0, 0, 0, 0); if (text == null) { return ret; } String[] alines = text.split("\\n"); ArrayList<String> lines = new ArrayList<String>(); for (String s : alines) { if (wrap && fm.stringWidth(s) > width) { // need to split this up into multiple lines... List<String> splitLines = wrapString(s, fm, width); lines.addAll(splitLines); } else { lines.add(s); } } int numlines = lines.size(); while (fm.getHeight() * numlines > height) { numlines--; } if (numlines > 0) { int maxwidth = 0; int minxoffset = y + width; int totalheight = (numlines * fm.getHeight()); int linestart = ((height / 2) - (totalheight / 2)); if (!wrap) { ret.y = y + linestart; } else { ret.y = y; linestart = 0; } for (int idx = 0; idx < numlines; ++idx) { String line = lines.get(idx); int stringWidth = fm.stringWidth(line); // the width of the label depends on the font : // if the width of the label is larger than the item if (stringWidth > 0 && width < stringWidth) { // We have to truncate the label line = clipString(null, fm, line, width); stringWidth = fm.stringWidth(line); } int xoffset = 0; int yoffset = linestart + fm.getHeight() - fm.getDescent(); if (align == TEXT_ALIGN_RIGHT) { xoffset = (width - stringWidth); } else if (align == TEXT_ALIGN_CENTER) { xoffset = (int) Math.round((double) (width - stringWidth) / (double) 2); } if (xoffset < minxoffset) { minxoffset = xoffset; } g.drawString(line, x + xoffset, y + yoffset); if (stringWidth > maxwidth) { maxwidth = stringWidth; } linestart += fm.getHeight(); } ret.width = maxwidth; ret.height = totalheight; ret.x = x + minxoffset; // Debug only... if (DEBUG) { Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(1)); g.setColor(Color.blue); g.drawRect(ret.x, ret.y, ret.width, ret.height); g.setColor(Color.green); g.drawRect(x, y, width, height); } return ret; } return ret; }
From source file:VASSAL.counters.Labeler.java
public static void drawLabel(Graphics g, String text, int x, int y, Font f, int hAlign, int vAlign, Color fgColor, Color bgColor, Color borderColor) { g.setFont(f); final int width = g.getFontMetrics().stringWidth(text + " "); final int height = g.getFontMetrics().getHeight(); int x0 = x;//w ww . j av a 2 s. c o m int y0 = y; switch (hAlign) { case CENTER: x0 = x - width / 2; break; case LEFT: x0 = x - width; break; } switch (vAlign) { case CENTER: y0 = y - height / 2; break; case BOTTOM: y0 = y - height; break; } if (bgColor != null) { g.setColor(bgColor); g.fillRect(x0, y0, width, height); } if (borderColor != null) { g.setColor(borderColor); g.drawRect(x0, y0, width, height); } g.setColor(fgColor); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawString(" " + text + " ", x0, y0 + g.getFontMetrics().getHeight() - g.getFontMetrics().getDescent()); }
From source file:Main.java
public void paint(Graphics g) { g.setFont(new Font("SansSerif", Font.BOLD, 12)); FontMetrics fm = g.getFontMetrics(); g.drawString("Current font: " + g.getFont(), 10, 40); g.drawString("Ascent: " + fm.getAscent(), 10, 55); g.drawString("Descent: " + fm.getDescent(), 10, 70); g.drawString("Height: " + fm.getHeight(), 10, 85); g.drawString("Leading: " + fm.getLeading(), 10, 100); Font font = new Font("Serif", Font.ITALIC, 14); fm = g.getFontMetrics(font);// w w w . j av a 2 s. co m g.setFont(font); g.drawString("Current font: " + font, 10, 130); g.drawString("Ascent: " + fm.getAscent(), 10, 145); g.drawString("Descent: " + fm.getDescent(), 10, 160); g.drawString("Height: " + fm.getHeight(), 10, 175); g.drawString("Leading: " + fm.getLeading(), 10, 190); }
From source file:StringRectPaintPanel.java
public void paint(Graphics g) { g.setFont(new Font("", 0, 100)); FontMetrics fm = getFontMetrics(new Font("", 0, 100)); String s = "java2s"; int x = 5;// w ww . j a va 2s . c o m int y = 5; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int h = fm.getHeight(); int w = fm.charWidth(c); g.drawRect(x, y, w, h); g.drawString(String.valueOf(c), x, y + h); x = x + w; } }
From source file:Main.java
public void paint(Graphics g) { int fontSize = 20; g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); g.drawString("www.java2s.com", 10, 20); }
From source file:Main.java
public void paint(Graphics g) { int fontSize = 20; g.setFont(new Font("TimesRoman", Font.ITALIC, fontSize)); String s = "www.java2s.com"; g.setColor(Color.black);//from w w w . jav a 2 s . co m g.drawString(s, 30, 30); }