Example usage for java.awt Graphics dispose

List of usage examples for java.awt Graphics dispose

Introduction

In this page you can find the example usage for java.awt Graphics dispose.

Prototype

public abstract void dispose();

Source Link

Document

Disposes of this graphics context and releases any system resources that it is using.

Usage

From source file:se.llbit.chunky.world.SkymapTexture.java

@Override
public void setTexture(BufferedImage newImage) {

    if (newImage.getType() == BufferedImage.TYPE_INT_ARGB) {
        image = newImage;/*ww w.  j  a va2  s.c  o  m*/
    } else {
        // convert to ARGB
        image = new BufferedImage(newImage.getWidth(), newImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.createGraphics();
        g.drawImage(newImage, 0, 0, null);
        g.dispose();
    }

    DataBufferInt dataBuffer = (DataBufferInt) image.getRaster().getDataBuffer();
    data = dataBuffer.getData();

    width = image.getWidth();
    height = image.getHeight();
    avgColor = ImageTools.calcAvgColor(image);

    Log.info("Preprocessing skymap texture");
    long start = System.currentTimeMillis();

    // gamma correct the texture

    int segX = 4;
    int segY = 4;
    if (width < segX)
        segX = 1;
    if (height < segY)
        segY = 1;
    TexturePreprocessor[][] preprocessor = new TexturePreprocessor[segX][segY];
    int w = width / segX;
    int h = height / segY;
    for (int i = 0; i < segX; ++i) {
        int x0 = w * i;
        int x1 = x0 + w - 1;
        if ((i + 1) == segX)
            x1 = width - 1;
        for (int j = 0; j < segY; ++j) {
            int y0 = h * j;
            int y1 = y0 + h - 1;
            if ((j + 1) == segY)
                y1 = height - 1;
            preprocessor[i][j] = new TexturePreprocessor(x0, x1, y0, y1);
            preprocessor[i][j].start();
        }
    }

    for (int i = 0; i < segX; ++i) {
        for (int j = 0; j < segY; ++j) {
            try {
                preprocessor[i][j].join();
            } catch (InterruptedException e) {
            }
        }
    }

    long time = System.currentTimeMillis() - start;

    Log.info("Skymap preprocessing took " + time + "ms");

}

From source file:MultiBufferTest.java

public MultiBufferTest(int numBuffers, GraphicsDevice device) {
    try {//from  w w w  .  j a v a  2  s  .  c o  m
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);
        mainFrame.setUndecorated(true);
        mainFrame.setIgnoreRepaint(true);
        device.setFullScreenWindow(mainFrame);
        if (device.isDisplayChangeSupported()) {
            chooseBestDisplayMode(device);
        }
        Rectangle bounds = mainFrame.getBounds();
        mainFrame.createBufferStrategy(numBuffers);
        BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
        for (float lag = 2000.0f; lag > 0.00000006f; lag = lag / 1.33f) {
            for (int i = 0; i < numBuffers; i++) {
                Graphics g = bufferStrategy.getDrawGraphics();
                if (!bufferStrategy.contentsLost()) {
                    g.setColor(COLORS[i]);
                    g.fillRect(0, 0, bounds.width, bounds.height);
                    bufferStrategy.show();
                    g.dispose();
                }
                try {
                    Thread.sleep((int) lag);
                } catch (InterruptedException e) {
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        device.setFullScreenWindow(null);
    }
}

From source file:com.krawler.esp.handlers.genericFileUpload.java

private BufferedImage toBufferedImage(Image image) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.createGraphics();
    g.setColor(Color.white);//from  ww  w  .  jav  a2 s  . c  o  m
    g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bufferedImage;
}

From source file:org.jimcat.gui.borders.RoundedShadowBorder.java

/**
 * //from  www.j a v a2 s.co  m
 * paint this type of border
 * 
 * @see org.jimcat.gui.borders.RoundedBorder#paintBorder(java.awt.Component,
 *      java.awt.Graphics, int, int, int, int)
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Color borderColor = c.getBackground();

    // check if old image is still useable
    if (imageBuffer == null || width != lastWidth || height != lastHeight
            || !ObjectUtils.equals(borderColor, lastColor)) {
        // update image buffer
        imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        lastHeight = height;
        lastWidth = width;
        lastColor = borderColor;

        int shadow_width = SHADOW_WIDTH * 2;

        int xb = Math.max(x + SHADOW_WIDTH - SHADOW_DISPLACEMENT.x, x);
        int yb = Math.max(y + SHADOW_WIDTH - SHADOW_DISPLACEMENT.y, y);

        int xs = Math.max(x, x + SHADOW_DISPLACEMENT.x);
        int ys = Math.max(y, y + SHADOW_DISPLACEMENT.y);

        BufferedImage border = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics imgG = border.createGraphics();
        super.paintBorder(c, imgG, xb, yb, width - shadow_width - Math.abs(SHADOW_DISPLACEMENT.x),
                height - shadow_width - Math.abs(SHADOW_DISPLACEMENT.y));
        imgG.dispose();

        BufferedImage shadow = shadowRenderer.createShadow(border);

        Graphics2D g2 = (Graphics2D) imageBuffer.getGraphics();
        g2.drawImage(shadow, xs, ys, null);
        g2.drawImage(border, xb, yb, null);
        g2.dispose();
    }
    g.drawImage(imageBuffer, x, y, null);
}

From source file:com.pureinfo.tgirls.servlet.TestServlet.java

private BufferedImage getImg(File _temp, float _i) throws IOException {
    BufferedImage output;//from  ww w  .  j a va  2s  .  c om
    Image img = ImageIO.read(_temp);
    int width = img.getWidth(null);
    int height = img.getHeight(null);

    Float f = width * _i;
    width = f.intValue();
    f = height * _i;
    height = f.intValue();

    output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = output.getGraphics();
    g.drawImage(img, 0, 0, width, height, null);
    g.dispose();
    return output;
}

From source file:haven.Utils.java

public static BufferedImage outline2(BufferedImage img, Color col) {
    BufferedImage ol = outline(img, col);
    Graphics g = ol.getGraphics();
    g.drawImage(img, 1, 1, null);/* w  w w . j  a v  a2s . co m*/
    g.dispose();
    return (ol);
}

From source file:com.krawler.esp.handlers.FileUploadHandler.java

private BufferedImage toBufferedImage(Image image, int type) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bufferedImage.createGraphics();
    if (type == BufferedImage.TYPE_INT_RGB) {
        g.setColor(Color.white);/*from ww w. j  ava 2s .  co  m*/
        g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    }
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bufferedImage;
}

From source file:org.mili.core.graphics.GraphicsUtilTest.java

@Before
public void setUp() throws Exception {
    FileUtils.deleteDirectory(this.dir);
    this.dir.mkdirs();
    // original image
    BufferedImage outImg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics g = outImg.getGraphics();
    g.setColor(Color.YELLOW);/*from   w w  w  . ja  v a2s .c o  m*/
    g.fillRect(0, 0, 50, 50);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "test.jpg"));
    // original image 2
    outImg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    g = outImg.getGraphics();
    g.setColor(Color.YELLOW);
    g.fillRect(0, 0, 200, 200);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "test_big.jpg"));
    // image to set
    outImg = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
    g = outImg.getGraphics();
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 20, 20);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "block.jpg"));
}

From source file:com.aistor.common.servlet.ValidateCodeServlet.java

private void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    /*/*from  www.  j  ava  2s  . c o  m*/
     * ??
     */
    String width = request.getParameter("width");
    String height = request.getParameter("height");
    if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
        w = NumberUtils.toInt(width);
        h = NumberUtils.toInt(height);
    }

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    /*
     * ?
     */
    createBackground(g);

    /*
     * ?
     */
    String s = createCharacter(g);
    request.getSession().setAttribute("validateCode", s);

    g.dispose();
    OutputStream out = response.getOutputStream();
    ImageIO.write(image, "JPEG", out);
    out.close();

}

From source file:apm.common.servlet.ValidateCodeServlet.java

private void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    /*/*  w  w w  .jav a2  s.  c  o m*/
     * ??
     */
    String width = request.getParameter("width");
    String height = request.getParameter("height");
    if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
        w = NumberUtils.toInt(width);
        h = NumberUtils.toInt(height);
    }

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    /*
     * ?
     */
    createBackground(g);

    /*
     * ?
     */
    String s = createCharacter(g);
    request.getSession().setAttribute(VALIDATE_CODE, s);

    g.dispose();
    OutputStream out = response.getOutputStream();
    ImageIO.write(image, "JPEG", out);
    out.close();

}