List of usage examples for java.awt Graphics2D setColor
public abstract void setColor(Color c);
From source file:org.apache.ofbiz.common.CommonEvents.java
public static String getCaptcha(HttpServletRequest request, HttpServletResponse response) { try {/*from w ww . j av a 2 s . c o m*/ Delegator delegator = (Delegator) request.getAttribute("delegator"); final String captchaSizeConfigName = StringUtils.defaultIfEmpty(request.getParameter("captchaSize"), "default"); final String captchaSizeConfig = EntityUtilProperties.getPropertyValue("captcha", "captcha." + captchaSizeConfigName, delegator); final String[] captchaSizeConfigs = captchaSizeConfig.split("\\|"); final String captchaCodeId = StringUtils.defaultIfEmpty(request.getParameter("captchaCodeId"), ""); // this is used to uniquely identify in the user session the attribute where the captcha code for the last captcha for the form is stored final int fontSize = Integer.parseInt(captchaSizeConfigs[0]); final int height = Integer.parseInt(captchaSizeConfigs[1]); final int width = Integer.parseInt(captchaSizeConfigs[2]); final int charsToPrint = UtilProperties.getPropertyAsInteger("captcha", "captcha.code_length", 6); final char[] availableChars = EntityUtilProperties .getPropertyValue("captcha", "captcha.characters", delegator).toCharArray(); //It is possible to pass the font size, image width and height with the request as well Color backgroundColor = Color.gray; Color borderColor = Color.DARK_GRAY; Color textColor = Color.ORANGE; Color circleColor = new Color(160, 160, 160); Font textFont = new Font("Arial", Font.PLAIN, fontSize); int circlesToDraw = 6; float horizMargin = 20.0f; double rotationRange = 0.7; // in radians BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bufferedImage.getGraphics(); g.setColor(backgroundColor); g.fillRect(0, 0, width, height); //Generating some circles for background noise g.setColor(circleColor); for (int i = 0; i < circlesToDraw; i++) { int circleRadius = (int) (Math.random() * height / 2.0); int circleX = (int) (Math.random() * width - circleRadius); int circleY = (int) (Math.random() * height - circleRadius); g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2); } g.setColor(textColor); g.setFont(textFont); FontMetrics fontMetrics = g.getFontMetrics(); int maxAdvance = fontMetrics.getMaxAdvance(); int fontHeight = fontMetrics.getHeight(); String captchaCode = RandomStringUtils.random(6, availableChars); float spaceForLetters = -horizMargin * 2 + width; float spacePerChar = spaceForLetters / (charsToPrint - 1.0f); for (int i = 0; i < captchaCode.length(); i++) { // this is a separate canvas used for the character so that // we can rotate it independently int charWidth = fontMetrics.charWidth(captchaCode.charAt(i)); int charDim = Math.max(maxAdvance, fontHeight); int halfCharDim = (charDim / 2); BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB); Graphics2D charGraphics = charImage.createGraphics(); charGraphics.translate(halfCharDim, halfCharDim); double angle = (Math.random() - 0.5) * rotationRange; charGraphics.transform(AffineTransform.getRotateInstance(angle)); charGraphics.translate(-halfCharDim, -halfCharDim); charGraphics.setColor(textColor); charGraphics.setFont(textFont); int charX = (int) (0.5 * charDim - 0.5 * charWidth); charGraphics.drawString("" + captchaCode.charAt(i), charX, ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent())); float x = horizMargin + spacePerChar * (i) - charDim / 2.0f; int y = ((height - charDim) / 2); g.drawImage(charImage, (int) x, y, charDim, charDim, null, null); charGraphics.dispose(); } // Drawing the image border g.setColor(borderColor); g.drawRect(0, 0, width - 1, height - 1); g.dispose(); response.setContentType("image/jpeg"); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); HttpSession session = request.getSession(); Map<String, String> captchaCodeMap = UtilGenerics.checkMap(session.getAttribute("_CAPTCHA_CODE_")); if (captchaCodeMap == null) { captchaCodeMap = new HashMap<String, String>(); session.setAttribute("_CAPTCHA_CODE_", captchaCodeMap); } captchaCodeMap.put(captchaCodeId, captchaCode); } catch (Exception ioe) { Debug.logError(ioe.getMessage(), module); } return "success"; }
From source file:com.github.lucapino.sheetmaker.PreviewJFrame.java
public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); // This is what we want, but it only does hard-clipping, i.e. aliasing // g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape // in fully opaque white with antialiasing enabled... g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); // ... then compositing the image on top, // using the white shape from above as alpha source g2.setComposite(AlphaComposite.SrcIn); g2.drawImage(image, 0, 0, null);/*from ww w . ja v a 2 s .c o m*/ g2.dispose(); return output; }
From source file:com.zacwolf.commons.email.Email.java
public static BufferedImage makeRoundedBanner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight() + 10; BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null);/*w w w . j a v a 2 s. c o m*/ g2.setComposite(AlphaComposite.SrcOver); // g2.setColor(new Color(153,153,153));//slight grey border // g2.drawRoundRect(0, 0, w-1, h, cornerRadius, cornerRadius); g2.dispose(); return output.getSubimage(0, 0, image.getWidth(), image.getHeight()); }
From source file:com.zacwolf.commons.email.Email.java
public static BufferedImage makeRoundedFooter(int width, int cornerRadius, Color bgcolor, Color border) throws Exception { int height = (cornerRadius * 2) + 10; BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(bgcolor); g2.fillRoundRect(0, 0, width, height - 1, cornerRadius, cornerRadius); g2.setComposite(AlphaComposite.SrcOver); if (border != null) { g2.setColor(border);/*from w w w . java2s.c o m*/ g2.drawRoundRect(0, 0, width - 1, height - 2, cornerRadius, cornerRadius); } g2.dispose(); Rectangle clip = createClip(output, new Dimension(width, cornerRadius), 0, height - cornerRadius - 1); return output.getSubimage(clip.x, clip.y, clip.width, clip.height); }
From source file:com.piaoyou.util.ImageUtil.java
public static BufferedImage getProductionImage(String productVersion, String productRealeaseDate) { String str = productVersion + " on " + productRealeaseDate; int IMAGE_WIDTH = 250; int IMAGE_HEIGHT = 30; BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.setBackground(Color.blue);/*from w ww.j av a 2 s . com*/ g.setColor(Color.white); g.draw3DRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, false); FontMetrics fontMetrics = g.getFontMetrics(); int strWidth = fontMetrics.stringWidth(str); int strHeight = fontMetrics.getAscent() + fontMetrics.getDescent(); g.drawString(str, (IMAGE_WIDTH - strWidth) / 2, IMAGE_HEIGHT - ((IMAGE_HEIGHT - strHeight) / 2) - fontMetrics.getDescent()); g.dispose(); // free resource return bufferedImage; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
/** * ?????????/*from ww w . java 2s . c o m*/ * * @param imgfile * @param dim * @return */ public static BufferedImage shrinkImage(BufferedImage imgfile, int width, int height) { int iwidth = imgfile.getWidth(); int iheight = imgfile.getHeight(); double ratio = Math.min((double) width / (double) iwidth, (double) height / (double) iheight); int shrinkedWidth = (int) (iwidth * ratio); int shrinkedHeight = (int) (iheight * ratio); // ?? Image targetImage = imgfile.getScaledInstance(shrinkedWidth, shrinkedHeight, Image.SCALE_AREA_AVERAGING); BufferedImage tmpImage = new BufferedImage(targetImage.getWidth(null), targetImage.getHeight(null), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = tmpImage.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, shrinkedWidth, shrinkedHeight); g.drawImage(targetImage, 0, 0, null); return tmpImage; }
From source file:com.aimluck.eip.fileupload.util.FileuploadUtils.java
public static BufferedImage transformImage(BufferedImage image, AffineTransform transform, int newWidth, int newHeight) throws Exception { AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); BufferedImage destinationImage = new BufferedImage(newWidth, newHeight, image.getType()); Graphics2D g = destinationImage.createGraphics(); g.setColor(Color.WHITE); destinationImage = op.filter(image, destinationImage); return destinationImage; }
From source file:Hexagon.java
public static BufferedImage drawCodePoint(char codePoint, int width, int height, Font font, Color color) { BufferedImage img = createImage(width, height); Graphics2D g2 = img.createGraphics(); String text = "" + codePoint; g2.setColor(color); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), text); g2.drawGlyphVector(gv, 0f, (float) gv.getGlyphMetrics(0).getBounds2D().getHeight()); return img;/*from w w w. j ava2s .c om*/ }
From source file:com.ricemap.spateDB.operations.Plot.java
public static <S extends Shape> void plotLocal(Path inFile, Path outFile, S shape, int width, int height, Color color, boolean showBorders, boolean showBlockCount, boolean showRecordCount) throws IOException { FileSystem inFs = inFile.getFileSystem(new Configuration()); Prism fileMbr = FileMBR.fileMBRLocal(inFs, inFile, shape); LOG.info("FileMBR: " + fileMbr); // Adjust width and height to maintain aspect ratio if ((fileMbr.x2 - fileMbr.x1) / (fileMbr.y2 - fileMbr.y1) > (double) width / height) { // Fix width and change height height = (int) ((fileMbr.y2 - fileMbr.y1) * width / (fileMbr.x2 - fileMbr.x1)); } else {/*from www . j av a 2 s . c om*/ width = (int) ((fileMbr.x2 - fileMbr.x1) * height / (fileMbr.y2 - fileMbr.y1)); } double scale2 = (double) width * height / ((double) (fileMbr.x2 - fileMbr.x1) * (fileMbr.y2 - fileMbr.y1)); // Create an image BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = image.createGraphics(); Color bg_color = new Color(0, 0, 0, 0); graphics.setBackground(bg_color); graphics.clearRect(0, 0, width, height); graphics.setColor(color); long fileLength = inFs.getFileStatus(inFile).getLen(); ShapeRecordReader<S> reader = new ShapeRecordReader<S>(inFs.open(inFile), 0, fileLength); Prism cell = reader.createKey(); while (reader.next(cell, shape)) { drawShape(graphics, shape, fileMbr, width, height, scale2); } reader.close(); graphics.dispose(); FileSystem outFs = outFile.getFileSystem(new Configuration()); OutputStream out = outFs.create(outFile, true); ImageIO.write(image, "png", out); out.close(); }
From source file:net.pms.util.UMSUtils.java
@SuppressWarnings("deprecation") public static InputStream scaleThumb(InputStream in, RendererConfiguration r) throws IOException { if (in == null) { return in; }/*from ww w .j a v a 2s .c o m*/ String ts = r.getThumbSize(); if (StringUtils.isEmpty(ts) && StringUtils.isEmpty(r.getThumbBG())) { // no need to convert here return in; } int w; int h; Color col = null; BufferedImage img; try { img = ImageIO.read(in); } catch (Exception e) { // catch whatever is thrown at us // we can at least log it LOGGER.debug("couldn't read thumb to manipulate it " + e); img = null; // to make sure } if (img == null) { return in; } w = img.getWidth(); h = img.getHeight(); if (StringUtils.isNotEmpty(ts)) { // size limit thumbnail w = getHW(ts.split("x"), 0); h = getHW(ts.split("x"), 1); if (w == 0 || h == 0) { LOGGER.debug("bad thumb size {} skip scaling", ts); w = h = 0; // just to make sure } } if (StringUtils.isNotEmpty(r.getThumbBG())) { try { Field field = Color.class.getField(r.getThumbBG()); col = (Color) field.get(null); } catch (Exception e) { LOGGER.debug("bad color name " + r.getThumbBG()); } } if (w == 0 && h == 0 && col == null) { return in; } ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage img1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = img1.createGraphics(); if (col != null) { g.setColor(col); } g.fillRect(0, 0, w, h); g.drawImage(img, 0, 0, w, h, null); ImageIO.write(img1, "jpeg", out); out.flush(); return new ByteArrayInputStream(out.toByteArray()); }