List of usage examples for java.awt Graphics2D transform
public abstract void transform(AffineTransform Tx);
From source file:org.jcurl.core.swing.RockLocationDisplayBase.java
/** * Callback to draw the rocks. Sets the transformation wc to dc and * delegates to {@link #paintRocksWC(Graphics2D, PositionSet, int)}. * //www . j a va 2s. c o m * @param g2 * graphics context. */ protected void paintRocksDC(final Graphics2D g2) { g2.transform(wc_mat); // all rocks paintRocksWC(g2, rocks, PositionSet.ALL_MASK); }
From source file:org.ofbiz.common.CommonEvents.java
public static String getCaptcha(HttpServletRequest request, HttpServletResponse response) { try {/* w w w. j a va 2 s . c om*/ Delegator delegator = (Delegator) request.getAttribute("delegator"); final String captchaSizeConfigName = StringUtils.defaultIfEmpty(request.getParameter("captchaSize"), "default"); final String captchaSizeConfig = EntityUtilProperties.getPropertyValue("captcha.properties", "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.properties", "captcha.code_length", 6); final char[] availableChars = EntityUtilProperties .getPropertyValue("captcha.properties", "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:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java
private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) { if (paint instanceof Color) { final Color color = (Color) paint; final int alpha = color.getAlpha(); if (fill) { if (alpha != currentFillGState) { currentFillGState = alpha; PdfGState gs = fillGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(alpha / 255.00f); fillGState[alpha] = gs; }//w w w .j a v a 2 s .c o m cb.setGState(gs); } cb.setColorFill(color); } else { if (alpha != currentStrokeGState) { currentStrokeGState = alpha; PdfGState gs = strokeGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setStrokeOpacity(alpha / 255.0f); strokeGState[alpha] = gs; } cb.setGState(gs); } cb.setColorStroke(color); } } else if (paint instanceof GradientPaint) { final GradientPaint gp = (GradientPaint) paint; final Point2D p1 = gp.getPoint1(); transform.transform(p1, p1); final Point2D p2 = gp.getPoint2(); transform.transform(p2, p2); final Color c1 = gp.getColor1(); final Color c2 = gp.getColor2(); final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(), normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2); final PdfShadingPattern pat = new PdfShadingPattern(shading); if (fill) { cb.setShadingFill(pat); } else { cb.setShadingStroke(pat); } } else if (paint instanceof TexturePaint) { try { final TexturePaint tp = (TexturePaint) paint; final BufferedImage img = tp.getImage(); final Rectangle2D rect = tp.getAnchorRect(); final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight()); final AffineTransform inverse = this.normalizeMatrix(); inverse.translate(rect.getX(), rect.getY()); inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight()); final double[] mx = new double[6]; inverse.getMatrix(mx); pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) { cb.setPatternFill(pattern); } else { cb.setPatternStroke(pattern); } } catch (Exception ex) { if (fill) { cb.setColorFill(Color.gray); } else { cb.setColorStroke(Color.gray); } } } else { try { int type = BufferedImage.TYPE_4BYTE_ABGR; if (paint.getTransparency() == Transparency.OPAQUE) { type = BufferedImage.TYPE_3BYTE_BGR; } final BufferedImage img = new BufferedImage((int) width, (int) height, type); final Graphics2D g = (Graphics2D) img.getGraphics(); g.transform(transform); final AffineTransform inv = transform.createInverse(); Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()); fillRect = inv.createTransformedShape(fillRect); g.setPaint(paint); g.fill(fillRect); if (invert) { final AffineTransform tx = new AffineTransform(); tx.scale(1, -1); tx.translate(-xoffset, -yoffset); g.drawImage(img, tx, null); } g.dispose(); // g = null; final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); final PdfPatternPainter pattern = cb.createPattern(width, height); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) { cb.setPatternFill(pattern); } else { cb.setPatternStroke(pattern); } } catch (Exception ex) { if (fill) { cb.setColorFill(Color.gray); } else { cb.setColorStroke(Color.gray); } } } }
From source file:org.tros.logo.swing.LogoPanel.java
/** * Paint.//from w ww . ja v a 2 s . c o m * * @param g */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; turtleState.width = getWidth(); turtleState.height = getHeight(); draw(g2d, turtleState); if (turtleState.showTurtle && turtle != null) { double x = turtleState.penX - (turtle.getWidth() / 2.0); double y = turtleState.penY - (turtle.getHeight() / 2.0); AffineTransform translateInstance = AffineTransform .getRotateInstance(turtleState.angle + (Math.PI / 2.0), turtleState.penX, turtleState.penY); AffineTransform saveXform = g2d.getTransform(); g2d.transform(translateInstance); g2d.drawImage(turtle, (int) x, (int) y, null); g2d.setTransform(saveXform); } }
From source file:se.vgregion.webbisar.svc.impl.ImageUtil.java
/** * Resizes and overwrites the image. Will keep the original pictures dimensions. * //from w ww.j av a 2s . c o m * @param sourceFile * the file to rezise * @param newLongSideSize * the new size, in pixels. * @param quality * a number between 0 and 100 where 100 gives the best quality * @throws IOException */ public synchronized static void scaleImage(File sourceFile, ImageSize imageSize, float quality) throws IOException { // System.gc(); BufferedImage sourceImage = ImageIO.read(sourceFile); int srcWidth = sourceImage.getWidth(); int srcHeight = sourceImage.getHeight(); double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = srcWidth > srcHeight ? imageSize.getWidth() : imageSize.getHeight(); double multiplier = longSideForDest / longSideForSource; int destWidth = (int) (srcWidth * multiplier); int destHeight = (int) (srcHeight * multiplier); BufferedImage destImage = new BufferedImage((int) imageSize.getWidth(), (int) imageSize.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = destImage.createGraphics(); graphics.setPaint(Color.WHITE); graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight()); AffineTransform affineTransform = AffineTransform.getScaleInstance(multiplier, multiplier); AffineTransform trans = new AffineTransform(); trans.setToTranslation((imageSize.getWidth() - destWidth) / 2, (imageSize.getHeight() - destHeight) / 2); graphics.transform(trans); graphics.drawRenderedImage(sourceImage, affineTransform); saveImageAsJPEG(sourceFile.getAbsolutePath(), destImage, quality); }
From source file:tufts.vue.LWComponent.java
/** transform relative to the child after already being transformed relative to the parent */ protected void transformDownG(final Graphics2D a) { if (mTemporaryTransform != null) { a.transform(mTemporaryTransform); } else {// ww w .jav a2 s.c om a.translate(this.x, this.y); if (this.scale != 1) a.scale(this.scale, this.scale); } }
From source file:VASSAL.counters.Labeler.java
public void draw(Graphics g, int x, int y, Component obs, double zoom) { updateCachedImage();//w w w. ja va2 s . c o m piece.draw(g, x, y, obs, zoom); // FIXME: We should be drawing the text at the right size, not scaling it! final Point p = getLabelPosition(); final int labelX = x + (int) (zoom * p.x); final int labelY = y + (int) (zoom * p.y); AffineTransform saveXForm = null; final Graphics2D g2d = (Graphics2D) g; if (rotateDegrees != 0) { saveXForm = g2d.getTransform(); final AffineTransform newXForm = AffineTransform.getRotateInstance(Math.toRadians(rotateDegrees), x, y); g2d.transform(newXForm); } imagePainter.draw(g, labelX, labelY, zoom, obs); if (rotateDegrees != 0) { g2d.setTransform(saveXForm); } }