// initialize default image private BufferedImage getDefaultImage(int w, int h) { BufferedImage defaultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = defaultImage.createGraphics(); graphics2D.setColor(new Color(200, 200, 200)); graphics2D.fillRect(0, 0, w, h); graphics2D.setColor(new Color(130, 130, 130)); graphics2D.drawRect(0, 0, w - 1, h - 1); return defaultImage; }
public void generarFondo(Component componente) { boolean dibujarFondo = false; Rectangle med = this.getBounds(); Rectangle areaDibujo = this.getBounds(); BufferedImage tmp; GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); if (Principal.fondoBlur) { dibujarFondo = true; } if (dibujarFondo) { JRootPane root = SwingUtilities.getRootPane(this); blurBuffer = GraphicsUtilities.createCompatibleImage(Principal.sysAncho, Principal.sysAlto); Graphics2D g2 = blurBuffer.createGraphics(); g2.setClip(med); blurBuffer = blurBuffer.getSubimage(med.x, med.y, med.width, med.height); ((Escritorio) Principal.getEscritorio()).getFrameEscritorio().paint(g2); g2.dispose(); backBuffer = blurBuffer; // blurBuffer = toGrayScale(blurBuffer); blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, getWidth() / 2); blurBuffer = new GaussianBlurFilter(4).filter(blurBuffer, null); g2 = (Graphics2D) blurBuffer.getGraphics(); g2.setColor(new Color(0, 0, 0, 195)); g2.fillRect(0, 0, Principal.sysAncho, Principal.sysAlto); listo = true; } }
public static void saveJPG(Image img, String s) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); FileOutputStream out = null; try { out = new FileOutputStream(s); } catch (java.io.FileNotFoundException io) { System.out.println("File Not Found"); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(0.5f, false); encoder.setJPEGEncodeParam(param); try { encoder.encode(bi); out.close(); } catch (java.io.IOException io) { System.out.println("IOException"); } }
// buffered images are just better. protected static BufferedImage imageToBufferedImage(Image img) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); return bi; }
public void scaleImage( Image image, int p_width, int p_height, boolean keepaspect) /*throws Exception*/ { int thumbWidth = p_width; int thumbHeight = p_height; // Make sure the aspect ratio is maintained, so the image is not skewed if (keepaspect) { double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } } // Draw the scaled image BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); this.img = thumbImage; }
/** * Appends a frame to the current video. * * @param image the image to append * @return true if image successfully appended */ protected boolean append(Image image) { BufferedImage bi; if (image instanceof BufferedImage) { bi = (BufferedImage) image; } else { bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); g.drawImage(image, 0, 0, null); } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { if (!editing) { videoMedia.beginEdits(); editing = true; } ImageIO.write(bi, "png", out); // $NON-NLS-1$ QTHandle handle = new QTHandle(out.toByteArray()); DataRef dataRef = new DataRef(handle, kDataRefFileExtensionTag, "png"); // $NON-NLS-1$ GraphicsImporter importer = new GraphicsImporter(dataRef); ImageDescription description = importer.getImageDescription(); int duration = (int) (frameDuration * 0.6); videoMedia.addSample( handle, 0, // data offset handle.getSize(), duration, description, 1, // number of samples 0); // key frame?? } catch (Exception ex) { ex.printStackTrace(); return false; } return true; }
/** paint the canvas into a image file of given width and height */ public void writeToImage(String s, int w, int h) { String ext; File f; try { ext = s.substring(s.lastIndexOf(".") + 1); f = new File(s); } catch (Exception e) { System.out.println(e); return; } if (!ext.equals("jpg") && !ext.equals("png")) { System.out.println("Cannot write to file: Illegal extension " + ext); return; } boolean opq = true; if (theOpaque != null) opq = theOpaque; BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setBackground(Color.white); g2.setPaint(Color.black); g2.setStroke(new BasicStroke(1)); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); doBuffer(g2, true, new Rectangle(0, 0, w, h)); try { ImageIO.write(image, ext, f); } catch (Exception e) { System.out.println(e); } }
private static BufferedImage makeBufferedImage(Icon icon, int w, int h) { BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); icon.paintIcon(null, g, (w - icon.getIconWidth()) / 2, (h - icon.getIconWidth()) / 2); g.dispose(); return image; }
/** * 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; }
/* applet init event. Should initialize the applet. */ public void init() { backbuffer = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB); g2d = backbuffer.createGraphics(); iconSheet = getImage(this.getClass().getResource("data/icons2.png")); wateringCanImage = getImageFromIconSheet(0, 0); wateringSplashImage = getImageFromIconSheet(1, 0); flowerImage = getImageFromIconSheet(2, 0); seedImage = getImageFromIconSheet(3, 0); skullImage = getImageFromIconSheet(4, 0); tapImage = getImageFromIconSheet(5, 0); playerImage = getImageFromIconSheet(0, 1); flowerStalkImage = getImageFromIconSheet(2, 1); clockImage = getImageFromIconSheet(4, 1); tapSplashImage = getImageFromIconSheet(5, 1); dirtImage = getImageFromIconSheet(0, 2); brickImage = getImageFromIconSheet(0, 3); player.image = playerImage; pickupAudio = getAudioClip(this.getClass().getResource("data/Pickup.wav")); for (int i = 0; i < 14; i++) { flowerList[i] = new Flower(); flowerList[i].height = rand.nextInt(14); } addKeyListener(this); }
public void zoomIn() { Dimension asz = this.getSize(); int maxzf = 3; int coef = 1; int r; cmdline = "/bin/sh get.sh " + j2kfilename + " " + iw + " " + ih + " " + rect.x + " " + rect.y + " " + rect.width + " " + rect.height; Exec.execPrint(cmdline); rect.x = rect.y = rect.width = rect.height = 0; img = pgm.open("out.pgm"); iw = img.getWidth(this); ih = img.getHeight(this); bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); big = bi.createGraphics(); selected = 0; fullRefresh = true; repaint(); }
private <T> T scaleImageUsingAffineTransformation(final BufferedImage bufferedImage, T target) { BufferedImage destinationImage = generateDestinationImage(); Graphics2D graphics2D = destinationImage.createGraphics(); AffineTransform transformation = AffineTransform.getScaleInstance( ((double) getQualifiedWidth() / bufferedImage.getWidth()), ((double) getQualifiedHeight() / bufferedImage.getHeight())); graphics2D.drawRenderedImage(bufferedImage, transformation); graphics2D.addRenderingHints(retrieveRenderingHints()); try { if (target instanceof File) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "File")); ImageIO.write(destinationImage, imageType.toString(), (File) target); } else if (target instanceof ImageOutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "ImageOutputStream")); ImageIO.write(destinationImage, imageType.toString(), (ImageOutputStream) target); } else if (target instanceof OutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "OutputStream")); ImageIO.write(destinationImage, imageType.toString(), (OutputStream) target); } else { target = null; } } catch (IOException e) { e.printStackTrace(); } return target; }
public BufferedImage createCrystalCase(Image cover) { BufferedImage crystal = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = crystal.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); int width = cover.getWidth(null); int height = cover.getHeight(null); float scale; if (width > height) { scale = (float) IMAGE_WIDTH / (float) width; } else { scale = (float) IMAGE_HEIGHT / (float) height; } int scaledWidth = (int) ((float) width * scale); int scaledHeight = (int) ((float) height * scale); int x = (IMAGE_WIDTH - scaledWidth) / 2; int y = (IMAGE_HEIGHT - scaledHeight) / 2; g2.drawImage(cover, x, y, scaledWidth, scaledHeight, null); g2.dispose(); return crystal; }
// This is used to create a CImage from a Image public CImage createFromImage(final Image image) { if (image == null) return null; MediaTracker mt = new MediaTracker(new Label()); final int id = 0; mt.addImage(image, id); try { mt.waitForID(id); } catch (InterruptedException e) { } if (mt.isErrorID(id)) { return null; } int w = image.getWidth(null); int h = image.getHeight(null); BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2 = bimg.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.drawImage(image, 0, 0, null); g2.dispose(); int[] buffer = ((DataBufferInt) bimg.getRaster().getDataBuffer()).getData(); return new CImage(nativeCreateNSImageFromArray(buffer, w, h)); }
public BufferedImage filter(BufferedImage src, BufferedImage dst) { if (dst == null) dst = createCompatibleDestImage(src, null); int width = src.getWidth(); int height = src.getHeight(); int numScratches = (int) (density * width * height / 100); ArrayList<Line2D> lines = new ArrayList<Line2D>(); { float l = length * width; Random random = new Random(seed); Graphics2D g = dst.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(new Color(color)); g.setStroke(new BasicStroke(this.width)); for (int i = 0; i < numScratches; i++) { float x = width * random.nextFloat(); float y = height * random.nextFloat(); float a = angle + ImageMath.TWO_PI * (angleVariation * (random.nextFloat() - 0.5f)); float s = (float) Math.sin(a) * l; float c = (float) Math.cos(a) * l; float x1 = x - c; float y1 = y - s; float x2 = x + c; float y2 = y + s; g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); lines.add(new Line2D.Float(x1, y1, x2, y2)); } g.dispose(); } if (false) { // int[] inPixels = getRGB( src, 0, 0, width, height, null ); int[] inPixels = new int[width * height]; int index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float sx = x, sy = y; for (int i = 0; i < numScratches; i++) { Line2D.Float l = (Line2D.Float) lines.get(i); float dot = (l.x2 - l.x1) * (sx - l.x1) + (l.y2 - l.y1) * (sy - l.y1); if (dot > 0) inPixels[index] |= (1 << i); } index++; } } Colormap colormap = new LinearColormap(); index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float f = (float) (inPixels[index] & 0x7fffffff) / 0x7fffffff; inPixels[index] = colormap.getColor(f); index++; } } setRGB(dst, 0, 0, width, height, inPixels); } return dst; }
private void applyAlphaMask( BufferedImage buffer, BufferedImage alphaMask, int itemWidth, int itemHeight) { Graphics2D g2 = buffer.createGraphics(); g2.setComposite(AlphaComposite.DstOut); g2.drawImage(alphaMask, null, 0, itemHeight); g2.dispose(); }
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; }
/** * Scale the given BufferedImage to the given width and height. Return the new scaled * BufferedImage. */ public static BufferedImage scale(BufferedImage bsrc, int width, int height) { AffineTransform at = AffineTransform.getScaleInstance( (double) width / bsrc.getWidth(), (double) height / bsrc.getHeight()); BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bdest.createGraphics(); g.drawRenderedImage(bsrc, at); return bdest; }
public void plot(Color c, int x0, int y0, int z0) { Graphics2D g = bi.createGraphics(); g.setColor(c); if (x0 >= XRES || y0 >= YRES || x0 < 0 || y0 < 0) return; if (z0 > zbuffer[x0][y0]) { g.drawLine(x0, y0, x0, y0); zbuffer[x0][y0] = z0; } }
@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; }
private static ImageIcon makeGrayImageIcon1(Image img) { BufferedImage source = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = source.createGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); BufferedImage destination = colorConvert.filter(source, null); return new ImageIcon(destination); }
private static BufferedImage ensureBufferCapacity( final int width, final int height, BufferedImage img) { if (img == null || img.getWidth() < width || img.getHeight() < height) { img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); } else { final Graphics2D g2 = img.createGraphics(); g2.setComposite(AlphaComposite.Clear); g2.fillRect(0, 0, width, height); g2.dispose(); } return img; }
private static ImageIcon makeGrayImageIcon2(Image img) { int w = img.getWidth(null); int h = img.getHeight(null); BufferedImage destination = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); Graphics g = destination.createGraphics(); //// g.setColor(Color.WHITE); // https://community.oracle.com/thread/1373262 Color to Grayscale to Binary // g.fillRect(0, 0, w, h); // need to pre-fill(alpha?) g.drawImage(img, 0, 0, null); g.dispose(); return new ImageIcon(destination); }
// init private static void init() { if (frame != null) frame.setVisible(false); frame = new JFrame(); offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); offscreen = offscreenImage.createGraphics(); onscreen = onscreenImage.createGraphics(); setXscale(); setYscale(); offscreen.setColor(DEFAULT_CLEAR_COLOR); offscreen.fillRect(0, 0, width, height); setPenColor(); setPenRadius(); setFont(); clear(); // add antialiasing RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); offscreen.addRenderingHints(hints); // frame stuff ImageIcon icon = new ImageIcon(onscreenImage); JLabel draw = new JLabel(icon); draw.addMouseListener(std); draw.addMouseMotionListener(std); frame.setContentPane(draw); frame.addKeyListener(std); // JLabel cannot get keyboard focus frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // closes only current window frame.setTitle("Standard Draw"); frame.setJMenuBar(createMenuBar()); frame.pack(); frame.requestFocusInWindow(); frame.setVisible(true); }
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; }
/** Extracts image pixels into byte array "pixels" */ protected void getImagePixels() { int w = image.getWidth(); int h = image.getHeight(); int type = image.getType(); if ((w != width) || (h != height) || (type != BufferedImage.TYPE_3BYTE_BGR)) { // create new image with right size/format BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = temp.createGraphics(); g.drawImage(image, 0, 0, null); image = temp; } pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); }
private static ImageIcon scale(ImageIcon src) { // System.out.println(scaleFactor); if (scaleFactor > 1) { int w = (int) (scaleFactor * src.getIconWidth()); int h = (int) (scaleFactor * src.getIconHeight()); int type = BufferedImage.TYPE_INT_ARGB; BufferedImage dst = new BufferedImage(w, h, type); Graphics2D g2 = dst.createGraphics(); g2.drawImage(src.getImage(), 0, 0, w, h, null); g2.dispose(); return new ImageIcon(dst); } else return src; }
public final BufferedImage filter(BufferedImage src, BufferedImage dst) { if (src == dst) { // awt.252=Source can't be same as the destination throw new IllegalArgumentException(Messages.getString("awt.252")); // $NON-NLS-1$ } ColorModel srcCM = src.getColorModel(); BufferedImage finalDst = null; if (srcCM instanceof IndexColorModel && (iType != TYPE_NEAREST_NEIGHBOR || srcCM.getPixelSize() % 8 != 0)) { src = ((IndexColorModel) srcCM).convertToIntDiscrete(src.getRaster(), true); srcCM = src.getColorModel(); } if (dst == null) { dst = createCompatibleDestImage(src, srcCM); } else { if (!srcCM.equals(dst.getColorModel())) { // Treat BufferedImage.TYPE_INT_RGB and // BufferedImage.TYPE_INT_ARGB as same if (!((src.getType() == BufferedImage.TYPE_INT_RGB || src.getType() == BufferedImage.TYPE_INT_ARGB) && (dst.getType() == BufferedImage.TYPE_INT_RGB || dst.getType() == BufferedImage.TYPE_INT_ARGB))) { finalDst = dst; dst = createCompatibleDestImage(src, srcCM); } } } // Skip alpha channel for TYPE_INT_RGB images if (slowFilter(src.getRaster(), dst.getRaster()) != 0) { // awt.21F=Unable to transform source throw new ImagingOpException(Messages.getString("awt.21F")); // $NON-NLS-1$ // TODO - uncomment // if (ippFilter(src.getRaster(), dst.getRaster(), src.getType()) != // 0) // throw new ImagingOpException ("Unable to transform source"); } if (finalDst != null) { Graphics2D g = finalDst.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(dst, 0, 0, null); } else { finalDst = dst; } return finalDst; }