/** * Creates a single image. * * @param image The image to create. * @param constraint The constraint indicating to add the scale bar. * @param name The name of the image. * @throws EncoderException * @throws IOException */ private void writeSingleImage(BufferedImage image, boolean constraint, String name) throws EncoderException, IOException { int width = image.getWidth(); int h = image.getHeight(); String v = getUnitBarValue(); int s = (int) model.getUnitBarSize(); BufferedImage newImage = new BufferedImage(width, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) newImage.getGraphics(); ImagePaintingFactory.setGraphicRenderingSettings(g2); // Paint the original image. g2.drawImage(image, null, 0, 0); if (constraint) ImagePaintingFactory.paintScaleBar(g2, width - s - 10, h - 10, s, v); writeImage(newImage, name); }
/** * Overridden to paint the image. * * @see javax.swing.JComponent#paintComponent(Graphics) */ public void paintComponent(Graphics g) { if (image == null) return; Graphics2D g2D = (Graphics2D) g; ImagePaintingFactory.setGraphicRenderingSettings(g2D); g2D.drawImage(image, null, 0, 0); g2D.dispose(); }
/** * Saves the displayed images. * * @param init Pass <code>true</code> to initialize the images to save, <code>false</code> * otherwise. */ void saveImage(boolean init) { UserNotifier un = ImViewerAgent.getRegistry().getUserNotifier(); if (init) createImages(uiDelegate.getSavingType()); // Builds the image to display. boolean unitBar = model.isUnitBar(); String v = getUnitBarValue(); int s = (int) getUnitBarSize(); boolean constrain; try { String name = uiDelegate.getSelectedFilePath(); // make sure the parent directory paths all exist FileUtils.forceMkdir(new File(name).getParentFile()); if (imageComponents == null) { constrain = unitBar && v != null && s < mainImage.getWidth() && imageType == ImgSaverUI.IMAGE; writeSingleImage(mainImage, constrain, name); } else { if (mainImage == null) return; Iterator i; int h, w; BufferedImage newImage; Graphics2D g2; if (uiDelegate.isSaveImagesInSeparatedFiles()) { constrain = unitBar && v != null && s < mainImage.getWidth() && imageType == ImgSaverUI.IMAGE; writeSingleImage(mainImage, constrain, name); i = imageComponents.iterator(); int j = 0; while (i.hasNext()) { constrain = unitBar && v != null && imageType != ImgSaverUI.LENS_IMAGE_AND_COMPONENTS; writeSingleImage((BufferedImage) i.next(), constrain, name + "_" + j); j++; } } else { int width = mainImage.getWidth(); h = mainImage.getHeight(); int n = imageComponents.size(); w = width * (n + 1) + ImgSaverPreviewer.SPACE * (n - 1); newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); g2 = (Graphics2D) newImage.getGraphics(); g2.setColor(Color.WHITE); ImagePaintingFactory.setGraphicRenderingSettings(g2); // Paint the original image. i = imageComponents.iterator(); int x = 0; while (i.hasNext()) { g2.drawImage((BufferedImage) i.next(), null, x, 0); if (unitBar && v != null && imageType != ImgSaverUI.LENS_IMAGE_AND_COMPONENTS) ImagePaintingFactory.paintScaleBar(g2, x + width - s - 10, h - 10, s, v); x += width; g2.fillRect(x, 0, ImgSaverPreviewer.SPACE, h); x += ImgSaverPreviewer.SPACE; } g2.drawImage(mainImage, null, x, 0); if (unitBar && v != null && !(imageType == ImgSaverUI.LENS_IMAGE_AND_COMPONENTS || imageType == ImgSaverUI.LENS_IMAGE)) ImagePaintingFactory.paintScaleBar(g2, x + width - s - 10, h - 10, s, v); writeImage(newImage, name); } } } catch (Exception e) { if (e instanceof IOException || e.getCause() instanceof IOException) un.notifyInfo( "Save Image failure", "Could not access file " + uiDelegate.getSelectedFilePath() + "\nMake sure you have the necessary permissions to perform this action."); else un.notifyError("Save Image failure", "An error occurred while saving the image.", e); return; } notifySave(getExtendedName(uiDelegate.getSelectedFilePath(), format)); if (uiDelegate.isSetDefaultFolder()) UIUtilities.setDefaultFolder(uiDelegate.getCurrentDirectory()); }