Refine search
@Signature public PImage rotate(double angle) { double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle))); int w = image.getWidth(null), h = image.getHeight(null); int neww = (int) Math.floor(w*cos + h*sin), newh = (int) Math.floor(h*cos + w*sin); BufferedImage bimg = new BufferedImage(neww, newh, image.getType()); Graphics2D g = bimg.createGraphics(); g.translate((neww-w)/2, (newh-h)/2); g.rotate(Math.toRadians(angle), w/2, h/2); g.drawRenderedImage(image, null); g.dispose(); this.image = bimg; return this; }
/** * 旋转图片为指定角度<br> * 来自:http://blog.51cto.com/cping1982/130066 * * @param degree 旋转角度 * @return 旋转后的图片 * @since 3.2.2 */ public Img rotate(int degree) { final BufferedImage image = getValidSrcImg(); int width = image.getWidth(null); int height = image.getHeight(null); final Rectangle rectangle = calcRotatedSize(width, height, degree); final BufferedImage targetImg = new BufferedImage(rectangle.width, rectangle.height, getTypeInt()); Graphics2D graphics2d = targetImg.createGraphics(); // 抗锯齿 graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // 从中心旋转 graphics2d.translate((rectangle.width - width) / 2, (rectangle.height - height) / 2); graphics2d.rotate(Math.toRadians(degree), width / 2, height / 2); graphics2d.drawImage(image, 0, 0, null); graphics2d.dispose(); this.targetImage = targetImg; return this; }
/** * 旋转图片为指定角度<br> * 来自:http://blog.51cto.com/cping1982/130066 * * @param degree 旋转角度 * @return 旋转后的图片 * @since 3.2.2 */ public Img rotate(int degree) { final BufferedImage image = getValidSrcImg(); int width = image.getWidth(null); int height = image.getHeight(null); final Rectangle rectangle = calcRotatedSize(width, height, degree); final BufferedImage targetImg = new BufferedImage(rectangle.width, rectangle.height, getTypeInt()); Graphics2D graphics2d = targetImg.createGraphics(); // 抗锯齿 graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // 从中心旋转 graphics2d.translate((rectangle.width - width) / 2, (rectangle.height - height) / 2); graphics2d.rotate(Math.toRadians(degree), width / 2, height / 2); graphics2d.drawImage(image, 0, 0, null); graphics2d.dispose(); this.targetImage = targetImg; return this; }
graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY()); graphics.scale(-1, 1); graphics.translate(-anchor.getX(), -anchor.getY()); graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight()); graphics.scale(1, -1); graphics.translate(-anchor.getX(), -anchor.getY()); graphics.translate(centerX, centerY); graphics.rotate(Math.toRadians(rotation)); graphics.translate(-centerX, -centerY);
final double ax = anchor.getX(); final double ay = anchor.getY(); graphics.translate(ax + anchor.getWidth(), ay); graphics.scale(-1, 1); graphics.translate(-ax, -ay); final double cx = anchor.getCenterX(); final double cy = anchor.getCenterY(); graphics.translate(cx, cy); graphics.rotate(Math.toRadians(textRot)); graphics.translate(-cx, -cy); final double cy = anchor.getCenterY(); graphics.translate(cx, cy); graphics.rotate(Math.toRadians(deg)); graphics.translate(-cx, -cy);
private static BufferedImage rotateImage(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int width = image.getWidth(), height = image.getHeight(); int newWidth = (int) Math.floor(width * cos + height * sin), newHeight = (int) Math.floor(height * cos + width * sin); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); BufferedImage result = gc.createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT); Graphics2D g = result.createGraphics(); g.translate((newWidth - width) / 2, (newHeight - height) / 2); g.rotate(angle, width / 2, height / 2); g.drawRenderedImage(image, null); g.dispose(); return result; }
/** * Rotates an image. Actually rotates a new copy of the image. * * @param img The image to be rotated * @param angle The angle in degrees * @return The rotated image */ public static Image rotate(Image img, double angle) { double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle))); int w = img.getWidth(null), h = img.getHeight(null); int neww = (int) Math.floor(w*cos + h*sin), newh = (int) Math.floor(h*cos + w*sin); BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh)); Graphics2D g = bimg.createGraphics(); g.translate((neww-w)/2, (newh-h)/2); g.rotate(Math.toRadians(angle), w/2, h/2); g.drawRenderedImage(toBufferedImage(img), null); g.dispose(); return toImage(bimg); }
public static BufferedImage rotate(BufferedImage image, double angle) { double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); int w = image.getWidth(), h = image.getHeight(); int neww = (int)Math.floor(w*cos+h*sin), newh = (int) Math.floor(h * cos + w * sin); GraphicsConfiguration gc = getDefaultConfiguration(); BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT); Graphics2D g = result.createGraphics(); g.translate((neww - w) / 2, (newh - h) / 2); g.rotate(angle, w / 2, h / 2); g.drawRenderedImage(image, null); g.dispose(); return result; } private static GraphicsConfiguration getDefaultConfiguration() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); return gd.getDefaultConfiguration(); }
/** * Rotates an image. * * @param image the original image * @param angle the degree of rotation * @return a rotated image */ public static BufferedImage rotateImage(BufferedImage image, double angle) { double theta = Math.toRadians(angle); double sin = Math.abs(Math.sin(theta)); double cos = Math.abs(Math.cos(theta)); int w = image.getWidth(); int h = image.getHeight(); int newW = (int) Math.floor(w * cos + h * sin); int newH = (int) Math.floor(h * cos + w * sin); BufferedImage tmp = new BufferedImage(newW, newH, image.getType()); Graphics2D g2d = tmp.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.translate((newW - w) / 2, (newH - h) / 2); g2d.rotate(theta, w / 2, h / 2); g2d.drawImage(image, 0, 0, null); g2d.dispose(); return tmp; }
protected void paintComponent(Graphics g) { super.paintComponent(g); float ex = endX - startX; float ey = endY - startY; if (ex == 0 && ey == 0) { return; } float length = (float) Math.sqrt(ex * ex + ey * ey); float bendPercent = (float) Math.asin(ey / length); if (endX > startX) { bendPercent = -bendPercent; } Area arrow = getArrow(length, bendPercent); Graphics2D g2d = (Graphics2D) g; g2d.translate(startX, startY); g2d.rotate(Math.atan2(ey, ex)); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setComposite(composite); g2d.setColor(this.color); g2d.fill(arrow); g2d.setColor(Color.BLACK); g2d.draw(arrow); }
@Override public void paint(Graphics g) { if (!displayEnabled) { return; } if (!isValid()) { super.validate(); } Graphics2D g2d = (Graphics2D) g; if (transformAngle < 1) { float edgeOffset = (cardWidth + cardXOffset) / 2f; g2d.translate(edgeOffset * (1 - transformAngle), 0); g2d.scale(transformAngle, 1); } if (tappedAngle + flippedAngle > 0) { g2d = (Graphics2D) g2d.create(); float edgeOffset = cardWidth / 2f; double angle = tappedAngle + (Math.abs(flippedAngle - FLIPPED_ANGLE) < 0.001 ? 0 : flippedAngle); g2d.rotate(angle, cardXOffset + edgeOffset, cardYOffset + cardHeight - edgeOffset); } super.paint(g2d); }