List of usage examples for java.awt Graphics2D drawImage
public abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer);
From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java
/** * Scales an image to the desired dimensions. * * @param img Original image//from w w w .ja v a 2 s .c o m * @param newW Target width * @param newH Target height * @return Scaled image */ public static BufferedImage scale(BufferedImage img, int newW, int newH) { int w = img.getWidth(); int h = img.getHeight(); final BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); final Graphics2D g = dimg.createGraphics(); // use provided rendering hint, default is bilinear switch (scalingHint) { case "n": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); break; case "b": g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); break; } g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); g.dispose(); return dimg; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled/*ww w .java 2 s . c o m*/ * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static BufferedImage getBufferedImage(final ImageIcon icon) { Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); int w = icon.getIconWidth(); int h = icon.getIconHeight(); BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null); graphics2D.dispose(); return bufferedImage; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled/*from w w w.j a v a 2s. c o m*/ * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio) { if (icon != null) { int dstWidth = newMaxWidth; int dstHeight = newMaxHeight; int srcWidth = icon.getIconWidth(); int srcHeight = icon.getIconHeight(); if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size dstWidth = icon.getIconWidth(); dstHeight = icon.getIconHeight(); } if (maintainRatio) { double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = Math.max(dstWidth, dstHeight); double multiplier = longSideForDest / longSideForSource; dstWidth = (int) (srcWidth * multiplier); dstHeight = (int) (srcHeight * multiplier); } Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null); graphics2D.dispose(); return thumbImage; } return null; }
From source file:Main.java
public void createThumbnail(File file) throws Exception { BufferedImage img = ImageIO.read(file); BufferedImage thumb = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) thumb.getGraphics(); g2d.drawImage(img, 0, 0, thumb.getWidth() - 1, thumb.getHeight() - 1, 0, 0, img.getWidth() - 1, img.getHeight() - 1, null);/*from w ww. j a v a 2 s. c om*/ g2d.dispose(); ImageIO.write(thumb, "PNG", new File("thumb.png")); }
From source file:bachelorthesis.ocr.domain.DomainFacade.java
private Captcha createCaptcha(String buildString, int width, int height) throws RuntimeException { try {// ww w . ja va 2 s . co m CaptchaBuilder captchaBuilder = new CaptchaBuilder(40, 50, buildString); Captcha c = captchaBuilder.buildCaptcha(); BufferedImage img = c.getImage(); // check if size == the default size (40*50) if not scale if (width != 40 || height != 50) { BufferedImage resized = new BufferedImage(width, height, img.getType()); Graphics2D g = resized.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null); g.dispose(); //build new CAPTCHA WITH THE NEW IMAGE SIZE c = new Captcha(c.getBuildSequence(), c.getAnswer(), c.isCaseSensative(), img, new Date()); } return c; } catch (ParseException ex) { Logger.getLogger(DomainFacade.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException("error creating CAPTCHA"); } }
From source file:de.dakror.villagedefense.game.entity.creature.Creature.java
@Override public void draw(Graphics2D g) { drawBump(g, false);//from w w w. j a v a 2 s . com g.drawImage(image, (int) x, (int) y, (int) x + width, (int) y + height, frame * width, dir * height, frame * width + width, dir * height + height, Game.w); drawBump(g, true); }
From source file:org.aludratest.cloud.selenium.SeleniumResourceBean.java
private byte[] takeSeleniumResourceScreenshot(String seleniumUrl) { String url = seleniumUrl;/*from w w w. ja v a 2 s. c om*/ url += "/selenium-server/driver/?cmd=captureScreenshotToString"; InputStream in = null; try { in = new URL(url).openStream(); in.read(new byte[3]); // read away "OK," ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(in, baos); // decode Base64 byte[] rawImageData = Base64.decodeBase64(baos.toByteArray()); // create image from bytes BufferedImage img = ImageIO.read(new ByteArrayInputStream(rawImageData)); // shrink image float sizeFactor = 2; BufferedImage imgSmall = new BufferedImage((int) (img.getWidth() / sizeFactor), (int) (img.getHeight() / sizeFactor), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = imgSmall.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(img, 0, 0, imgSmall.getWidth(), imgSmall.getHeight(), 0, 0, img.getWidth(), img.getHeight(), null); g2d.dispose(); // get PNG bytes baos = new ByteArrayOutputStream(); ImageIO.write(imgSmall, "png", baos); return baos.toByteArray(); } catch (IOException e) { LOG.warn("Could not take Selenium screenshot: " + e.getMessage()); return null; } finally { IOUtils.closeQuietly(in); } }
From source file:WeatherWizard.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension size = getSize();/* ww w . j a v a2 s . c om*/ Composite origComposite; setupWeatherReport(); origComposite = g2.getComposite(); if (alpha0 != null) g2.setComposite(alpha0); g2.drawImage(img0, 0, 0, size.width, size.height, 0, 0, img0.getWidth(null), img0.getHeight(null), null); if (img1 != null) { if (alpha1 != null) g2.setComposite(alpha1); g2.drawImage(img1, 0, 0, size.width, size.height, 0, 0, img1.getWidth(null), img1.getHeight(null), null); } g2.setComposite(origComposite); // Freezing, Cold, Cool, Warm, Hot, // Blue, Green, Yellow, Orange, Red Font font = new Font("Serif", Font.PLAIN, 36); g.setFont(font); String tempString = feels + " " + temperature + "F"; FontRenderContext frc = ((Graphics2D) g).getFontRenderContext(); Rectangle2D boundsTemp = font.getStringBounds(tempString, frc); Rectangle2D boundsCond = font.getStringBounds(condStr, frc); int wText = Math.max((int) boundsTemp.getWidth(), (int) boundsCond.getWidth()); int hText = (int) boundsTemp.getHeight() + (int) boundsCond.getHeight(); int rX = (size.width - wText) / 2; int rY = (size.height - hText) / 2; g.setColor(Color.LIGHT_GRAY); g2.fillRect(rX, rY, wText, hText); g.setColor(textColor); int xTextTemp = rX - (int) boundsTemp.getX(); int yTextTemp = rY - (int) boundsTemp.getY(); g.drawString(tempString, xTextTemp, yTextTemp); int xTextCond = rX - (int) boundsCond.getX(); int yTextCond = rY - (int) boundsCond.getY() + (int) boundsTemp.getHeight(); g.drawString(condStr, xTextCond, yTextCond); }
From source file:eu.udig.style.advanced.utils.Utilities.java
/** * Creates an {@link Image} for the given rule. * /*from ww w. ja v a 2 s.c om*/ * @param rule the rule for which to create the image. * @param width the image width. * @param height the image height. * @return the generated image. */ public static BufferedImage pointRuleToImage(final Rule rule, int width, int height) { DuplicatingStyleVisitor copyStyle = new DuplicatingStyleVisitor(); rule.accept(copyStyle); Rule newRule = (Rule) copyStyle.getCopy(); int pointSize = 0; Stroke stroke = null; Symbolizer[] symbolizers = newRule.getSymbolizers(); if (symbolizers.length > 0) { Symbolizer symbolizer = newRule.getSymbolizers()[0]; if (symbolizer instanceof PointSymbolizer) { PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer; pointSize = SLDs.pointSize(pointSymbolizer); stroke = SLDs.stroke(pointSymbolizer); } } int strokeSize = 0; if (stroke != null) { strokeSize = SLDs.width(stroke); if (strokeSize < 0) { strokeSize = 1; stroke.setWidth(ff.literal(strokeSize)); } } pointSize = pointSize + 2 * strokeSize; if (pointSize <= 0) { pointSize = width; } // pointSize = width; BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); BufferedImage pointImage = new BufferedImage(pointSize, pointSize, BufferedImage.TYPE_INT_ARGB); Point point = d.point(pointSize / 2, pointSize / 2); d.drawDirect(pointImage, d.feature(point), newRule); Graphics2D g2d = finalImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (pointSize > width || pointSize > height) { g2d.drawImage(pointImage, 0, 0, width, height, 0, 0, pointSize, pointSize, null); } else { int x = width / 2 - pointSize / 2; int y = height / 2 - pointSize / 2; g2d.drawImage(pointImage, x, y, null); } g2d.dispose(); return finalImage; }
From source file:algo.PlotBar.java
private BufferedImage bufferResize(BufferedImage original, double widthFactor, double heightFactor) { // original image width & height int w, h;//ww w . j a va2s . c o m w = original.getHeight(); h = original.getWidth(); // System.out.println(original.getHeight()); // System.out.println(original.getWidth()); // new width & height calculated by multiplying factor int newWidth = new Double(original.getWidth() * widthFactor).intValue(); int newHeight = new Double(original.getWidth() * heightFactor).intValue(); // new resized image BufferedImage buffResized = new BufferedImage(newWidth, newHeight, original.getType()); Graphics2D g = buffResized.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, w, h, null); g.dispose(); return buffResized; }