Search in sources :

Example 1 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 2 with Graphics2D

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

the class SparklinesResource method smooth.

@Path("smooth")
@GET
public Response smooth(@DefaultValue("2") @QueryParam("step") final int step, @DefaultValue("true") @QueryParam("min-m") final boolean hasMin, @DefaultValue("true") @QueryParam("max-m") final boolean hasMax, @DefaultValue("true") @QueryParam("last-m") final boolean hasLast, @DefaultValue("blue") @QueryParam("min-color") final ColorParam minColor, @DefaultValue("green") @QueryParam("max-color") final ColorParam maxColor, @DefaultValue("red") @QueryParam("last-color") final ColorParam lastColor) {
    final BufferedImage image = new BufferedImage(data.size() * step - 4, imageHeight, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = image.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, image.getWidth(), image.getHeight());
    g.setColor(Color.gray);
    final int[] xs = new int[data.size()];
    final int[] ys = new int[data.size()];
    final int gap = 4;
    final float d = (limits.width() + 1) / (float) (imageHeight - gap);
    for (int i = 0, x = 0; i < data.size(); i++, x += step) {
        final int v = data.get(i);
        xs[i] = x;
        ys[i] = imageHeight - 3 - (int) ((v - limits.lower()) / d);
    }
    g.drawPolyline(xs, ys, data.size());
    if (hasMin) {
        final int i = data.indexOf(Collections.min(data));
        g.setColor(minColor);
        g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
    }
    if (hasMax) {
        final int i = data.indexOf(Collections.max(data));
        g.setColor(maxColor);
        g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
    }
    if (hasMax) {
        g.setColor(lastColor);
        g.fillRect(xs[xs.length - 1] - step / 2, ys[ys.length - 1] - step / 2, step, step);
    }
    return Response.ok(image).tag(tag).build();
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with Graphics2D

use of java.awt.Graphics2D in project Openfire by igniterealtime.

the class GraphServlet method writePDFContent.

private void writePDFContent(HttpServletRequest request, HttpServletResponse response, JFreeChart[] charts, Statistic[] stats, long starttime, long endtime, int width, int height) throws IOException {
    try {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new PDFEventListener(request));
        document.open();
        int index = 0;
        int chapIndex = 0;
        for (Statistic stat : stats) {
            String serverName = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
            String dateName = JiveGlobals.formatDate(new Date(starttime)) + " - " + JiveGlobals.formatDate(new Date(endtime));
            Paragraph paragraph = new Paragraph(serverName, FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
            document.add(paragraph);
            paragraph = new Paragraph(dateName, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.PLAIN));
            document.add(paragraph);
            document.add(Chunk.NEWLINE);
            document.add(Chunk.NEWLINE);
            Paragraph chapterTitle = new Paragraph(++chapIndex + ". " + stat.getName(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
            document.add(chapterTitle);
            // total hack: no idea what tags people are going to use in the description
            // possibly recommend that we only use a <p> tag?
            String[] paragraphs = stat.getDescription().split("<p>");
            for (String s : paragraphs) {
                Paragraph p = new Paragraph(s);
                document.add(p);
            }
            document.add(Chunk.NEWLINE);
            PdfContentByte contentByte = writer.getDirectContent();
            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphs2D = template.createGraphics(width, height, new DefaultFontMapper());
            Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
            charts[index++].draw(graphs2D, rectangle2D);
            graphs2D.dispose();
            float x = (document.getPageSize().width() / 2) - (width / 2);
            contentByte.addTemplate(template, x, writer.getVerticalPosition(true) - height);
            document.newPage();
        }
        document.close();
        // setting some response headers
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        // setting the content type
        response.setContentType("application/pdf");
        // the contentlength is needed for MSIE!!!
        response.setContentLength(baos.size());
        // write ByteArrayOutputStream to the ServletOutputStream
        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();
    } catch (DocumentException e) {
        Log.error("error creating PDF document: " + e.getMessage());
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) ServletOutputStream(javax.servlet.ServletOutputStream) Rectangle2D(java.awt.geom.Rectangle2D) DefaultFontMapper(com.lowagie.text.pdf.DefaultFontMapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.lowagie.text.Document) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) Date(java.util.Date) Paragraph(com.lowagie.text.Paragraph) Graphics2D(java.awt.Graphics2D) Statistic(org.jivesoftware.openfire.stats.Statistic) DocumentException(com.lowagie.text.DocumentException) PdfContentByte(com.lowagie.text.pdf.PdfContentByte)

Example 4 with Graphics2D

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

the class CombinedTexture method scale.

/**
     * This method scales the given texture to the given size.
     * 
     * @param texture
     *            the texture to be scaled
     * @param width
     *            new width of the texture
     * @param height
     *            new height of the texture
     */
private void scale(Texture2D texture, int width, int height) {
    // first determine if scaling is required
    boolean scaleRequired = texture.getImage().getWidth() != width || texture.getImage().getHeight() != height;
    if (scaleRequired) {
        Image image = texture.getImage();
        BufferedImage sourceImage = ImageToAwt.convert(image, false, true, 0);
        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();
        BufferedImage targetImage = new BufferedImage(width, height, sourceImage.getType());
        Graphics2D g = targetImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(sourceImage, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
        g.dispose();
        Image output = new ImageLoader().load(targetImage, false);
        image.setWidth(width);
        image.setHeight(height);
        image.setData(output.getData(0));
        image.setFormat(output.getFormat());
    }
}
Also used : Image(com.jme3.texture.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 5 with Graphics2D

use of java.awt.Graphics2D in project jna by java-native-access.

the class DragHandler method createDragImage.

/** Create an image from the given icon.  The image is provided to the
     * native handler if drag images are supported natively.
     * @param gc current graphics configuration.
     * @param icon Icon on which to base the drag image.
     * @return image based on the given icon.
     */
protected Image createDragImage(GraphicsConfiguration gc, Icon icon) {
    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    BufferedImage image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g = (Graphics2D) image.getGraphics();
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(0, 0, w, h);
    // Ignore pixels in the buffered image
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, ghostAlpha));
    icon.paintIcon(dragSource, g, 0, 0);
    g.dispose();
    return image;
}
Also used : Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Graphics2D (java.awt.Graphics2D)1705 BufferedImage (java.awt.image.BufferedImage)803 Color (java.awt.Color)388 BasicStroke (java.awt.BasicStroke)185 Font (java.awt.Font)161 Point (java.awt.Point)142 AffineTransform (java.awt.geom.AffineTransform)140 Rectangle (java.awt.Rectangle)139 IOException (java.io.IOException)110 Rectangle2D (java.awt.geom.Rectangle2D)99 Dimension (java.awt.Dimension)92 GradientPaint (java.awt.GradientPaint)90 Image (java.awt.Image)90 FontMetrics (java.awt.FontMetrics)87 Paint (java.awt.Paint)81 Graphics (java.awt.Graphics)79 File (java.io.File)65 Point2D (java.awt.geom.Point2D)59 Stroke (java.awt.Stroke)55 TextLayout (java.awt.font.TextLayout)49