List of usage examples for java.awt.geom AffineTransform getScaleInstance
public static AffineTransform getScaleInstance(double sx, double sy)
From source file:Main.java
@Override public void paintComponent(Graphics gr) { Color c = gr.getColor();/*from w w w .j av a2 s .c o m*/ setForeground(new Color(0, 0, 0, 0)); super.paintComponent(gr); setForeground(c); gr.setColor(c); Graphics2D g = (Graphics2D) gr; g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); double pw = this.getPreferredSize().width; double ph = this.getPreferredSize().height; double w = this.getSize().width; double h = this.getSize().height; g.setColor(this.getForeground()); AffineTransform stretch = AffineTransform.getScaleInstance(w / pw, h / ph); g.setTransform(stretch); g.drawString(getText(), 0, this.getFont().getSize()); }
From source file:imageLines.ImageHelpers.java
/**Scale an image to the specified width and height*/ public static BufferedImage scale(BufferedImage img, int height, int width) { BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bdest.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance((double) width / img.getWidth(), (double) height / img.getHeight()); g.drawRenderedImage(img, at);/*from w w w . java2 s . c o m*/ return bdest; }
From source file:edu.jhuapl.graphs.jfreechart.utils.HighResChartUtil.java
/** * Returns a high resolution BufferedImage of the chart. Uses the default DPI_FILE_RESOLUTION. * * @param resolution The resolution, in dots per inch, of the image to generate. * @return the buffered image./*from w w w .j a va2s . c om*/ */ public static BufferedImage getHighResChartImage(ChartPanel chartPanel, int resolution) { int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); double scaleRatio = resolution / screenResolution; int width = chartPanel.getWidth(); int height = chartPanel.getHeight(); int rasterWidth = (int) (width * scaleRatio); int rasterHeight = (int) (height * scaleRatio); BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.transform(AffineTransform.getScaleInstance(scaleRatio, scaleRatio)); chartPanel.getChart().draw(g2, new Rectangle2D.Double(0, 0, width, height), null); g2.dispose(); return image; }
From source file:com.webpagebytes.cms.DefaultImageProcessor.java
public boolean resizeImage(WPBFileStorage cloudStorage, WPBFilePath cloudFile, int desiredSize, String outputFormat, OutputStream os) throws WPBException { InputStream is = null;/* w ww . j a va 2 s .c o m*/ try { //get the file content WPBFileInfo fileInfo = cloudStorage.getFileInfo(cloudFile); String type = fileInfo.getContentType().toLowerCase(); if (!type.startsWith("image")) { return false; } is = cloudStorage.getFileContent(cloudFile); BufferedImage bufImg = ImageIO.read(is); Dimension<Integer> newSize = getResizeSize(bufImg.getWidth(), bufImg.getHeight(), desiredSize); BufferedImage bdest = new BufferedImage(newSize.getX(), newSize.getY(), BufferedImage.TYPE_INT_RGB); Graphics2D g = bdest.createGraphics(); Dimension<Double> scale = getResizeScale(bufImg.getHeight(), bufImg.getWidth(), desiredSize); AffineTransform at = AffineTransform.getScaleInstance(scale.getX(), scale.getY()); g.drawRenderedImage(bufImg, at); ImageIO.write(bdest, outputFormat, os); return true; } catch (Exception e) { throw new WPBException("Cannot resize image ", e); } finally { IOUtils.closeQuietly(is); } }
From source file:AntiAlias.java
/** Draw the example */ public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; BufferedImage image = // Create an off-screen image new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB); Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing // Set the background to a gradient fill. The varying color of // the background helps to demonstrate the anti-aliasing effect ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white)); ig.fillRect(0, 0, 65, 35);//from www . j ava 2 s . com // Set drawing attributes for the foreground. // Most importantly, turn on anti-aliasing. ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); // Now draw pure blue text and a pure red oval ig.setColor(Color.blue); ig.drawString("Java", 9, 22); ig.setColor(Color.red); ig.drawOval(1, 1, 62, 32); // Finally, scale the image by a factor of 10 and display it // in the window. This will allow us to see the anti-aliased pixels g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this); // Draw the image one more time at its original size, for comparison g.drawImage(image, 0, 0, this); }
From source file:eu.novait.imagerenamer.model.ImageFile.java
private void createThumbnail() { try {/*w ww . j av a 2 s . c o m*/ BufferedImage tmp = ImageIO.read(this.filepath); int tmpW = tmp.getWidth(); int tmpH = tmp.getHeight(); double ratio = (double) tmpW / (double) tmpH; int w = 400; int h = (int) Math.round(w / ratio); BufferedImage bi = getCompatibleImage(w, h); Graphics2D g2d = bi.createGraphics(); double xScale = (double) w / tmp.getWidth(); double yScale = (double) h / tmp.getHeight(); AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale); g2d.drawRenderedImage(tmp, at); g2d.dispose(); this.setThumbnail(bi); } catch (IOException ex) { Logger.getLogger(ImageFile.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:Paints.java
/** Draw the example */ public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; // Paint the entire background using a GradientPaint. // The background color varies diagonally from deep red to pale blue g.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0), WIDTH, HEIGHT, new Color(200, 200, 255))); g.fillRect(0, 0, WIDTH, HEIGHT); // fill the background // Use a different GradientPaint to draw a box. // This one alternates between deep opaque green and transparent green. // Note: the 4th arg to Color() constructor specifies color opacity g.setPaint(new GradientPaint(0, 0, new Color(0, 150, 0), 20, 20, new Color(0, 150, 0, 0), true)); g.setStroke(new BasicStroke(15)); // use wide lines g.drawRect(25, 25, WIDTH - 50, HEIGHT - 50); // draw the box // The glyphs of fonts can be used as Shape objects, which enables // us to use Java2D techniques with letters Just as we would with // any other shape. Here we get some letter shapes to draw. Font font = new Font("Serif", Font.BOLD, 10); // a basic font Font bigfont = // a scaled up version font.deriveFont(AffineTransform.getScaleInstance(30.0, 30.0)); GlyphVector gv = bigfont.createGlyphVector(g.getFontRenderContext(), "JAV"); Shape jshape = gv.getGlyphOutline(0); // Shape of letter J Shape ashape = gv.getGlyphOutline(1); // Shape of letter A Shape vshape = gv.getGlyphOutline(2); // Shape of letter V // We're going to outline the letters with a 5-pixel wide line g.setStroke(new BasicStroke(5.0f)); // We're going to fake shadows for the letters using the // following Paint and AffineTransform objects Paint shadowPaint = new Color(0, 0, 0, 100); // Translucent black AffineTransform shadowTransform = AffineTransform.getShearInstance(-1.0, 0.0); // Shear to the right shadowTransform.scale(1.0, 0.5); // Scale height by 1/2 // Move to the baseline of our first letter g.translate(65, 270);// w w w. j a v a 2s . c o m // Draw the shadow of the J shape g.setPaint(shadowPaint); g.translate(15, 20); // Compensate for the descender of the J // transform the J into the shape of its shadow, and fill it g.fill(shadowTransform.createTransformedShape(jshape)); g.translate(-15, -20); // Undo the translation above // Now fill the J shape with a solid (and opaque) color g.setPaint(Color.blue); // Fill with solid, opaque blue g.fill(jshape); // Fill the shape g.setPaint(Color.black); // Switch to solid black g.draw(jshape); // And draw the outline of the J // Now draw the A shadow g.translate(75, 0); // Move to the right g.setPaint(shadowPaint); // Set shadow color g.fill(shadowTransform.createTransformedShape(ashape)); // draw shadow // Draw the A shape using a solid transparent color g.setPaint(new Color(0, 255, 0, 125)); // Transparent green as paint g.fill(ashape); // Fill the shape g.setPaint(Color.black); // Switch to solid back g.draw(ashape); // Draw the outline // Move to the right and draw the shadow of the letter V g.translate(175, 0); g.setPaint(shadowPaint); g.fill(shadowTransform.createTransformedShape(vshape)); // We're going to fill the next letter using a TexturePaint, which // repeatedly tiles an image. The first step is to obtain the image. // We could load it from an image file, but here we create it // ourselves by drawing a into an off-screen image. Note that we use // a GradientPaint to fill the off-screen image, so the fill pattern // combines features of both Paint classes. BufferedImage tile = // Create an image new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB); Graphics2D tg = tile.createGraphics(); // Get its Graphics for drawing tg.setColor(Color.pink); tg.fillRect(0, 0, 50, 50); // Fill tile background with pink tg.setPaint(new GradientPaint(40, 0, Color.green, // diagonal gradient 0, 40, Color.gray)); // green to gray tg.fillOval(5, 5, 40, 40); // Draw a circle with this gradient // Use this new tile to create a TexturePaint and fill the letter V g.setPaint(new TexturePaint(tile, new Rectangle(0, 0, 50, 50))); g.fill(vshape); // Fill letter shape g.setPaint(Color.black); // Switch to solid black g.draw(vshape); // Draw outline of letter // Move to the right and draw the shadow of the final A g.translate(160, 0); g.setPaint(shadowPaint); g.fill(shadowTransform.createTransformedShape(ashape)); g.fill(ashape); // Fill letter A g.setPaint(Color.black); // Revert to solid black g.draw(ashape); // Draw the outline of the A }
From source file:TransformScale.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Use antialiasing. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Move the origin to 75, 75. AffineTransform at = AffineTransform.getTranslateInstance(75, 75); g2.transform(at);// w ww.j a v a2 s. co m // Draw the shapes in their original locations. g2.setPaint(Color.black); g2.draw(axes); g2.draw(shape); // Transform the Graphics2D. g2.transform(AffineTransform.getScaleInstance(3, 3)); // Draw the "new" shapes in dashed. g2.transform(AffineTransform.getTranslateInstance(75, 75)); Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0); g2.setStroke(stroke); g2.draw(axes); g2.draw(shape); }
From source file:ImageUtil.java
public static BufferedImage scale(BufferedImage src, int width, int height) throws IOException { BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = dest.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(), (double) height / src.getHeight()); g.drawRenderedImage(src, at);/*from w w w .j a v a 2 s .c o m*/ return dest; }
From source file:Sampler.java
private void createTransformations() { AffineTransform at;//from w w w . ja v a 2s. c om at = AffineTransform.getRotateInstance(Math.PI / 6, 0, 285); mOps.put("Rotate nearest neighbor", new AffineTransformOp(at, null)); RenderingHints rh = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); mOps.put("Rotate bilinear", new AffineTransformOp(at, rh)); at = AffineTransform.getScaleInstance(.5, .5); mOps.put("Scale .5, .5", new AffineTransformOp(at, null)); at = AffineTransform.getRotateInstance(Math.PI / 6); mOps.put("Rotate bilinear (origin)", new AffineTransformOp(at, rh)); }