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(); }
/** * Construct a GIFEncoder. The constructor will convert the image to an indexed color array. * <B>This may take some time.</B> * * <p> * * @param image The image to encode. The image <B>must</B> be completely loaded. * @exception AWTException Will be thrown if the pixel grab fails. This can happen if Java runs * out of memory. It may also indicate that the image contains more than 256 colors. */ public GIFEncoder(Image image) throws AWTException { width_ = (short) image.getWidth(null); height_ = (short) image.getHeight(null); int values[] = new int[width_ * height_]; PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_); try { if (grabber.grabPixels() != true) throw new AWTException("Grabber returned false: " + grabber.status()); } catch (InterruptedException e) {; } byte r[][] = new byte[width_][height_]; byte g[][] = new byte[width_][height_]; byte b[][] = new byte[width_][height_]; int index = 0; for (int y = 0; y < height_; ++y) for (int x = 0; x < width_; ++x) { r[x][y] = (byte) ((values[index] >> 16) & 0xFF); g[x][y] = (byte) ((values[index] >> 8) & 0xFF); b[x][y] = (byte) ((values[index]) & 0xFF); ++index; } ToIndexedColor(r, g, b); }
// 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 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; }
/** * 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; }
// /////////////////////////////////////////////////////////////// // 最临近插值算法----不考虑坐标的小数部分 // 参数:img:要缩放的Image对象 // dstW:目标图像宽 // dstH:目标图像高 // comp:组件参数,比如Applet // /////////////////////////////////////////////////////////////// public static Image simpleScale(Image img, int dstW, int dstH) { OperateImage OI = new OperateImage(); int[] scaled, src; double widthFactor, heightFactor; int srcX = 0, srcY = 0, srcW, srcH; src = OI.takeImg(img, img.getWidth(null), img.getHeight(null)); scaled = new int[dstW * dstH]; // 存放缩放后的图片 srcW = img.getWidth(null); srcH = img.getHeight(null); widthFactor = srcW / (dstW + 0.0); // System.out.println("widthFactor:"+widthFactor); heightFactor = srcH / (dstH + 0.0); // System.out.println("heightFactor:"+heightFactor); for (int a = 0; a < dstH; a++) for (int b = 0; b < dstW; b++) { if ((b * widthFactor) % 1 >= 0.5) srcX = (int) (b * widthFactor) + 1; else srcX = (int) (b * widthFactor); if ((a * heightFactor) % 1 >= 0.5) srcY = (int) (a * heightFactor) + 1; else srcY = (int) (a * heightFactor); if (srcX > srcW - 1) srcX = srcW - 1; if (srcY > srcH - 1) srcY = srcH - 1; scaled[a * dstW + b] = src[srcY * srcW + srcX]; } // System.out.println("最临近插值算法完成!"); return OI.madeImg(scaled, dstW, dstH); }
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; }
public static BufferedImage toBufferedImage(Image image) { int width = image.getWidth(null), height = image.getHeight(null); if (width <= 0) width = 1; if (height <= 0) height = 1; BufferedImage ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); ret.getGraphics().drawImage(image, 0, 0, null); return ret; }
// set size of the button (inherited from JButton =) ) // changes the size of the button, and resizes the image to fit it. public void setSize(int x, int y) { if (!mode.equals("String")) { width = x; height = x; image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); BWimage = BWimage.getScaledInstance(width, height, Image.SCALE_SMOOTH); } }
private void scaleImage() { Image img = back.getScaledInstance(scx(back.getWidth()), scy(back.getHeight()), Image.SCALE_SMOOTH); back = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = back.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); }
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); }
/** * Creates a new JpegInfo object. * * @param image DOCUMENT ME! */ public JpegInfo(Image image) { Components = new Object[NumberOfComponents]; compWidth = new int[NumberOfComponents]; compHeight = new int[NumberOfComponents]; BlockWidth = new int[NumberOfComponents]; BlockHeight = new int[NumberOfComponents]; imageobj = image; imageWidth = image.getWidth(null); imageHeight = image.getHeight(null); Comment = "JPEG Encoder Copyright 1998, James R. Weeks and BioElectroMech. "; getYCCArray(); }
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); }
/** * Draw picture (gif, jpg, or png) centered on (x, y). * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @throws RuntimeException if the image is corrupt */ public static void picture(double x, double y, String s) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); int ws = image.getWidth(null); int hs = image.getHeight(null); if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt"); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null); draw(); }
public FrontBack(SJGame sjg) { super(sjg); frontImage = sjg.createImage(sjg.getWidth(), sjg.getHeight()); backImage = sjg.createImage(sjg.getWidth(), sjg.getHeight()); setFront(frontImage.getGraphics()); setBack(backImage.getGraphics()); getFront().setColor(Color.white); getFront().fillRect(0, 0, sjg.getWidth(), sjg.getHeight()); getFront().setColor(Color.black); getBack().setColor(Color.white); getBack().fillRect(0, 0, sjg.getWidth(), sjg.getHeight()); getBack().setColor(Color.black); }
/** Scales an image. To be used for tiles. Probably not the most efficient scaling method. */ public static Image scaleImage(Image orig, int scale) { Image result; if (scale != 1) { int width = orig.getWidth(null); int height = orig.getHeight(null); // Scale cropped image to proper size result = new BufferedImage(width * scale, height * scale, BufferedImage.TYPE_INT_ARGB); Graphics g = ((BufferedImage) result).createGraphics(); g.drawImage(orig, 0, 0, width * scale, height * scale, 0, 0, width - 1, height - 1, null); g.dispose(); } else { return orig; } return result; }
/** * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @param degrees is the number of degrees to rotate counterclockwise * @throws IllegalArgumentException if the image is corrupt */ public static void picture(double x, double y, String s, double degrees) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); int ws = image.getWidth(null); int hs = image.getHeight(null); if (ws < 0 || hs < 0) throw new IllegalArgumentException("image " + s + " is corrupt"); offscreen.rotate(Math.toRadians(-degrees), xs, ys); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null); offscreen.rotate(Math.toRadians(+degrees), xs, ys); draw(); }
// This method returns a buffered image with the contents of an image // Source: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); // Determine if the image has transparent pixels; for this method's // implementation, see Determining If an Image Has Transparent Pixels boolean hasAlpha = hasAlpha(image); // Create a buffered image with a format that's compatible with the screen BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { // Determine the type of transparency of the new buffered image int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; } // Create the buffered image GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // The system does not have a screen } if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
private void gameRender() { if (dbImage == null) { dbImage = createImage(PWIDTH, PHEIGHT); if (dbImage == null) { System.out.println("dbImage is null"); return; } else dbg = dbImage.getGraphics(); } // draw a white background dbg.setColor(Color.white); dbg.fillRect(0, 0, PWIDTH, PHEIGHT); // draw the game elements: order is important ribsMan.display(dbg); // the background ribbons bricksMan.display(dbg); // the bricks jack.drawSprite(dbg); // the sprites fireball.drawSprite(dbg); if (showExplosion) // draw the explosion (in front of jack) dbg.drawImage(explosionPlayer.getCurrentImage(), xExpl, yExpl, null); reportStats(dbg); if (gameOver) gameOverMessage(dbg); if (showHelp) // draw the help at the very front (if switched on) dbg.drawImage( helpIm, (PWIDTH - helpIm.getWidth()) / 2, (PHEIGHT - helpIm.getHeight()) / 2, null); } // end of gameRender()
/** Overrides <code>Graphics.drawImage</code>. */ public boolean drawImage(Image img, int x, int y, ImageObserver observer) { DebugGraphicsInfo info = info(); if (debugLog()) { info.log(toShortString() + " Drawing image: " + img + " at: " + new Point(x, y)); } if (isDrawingBuffer()) { if (debugBuffered()) { Graphics debugGraphics = debugGraphics(); debugGraphics.drawImage(img, x, y, observer); debugGraphics.dispose(); } } else if (debugFlash()) { int i, count = (info.flashCount * 2) - 1; ImageProducer oldProducer = img.getSource(); ImageProducer newProducer = new FilteredImageSource(oldProducer, new DebugGraphicsFilter(info.flashColor)); Image newImage = Toolkit.getDefaultToolkit().createImage(newProducer); DebugGraphicsObserver imageObserver = new DebugGraphicsObserver(); Image imageToDraw; for (i = 0; i < count; i++) { imageToDraw = (i % 2) == 0 ? newImage : img; loadImage(imageToDraw); graphics.drawImage(imageToDraw, x, y, imageObserver); Toolkit.getDefaultToolkit().sync(); sleep(info.flashTime); } } return graphics.drawImage(img, x, y, observer); }
/** * Return the Image pixels in default Java int ARGB format. * * @return */ public static int[] getImagePixels(Image image) { int[] pixelsARGB = null; if (image != null) { int imgw = image.getWidth(null); int imgh = image.getHeight(null); pixelsARGB = new int[imgw * imgh]; PixelGrabber pg = new PixelGrabber(image, 0, 0, imgw, imgh, pixelsARGB, 0, imgw); try { pg.grabPixels(); } catch (Exception e) { GLApp.err("Pixel Grabbing interrupted!"); return null; } } return pixelsARGB; }
private BufferedImage readImageFile(File imageFile) { ImageIcon icon = null; try { icon = new ImageIcon(imageFile.toURI().toURL()); } catch (Exception e) { Cheshire.exit("Could not read image file %s", imageFile.toString()); } Image image = icon.getImage(); BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = buffImage.getGraphics(); g.drawImage(image, 0, 0, null); return buffImage; }
public void init() { String str; int port; imgId = 4; if (isApplet && (((hostname = this.getParameter("hostname")) == null) || hostname.equals(""))) hostname = "localhost"; if (!isApplet || ((str = this.getParameter("cmdPort")) == null)) { port = 3000; } else { port = new Integer(str).intValue(); } this.setSize(512, 512); Dimension asz = this.getSize(); zl.x2 = asz.width; zl.y2 = asz.height; cmdline = "/bin/sh get.sh " + j2kfilename + " " + asz.width + " " + asz.height + " " + zl.x1 + " " + zl.y1 + " " + zl.x2 + " " + zl.y2; Exec.execPrint(cmdline); img = pgm.open("out.pgm"); iw = img.getWidth(this); ih = img.getHeight(this); setBackground(Color.black); bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); big = bi.createGraphics(); myMML = new MML(this); addMouseListener(myMML); addMouseMotionListener(myMML); }
/** * Uses a ImageFilter to return a cropped version of the image. Hopefully useful when handling * tilesets. */ public static Image getTile(Image tileset, int x1, int x2, int y1, int y2, int scale) { JPanel producer = new JPanel(); // Crop tileset to grab tile ImageFilter cropper = new CropImageFilter(x1, y1, x2 - x1, y2 - y1); Image cropped = producer.createImage(new FilteredImageSource(tileset.getSource(), cropper)); return scaleImage(cropped, scale); }
/** Open a file and load the image. */ public void openFile() { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); String[] extensions = ImageIO.getReaderFileSuffixes(); chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions)); int r = chooser.showOpenDialog(this); if (r != JFileChooser.APPROVE_OPTION) return; try { Image img = ImageIO.read(chooser.getSelectedFile()); image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(img, 0, 0, null); } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } repaint(); }
public ImageOps() { setBackground(Color.white); for (int i = 0; i < imgName.length; i++) { Image image = getImage(imgName[i]); int iw = image.getWidth(this); int ih = image.getHeight(this); img[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB); img[i].createGraphics().drawImage(image, 0, 0, null); } slider1 = new JSlider(JSlider.VERTICAL, 0, 255, low); slider1.setPreferredSize(new Dimension(15, 100)); slider1.addChangeListener(this); slider2 = new JSlider(JSlider.VERTICAL, 0, 255, high); slider2.setPreferredSize(new Dimension(15, 100)); slider2.addChangeListener(this); setControls(new Component[] {new DemoControls(this), slider1, slider2}); setConstraints(new String[] {BorderLayout.NORTH, BorderLayout.WEST, BorderLayout.EAST}); }
/** Overrides <code>Graphics.drawImage</code>. */ public boolean drawImage( Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) { DebugGraphicsInfo info = info(); if (debugLog()) { info.log( toShortString() + " Drawing image: " + img + " destination: " + new Rectangle(dx1, dy1, dx2, dy2) + " source: " + new Rectangle(sx1, sy1, sx2, sy2) + ", bgcolor: " + bgcolor); } if (isDrawingBuffer()) { if (debugBuffered()) { Graphics debugGraphics = debugGraphics(); debugGraphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer); debugGraphics.dispose(); } } else if (debugFlash()) { int i, count = (info.flashCount * 2) - 1; ImageProducer oldProducer = img.getSource(); ImageProducer newProducer = new FilteredImageSource(oldProducer, new DebugGraphicsFilter(info.flashColor)); Image newImage = Toolkit.getDefaultToolkit().createImage(newProducer); DebugGraphicsObserver imageObserver = new DebugGraphicsObserver(); Image imageToDraw; for (i = 0; i < count; i++) { imageToDraw = (i % 2) == 0 ? newImage : img; loadImage(imageToDraw); graphics.drawImage( imageToDraw, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, imageObserver); Toolkit.getDefaultToolkit().sync(); sleep(info.flashTime); } } return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer); }
/** * 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; }
/** * Returns a smoothly scaled image using getScaledInstance. This method has interesting behaviour. * The scaled image retains its type (indexed/rgb and bitmask/translucent), and the algorithm * tries to scale smoothly within these constraints. For indexed, interpolated pixels are rounded * to the existing indexed colours. For bitmask, the behaviour depends on the platform. On * WinXP/J1.2 I found that the colour _behind_ each transparent pixel is used to interpolate * between nontransparent and transparent pixels. On BSD/J1.4 I found that the colours of * transparent pixels are never used, and only the nontransparent pixels are used when * interpolating a region with mixed transparent/nontransparent pixels. */ public JGImage scale(int width, int height) { // BufferedImage dstimg = createCompatibleImage(width,height); // BufferedImage srcimg = toBuffered(img); Image scaledimg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); try { /* this is necessary for scaled images too */ ensureLoaded(scaledimg); } catch (Exception e) { System.err.println("Error scaling image."); } return new JREImage(scaledimg); }