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:edu.ku.brc.specify.rstools.GoogleEarthExporter.java

/**
 * Takes an ImageIcon (in memory) and writes it out to a file.
 * @param icon the image icon/*from w w  w.  jav a  2 s .c  om*/
 * @param output the destination file
 * @return true on success
 * @throws IOException
 */
protected boolean writeImageIconToFile(final ImageIcon icon, final File output) throws IOException {
    if (icon != null && icon.getIconWidth() > 0 && icon.getIconHeight() > 0 && output != null) {
        try {
            BufferedImage bimage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            if (bimage != null) {
                Graphics g = bimage.createGraphics();
                if (g != null) {
                    g.drawImage(icon.getImage(), 0, 0, null);
                    g.dispose();
                    ImageIO.write(bimage, "PNG", output);
                    return true;
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GoogleEarthExporter.class, ex);
            // no need to throw an exception or display it
        }
    }
    return false;
}

From source file:de.tor.tribes.ui.views.DSWorkbenchRankFrame.java

@Override
public void actionPerformed(ActionEvent e) {
    RankTableTab activeTab = getActiveTab();
    if (e.getActionCommand() != null && activeTab != null) {
        if (e.getActionCommand().equals("Find")) {
            BufferedImage back = ImageUtils.createCompatibleBufferedImage(3, 3, BufferedImage.TRANSLUCENT);
            Graphics g = back.getGraphics();
            g.setColor(new Color(120, 120, 120, 120));
            g.fillRect(0, 0, back.getWidth(), back.getHeight());
            g.setColor(new Color(120, 120, 120));
            g.drawLine(0, 0, 3, 3);/*from  w w  w.  ja  va 2 s. c om*/
            g.dispose();
            TexturePaint paint = new TexturePaint(back,
                    new Rectangle2D.Double(0, 0, back.getWidth(), back.getHeight()));
            jxSearchPane.setBackgroundPainter(new MattePainter(paint));
            DefaultListModel model = new DefaultListModel();

            for (int i = 0; i < activeTab.getRankTable().getColumnCount(); i++) {
                TableColumnExt col = activeTab.getRankTable().getColumnExt(i);
                if (col.isVisible()) {
                    if (col.getTitle().equals("Name") || col.getTitle().equals("Tag")
                            || col.getTitle().equals("Stamm")) {
                        model.addElement(col.getTitle());
                    }

                }
            }
            jXColumnList.setModel(model);
            jXColumnList.setSelectedIndex(0);
            jxSearchPane.setVisible(true);
        }
    }

}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???//www .j  ava2  s  .c  om
 * @param request
 * @param response
 */
public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 
    int width = 60, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // ?
    Graphics g = image.getGraphics();

    // ??
    Random random = new Random();

    // 
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    // 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    // 
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // ?155?????
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789";
    // ????(4?)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        int start = random.nextInt(base.length());
        String rand = base.substring(start, start + 1);
        sRand = sRand.concat(rand);
        // ??
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);
    }

    // ??SESSION
    request.getSession().setAttribute("entrymrand", sRand);

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

    out.flush();
    out.close();
}

From source file:it.geosolutions.opensdi2.mvc.BaseFileManager.java

/**
 * Generate a image thumb//from   w ww  .j  a va 2s . co m
 * 
 * @param toServeUp
 * @param thumbPath
 * @return
 * @throws IOException
 */
protected InputStream getImageThumb(File toServeUp, String thumbPath) throws IOException {
    BufferedImage image = ImageIO.read(toServeUp);
    ImageIcon img = new ImageIcon(
            new ImageIcon(image).getImage().getScaledInstance(THUMB_W, THUMB_H, Image.SCALE_FAST));
    BufferedImage bimage = new BufferedImage(THUMB_W, THUMB_H, BufferedImage.TYPE_INT_RGB);

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    g.drawImage(img.getImage(), 0, 0, null);
    g.dispose();
    ImageIO.write(bimage, "jpg", new File(thumbPath));
    return new java.io.FileInputStream(thumbPath);
}

From source file:org.opensha.commons.util.FileUtils.java

/**
 * Prints a Text file/*from   w w w  . ja v  a 2s . c om*/
 * @param pjob PrintJob  created using getToolkit().getPrintJob(JFrame,String,Properties);
 * @param pg Graphics
 * @param textToPrint String
 */
public static void print(PrintJob pjob, Graphics pg, String textToPrint) {

    int margin = 60;

    int pageNum = 1;
    int linesForThisPage = 0;
    int linesForThisJob = 0;
    // Note: String is immutable so won't change while printing.
    if (!(pg instanceof PrintGraphics)) {
        throw new IllegalArgumentException("Graphics context not PrintGraphics");
    }
    StringReader sr = new StringReader(textToPrint);
    LineNumberReader lnr = new LineNumberReader(sr);
    String nextLine;
    int pageHeight = pjob.getPageDimension().height - margin;
    Font helv = new Font("Monaco", Font.PLAIN, 12);
    //have to set the font to get any output
    pg.setFont(helv);
    FontMetrics fm = pg.getFontMetrics(helv);
    int fontHeight = fm.getHeight();
    int fontDescent = fm.getDescent();
    int curHeight = margin;
    try {
        do {
            nextLine = lnr.readLine();
            if (nextLine != null) {
                if ((curHeight + fontHeight) > pageHeight) {
                    // New Page
                    if (linesForThisPage == 0)
                        break;

                    pageNum++;
                    linesForThisPage = 0;
                    pg.dispose();
                    pg = pjob.getGraphics();
                    if (pg != null) {
                        pg.setFont(helv);
                    }
                    curHeight = 0;
                }
                curHeight += fontHeight;
                if (pg != null) {
                    pg.drawString(nextLine, margin, curHeight - fontDescent);
                    linesForThisPage++;

                    linesForThisJob++;
                }
            }
        } while (nextLine != null);
    } catch (EOFException eof) {
        // Fine, ignore
    } catch (Throwable t) { // Anything else
        t.printStackTrace();
    }
}

From source file:se.llbit.chunky.resources.Texture.java

public void setTexture(BufferedImage newImage) {
    if (newImage.getType() == BufferedImage.TYPE_INT_ARGB) {
        image = newImage;/*from   w  w  w .  j  a  v a 2 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();
    }

    // gamma correct the texture
    avgColorLinear = new float[] { 0, 0, 0, 0 };

    DataBufferInt dataBuffer = (DataBufferInt) image.getRaster().getDataBuffer();
    int[] data = dataBuffer.getData();
    width = image.getWidth();
    height = image.getHeight();
    linear = new float[width * height][4];
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            int index = width * y + x;
            Color.getRGBAComponents(data[index], linear[index]);
            linear[index][0] = (float) FastMath.pow(linear[index][0], Scene.DEFAULT_GAMMA);
            linear[index][1] = (float) FastMath.pow(linear[index][1], Scene.DEFAULT_GAMMA);
            linear[index][2] = (float) FastMath.pow(linear[index][2], Scene.DEFAULT_GAMMA);
            avgColorLinear[0] += linear[index][3] * linear[index][0];
            avgColorLinear[1] += linear[index][3] * linear[index][1];
            avgColorLinear[2] += linear[index][3] * linear[index][2];
            avgColorLinear[3] += linear[index][3];
        }
    }

    if (PersistentSettings.getSingleColorTextures()) {
        float[] avgColorFlat = { 0, 0, 0 };
        if (avgColorLinear[3] > 0.001) {
            avgColorFlat[0] = avgColorLinear[0] / avgColorLinear[3];
            avgColorFlat[1] = avgColorLinear[1] / avgColorLinear[3];
            avgColorFlat[2] = avgColorLinear[2] / avgColorLinear[3];
        }
        for (int y = 0; y < height; ++y) {
            for (int x = 0; x < width; ++x) {
                int index = width * y + x;
                linear[index][0] = avgColorFlat[0];
                linear[index][1] = avgColorFlat[1];
                linear[index][2] = avgColorFlat[2];
                linear[index][3] = 1;
            }
        }
    }

    avgColorLinear[0] /= width * height;
    avgColorLinear[1] /= width * height;
    avgColorLinear[2] /= width * height;
    avgColorLinear[3] /= width * height;

    avgColor = Color.getRGBA(FastMath.pow(avgColorLinear[0], 1 / Scene.DEFAULT_GAMMA),
            FastMath.pow(avgColorLinear[1], 1 / Scene.DEFAULT_GAMMA),
            FastMath.pow(avgColorLinear[2], 1 / Scene.DEFAULT_GAMMA), avgColorLinear[3]);
}

From source file:TextBouncer.java

protected void render() {
    Graphics g = getGraphics();//from  w w  w  .  jav  a 2  s.  co m
    if (g != null) {
        Dimension d = getSize();
        if (checkImage(d)) {
            Graphics imageGraphics = image.getGraphics();
            // Clear the image background.
            imageGraphics.setColor(getBackground());
            imageGraphics.fillRect(0, 0, d.width, d.height);
            imageGraphics.setColor(getForeground());
            // Draw this component offscreen.
            paint(imageGraphics);
            // Now put the offscreen image on the screen.
            g.drawImage(image, 0, 0, null);
            // Clean up.
            imageGraphics.dispose();
        }
        g.dispose();
    }
}

From source file:TrackFocusDemo.java

protected void paintComponent(Graphics graphics) {
    Graphics g = graphics.create();

    //Draw in our entire space, even if isOpaque is false.
    g.setColor(Color.WHITE);//from  w  w w.j ava2s .com
    g.fillRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));

    if (image != null) {
        //Draw image at its natural size of 125x125.
        g.drawImage(image, 0, 0, this);
    }

    //Add a border, red if picture currently has focus
    if (isFocusOwner()) {
        g.setColor(Color.RED);
    } else {
        g.setColor(Color.BLACK);
    }
    g.drawRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));
    g.dispose();
}

From source file:org.deegree.portal.standard.wms.control.DynLegendListener.java

/**
 * In case the legend can not be obtained from the OGCLayer, this method is called to create a dummy legend image
 * plus the legend title//from www .j av a 2 s .com
 * 
 * @param layerName
 * @return BufferedImage holding the created dummy legend
 */
private BufferedImage createMissingLegend(String layerName) {
    LOG.logDebug("URL is null. Drawing the image from a missingImage variable in init params");
    BufferedImage missingLegend = new BufferedImage(80, 15, BufferedImage.TYPE_INT_RGB);
    Graphics g = missingLegend.getGraphics();
    Rectangle2D rect = g.getFontMetrics().getStringBounds(layerName, g);
    g.dispose();
    missingLegend = new BufferedImage(rect.getBounds().width + 80, missingImg.getHeight() + 15,
            BufferedImage.TYPE_INT_ARGB);
    g = missingLegend.getGraphics();
    g.drawImage(missingImg, 0, 0, null);
    g.setColor(Color.RED);
    if (useLayerTitle) {
        g.drawString(layerName, missingImg.getWidth() + 5,
                missingImg.getHeight() / 2 + g.getFont().getSize() / 2);
    }
    g.dispose();

    return missingLegend;
}

From source file:com.synnex.saas.platform.core.servlet.CaptchaServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//  w  w  w.  j  a  v a 2s .  co m
        int width = 50;
        int height = 18;
        String captchaCode = RandomStringUtils.random(4, true, true);
        HttpSession session = request.getSession(true);
        session.setAttribute("captchaCode", captchaCode);

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

        ServletOutputStream out = response.getOutputStream();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        Font mFont = new Font("Times New Roman", Font.BOLD, 18);
        g.setFont(mFont);
        g.setColor(getRandColor(160, 200));
        Random random = new Random();
        for (int i = 0; i < 155; i++) {
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            int x3 = random.nextInt(12);
            int y3 = random.nextInt(12);
            g.drawLine(x2, y2, x2 + x3, y2 + y3);
        }
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(captchaCode, 2, 16);
        g.dispose();
        ImageIO.write((BufferedImage) image, "JPEG", out);
        out.close();
    } catch (Exception e) {
        logger.error("Generate captcha failed.", e);
    }
}