private void drawWeaponTokenForRoom( Graphics g, Board board, Room room, double centreX, double topY, double step) { Weapon weapon = _weaponLocations.get(room); if (weapon != null) { Image weaponTokenImage = weapon.tokenImage(); double weaponTokenWidth = weaponTokenImage.getWidth(null); double weaponTokenHeight = weaponTokenImage.getHeight(null); double longestSideLength = step * WeaponTokenRatio; // scale so that the longest side is always larger if (weaponTokenWidth >= weaponTokenHeight) { weaponTokenWidth = longestSideLength; double ratio = weaponTokenImage.getWidth(null) / weaponTokenWidth; weaponTokenHeight = weaponTokenHeight / ratio; } else { weaponTokenHeight = longestSideLength; double ratio = weaponTokenImage.getHeight(null) / weaponTokenHeight; weaponTokenWidth = weaponTokenWidth / ratio; } g.drawImage( weaponTokenImage, (int) (centreX - weaponTokenWidth / 2), (int) (topY), (int) weaponTokenWidth, (int) weaponTokenHeight, null); } }
public static BufferedImage fetchFromURL(URL url) { Image i = new ImageIcon(url).getImage(); if (i.getWidth(null) <= 0 || i.getHeight(null) <= 0) { CrimsonLog.warning("BAD IMAGE! (%s)", url); return null; } BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB); bi.getGraphics().drawImage(i, 0, 0, null); return bi; }
public void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage( image, (this.getWidth() / 2) - (image.getWidth(this) / 2), (this.getHeight() / 2) - (image.getHeight(this) / 2), image.getWidth(this), image.getHeight(this), this); } }
private BufferedImage getAppImage() { assertIsDispatchThread(); try { if (myAppImage != null) return myAppImage; Object app = getApp(); Image appImage = (Image) getAppMethod("getDockIconImage").invoke(app); if (appImage == null) return null; int width = appImage.getWidth(null); int height = appImage.getHeight(null); BufferedImage img = UIUtil.createImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.drawImage(appImage, null, null); myAppImage = img; } catch (NoSuchMethodException e) { return null; } catch (Exception e) { LOG.error(e); } return myAppImage; }
private void scaleImage() { width = image.getWidth(this); height = image.getHeight(this); double ratio = 1.0; /* * Determine how to scale the image. Since the accessory can expand * vertically make sure we don't go larger than 150 when scaling * vertically. */ if (width >= height) { ratio = (double) (ACCSIZE - 5) / width; width = ACCSIZE - 5; height = (int) (height * ratio); } else { if (getHeight() > 150) { ratio = (double) (ACCSIZE - 5) / height; height = ACCSIZE - 5; width = (int) (width * ratio); } else { ratio = (double) getHeight() / height; height = getHeight(); width = (int) (width * ratio); } } image = image.getScaledInstance(width, height, Image.SCALE_DEFAULT); }
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(); }
// 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; }
/** * Create a splash screen (borderless graphic for display while other operations are taking * place). * * @param filename a class-relative path to the splash graphic * @param callingClass the class to which the graphic filename location is relative */ public SplashScreen(String filename, Class callingClass) { super(new Frame()); URL imageURL = callingClass.getResource(filename); image = Toolkit.getDefaultToolkit().createImage(imageURL); // Load the image MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForID(0); } catch (InterruptedException ie) { } // Center the window on the screen int imgWidth = image.getWidth(this); int imgHeight = image.getHeight(this); setSize(imgWidth, imgHeight); Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screenDim.width - imgWidth) / 2, (screenDim.height - imgHeight) / 2); setVisible(true); repaint(); // if on a single processor machine, wait for painting (see Fast Java Splash Screen.pdf) if (!EventQueue.isDispatchThread()) { synchronized (this) { while (!this.paintCalled) { try { this.wait(); } catch (InterruptedException e) { } } } } }
public void paint(Graphics gg) { int faceSize = Math.min(getWidth() - 4, getHeight() - 4); if (face == null) face = new PADFaceMapped( Math.max(2, (getWidth() - faceSize) / 2), Math.max(2, (getHeight() - faceSize) / 2), faceSize); if (buffer == null) { im = this.createImage(getWidth(), getHeight()); buffer = im.getGraphics(); } super.paint(buffer); buffer.setColor(new Color(255, 255, 255, 0)); buffer.fillRect(0, 0, im.getWidth(null), im.getHeight(null)); face.setDimensions( Math.max(2, (getWidth() - faceSize) / 2), Math.max(2, (getHeight() - faceSize) / 2), faceSize); face.paint(buffer); // draw buffer to screen gg.drawImage(im, 0, 0, null, null); }
/** * 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 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"); } }
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(); }
public void paint(Graphics g) { super.paint(g); if (myResizable && myDrawMacCorner) { g.drawImage( ourMacCorner, getX() + getWidth() - ourMacCorner.getWidth(this), getY() + getHeight() - ourMacCorner.getHeight(this), this); } }
public void update(Graphics g) { Graphics gr; if (offScreenBuffer == null || (!(offScreenBuffer.getWidth(this) == this.size().width && offScreenBuffer.getHeight(this) == this.size().height))) { offScreenBuffer = this.createImage(size().width, size().height); } gr = offScreenBuffer.getGraphics(); paint(gr); g.drawImage(offScreenBuffer, 0, 0, this); }
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); }
/** * 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(); }
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); }
/** * The image we're replacing. * * @return see desc */ public Image getImage() { if (!_loaded && _imageResource.isLoaded()) { Image image = ((AWTFSImage) _imageResource.getImage()).getImage(); if (_doScaleImage && (_targetWidth > 0 || _targetHeight > 0)) { int w = image.getWidth(null); int h = image.getHeight(null); int newW = _targetWidth; int newH = _targetHeight; if (newW == -1) { newW = (int) (w * ((double) newH / h)); } if (newH == -1) { newH = (int) (h * ((double) newW / w)); } if (w != newW || h != newH) { if (image instanceof BufferedImage) { image = ImageUtil.getScaledInstance((BufferedImage) image, newW, newH); } else { if (true) { throw new RuntimeException( "image is not a buffered image! " + _imageResource.getImageUri()); } String scalingType = Configuration.valueFor("xr.image.scale", "HIGH").trim(); if (scalingType.equalsIgnoreCase("HIGH") || scalingType.equalsIgnoreCase("MID")) { image = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); } else { image = image.getScaledInstance(newW, newH, Image.SCALE_FAST); } } } _image = image; } else { _image = image; } _loaded = true; XRLog.load( Level.FINE, "Icon: replaced image " + _imageResource.getImageUri() + ", repaint requested"); SwingUtilities.invokeLater( new Runnable() { public void run() { repaintListener.repaintRequested(_doScaleImage); } }); } return _image; }
private void displayImage(Image image) { if (DEBUG.RESOURCE || DEBUG.IMAGE) out("displayImage " + Util.tag(image)); mImage = image; if (mImage != null) { mImageWidth = mImage.getWidth(null); mImageHeight = mImage.getHeight(null); if (DEBUG.IMAGE) out("displayImage " + mImageWidth + "x" + mImageHeight); } clearStatus(); repaint(); }
/** * Paints this button. * * @param g the <tt>Graphics</tt> object used for painting */ private void internalPaintComponent(Graphics2D g) { AntialiasingManager.activateAntialiasing(g); // Paint a roll over fade out. FadeTracker fadeTracker = FadeTracker.getInstance(); float visibility = this.getModel().isRollover() ? 1.0f : 0.0f; if (fadeTracker.isTracked(this, FadeKind.ROLLOVER)) { visibility = fadeTracker.getFade(this, FadeKind.ROLLOVER); } visibility /= 2; if (visibility != 0.0f) { g.setColor(new Color(borderColor[0], borderColor[1], borderColor[2], visibility)); if (bgImage != null) g.fillRoundRect( (this.getWidth() - bgImage.getWidth(null)) / 2, (this.getHeight() - bgImage.getHeight(null)) / 2, bgImage.getWidth(null) - 1, bgImage.getHeight(null) - 1, 20, 20); else g.fillRoundRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, 20, 20); } if (bgImage != null) { g.drawImage( bgImage, (this.getWidth() - bgImage.getWidth(null)) / 2, (this.getHeight() - bgImage.getHeight(null)) / 2, null); } else { g.setColor(getBackground()); g.fillRoundRect(1, 1, this.getWidth() - 2, this.getHeight() - 2, 20, 20); } }
public Dimension getPreferredSize() { int w, h; Dimension result; if (image == null) { result = new Dimension(0, 0); } else { w = image.getWidth(null); h = image.getHeight(null); result = new Dimension(w > 0 ? w : 0, h > 0 ? h : 0); } return result; }
@Nullable private static Icon checkIcon(final Image image, @NotNull URL url) { if (image == null || image.getHeight(LabelHolder.ourFakeComponent) < 1) { // image wasn't loaded or broken return null; } final Icon icon = getIcon(image); if (icon != null && !isGoodSize(icon)) { LOG.error("Invalid icon: " + url); // # 22481 return EMPTY_ICON; } return icon; }
public void drawRocket( Image image, int xpos, int ypos, double rot, double xscale, double yscale) { int width = rocketImage.getWidth(this); int height = rocketImage.getHeight(this); g.translate(xpos, ypos); g.rotate(rot * Math.PI / 180.0); g.scale(xscale, yscale); g.drawImage(image, -width / 2, -height / 2, width, height, this); g.scale(1.0 / xscale, 1.0 / yscale); g.rotate(-rot * Math.PI / 180.0); g.translate(-xpos, -ypos); }
/** * Gets a buffered image. * * @return the image */ public BufferedImage getBufferedImage() { if (image == null && isAnImage) { Image im = getImage(); if (im == null) { isAnImage = false; } else { image = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.drawImage(im, 0, 0, null); } } return image; }
@NotNull private static Image scaleImage(Image image, float scale) { int w = image.getWidth(null); int h = image.getHeight(null); if (w <= 0 || h <= 0) { return image; } int width = (int) (scale * w); int height = (int) (scale * h); // Using "QUALITY" instead of "ULTRA_QUALITY" results in images that are less blurry // because ultra quality performs a few more passes when scaling, which introduces blurriness // when the scaling factor is relatively small (i.e. <= 3.0f) -- which is the case here. return Scalr.resize(ImageUtil.toBufferedImage(image), Scalr.Method.QUALITY, width, height); }
/** 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(); }
public void init() { this.setPreferredSize(new Dimension(640, 480)); pane = new JPanel(); pane.setLayout(null); pane.setBackground(Color.CYAN); img = getImage(getCodeBase(), "zil_walk_front.gif"); zil = new JLabel(new ImageIcon(img)); zil.setSize(img.getWidth(this), img.getHeight(this)); addKeyListener(this); pane.add(zil, 0, 0); add(pane, BorderLayout.CENTER); setFocusable(true); }
public Defender(int x, int y, int type, Component parentFrame) { super(x, y, 0, 0); this.centre = centre; this.size = size; image = new ImageIcon(IMAGE_NAMES[type]).getImage(); // no health points for flask, ruler, highlighter, pen, gluestick // more health points for eraser // others have the same health point health = 150; strengthPoints = 25; // Set the size of the enemy based on the image size setSize(image.getWidth(parentFrame), image.getHeight(parentFrame)); }
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; AffineTransform origXform = g2d.getTransform(); AffineTransform newXform = (AffineTransform)(origXform.clone()); //center of rotation is center of the panel int xRot = this.getWidth()/2; int yRot = this.getHeight()/2; newXform.rotate(Math.toRadians(currentAngle), xRot, yRot); g2d.setTransform(newXform); //draw image centered in panel int x = (getWidth() - image.getWidth(this))/2; int y = (getHeight() - image.getHeight(this))/2; g2d.drawImage(image, x, y, this); g2d.setTransform(origXform); }