public ArrayList<BufferedImage> Split(BufferedImage orgBilde) throws IOException { ArrayList<BufferedImage> bilder = new ArrayList<>(); int rader = 3; int kolonner = 2; int bildeBit = rader * kolonner; int splittetBredde = orgBilde.getWidth() / kolonner; int splittetHøyde = orgBilde.getHeight() / rader; int count = 0; BufferedImage imgs[] = new BufferedImage[bildeBit]; for (int x = 0; x < rader; x++) { for (int y = 0; y < kolonner; y++) { imgs[count] = new BufferedImage(splittetBredde, splittetHøyde, orgBilde.getType()); Graphics2D gr = imgs[count++].createGraphics(); gr.drawImage( orgBilde, 0, 0, splittetBredde, splittetHøyde, splittetBredde * y, splittetHøyde * x, splittetBredde * y + splittetBredde, splittetHøyde * x + splittetHøyde, null); gr.dispose(); } } bilder.addAll(Arrays.asList(imgs)); return bilder; }
/** * Draws a double headed arrow with arrow heads of a given width and height. * * @param aG the canvas to draw on; * @param aX1 the starting X position of the arrow; * @param aY1 the starting Y position of the arrow; * @param aX2 the ending X position of the arrow; * @param aY2 the ending Y position of the arrow; * @param aArrowWidth the total width of the arrow head; * @param aArrowHeight the total height of the arrow head. */ public static final void drawDoubleHeadedArrow( final Graphics aG, final int aX1, final int aY1, final int aX2, final int aY2, final int aArrowWidth, final int aArrowHeight) { final Graphics2D g2d = (Graphics2D) aG.create(); final int lineWidth = Math.abs(aX2 - aX1); final int threshold = (2 * aArrowWidth) + 2; try { int x1 = aX1; int x2 = aX2; if (lineWidth > threshold) { drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight); // why x2 needs to be shifted by one pixel is beyond me... drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight); x1 += aArrowWidth - 1; x2 -= aArrowWidth + 1; } g2d.drawLine(x1, aY1, x2, aY2); } finally { g2d.dispose(); } }
@Override public BufferedImage makeIcon( final int width, final int height, final Path2D iconShape, final boolean allowAlpha) { final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = result.createGraphics(); g.setStroke(new BasicStroke(0.05f)); final Color c; if (allowAlpha) { c = new Color( this.color.getRed(), this.color.getGreen(), this.color.getBlue(), this.color.getAlpha()); } else { c = new Color(this.color.getRGB()); } if (iconShape != null) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setColor(c); final Shape path = makeTransformedPathForSize(width, height, iconShape); g.fill(path); } else { g.setColor(c); g.fillRect(0, 0, width, height); } g.dispose(); return result; }
/** * Returns the specified image as icon. * * @param name name of icon * @return icon */ public static ImageIcon icon(final String name) { ImageIcon ii = ICONS.get(name); if (ii != null) return ii; Image img; if (GUIConstants.scale > 1) { // choose large image or none final URL url = GUIConstants.large() ? BaseXImages.class.getResource("/img/" + name + "_32.png") : null; if (url == null) { // resize low-res image if no hi-res image exists img = get(url(name)); final int w = (int) (img.getWidth(null) * GUIConstants.scale); final int h = (int) (img.getHeight(null) * GUIConstants.scale); final BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(img, 0, 0, w, h, null); g2.dispose(); img = tmp; } else { img = get(url); } } else { img = get(name); } ii = new ImageIcon(img); ICONS.put(name, ii); return ii; }
public static BufferedImage convertToARGB(BufferedImage image) { BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return newImage; }
public static BufferedImage resize(BufferedImage image, int width, int height) { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.addRenderingHints( new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); return bi; }
@SuppressWarnings("unchecked") private <T> T scaleImageUsingGraphics2D(final BufferedImage bufferedImage, T target) { BufferedImage destinationImage = generateDestinationImage(); Graphics2D graphics2D = destinationImage.createGraphics(); graphics2D.addRenderingHints(retrieveRenderingHints()); graphics2D.drawImage(bufferedImage, O_X, O_Y, getQualifiedWidth(), getQualifiedHeight(), null); graphics2D.dispose(); target = (T) destinationImage; return target; }
public static BufferedImage cropImage(BufferedImage bi, int x, int y, int w, int h) { BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); g2.fillRect(0, 0, w, h); g2.drawImage(bi, -x, -y, null); // this); g2.dispose(); return image; }
public static BufferedImage scaleImage(BufferedImage bi, double scale) { int w1 = (int) (Math.round(scale * bi.getWidth())); int h1 = (int) (Math.round(scale * bi.getHeight())); BufferedImage image = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); g2.fillRect(0, 0, w1, h1); g2.drawImage(bi, 0, 0, w1, h1, null); // this); g2.dispose(); return image; }
public static BufferedImage rotateImage(BufferedImage bi) { int w = bi.getWidth(); int h = bi.getHeight(); BufferedImage image = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); // getBackground()); g2.fillRect(0, 0, h, w); g2.rotate(90 * Math.PI / 180); g2.drawImage(bi, 0, -h, w, h, null); // this); g2.dispose(); return image; }
public Example(BufferedImage image) { // copy image int width = image.getWidth(); int height = image.getHeight(); this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = this.image.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); // create icon if (Math.max(width, height) > ICON_SIZE) { double scale = Math.min((double) ICON_SIZE / width, (double) ICON_SIZE / height); width *= scale; height *= scale; } BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); g2 = buf.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(image, 0, 0, width, height, null); g2.dispose(); this.icon = new ImageIcon(buf); }
/* * Create a BufferedImage for Swing components. * All or part of the component can be captured to an image. * * @param component Swing component to create image from * @param region The region of the component to be captured to an image * @param fileName name of file to be created or null * @return image the image for the given region * @exception IOException if an error occurs during writing */ public static BufferedImage createImage(JComponent component, Rectangle region, String fileName) throws IOException { boolean opaqueValue = component.isOpaque(); component.setOpaque(true); BufferedImage image = new BufferedImage(region.width, region.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); g2d.setClip(region); component.paint(g2d); g2d.dispose(); component.setOpaque(opaqueValue); ScreenCapture.writeImage(image, fileName); return image; }
@Override public void prerasterizeIcon(final Shape shape) { final Rectangle r = shape.getBounds(); final BufferedImage prerasterized = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = prerasterized.createGraphics(); g.setStroke(new BasicStroke(0.05f)); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setColor(this.color); g.fill(shape); g.dispose(); this.prerasterizedImage = prerasterized; }
/** * Writes this map to a JPG file. * * @param filename the path and name of the file to create. */ public void writeToJPGFile(String filename) throws IOException { this.prepareToDraw(); BufferedImage buffImage = new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = buffImage.createGraphics(); try { p.draw(graphics2D); System.out.println("Writing picture to " + filename); ImageIO.write(buffImage, "JPG", new File(filename)); } finally { graphics2D.dispose(); } }
/** * Converts a given Image into a BufferedImage * * @param img The Image to be converted * @param type The color model of BufferedImage * @return The converted BufferedImage */ public static BufferedImage toBufferedImage(Image img, int type) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), type); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; }
private BufferedImage createCustomImage() { BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); int dx = image.getWidth() / SQUARES; int dy = image.getHeight() / SQUARES; for (int i = 0; i < SQUARES; ++i) { for (int j = 0; j < SQUARES; ++j) { g2d.setColor(new Color(rand.nextInt())); g2d.fillRect(i * dx, j * dy, dx, dy); } } g2d.setColor(Color.GREEN); g2d.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1); g2d.dispose(); return image; }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(x, y); g2.setStroke(new BasicStroke(BORDER_WIDTH)); g2.setPaint(LINE_COLOR); g2.draw(BORDER); g2.setStroke(new BasicStroke(SLIT_WIDTH)); g2.setColor(UIManager.getColor("Panel.background")); int n = SLIT_NUM + 1; int v = ICON_SIZE / n; int m = n * v; for (int i = 1; i < n; i++) { int a = i * v; g2.drawLine(a, 0, a, m); g2.drawLine(0, a, m, a); } // g2.drawLine(1 * v, 0 * v, 1 * v, 4 * v); // g2.drawLine(2 * v, 0 * v, 2 * v, 4 * v); // g2.drawLine(3 * v, 0 * v, 3 * v, 4 * v); // g2.drawLine(0 * v, 1 * v, 4 * v, 1 * v); // g2.drawLine(0 * v, 2 * v, 4 * v, 2 * v); // g2.drawLine(0 * v, 3 * v, 4 * v, 3 * v); g2.setPaint(LINE_COLOR); Rectangle2D b = ARROW.getBounds(); Point2D p = new Point2D.Double(b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d); AffineTransform toCenterAT = AffineTransform.getTranslateInstance(ICON_SIZE / 2d - p.getX(), ICON_SIZE / 2d - p.getY()); g2.fill(toCenterAT.createTransformedShape(ARROW)); g2.dispose(); }
/** [Advanced] */ public void dispose() { Graphics2D g = getG(); if (g != null) g.dispose(); }
private void process() { int width = Integer.parseInt(xres.getText()); int height = Integer.parseInt(yres.getText()); boolean preserveAspect = aspect.isSelected(); Color bg = new Color(red.getValue(), green.getValue(), blue.getValue()); String outDir = output.getText(); String suffix = ((String) format.getSelectedItem()).toLowerCase(); String preText = prepend.getText(); String appText = append.getText(); int scaleType = -1; String alg = (String) algorithm.getSelectedItem(); if (alg.equals("Smooth")) scaleType = Image.SCALE_SMOOTH; else if (alg.equals("Standard")) scaleType = Image.SCALE_DEFAULT; else if (alg.equals("Fast")) scaleType = Image.SCALE_FAST; else if (alg.equals("Replicate")) scaleType = Image.SCALE_REPLICATE; else if (alg.equals("Area averaging")) { scaleType = Image.SCALE_AREA_AVERAGING; } DefaultListModel model = (DefaultListModel) list.getModel(); int size = model.size(); progress.setValue(0); progress.setMaximum(4 * size); for (int i = 0; i < size; i++) { ThumbFile tf = (ThumbFile) model.elementAt(i); list.setSelectedValue(tf, true); String tail = " (" + (i + 1) + " of " + size + ")"; progress.setValue(4 * i); progress.setString("Reading" + tail); // construct input and output filenames String inFile = tf.getPath(); String outFile = outDir + SLASH + tf.getName(); int ndx = outFile.lastIndexOf(SLASH); String s1 = outFile.substring(0, ndx + SLASH.length()); String s2 = outFile.substring(ndx + SLASH.length()); int dot_ndx = s2.lastIndexOf("."); if (dot_ndx >= 0) s2 = s2.substring(0, dot_ndx); // make the thumbnail file name outFile = s1 + preText + s2 + appText + "." + suffix; // read in the file to an image BufferedImage image = null; try { image = ImageIO.read(new File(inFile)); } catch (IOException exc) { exc.printStackTrace(); } progress.setValue(4 * i + 1); progress.setString("Resizing" + tail); // resize image int w, h; if (preserveAspect) { int ow = image.getWidth(); int oh = image.getHeight(); double oasp = (double) ow / oh; double tasp = (double) width / height; if (oasp > tasp) { w = width; h = (int) (w / oasp); } else { h = height; w = (int) (oasp * h); } } else { w = width; h = height; } Image resized = image.getScaledInstance(w, h, scaleType); progress.setValue(4 * i + 2); progress.setString("Painting" + tail); // create thumbnail BufferedImage thumb = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = thumb.createGraphics(); g2d.setColor(bg); g2d.fillRect(0, 0, width, height); g2d.drawImage(resized, (width - w) / 2, (height - h) / 2, this); g2d.dispose(); progress.setValue(4 * i + 3); progress.setString("Writing" + tail); // save thumbnail to disk File out = new File(outFile); File parent = out.getParentFile(); if (parent != null && !parent.exists()) parent.mkdirs(); try { ImageIO.write(thumb, suffix, out); } catch (IOException exc) { exc.printStackTrace(); } } list.setSelectedIndices(new int[0]); progress.setValue(4 * size); progress.setString("Complete"); }
/** * Creates new frame image from current data (and previous frames as specified by their * disposition codes). */ protected void setPixels() { // expose destination image's pixels as int array int[] dest = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // fill in starting image contents based on last image's dispose code if (lastDispose > 0) { if (lastDispose == 3) { // use image before last int n = frameCount - 2; if (n > 0) { lastImage = getFrame(n - 1); } else { lastImage = null; } } if (lastImage != null) { int[] prev = ((DataBufferInt) lastImage.getRaster().getDataBuffer()).getData(); System.arraycopy(prev, 0, dest, 0, width * height); // copy pixels if (lastDispose == 2) { // fill last image rect area with background color Graphics2D g = image.createGraphics(); Color c = null; if (transparency) { c = new Color(0, 0, 0, 0); // assume background is transparent } else { c = new Color(lastBgColor); // use given background color } g.setColor(c); g.setComposite(AlphaComposite.Src); // replace area g.fill(lastRect); g.dispose(); } } } // copy each source line to the appropriate place in the destination int pass = 1; int inc = 8; int iline = 0; for (int i = 0; i < ih; i++) { int line = i; if (interlace) { if (iline >= ih) { pass++; switch (pass) { case 2: iline = 4; break; case 3: iline = 2; inc = 4; break; case 4: iline = 1; inc = 2; } } line = iline; iline += inc; } line += iy; if (line < height) { int k = line * width; int dx = k + ix; // start of line in dest int dlim = dx + iw; // end of dest line if ((k + width) < dlim) { dlim = k + width; // past dest edge } int sx = i * iw; // start of line in source while (dx < dlim) { // map color and insert in destination int index = ((int) pixels[sx++]) & 0xff; int c = act[index]; if (c != 0) { dest[dx] = c; } dx++; } } } }