Example usage for java.awt Graphics2D setRenderingHints

List of usage examples for java.awt Graphics2D setRenderingHints

Introduction

In this page you can find the example usage for java.awt Graphics2D setRenderingHints.

Prototype

public abstract void setRenderingHints(Map<?, ?> hints);

Source Link

Document

Replaces the values of all preferences for the rendering algorithms with the specified hints .

Usage

From source file:org.polymap.service.geoserver.spring.PipelineMapProducer.java

public void writeTo(final OutputStream out) throws ServiceException, IOException {
    Timer timer = new Timer();

    // single layer? -> request ENCODED_IMAGE
    if (mapContext.getLayerCount() == 1) {
        MapLayer mapLayer = mapContext.getLayers()[0];
        ILayer layer = loader.findLayer(mapLayer);
        try {//w  w w .j  av a2s .c  o m
            Pipeline pipeline = loader.getOrCreatePipeline(layer, LayerUseCase.ENCODED_IMAGE);

            ProcessorRequest request = prepareProcessorRequest();
            pipeline.process(request, new ResponseHandler() {
                public void handle(ProcessorResponse pipeResponse) throws Exception {

                    HttpServletResponse response = GeoServerWms.response.get();
                    if (pipeResponse == EncodedImageResponse.NOT_MODIFIED) {
                        log.info("Response: 304!");
                        response.setStatus(304);
                    } else {
                        long lastModified = ((EncodedImageResponse) pipeResponse).getLastModified();
                        // allow caches and browser clients to cache for 1h
                        //response.setHeader( "Cache-Control", "public,max-age=3600" );
                        if (lastModified > 0) {
                            response.setHeader("Cache-Control", "no-cache,must-revalidate");
                            response.setDateHeader("Last-Modified", lastModified);
                        } else {
                            response.setHeader("Cache-Control", "no-cache,must-revalidate");
                            response.setDateHeader("Expires", 0);
                        }

                        byte[] chunk = ((EncodedImageResponse) pipeResponse).getChunk();
                        int len = ((EncodedImageResponse) pipeResponse).getChunkSize();
                        out.write(chunk, 0, len);
                    }
                }
            });
            log.debug("    flushing response stream. (" + timer.elapsedTime() + "ms)");
            out.flush();
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    // multiple layers -> render into one image
    else {
        List<Job> jobs = new ArrayList();
        final Map<MapLayer, Image> images = new HashMap();

        // run jobs for all layers
        for (final MapLayer mapLayer : mapContext.getLayers()) {
            final ILayer layer = loader.findLayer(mapLayer);
            // job
            UIJob job = new UIJob(getClass().getSimpleName() + ": " + layer.getLabel()) {
                protected void runWithException(IProgressMonitor monitor) throws Exception {
                    try {
                        // XXX this excludes Cache304 (which support EncodedImageResponse only)
                        Pipeline pipeline = loader.getOrCreatePipeline(layer, LayerUseCase.IMAGE);

                        GetMapRequest targetRequest = prepareProcessorRequest();
                        pipeline.process(targetRequest, new ResponseHandler() {
                            public void handle(ProcessorResponse pipeResponse) throws Exception {
                                Image layerImage = ((ImageResponse) pipeResponse).getImage();
                                images.put(mapLayer, layerImage);
                            }
                        });
                    } catch (Exception e) {
                        // XXX put a special image in the map
                        log.warn("", e);
                        images.put(mapLayer, null);
                        throw e;
                    }
                }
            };
            job.schedule();
            jobs.add(job);
        }

        // join jobs
        for (Job job : jobs) {
            try {
                job.join();
            } catch (InterruptedException e) {
                // XXX put a special image in the map
                log.warn("", e);
            }
        }

        // put images together (MapContext order)
        Graphics2D g = null;
        try {
            // result image
            BufferedImage result = ImageUtils.createImage(mapContext.getMapWidth(), mapContext.getMapHeight(),
                    null, true);
            g = result.createGraphics();

            // rendering hints
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
            hints.add(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
            g.setRenderingHints(hints);

            for (MapLayer mapLayer : mapContext.getLayers()) {
                Image layerImage = images.get(mapLayer);

                // load image data
                //                  new javax.swing.ImageIcon( image ).getImage();

                ILayer layer = loader.findLayer(mapLayer);
                int rule = AlphaComposite.SRC_OVER;
                float alpha = ((float) layer.getOpacity()) / 100;

                g.setComposite(AlphaComposite.getInstance(rule, alpha));
                g.drawImage(layerImage, 0, 0, null);
            }
            encodeImage(result, out);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }
    }
}

From source file:org.structnetalign.util.GraphImageWriter.java

private static BufferedImage getImage(VisualizationImageServer<Integer, Edge> vv, Point2D center, Dimension d) {
    int width = vv.getWidth();
    int height = vv.getHeight();

    float scalex = (float) width / d.width;
    float scaley = (float) height / d.height;
    try {/*  w  ww .  j ava 2s.  c  o  m*/
        vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).scale(scalex, scaley,
                center);

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = bi.createGraphics();
        graphics.setRenderingHints(vv.getRenderingHints());
        vv.paint(graphics);
        graphics.dispose();
        return bi;
    } finally {
        vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).setToIdentity();
    }
}

From source file:test.unit.be.fedict.eid.applet.service.KmlGeneratorTest.java

@Test
public void identityWithAddressAndPhotoKml() throws Exception {
    // setup//from ww w .j a  v  a 2s.  co  m
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generateKml(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}

From source file:test.unit.be.fedict.eid.applet.service.PdfGeneratorTest.java

@Test
public void identityWithAddressAndPhotoPdf() throws Exception {
    // setup//  w  w w. j av a2 s  .c  o m
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generatePdf(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}

From source file:test.unit.be.fedict.eid.applet.service.VcardGeneratorTest.java

@Test
public void identityWithAddressAndPhotoVcard() throws Exception {
    // setup/*ww  w.j av  a  2 s .  c  o  m*/
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generateVcard(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}