Search in sources :

Example 46 with Graphics2D

use of java.awt.Graphics2D in project processing by processing.

the class Toolkit method prepareGraphics.

/**
   * Handles scaling for high-res displays, also sets text anti-aliasing
   * options to be far less ugly than the defaults.
   * Moved to a utility function because it's used in several classes.
   * @return a Graphics2D object, as a bit o sugar
   */
public static Graphics2D prepareGraphics(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    //float z = zoom * (Toolkit.isRetina() ? 2 : 1);
    if (Toolkit.isRetina()) {
        // scale everything 2x, will be scaled down when drawn to the screen
        g2.scale(2, 2);
    }
    //g2.scale(z, z);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    if (Toolkit.isRetina()) {
        // Looks great on retina, not so great (with our font) on 1x
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    }
    zoomStroke(g2);
    return g2;
}
Also used : Graphics2D(java.awt.Graphics2D)

Example 47 with Graphics2D

use of java.awt.Graphics2D in project processing by processing.

the class PSurfaceAWT method render.

/*
    @Override
    public void addNotify() {
//      System.out.println("adding notify");
      super.addNotify();
      // prior to Java 7 on OS X, this no longer works [121222]
//    createBufferStrategy(2);
    }
    */
protected synchronized void render() {
    if (canvas.isDisplayable() && graphics.image != null) {
        if (canvas.getBufferStrategy() == null) {
            canvas.createBufferStrategy(2);
        }
        BufferStrategy strategy = canvas.getBufferStrategy();
        if (strategy != null) {
            //        try {
            do {
                // are consistent in case the underlying surface was recreated
                do {
                    Graphics2D draw = (Graphics2D) strategy.getDrawGraphics();
                    // draw to width/height, since this may be a 2x image
                    draw.drawImage(graphics.image, 0, 0, sketchWidth, sketchHeight, null);
                    draw.dispose();
                } while (strategy.contentsRestored());
                // Display the buffer
                strategy.show();
            // Repeat the rendering if the drawing buffer was lost
            } while (strategy.contentsLost());
        }
    }
}
Also used : Graphics2D(java.awt.Graphics2D)

Example 48 with Graphics2D

use of java.awt.Graphics2D in project Fling by entertailion.

the class DragHereIcon method paintIcon.

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.translate(x, y);
    g2.setStroke(new BasicStroke(a));
    g2.setPaint(linec);
    g2.draw(new RoundRectangle2D.Float(a, a, size - 2 * a - 1, size - 2 * a - 1, r, r));
    // draw bounding box
    g2.setStroke(new BasicStroke(b));
    g2.setColor(UIManager.getColor("Panel.background"));
    g2.drawLine(1 * f, 0 * f, 1 * f, 4 * f);
    g2.drawLine(2 * f, 0 * f, 2 * f, 4 * f);
    g2.drawLine(3 * f, 0 * f, 3 * f, 4 * f);
    g2.drawLine(0 * f, 1 * f, 4 * f, 1 * f);
    g2.drawLine(0 * f, 2 * f, 4 * f, 2 * f);
    g2.drawLine(0 * f, 3 * f, 4 * f, 3 * f);
    // draw arrow
    g2.setPaint(linec);
    Rectangle2D b = s.getBounds();
    Point2D.Double p = new Point2D.Double(b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d);
    AffineTransform toCenterAT = AffineTransform.getTranslateInstance(size / 2d - p.getX(), size / 2d - p.getY());
    g2.fill(toCenterAT.createTransformedShape(s));
    g2.translate(-x, -y);
    g2.dispose();
}
Also used : BasicStroke(java.awt.BasicStroke) Point2D(java.awt.geom.Point2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) AffineTransform(java.awt.geom.AffineTransform) Graphics2D(java.awt.Graphics2D)

Example 49 with Graphics2D

use of java.awt.Graphics2D in project jmonkeyengine by jMonkeyEngine.

the class LwjglWindow method imageToGLFWImage.

/**
     * Convert the {@link BufferedImage} to the {@link GLFWImage}.
     */
private GLFWImage imageToGLFWImage(BufferedImage image) {
    if (image.getType() != BufferedImage.TYPE_INT_ARGB_PRE) {
        final BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics2D graphics = convertedImage.createGraphics();
        final int targetWidth = image.getWidth();
        final int targetHeight = image.getHeight();
        graphics.drawImage(image, 0, 0, targetWidth, targetHeight, null);
        graphics.dispose();
        image = convertedImage;
    }
    final ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int colorSpace = image.getRGB(j, i);
            buffer.put((byte) ((colorSpace << 8) >> 24));
            buffer.put((byte) ((colorSpace << 16) >> 24));
            buffer.put((byte) ((colorSpace << 24) >> 24));
            buffer.put((byte) (colorSpace >> 24));
        }
    }
    buffer.flip();
    final GLFWImage result = GLFWImage.create();
    result.set(image.getWidth(), image.getHeight(), buffer);
    return result;
}
Also used : GLFWImage(org.lwjgl.glfw.GLFWImage) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) GLFW.glfwWindowHint(org.lwjgl.glfw.GLFW.glfwWindowHint) Graphics2D(java.awt.Graphics2D)

Example 50 with Graphics2D

use of java.awt.Graphics2D in project AozoraEpub3 by hmdev.

the class ImageUtils method writeImage.

/** 大きすぎる画像は縮小して出力
	 * @param is 画像の入力ストリーム srcImageがあれば利用しないのでnull
	 * @param srcImage 読み込み済の場合は画像をこちらに設定 nullならisから読み込む
	 * @param zos 出力先Zipストリーム
	 * @param imageInfo 画像情報
	 * @param jpegQuality jpeg画質 (低画質 0.0-1.0 高画質)
	 * @param maxImagePixels 縮小する画素数
	 * @param maxImageW 縮小する画像幅
	 * @param maxImageH 縮小する画像高さ
	 * @param dispW 画面幅 余白除去後の縦横比補正用
	 * @param dispH 画面高さ 余白除去後の縦横比補正用
	 * @param autoMarginLimitH 余白除去 最大%
	 * @param autoMarginLimitV 余白除去 最大%
	 * @param autoMarginWhiteLevel 白画素として判別する白さ 100が白
	 * @param autoMarginPadding 余白除去後に追加するマージン */
public static void writeImage(InputStream is, BufferedImage srcImage, ZipArchiveOutputStream zos, ImageInfo imageInfo, float jpegQuality, LookupOp gammaOp, int maxImagePixels, int maxImageW, int maxImageH, int dispW, int dispH, int autoMarginLimitH, int autoMarginLimitV, int autoMarginWhiteLevel, float autoMarginPadding, int autoMarginNombre, float nombreSize) throws IOException {
    try {
        String ext = imageInfo.getExt();
        int imgW = imageInfo.getWidth();
        int imgH = imageInfo.getHeight();
        int w = imgW;
        int h = imgH;
        imageInfo.setOutWidth(imgW);
        imageInfo.setOutHeight(imgH);
        //余白チェック時に読み込んだ画像のバッファ
        byte[] imgBuf = null;
        //回転とコントラスト調整なら読み込んでおく
        if (srcImage == null && (imageInfo.rotateAngle != 0 || gammaOp != null))
            srcImage = readImage(ext, is);
        int[] margin = null;
        if (autoMarginLimitH > 0 || autoMarginLimitV > 0) {
            //1%
            int startPixel = (int) (w * 0.01);
            //3%
            int ignoreEdge = (int) (w * 0.03);
            //1%
            int dustSize = (int) (w * 0.01);
            //画像がなければ読み込み 変更なしの時にそのまま出力できるように一旦バッファに読み込む
            if (srcImage == null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(is, baos);
                imgBuf = baos.toByteArray();
                ByteArrayInputStream bais = new ByteArrayInputStream(imgBuf);
                try {
                    srcImage = readImage(ext, bais);
                } finally {
                    bais.close();
                }
            }
            margin = getPlainMargin(srcImage, autoMarginLimitH / 100f, autoMarginLimitV / 100f, autoMarginWhiteLevel / 100f, autoMarginPadding / 100f, startPixel, ignoreEdge, dustSize, autoMarginNombre, nombreSize);
            if (margin[0] == 0 && margin[1] == 0 && margin[2] == 0 && margin[3] == 0)
                margin = null;
            if (margin != null) {
                //元画像が幅か高さかチェック
                int mw = w - margin[0] - margin[2];
                int mh = h - margin[1] - margin[3];
                double dWH = dispW / (double) dispH;
                double mWH = mw / (double) mh;
                //縦横比で画面の横か縦に合わせる方向が変わらないようにマージンを調整する
                if (w / (double) h < dWH) {
                    //元が縦
                    if (mWH > dWH && mw > dispW) {
                        //余白除去で横にはみ出す
                        mh = (int) (mw / dWH);
                        //下マージンを伸ばす
                        margin[3] = h - margin[1] - mh;
                        if (margin[3] < 0) {
                            margin[3] = 0;
                            margin[1] = h - mh;
                        }
                    }
                } else {
                    //元が横
                    if (mWH < dWH && mh > dispH) {
                        //余白除去で縦にはみ出す
                        mw = (int) (mh * dWH);
                        double mLR = margin[0] + margin[2];
                        margin[0] = (int) ((w - mw) * margin[0] / mLR);
                        margin[2] = (int) ((w - mw) * margin[2] / mLR);
                    }
                }
                w = mw;
                h = mh;
            }
        }
        //倍率取得
        double scale = 1;
        //最大画素数指定
        if (maxImagePixels >= 10000)
            scale = Math.sqrt((double) maxImagePixels / (w * h));
        //最大幅指定
        if (maxImageW > 0)
            scale = Math.min(scale, (double) maxImageW / w);
        //最大高さ指定
        if (maxImageH > 0)
            scale = Math.min(scale, (double) maxImageH / h);
        if (scale >= 1 && (gammaOp == null || srcImage.getType() == BufferedImage.TYPE_INT_RGB)) {
            if (srcImage == null) {
                //変更なしならそのままファイル出力
                IOUtils.copy(is, zos);
            } else {
                if (margin == null && imgBuf != null && imageInfo.rotateAngle == 0) {
                    //余白除去が無く画像も編集されていなければバッファからそのまま出力
                    ByteArrayInputStream bais = new ByteArrayInputStream(imgBuf);
                    try {
                        IOUtils.copy(bais, zos);
                    } finally {
                        bais.close();
                    }
                } else {
                    //編集済の画像なら同じ画像形式で書き出し 余白があれば切り取る
                    if (imageInfo.rotateAngle != 0) {
                        BufferedImage outImage = new BufferedImage(h, w, srcImage.getType());
                        Graphics2D g = outImage.createGraphics();
                        try {
                            g.setColor(Color.WHITE);
                            g.fillRect(0, 0, h, w);
                            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                            g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                            int x = 0;
                            int y = 0;
                            AffineTransform at;
                            if (imageInfo.rotateAngle == 90) {
                                at = AffineTransform.getQuadrantRotateInstance(1, 0, 0);
                                at.translate(0, -imgH);
                                if (margin != null) {
                                    x = -margin[3];
                                    y = -margin[0];
                                }
                            } else {
                                at = AffineTransform.getQuadrantRotateInstance(-1, 0, 0);
                                at.translate(-imgW, 0);
                                if (margin != null) {
                                    x = -margin[1];
                                    y = -margin[2];
                                }
                            }
                            AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
                            g.drawImage(srcImage, ato, x, y);
                        } finally {
                            g.dispose();
                        }
                        //入れ替え
                        srcImage = outImage;
                    } else if (margin != null)
                        srcImage = srcImage.getSubimage(margin[0], margin[1], srcImage.getWidth() - margin[2] - margin[0], srcImage.getHeight() - margin[3] - margin[1]);
                    if (gammaOp != null) {
                        BufferedImage filterdImage = new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_INT_RGB);
                        srcImage = gammaOp.filter(srcImage, filterdImage);
                        srcImage = filterdImage;
                    }
                    _writeImage(zos, srcImage, ext, jpegQuality);
                    imageInfo.setOutWidth(srcImage.getWidth());
                    imageInfo.setOutHeight(srcImage.getHeight());
                    if (imageInfo.rotateAngle != 0)
                        LogAppender.println("画像回転" + ": " + imageInfo.getOutFileName() + " (" + h + "," + w + ")");
                }
            }
        } else {
            //縮小
            int scaledW = (int) (w * scale + 0.5);
            int scaledH = (int) (h * scale + 0.5);
            if (imageInfo.rotateAngle != 0) {
                scaledW = (int) (h * scale + 0.5);
                scaledH = (int) (w * scale + 0.5);
            }
            //画像がなければ読み込み
            if (srcImage == null)
                srcImage = readImage(ext, is);
            int imageType = srcImage.getType();
            BufferedImage outImage;
            ColorModel colorModel;
            WritableRaster raster;
            switch(gammaOp == null ? imageType : BufferedImage.TYPE_INT_RGB) {
                case BufferedImage.TYPE_BYTE_BINARY:
                    colorModel = srcImage.getColorModel();
                    colorModel = getGray16ColorModel();
                    raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                    outImage = new BufferedImage(colorModel, raster, true, null);
                    break;
                case BufferedImage.TYPE_BYTE_INDEXED:
                    colorModel = srcImage.getColorModel();
                    raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                    outImage = new BufferedImage(colorModel, raster, true, null);
                    break;
                /*case BufferedImage.TYPE_BYTE_GRAY:
				//PngEncoderのGRAYが薄くなるのでindexにする
				colorModel = srcImage.getColorModel();
				if (colorModel.getPixelSize() <= 4) colorModel = getGray16ColorModel();
				else colorModel = getGray256ColorModel();
				raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
				outImage = new BufferedImage(colorModel, raster, true, null);
				break;*/
                case BufferedImage.TYPE_BYTE_GRAY:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_BYTE_GRAY);
                    break;
                case BufferedImage.TYPE_USHORT_GRAY:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_USHORT_GRAY);
                    break;
                default:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
            }
            Graphics2D g = outImage.createGraphics();
            try {
                if (imageType == BufferedImage.TYPE_BYTE_BINARY && imageType == BufferedImage.TYPE_BYTE_INDEXED && imageType == BufferedImage.TYPE_INT_ARGB) {
                    g.setColor(Color.WHITE);
                    g.fillRect(0, 0, scaledW, scaledH);
                }
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
                int x = 0;
                int y = 0;
                if (imageInfo.rotateAngle == 0) {
                    if (margin != null) {
                        x = (int) (-margin[0] * scale + 0.5);
                        y = (int) (-margin[1] * scale + 0.5);
                    }
                } else if (imageInfo.rotateAngle == 90) {
                    at.rotate(Math.toRadians(imageInfo.rotateAngle), 0, 0);
                    at.translate(0, -imgH);
                    if (margin != null) {
                        x = (int) (-margin[3] * scale + 0.5);
                        y = (int) (-margin[0] * scale + 0.5);
                    }
                } else {
                    at.quadrantRotate(-1, 0, 0);
                    at.translate(-imgW, 0);
                    if (margin != null) {
                        x = (int) (-margin[1] * scale + 0.5);
                        y = (int) (-margin[2] * scale + 0.5);
                    }
                }
                AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
                g.drawImage(srcImage, ato, x, y);
            } finally {
                g.dispose();
            }
            //コントラスト調整
            if (gammaOp != null) {
                BufferedImage filterdImage = new BufferedImage(outImage.getWidth(), outImage.getHeight(), BufferedImage.TYPE_INT_RGB);
                outImage = gammaOp.filter(outImage, filterdImage);
                outImage = filterdImage;
                filterdImage = null;
                //インデックス化
                switch(imageType) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        colorModel = srcImage.getColorModel();
                        colorModel = getGray16ColorModel();
                        raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                        filterdImage = new BufferedImage(colorModel, raster, true, null);
                        break;
                    case BufferedImage.TYPE_BYTE_INDEXED:
                        colorModel = srcImage.getColorModel();
                        raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                        filterdImage = new BufferedImage(colorModel, raster, true, null);
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        filterdImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_BYTE_GRAY);
                        break;
                    case BufferedImage.TYPE_USHORT_GRAY:
                        filterdImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_USHORT_GRAY);
                        break;
                }
                if (filterdImage != null) {
                    g = filterdImage.createGraphics();
                    try {
                        g.drawImage(outImage, 0, 0, null);
                    } finally {
                        g.dispose();
                    }
                    outImage = filterdImage;
                }
            }
            _writeImage(zos, outImage, ext, jpegQuality);
            imageInfo.setOutWidth(outImage.getWidth());
            imageInfo.setOutHeight(outImage.getHeight());
            if (scale < 1) {
                LogAppender.append("画像縮小");
                if (imageInfo.rotateAngle != 0)
                    LogAppender.append("回転");
                LogAppender.println(": " + imageInfo.getOutFileName() + " (" + w + "," + h + ")→(" + scaledW + "," + scaledH + ")");
            }
            zos.flush();
        }
    } catch (Exception e) {
        LogAppender.println("画像読み込みエラー: " + imageInfo.getOutFileName());
        e.printStackTrace();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) AffineTransformOp(java.awt.image.AffineTransformOp) ByteArrayInputStream(java.io.ByteArrayInputStream) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) AffineTransform(java.awt.geom.AffineTransform)

Aggregations

Graphics2D (java.awt.Graphics2D)777 BufferedImage (java.awt.image.BufferedImage)379 Color (java.awt.Color)180 Font (java.awt.Font)77 BasicStroke (java.awt.BasicStroke)75 Rectangle (java.awt.Rectangle)65 Point (java.awt.Point)63 Rectangle2D (java.awt.geom.Rectangle2D)59 AffineTransform (java.awt.geom.AffineTransform)55 GradientPaint (java.awt.GradientPaint)48 Dimension (java.awt.Dimension)46 Paint (java.awt.Paint)44 FontMetrics (java.awt.FontMetrics)35 IOException (java.io.IOException)33 Graphics (java.awt.Graphics)31 Stroke (java.awt.Stroke)30 Image (java.awt.Image)29 RadialGradientPaint (java.awt.RadialGradientPaint)26 VolatileImage (java.awt.image.VolatileImage)26 File (java.io.File)26