List of usage examples for java.awt Graphics setColor
public abstract void setColor(Color c);
From source file:FrameDemo2.java
protected static Image createFDImage() { // Create a 16x16 pixel image. BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB); // Draw into it. Graphics g = bi.getGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, 15, 15);//from w w w . ja v a2 s. c o m g.setColor(Color.RED); g.fillOval(5, 3, 6, 6); // Clean up. g.dispose(); // Return it. return bi; }
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/*w ww .ja va 2s .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:Main.java
public static void fillHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) { int steps = colors.length; double dy = (double) h / (double) (steps); if (dy <= 3.001) { int y1 = y; for (int i = 0; i < steps; i++) { int y2 = y + (int) Math.round((double) i * dy); g.setColor(colors[i]); if (i == (steps - 1)) { g.fillRect(x, y1, w, y + h - y1); } else { g.fillRect(x, y1, w, y2 - y1); }//from w w w.j a va2s .c om y1 = y2; } } else { smoothFillHorGradient(g, colors, x, y, w, h); } }
From source file:Main.java
public static void fillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) { int steps = colors.length; double dy = (double) h / (double) steps; if (dy <= 3.001) { int y1 = y; for (int i = 0; i < steps; i++) { int y2 = y + (int) Math.round((double) i * dy); g.setColor(colors[colors.length - i - 1]); if (i == (steps - 1)) { g.fillRect(x, y1, w, y + h - y1); } else { g.fillRect(x, y1, w, y2 - y1); }/*from w w w .j a v a2 s. c o m*/ y1 = y2; } } else { smoothFillInverseHorGradient(g, colors, x, y, w, h); } }
From source file:com.klwork.common.utils.WebUtils.java
/** * ???//from w w w . j av a 2 s. co 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:Main.java
public static void smoothFillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) { Graphics2D g2D = (Graphics2D) g; Paint savedPaint = g2D.getPaint(); int steps = colors.length; double dy = (double) h / (double) steps; int y1 = y;//from w w w. j a va 2 s.c o m for (int i = 0; i < steps; i++) { int y2 = y + (int) Math.round((double) i * dy); g.setColor(colors[colors.length - i - 1]); if (i == (steps - 1)) { g2D.setPaint(null); g2D.setColor(colors[colors.length - i - 1]); g.fillRect(x, y1, w, y + h - y1); } else { g2D.setPaint(new GradientPaint(0, y1, colors[colors.length - i - 1], 0, y2, colors[colors.length - i - 2])); g.fillRect(x, y1, w, y2 - y1); } y1 = y2; } g2D.setPaint(savedPaint); }
From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java
/** * /*from w w w . ja v a 2 s. c o 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:net.sf.webphotos.tools.Thumbnail.java
private static boolean save(Image thumbnail, String nmArquivo) { try {// www .j a va2 s.c om // This code ensures that all the // pixels in the image are loaded. Image temp = new ImageIcon(thumbnail).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); g.drawImage(temp, 0, 0, null); g.dispose(); // write the jpeg to a file File file = new File(nmArquivo); // Recria o arquivo se existir if (file.exists()) { Util.out.println("Redefinindo a Imagem: " + nmArquivo); file.delete(); file = new File(nmArquivo); } // encodes image as a JPEG file ImageIO.write(bufferedImage, IMAGE_FORMAT, file); return true; } catch (IOException ioex) { ioex.printStackTrace(Util.err); return false; } }
From source file:PaintUtils.java
/** Uses translucent shades of white and black to draw highlights * and shadows around a rectangle, and then frames the rectangle * with a shade of gray (120)./*from w w w .ja v a2 s .co m*/ * <P>This should be called to add a finishing touch on top of * existing graphics. * @param g the graphics to paint to. * @param r the rectangle to paint. */ public static void drawBevel(Graphics g, Rectangle r) { drawColors(blacks, g, r.x, r.y + r.height, r.x + r.width, r.y + r.height, SwingConstants.SOUTH); drawColors(blacks, g, r.x + r.width, r.y, r.x + r.width, r.y + r.height, SwingConstants.EAST); drawColors(whites, g, r.x, r.y, r.x + r.width, r.y, SwingConstants.NORTH); drawColors(whites, g, r.x, r.y, r.x, r.y + r.height, SwingConstants.WEST); g.setColor(new Color(120, 120, 120)); g.drawRect(r.x, r.y, r.width, r.height); }
From source file:Main.java
public static void drawImage(BufferedImage image, Graphics g, Component parent) { Dimension size = parent.getSize(); Color background = parent.getBackground(); if (image != null && size.width > 0) { double ratio = (double) image.getHeight(null) / image.getWidth(null); int effectiveWidth = 1; int effectiveHeight = (int) ratio; while (effectiveHeight < size.height && effectiveWidth < size.width) { effectiveWidth++;/*from ww w .j av a 2 s . co m*/ effectiveHeight = (int) (ratio * effectiveWidth); } g.setColor(background); g.fillRect(0, 0, size.width, size.height); int cornerx = Math.abs((size.width - effectiveWidth) / 2); int cornery = Math.abs((size.height - effectiveHeight) / 2); g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0, image.getWidth(null), image.getHeight(null), null); } }