/** Frees the resources */ void freeResources() { if (images != null) { for (Image image : images) { if (image != null) image.dispose(); } images = null; } }
protected Graphics getBufferGraphics() { if (bufferImage == null) { bufferImage = createImage(getSize().width, getSize().height); buffer_graphics = bufferImage.getGraphics(); } return buffer_graphics; }
/** * Prepare a buffer for the image, return if the canvas is ready Only need to call this when the * size of the canvas is changed, Since we automatically detect the size change in paint() only * call this on start */ public boolean newImgBuf() { Dimension sz = getSize(); if (sz.width == 0 || sz.height == 0) return false; // quit if the current image already has the right size if (img != null && imgG != null && sz.equals(imgSize)) return true; img = createImage(sz.width, sz.height); if (imgG != null) imgG.dispose(); imgG = img.getGraphics(); imgSize = sz; return true; }
/** Make (scale down) the thumbnail */ private Image makeThumbnail(Image imageOriginal) { double ratio = (double) imageOriginal.getWidth(null) / (double) imageOriginal.getHeight(null); int b = THUMBNAIL.width; int h = THUMBNAIL.height; if (ratio > 0) { b = (int) (h * ratio); } else { h = (int) (b * ratio); } if (b == 0 || h == 0) { System.out.println( "ERROR makeThumbnail: width = " + b + ", height = " + h + ", imageRation = " + ratio); System.out.println( " Workaround: width = " + THUMBNAIL.width + ", height = " + THUMBNAIL.height); b = THUMBNAIL.width; h = THUMBNAIL.height; } return getScaledImage(imageOriginal, b, h); }
private void writeToFile(File selectedFile, ImageWriterSpiFileFilter ff) { try { ImageOutputStream ios = ImageIO.createImageOutputStream(selectedFile); ImageWriter iw = ff.getImageWriterSpi().createWriterInstance(); iw.setOutput(ios); ImageWriteParam iwp = iw.getDefaultWriteParam(); if (iwp.canWriteCompressed()) { iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // set maximum image quality iwp.setCompressionQuality(1.f); } Image image = viewerPanel.getImage(); BufferedImage bufferedImage; if (viewerPanel.getImage() instanceof BufferedImage) bufferedImage = (BufferedImage) viewerPanel.getImage(); else { bufferedImage = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics().drawImage(image, 0, 0, null); } iw.write(null, new IIOImage(bufferedImage, null, null), iwp); iw.dispose(); ios.close(); } catch (IOException ioe) { JOptionPane.showMessageDialog( viewerPanel, messagesBundle.getString( "ImageViewerPanelSaveAction." + "Error_during_image_saving_message_7"), //$NON-NLS-1$ messagesBundle.getString("ImageViewerPanelSaveAction." + "Error_dialog_title_8"), //$NON-NLS-1$ JOptionPane.ERROR_MESSAGE); ioe.printStackTrace(); } }
/** Make (scale down) the thumbnail */ private Image getScaledImage(Image image, int breite, int hoehe) { Image scaledImage = image.getScaledInstance(breite, hoehe, Image.SCALE_DEFAULT); try { MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(scaledImage, 0); mediaTracker.waitForID(0); } catch (InterruptedException e) { return null; } return scaledImage; }
/** * the constructor of the class * * @param image an image */ protected ImageRepresentation(Image image) { this.largeImage = image; this.smallImage = image.getScaledInstance(smallImageSize.width, smallImageSize.height, Image.SCALE_SMOOTH); }
public void onDispose() { image.flush(); image = null; }
/** * calls drawImage(Image,int,int,int,int,Color,ImageObserver) * * @see #drawImage(Image,int,int,int,int,Color,ImageObserver) */ public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) { return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer); }