Example usage for java.awt.geom AffineTransform getScaleInstance

List of usage examples for java.awt.geom AffineTransform getScaleInstance

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform getScaleInstance.

Prototype

public static AffineTransform getScaleInstance(double sx, double sy) 

Source Link

Document

Returns a transform representing a scaling transformation.

Usage

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static void saveToPNG(OutputStream os, BufferedImage bufferedImage) throws java.io.IOException {
    AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
    tx.translate(0, -bufferedImage.getHeight(null));
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);
    javax.imageio.ImageIO.write(bufferedImage, "png", os);
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a scaled BufferedImage/*from  ww w . jav a 2 s.  c  om*/
 *
 * @param bi        current image
 * @param maxWidth  new width
 * @param maxHeight new height
 * @return a new/scaled image
 */

/*public static BufferedImage ScaleImage(BufferedImage image, int width, int height) throws IOException {
   int imageWidth  = image.getWidth();
   int imageHeight = image.getHeight();
        
   double scaleX = (double)width/imageWidth;
   double scaleY = (double)height/imageHeight;
   AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
   AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
        
   return bilinearScaleOp.filter(
    image,
    new BufferedImage(width, height, image.getType()));
}*/

public BufferedImage ScaleImage(BufferedImage bi, int maxWidth, int maxHeight) {
    double originalWidth = bi.getWidth() * 1.0;
    double originalHeight = bi.getHeight() * 1.0;

    double widthRatio = (maxWidth * 1.0) / originalWidth;
    double heightRatio = (maxHeight * 1.0) / originalHeight;
    double newImageRatio = 0;
    if (widthRatio < heightRatio) {
        newImageRatio = widthRatio;
    } else {
        newImageRatio = heightRatio;
    }

    BufferedImage bdest = new BufferedImage((int) (originalWidth * newImageRatio),
            (int) (originalHeight * newImageRatio), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(newImageRatio, newImageRatio);
    g.drawRenderedImage(bi, at);

    return bdest;
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage returnValue = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D = returnValue.createGraphics();
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    AffineTransform at = AffineTransform.getScaleInstance((double) width / image.getWidth(),
            (double) height / image.getHeight());
    g2D.drawRenderedImage(image, at);/*from w w  w.  jav a2 s.co m*/
    return returnValue;
}

From source file:edu.osu.netmotifs.warswap.ui.GenerateMotifImages.java

public void generateImagesSize3(Graph<Integer, String> g, String fileName) {
    Transformer<Integer, Point2D> locationTransformer = new TransformerMotif(motifSize);
    Layout<Integer, String> layout = new StaticLayout<Integer, String>(g, locationTransformer);
    layout.setSize(new Dimension(100, 100));
    BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<Integer, String>(layout);
    vv.setPreferredSize(new Dimension(120, 120));
    Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
        public Paint transform(Integer i) {
            return colPerGraphHash.get(i);
        }//from  w w w . j a  va2  s  . c o  m
    };
    Transformer<Integer, Paint> vertexDrawPaint = new Transformer<Integer, Paint>() {
        public Paint transform(Integer i) {
            return colPerGraphHash.get(i);
        }
    };
    Transformer<Integer, Shape> vertexSize = new Transformer<Integer, Shape>() {
        public Shape transform(Integer i) {
            Ellipse2D circle = new Ellipse2D.Double(-10, -10, 20, 20);
            return AffineTransform.getScaleInstance(0.6, 0.6).createTransformedShape(circle);
        }
    };
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
    vv.getRenderContext().setVertexDrawPaintTransformer(vertexDrawPaint);
    vv.getRenderContext().setVertexShapeTransformer(vertexSize);

    JPanel jPanel = new JPanel();
    jPanel.add(vv);
    BufferedImage bufImage = null;
    bufImage = ScreenImage.createImage((JComponent) jPanel);

    imageOutFile = imagesOutDir + CONF.DIR_SEP + fileName + ".png";
    relativePathToImage = "images" + CONF.DIR_SEP + fileName + ".png";
    try {
        File outFile = new File(imageOutFile);
        ImageIO.write(bufImage, "png", outFile);
        //         System.out.println("wrote image to " + outFile);
    } catch (Exception e) {
        System.err.println("writeToImageFile(): " + e.getMessage());
    }
}

From source file:graphView.JungTreeViewer.java

public JungTreeViewer(ArrayList<String> versions) {

    // Added by Minh Nguyen for CS550 Term Project, KAIST, 2014
    Transformer<String, Shape> vertexSize = new Transformer<String, Shape>() {
        public Shape transform(String str) {
            Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
            // in this case, the vertex is twice as large
            return AffineTransform.getScaleInstance(1.5, 1.5).createTransformedShape(circle);
        }//from w  ww .j  a  v  a2s . c  o m
    };

    Transformer<String, Paint> vertexPaint = new Transformer<String, Paint>() {
        @Override
        public Paint transform(String str) {
            Color green = new Color(0, 1, 0, 0.1f);
            String[] chars = str.split("\\.");
            if (chars.length == 1) {
                return Color.blue;
            } else {
                return green;
            }
        }
    };

    // create a simple graph for the demo
    graph = new DelegateForest<String, Integer>();

    createTree(versions);

    layout = new TreeLayout<String, Integer>(graph);
    radialLayout = new RadialTreeLayout<String, Integer>(graph);
    radialLayout.setSize(new Dimension(700, 700));
    vv = new VisualizationViewer<String, Integer>(layout, new Dimension(700, 700)); //jy: size
    vv.setBackground(Color.white);
    vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    // add a listener for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller());
    //vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
    vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.BLACK));
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    //vv.getRenderContext().setVertexShapeTransformer(vertexSize);      

    rings = new Rings();

    Container content = getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);

    final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();

    vv.setGraphMouse(graphMouse);

    JComboBox<String> modeBox = graphMouse.getModeComboBox();
    modeBox.addItemListener(graphMouse.getModeListener());
    graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JToggleButton radial = new JToggleButton("Radial");
    radial.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {

                LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, layout,
                        radialLayout);
                Animator animator = new Animator(lt);
                animator.start();
                vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
                vv.addPreRenderPaintable(rings);
            } else {
                LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, radialLayout,
                        layout);
                Animator animator = new Animator(lt);
                animator.start();
                vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
                vv.removePreRenderPaintable(rings);
            }
            vv.repaint();
        }
    });

    JPanel scaleGrid = new JPanel(new GridLayout(1, 0));
    scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));

    JPanel controls = new JPanel();
    scaleGrid.add(plus);
    scaleGrid.add(minus);
    controls.add(radial);
    controls.add(scaleGrid);
    controls.add(modeBox);

    content.add(controls, BorderLayout.SOUTH);
}

From source file:ImageDrawingComponent.java

public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    switch (opIndex) {
    case 0: /* copy */
        g.drawImage(bi, 0, 0, null);//  ww  w  .j av  a2  s. co  m
        break;

    case 1: /* scale up using coordinates */
        g.drawImage(bi, 0, 0, w, h, /* dst rectangle */
                0, 0, w / 2, h / 2, /* src area of image */
                null);
        break;

    case 2: /* scale down using transform */
        g2.drawImage(bi, AffineTransform.getScaleInstance(0.7, 0.7), null);
        break;

    case 3: /* scale up using transform Op and BICUBIC interpolation */
        AffineTransform at = AffineTransform.getScaleInstance(1.5, 1.5);
        AffineTransformOp aop = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        g2.drawImage(bi, aop, 0, 0);
        break;

    case 4: /* low pass filter */
    case 5: /* sharpen */
        float[] data = (opIndex == 4) ? BLUR3x3 : SHARPEN3x3;
        ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data), ConvolveOp.EDGE_NO_OP, null);
        g2.drawImage(bi, cop, 0, 0);
        break;

    case 6: /* rescale */
        RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
        g2.drawImage(bi, rop, 0, 0);
        break;

    case 7: /* lookup */
        byte lut[] = new byte[256];
        for (int j = 0; j < 256; j++) {
            lut[j] = (byte) (256 - j);
        }
        ByteLookupTable blut = new ByteLookupTable(0, lut);
        LookupOp lop = new LookupOp(blut, null);
        g2.drawImage(bi, lop, 0, 0);
        break;

    default:
    }
}

From source file:MWC.GUI.JFreeChart.StepperChartPanel.java

/**
 * Paints the component by drawing the chart to fill the entire component,
 * but allowing for the insets (which will be non-zero if a border has been
 * set for this component).  To increase performance (at the expense of
 * memory), an off-screen buffer image can be used.
 *
 * @param g  the graphics device for drawing on.
 *//*  www  .j a  v  a2  s.com*/
public void paintWMFComponent(final Graphics g) {

    final Graphics2D g2 = (Graphics2D) g;

    // first determine the size of the chart rendering area...
    final Dimension size = getSize();
    final Insets insets = getInsets();
    available.setRect(insets.left, insets.top, size.getWidth() - insets.left - insets.right,
            size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    double scaleX = 1.0;
    double scaleY = 1.0;

    if (drawWidth < this.getMinimumDrawWidth()) {
        scaleX = drawWidth / getMinimumDrawWidth();
        drawWidth = getMinimumDrawWidth();
        scale = true;
    } else if (drawWidth > this.getMaximumDrawWidth()) {
        scaleX = drawWidth / getMaximumDrawWidth();
        drawWidth = getMaximumDrawWidth();
        scale = true;
    }

    if (drawHeight < this.getMinimumDrawHeight()) {
        scaleY = drawHeight / getMinimumDrawHeight();
        drawHeight = getMinimumDrawHeight();
        scale = true;
    } else if (drawHeight > this.getMaximumDrawHeight()) {
        scaleY = drawHeight / getMaximumDrawHeight();
        drawHeight = getMaximumDrawHeight();
        scale = true;
    }

    chartArea.setRect(0.0, 0.0, drawWidth, drawHeight);

    final AffineTransform saved = g2.getTransform();
    g2.translate(insets.left, insets.right);
    if (scale) {
        final AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
        g2.transform(st);
    }
    getChart().draw(g2, chartArea, this.getChartRenderingInfo());
    g2.setTransform(saved);

}

From source file:de.bamamoto.mactools.png2icns.Scaler.java

public static BufferedImage resize(BufferedImage source, int width, int height) {

    double xScale = ((double) width) / (double) source.getWidth();
    double yScale = ((double) height) / (double) source.getHeight();
    BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration()//ww w .ja  v  a2s.  c  om
            .createCompatibleImage(width, height, source.getColorModel().getTransparency());
    Graphics2D newImage = null;
    try {
        newImage = result.createGraphics();
        newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        newImage.drawRenderedImage(source, AffineTransform.getScaleInstance(xScale, yScale));
    } finally {
        if (newImage != null) {
            newImage.dispose();
        }
    }
    return result;
}

From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java

/**
 * Returns a regular <code>num_sides</code>-sided 
 * <code>Polygon</code> whose bounding 
 * box's width and height are defined by this instance's size and
 * aspect ratio functions for this vertex.
 * @param num_sides the number of sides of the polygon; must be >= 3.
 *//* w  ww  . ja va 2  s . com*/
public Shape getRegularPolygon(V v, int num_sides) {
    if (num_sides < 3)
        throw new IllegalArgumentException("Number of sides must be >= 3");
    Rectangle2D frame = getRectangle(v);
    float width = (float) frame.getWidth();
    float height = (float) frame.getHeight();

    // generate coordinates
    double angle = 0;
    thePolygon.reset();
    thePolygon.moveTo(0, 0);
    thePolygon.lineTo(width, 0);
    double theta = (2 * Math.PI) / num_sides;
    for (int i = 2; i < num_sides; i++) {
        angle -= theta;
        float delta_x = (float) (width * Math.cos(angle));
        float delta_y = (float) (width * Math.sin(angle));
        Point2D prev = thePolygon.getCurrentPoint();
        thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y);
    }
    thePolygon.closePath();

    // scale polygon to be right size, translate to center at (0,0)
    Rectangle2D r = thePolygon.getBounds2D();
    double scale_x = width / r.getWidth();
    double scale_y = height / r.getHeight();
    float translationX = (float) (r.getMinX() + r.getWidth() / 2);
    float translationY = (float) (r.getMinY() + r.getHeight() / 2);

    AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
    at.translate(-translationX, -translationY);

    Shape shape = at.createTransformedShape(thePolygon);
    return shape;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadThumbnailer.java

private BufferedImage scaleImage(BufferedImage image, float scaleFactor) {
    AffineTransform transform = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
    AffineTransformOp atoOp = new AffineTransformOp(transform, null);
    return atoOp.filter(image, null);
}