Search in sources :

Example 36 with Graphics2D

use of java.awt.Graphics2D in project opennms by OpenNMS.

the class TimelineRestService method image.

@GET
@Produces("image/png")
@Transactional
@Path("image/{nodeId}/{ipAddress}/{serviceName}/{start}/{end}/{width}")
public Response image(@Context final UriInfo uriInfo, @PathParam("nodeId") final int nodeId, @PathParam("ipAddress") final String ipAddress, @PathParam("serviceName") final String serviceName, @PathParam("start") final long start, @PathParam("end") final long end, @PathParam("width") final int width) throws IOException {
    long delta = end - start;
    OnmsOutageCollection onmsOutageCollection = queryOutages(uriInfo, nodeId, ipAddress, serviceName, start, end);
    BufferedImage bufferedImage = new BufferedImage(width, 20, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
    graphics2D.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
    graphics2D.setColor(Color.BLACK);
    int numLabels = TimescaleDescriptor.computeNumberOfLabels(graphics2D, delta, width);
    for (TimescaleDescriptor desc : TIMESCALE_DESCRIPTORS) {
        if (desc.match(delta, numLabels)) {
            desc.drawGreen(graphics2D, width);
            for (OnmsOutage onmsOutage : onmsOutageCollection) {
                desc.drawEvent(graphics2D, delta, start, width, onmsOutage);
            }
            desc.drawLine(graphics2D, delta, start, width);
            break;
        }
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", baos);
    byte[] imageData = baos.toByteArray();
    return Response.ok(imageData).build();
}
Also used : OnmsOutage(org.opennms.netmgt.model.OnmsOutage) OnmsOutageCollection(org.opennms.netmgt.model.OnmsOutageCollection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Example 37 with Graphics2D

use of java.awt.Graphics2D in project jdk8u_jdk by JetBrains.

the class BMPPluginTest method createTestImage.

private static BufferedImage createTestImage(int type) throws IOException {
    int w = 200;
    int h = 200;
    BufferedImage b = new BufferedImage(w, h, type);
    Graphics2D g = b.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.black);
    g.fillOval(10, 10, w - 20, h - 20);
    return b;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 38 with Graphics2D

use of java.awt.Graphics2D in project jdk8u_jdk by JetBrains.

the class BMPSubsamplingTest method getTestImage.

private BufferedImage getTestImage(int type) {
    BufferedImage dst = null;
    ColorModel colorModel = null;
    WritableRaster raster = null;
    switch(type) {
        case TYPE_INT_GRB:
            colorModel = new DirectColorModel(24, 0x0000ff00, 0x00ff0000, 0x000000ff);
            break;
        case TYPE_INT_GBR:
            colorModel = new DirectColorModel(24, 0x000000ff, 0x00ff0000, 0x0000ff00);
            break;
        case TYPE_INT_RBG:
            colorModel = new DirectColorModel(24, 0x00ff0000, 0x000000ff, 0x0000ff00);
            break;
        case TYPE_INT_BRG:
            colorModel = new DirectColorModel(24, 0x0000ff00, 0x000000ff, 0x00ff0000);
            break;
        case TYPE_3BYTE_RGB:
            dst = create3ByteImage(new int[] { 8, 8, 8 }, new int[] { 0, 1, 2 });
            break;
        case TYPE_3BYTE_GRB:
            dst = create3ByteImage(new int[] { 8, 8, 8 }, new int[] { 1, 0, 2 });
            break;
        case TYPE_USHORT_555_GRB:
            colorModel = new DirectColorModel(16, 0x03E0, 0x7C00, 0x001F);
            break;
        case TYPE_USHORT_555_BGR:
            colorModel = new DirectColorModel(16, 0x001F, 0x03E0, 0x7C00);
            break;
        case TYPE_USHORT_565_BGR:
            colorModel = new DirectColorModel(16, 0x001F, 0x07E0, 0xf800);
            break;
        case TYPE_4BPP_INDEXED:
            dst = createIndexImage(4);
            break;
        default:
            dst = new BufferedImage(w, h, type);
    }
    if (dst == null) {
        raster = colorModel.createCompatibleWritableRaster(w, h);
        dst = new BufferedImage(colorModel, raster, false, null);
    }
    Graphics2D g = dst.createGraphics();
    for (int i = 0; i < colors.length; i++) {
        g.setColor(colors[i]);
        g.fillRect(i * dx, 0, dx, h);
    }
    g.dispose();
    return dst;
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 39 with Graphics2D

use of java.awt.Graphics2D in project jdk8u_jdk by JetBrains.

the class HelvLtOblTest method drawText.

BufferedImage drawText(boolean doGV) {
    int w = 400;
    int h = 50;
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.black);
    Font f = helvFont.deriveFont(Font.PLAIN, 40);
    g.setFont(f);
    int x = 5;
    int y = h - 10;
    if (doGV) {
        FontRenderContext frc = new FontRenderContext(null, true, true);
        GlyphVector gv = f.createGlyphVector(frc, codes);
        g.drawGlyphVector(gv, 5, y);
    } else {
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g.drawString(str, x, y);
    }
    return bi;
}
Also used : GlyphVector(java.awt.font.GlyphVector) FontRenderContext(java.awt.font.FontRenderContext) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 40 with Graphics2D

use of java.awt.Graphics2D in project jdk8u_jdk by JetBrains.

the class BMPWriteParamTest method createTestImage.

private static BufferedImage createTestImage() throws IOException {
    int w = 50;
    int h = 50;
    BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = b.createGraphics();
    g.setColor(Color.red);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.white);
    for (int i = 10; i <= 10 + 30; i += 3) {
        g.drawLine(i, 10, i, 40);
        g.drawLine(10, i, 40, i);
    }
    return b;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Graphics2D (java.awt.Graphics2D)1716 BufferedImage (java.awt.image.BufferedImage)809 Color (java.awt.Color)394 BasicStroke (java.awt.BasicStroke)186 Font (java.awt.Font)161 Point (java.awt.Point)145 AffineTransform (java.awt.geom.AffineTransform)140 Rectangle (java.awt.Rectangle)139 IOException (java.io.IOException)112 Rectangle2D (java.awt.geom.Rectangle2D)100 Dimension (java.awt.Dimension)92 Image (java.awt.Image)91 GradientPaint (java.awt.GradientPaint)90 FontMetrics (java.awt.FontMetrics)87 Paint (java.awt.Paint)82 Graphics (java.awt.Graphics)79 File (java.io.File)65 Point2D (java.awt.geom.Point2D)59 Stroke (java.awt.Stroke)55 ImageIcon (javax.swing.ImageIcon)50