List of usage examples for java.awt Graphics2D drawImage
public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);
From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java
private void processImageElement(Graphics2D g2, Element imageElement) throws Exception { logger.info("Processing {}...", imageElement.getAttributeValue("Name")); int x = Integer.valueOf(imageElement.getAttributeValue("X")); int y = Integer.valueOf(imageElement.getAttributeValue("Y")); int width = Integer.valueOf(imageElement.getAttributeValue("Width")); int height = Integer.valueOf(imageElement.getAttributeValue("Height")); // File or Base64String String sourceType = imageElement.getAttributeValue("Source"); String sourceData = imageElement.getAttributeValue("SourceData"); String nullImageUrl = imageElement.getAttributeValue("NullImageUrl"); // String sourceDpi = imageElement.getAttributeValue("SourceDpi"); // boolean useSourceDpi = Boolean.valueOf(imageElement.getAttributeValue("UseSourceDpi")); BufferedImage tmpImage = null; switch (sourceType) { case "File": // load image from file if (StringUtils.isEmpty(sourceData)) { tmpImage = ImageIO.read(new File(nullImageUrl.replaceAll("\\\\", "/"))); } else {//w ww. jav a2 s .c om // tmpImage = ImageIO.read(new File(sourceData.replaceAll("\\\\", "/"))); tmpImage = ImageIO.read(new File(sourceData.replaceAll("\\\\", "/"))); } break; case "Base64String": // use substitution to retrieve fileName // RATINGSTARS if (sourceData.equalsIgnoreCase("%RATINGSTARS%")) { // // BufferedImage stars = ImageIO.read(new FileInputStream(settings.getStarsRating().replaceAll("\\\\", "/"))); // // // create stars // float starsNumber = Float.valueOf(tokenMap.get("%RATINGPERCENT%")) / 10F; // int fullStarsNumber = (int) Math.floor(starsNumber); // float starFraction = starsNumber - fullStarsNumber; // // // 1 star -> 24px, so 7.4 stars are 24x7.4 -> 178px // BufferedImage singleStar = stars.getSubimage(0, 0, 24, 24); // // //Initializing the final image // tmpImage = new BufferedImage(width, height, singleStar.getType()); // Graphics2D g2i = tmpImage.createGraphics(); // for (int i = 0; i < fullStarsNumber; i++) { // g2i.drawImage(singleStar, 24 * i, 0, null); // } // // crop the last star // BufferedImage croppedStar = singleStar.getSubimage(0, 0, Math.round(24 * starFraction), 24); // g2i.drawImage(croppedStar, 24 * fullStarsNumber, 0, null); } else { String imageUrl = tokenMap.get(sourceData); if (imageUrl != null) { tmpImage = ImageIO.read(new File(imageUrl.replaceAll("\\\\", "/"))); } } break; } if (tmpImage != null) { // process actions tmpImage = processActions(imageElement, tmpImage); // alway resize Scalr.resize(tmpImage, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_TO_WIDTH, width, height, Scalr.OP_ANTIALIAS); // BicubicScaleFilter scaleFilter = new BicubicScaleFilter(width, height); // tmpImage = scaleFilter.filter(tmpImage, null); g2.drawImage(tmpImage, x, y, width, height, null); } logger.info("{} processed...", imageElement.getAttributeValue("Name")); }
From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java
private void processTextElement(Graphics2D g2, Element textElement) { int x = Integer.valueOf(textElement.getAttributeValue("X")); int y = Integer.valueOf(textElement.getAttributeValue("Y")); int width = Integer.valueOf(textElement.getAttributeValue("Width")); int height = Integer.valueOf(textElement.getAttributeValue("Height")); String alignment = textElement.getAttributeValue("TextAlignment"); boolean multiline = Boolean.valueOf(textElement.getAttributeValue("Multiline").toLowerCase()); boolean antiAlias = textElement.getAttributeValue("TextQuality").equalsIgnoreCase("antialias"); Font font = parseFont(textElement.getAttributeValue("Font")); logger.info("Using font " + font); // now get the textim4java performance String text = textElement.getAttributeValue("Text"); // if text matches pattern of %VARIABLE%{MODIFIER} logger.info("parsing token {}", text); Matcher matcher = pattern.matcher(text); int start = 0; while (matcher.find(start)) { // apply modification text = text.replace(matcher.group(), applyModifier(matcher.group())); start = matcher.end();/*from ww w. j a v a 2s. c o m*/ } BufferedImage tmpImage; if (width > 0 && height > 0) { // create a transparent tmpImage tmpImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); } else { FontMetrics fm = g2.getFontMetrics(font); Rectangle outlineBounds = fm.getStringBounds(text, g2).getBounds(); // we need to create a transparent image to paint tmpImage = new BufferedImage(outlineBounds.width, outlineBounds.height, BufferedImage.TYPE_INT_ARGB); } Graphics2D g2d = tmpImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // } g2d.setFont(font); Color textColor = new Color(Integer.valueOf(textElement.getAttributeValue("ForeColor"))); g2d.setColor(textColor); Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f); g2d.setComposite(comp); drawString(g2d, text, new Rectangle(0, 0, width, height), Align.valueOf(alignment), 0, multiline); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); tmpImage = processActions(textElement, tmpImage); //// Graphics2D g2d = tmpImage.createGraphics(); // // set current font // g2.setFont(font); //// g2d.setComposite(AlphaComposite.Clear); //// g2d.fillRect(0, 0, width, height); //// g2d.setComposite(AlphaComposite.Src); // // TODO: we have to parse it // int strokeWidth = Integer.valueOf(textElement.getAttributeValue("StrokeWidth")); // // the color of the outline // if (strokeWidth > 0) { //// Color strokeColor = new Color(Integer.valueOf(textElement.getAttributeValue("StrokeColor"))); //// AffineTransform affineTransform; //// affineTransform = g2d.getTransform(); //// affineTransform.translate(width / 2 - (outlineBounds.width / 2), height / 2 //// + (outlineBounds.height / 2)); //// g2d.transform(affineTransform); //// // backup stroke width and color //// Stroke originalStroke = g2d.getStroke(); //// Color originalColor = g2d.getColor(); //// g2d.setColor(strokeColor); //// g2d.setStroke(new BasicStroke(strokeWidth)); //// g2d.draw(shape); //// g2d.setClip(shape); //// // restore stroke width and color //// g2d.setStroke(originalStroke); //// g2d.setColor(originalColor); // } //// // get the text color // Color textColor = new Color(Integer.valueOf(textElement.getAttributeValue("ForeColor"))); // g2.setColor(textColor); //// g2d.setBackground(Color.BLACK); //// g2d.setStroke(new BasicStroke(2)); //// g2d.setColor(Color.WHITE); // // draw the text // // drawString(g2, text, new Rectangle(x, y, width, height), Align.valueOf(alignment), 0, multiline); // g2.drawString(text, x, y); // Rectangle rect = new Rectangle(x, y, width, height); // defines the desired size and position // FontMetrics fm = g2.getFontMetrics(); // FontRenderContext frc = g2.getFontRenderContext(); // TextLayout tl = new TextLayout(text, g2.getFont(), frc); // AffineTransform transform = new AffineTransform(); // transform.setToTranslation(rect.getX(), rect.getY()); // if (Boolean.valueOf(textElement.getAttributeValue("AutoSize").toLowerCase())) { // double scaleY // = rect.getHeight() / (double) (tl.getOutline(null).getBounds().getMaxY() // - tl.getOutline(null).getBounds().getMinY()); // transform.scale(rect.getWidth() / (double) fm.stringWidth(text), scaleY); // } // Shape shape = tl.getOutline(transform); // g2.setClip(shape); // g2.fill(shape.getBounds()); // if (antiAlias) { // we need to restore antialias to none // g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // } // g2.drawString(text, x, y); // alway resize // BicubicScaleFilter scaleFilter = new BicubicScaleFilter(width, height); // tmpImage = scaleFilter.filter(tmpImage, null); // draw the image to the source g2.drawImage(tmpImage, x, y, width, height, null); try { ScreenImage.writeImage(tmpImage, "/tmp/images/" + textElement.getAttributeValue("Name") + ".png"); } catch (IOException ex) { } }