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:se.vgregion.webbisar.svc.impl.ImageUtil.java

/**
 * Resizes and overwrites the image. Will keep the original pictures dimensions.
 * /*from w  w  w  .  j  a  va 2  s . c  om*/
 * @param sourceFile
 *            the file to rezise
 * @param newLongSideSize
 *            the new size, in pixels.
 * @param quality
 *            a number between 0 and 100 where 100 gives the best quality
 * @throws IOException
 */
public synchronized static void scaleImage(File sourceFile, ImageSize imageSize, float quality)
        throws IOException {
    // System.gc();
    BufferedImage sourceImage = ImageIO.read(sourceFile);
    int srcWidth = sourceImage.getWidth();
    int srcHeight = sourceImage.getHeight();

    double longSideForSource = Math.max(srcWidth, srcHeight);
    double longSideForDest = srcWidth > srcHeight ? imageSize.getWidth() : imageSize.getHeight();
    double multiplier = longSideForDest / longSideForSource;

    int destWidth = (int) (srcWidth * multiplier);
    int destHeight = (int) (srcHeight * multiplier);

    BufferedImage destImage = new BufferedImage((int) imageSize.getWidth(), (int) imageSize.getHeight(),
            BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = destImage.createGraphics();
    graphics.setPaint(Color.WHITE);
    graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight());

    AffineTransform affineTransform = AffineTransform.getScaleInstance(multiplier, multiplier);
    AffineTransform trans = new AffineTransform();
    trans.setToTranslation((imageSize.getWidth() - destWidth) / 2, (imageSize.getHeight() - destHeight) / 2);
    graphics.transform(trans);

    graphics.drawRenderedImage(sourceImage, affineTransform);
    saveImageAsJPEG(sourceFile.getAbsolutePath(), destImage, quality);

}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Scale the given {@code source} {@link Image}.
 *
 * @param source Source image.//from   w w  w  . j  a  va 2  s .c  o m
 * @param width New width.
 * @param height New height.
 *
 * @return Scaled {@code source} image (uses bilinear interpolation).
 */
public static BufferedImage getScaledImage(Image source, int width, int height) {
    // convert the given Image into a BufferedImage if needed--makes things a
    // little easier.
    BufferedImage image;
    if (source instanceof BufferedImage) {
        image = (BufferedImage) source;
    } else {
        image = new BufferedImage(source.getWidth(null), source.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(source, 0, 0, null);
        g.dispose();
    }

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    double scaleX = (double) width / imageWidth;
    double scaleY = (double) height / imageHeight;
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    BufferedImageOp op = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
    return op.filter(image, new BufferedImage(width, height, image.getType()));
}

From source file:uk.ac.liverpool.narrative.SolutionGraphics.java

public void createVisualStateGraph(boolean dynamic, int iterations) {

    KKLayout<STRIPSState, Action> layout = new KKLayout<STRIPSState, Action>(sg.ggraph);// , new SDist<STRIPSState, Action>(ggraph));

    layout.setLengthFactor(1.7);//w  w  w .  jav  a  2s.  c om
    layout.setAdjustForGravity(false);
    // layout.setDisconnectedDistanceMultiplier(1.5);
    // layout.setExchangeVertices(true);
    layout.setSize(new Dimension(2000, 1200)); // sets the initial size of
    int i = 0;
    if (!dynamic) {
        layout.setMaxIterations(iterations);
        while (!layout.done() && i++ < iterations) {
            layout.step();
        }
        layout.lock(true);
    }
    final VisualizationViewer<STRIPSState, Action> vv = new VisualizationViewer<STRIPSState, Action>(layout);
    vvv = vv;
    vv.setPreferredSize(new Dimension(2000, 1200)); // Sets the viewing area

    final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
    graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
    vv.setGraphMouse(graphMouse);

    vv.setBackground(Color.white);
    vv.getRenderContext().setVertexLabelTransformer(new ConstantTransformer(""));
    vv.getRenderContext().setEdgeFontTransformer(new Transformer<Action, Font>() {
        @Override
        public Font transform(Action s) {

            Action ra = sg.realActions.get(s);
            Collection<Action> set = sg.actionToEdges.get(template.apply_template(ra));
            if (set != null && set.size() > 1)
                return DERIVE_FONT;
            return DERIVE_FONT2;
        }
    });

    vv.getRenderContext().setEdgeLabelTransformer(new Transformer<Action, String>() {
        @Override
        public String transform(Action a) {
            try {
                return sg.actionToString(template, a);
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }
    });

    vv.getRenderContext().setEdgeDrawPaintTransformer(new Transformer<Action, Paint>() {
        @Override
        public Paint transform(Action s) {
            sg.realActions.get(s);
            EdgeColor v = sg.edgeColors.get(s);
            if (v == null)
                return Color.yellow;
            if (v == EdgeColor.black)
                return Color.red;
            else
                return Color.lightGray;
        }
    });
    vv.getRenderContext().setVertexStrokeTransformer(new ConstantTransformer(new BasicStroke(0)));
    vv.getRenderContext().setVertexFillPaintTransformer(new Transformer<STRIPSState, Paint>() {

        @Override
        public Paint transform(STRIPSState s) {
            if (vv.getPickedVertexState().isPicked(s))
                return new Color(250, 250, 0, 207);
            Color c = cm.get(s);
            if (c != null)
                return c;

            if (sg.start.equals(s))
                return new Color(10, 200, 10, 197);
            else if (sg.ends.contains(s))
                return new Color(220, 10, 10, 197);
            return new Color(255, 210, 40, 197);
        }
    });
    vv.getRenderContext().setVertexShapeTransformer(new StoryStateTransformer());
    vv.getRenderContext()
            .setEdgeArrowTransformer(new DirectionalEdgeArrowTransformer<STRIPSState, Action>(10, 8, 1) {
                @Override
                public Shape transform(Context<Graph<STRIPSState, Action>, Action> context) {
                    if (sg.frequency.get(sg.ggraph.getEndpoints(context.element).getFirst()) != null) {
                        double c = Math.log(
                                sg.frequency.get(sg.ggraph.getEndpoints(context.element).getFirst())) * 0.8;
                        if (c > 1) {
                            Shape ss = AffineTransform.getScaleInstance(c, c)
                                    .createTransformedShape(super.transform(context));
                            return ss;
                        }
                    }

                    return super.transform(context);
                }

            });

    vv.setRenderer(new LabelsLastRenderer());

    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.AUTO);
    vv.getRenderContext().setEdgeStrokeTransformer(new Transformer<Action, Stroke>() {

        @Override
        public Stroke transform(Action a) {
            if (false)
                return new BasicStroke(
                        (sg.actionToEdges.get(template.apply_template(sg.realActions.get(a))).size()),
                        BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            if (sg.frequency.get(sg.ggraph.getEndpoints(a).getFirst()) != null)
                return new BasicStroke(
                        (float) (Math.log(sg.frequency.get(sg.ggraph.getEndpoints(a).getFirst())) * 6.0),
                        BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            else
                return new BasicStroke(1);
        }
    });
    vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.gray, false));
    vv.getRenderContext().setLabelOffset(20);
    vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));

    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);

    JPanel jp = new JPanel();
    jp.setBackground(Color.WHITE);
    jp.setLayout(new BorderLayout());
    jp.add(panel, BorderLayout.CENTER);
    JPanel top = new JPanel(new FlowLayout());

    jp.add(top, BorderLayout.NORTH);
    JFrame frame = new JFrame("Story state graph");
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(jp);
    frame.pack();
    frame.setVisible(true);

}

From source file:uky.article.imageviewer.views.SWTImageCanvas.java

/**
 * Synchronize the scrollbar with the image. If the transform is out
 * of range, it will correct it. This function considers only following
 * factors :<b> transform, image size, client area</b>.
 *//*  w  ww.j  a v a  2s  .com*/
public void syncScrollBars() {
    if (sourceImage == null) {
        redraw();
        return;
    }

    AffineTransform af = transform;
    double sx = af.getScaleX(), sy = af.getScaleY();
    double tx = af.getTranslateX(), ty = af.getTranslateY();
    if (tx > 0)
        tx = 0;
    if (ty > 0)
        ty = 0;

    ScrollBar horizontal = getHorizontalBar();
    horizontal.setIncrement(getClientArea().width / 100);
    horizontal.setPageIncrement(getClientArea().width);
    Rectangle imageBound = sourceImage.getBounds();
    int cw = getClientArea().width, ch = getClientArea().height;
    if (imageBound.width * sx > cw) { /* image is wider than client area */
        horizontal.setMaximum((int) (imageBound.width * sx));
        horizontal.setEnabled(true);
        if (((int) -tx) > horizontal.getMaximum() - cw)
            tx = -horizontal.getMaximum() + cw;
    } else { /* image is narrower than client area */
        horizontal.setEnabled(false);
        tx = (cw - imageBound.width * sx) / 2; //center if too small.
    }
    horizontal.setSelection((int) (-tx));
    horizontal.setThumb((getClientArea().width));

    ScrollBar vertical = getVerticalBar();
    vertical.setIncrement(getClientArea().height / 100);
    vertical.setPageIncrement((getClientArea().height));
    if (imageBound.height * sy > ch) { /* image is higher than client area */
        vertical.setMaximum((int) (imageBound.height * sy));
        vertical.setEnabled(true);
        if (((int) -ty) > vertical.getMaximum() - ch)
            ty = -vertical.getMaximum() + ch;
    } else { /* image is less higher than client area */
        vertical.setEnabled(false);
        ty = (ch - imageBound.height * sy) / 2; //center if too small.
    }
    vertical.setSelection((int) (-ty));
    vertical.setThumb((getClientArea().height));

    /* update transform. */
    af = AffineTransform.getScaleInstance(sx, sy);
    af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty));
    transform = af;

    redraw();
}

From source file:VASSAL.build.module.map.MapShader.java

public void draw(Graphics g, Map map) {
    if (shadingVisible) {

        double zoom = map.getZoom();
        buildStroke(zoom);//from  w  w w  .jav a  2  s . com

        final Graphics2D g2 = (Graphics2D) g;

        final Composite oldComposite = g2.getComposite();
        final Color oldColor = g2.getColor();
        final Paint oldPaint = g2.getPaint();
        final Stroke oldStroke = g2.getStroke();

        g2.setComposite(getComposite());
        g2.setColor(getColor());
        g2.setPaint(scaleImage && pattern.equals(TYPE_IMAGE) && imageName != null ? getTexture(zoom)
                : getTexture());
        Area area = getShadeShape(map);
        if (zoom != 1.0) {
            area = new Area(AffineTransform.getScaleInstance(zoom, zoom).createTransformedShape(area));
        }
        g2.fill(area);
        if (border) {
            g2.setComposite(getBorderComposite());
            g2.setStroke(getStroke(map.getZoom()));
            g2.setColor(getBorderColor());
            g2.draw(area);
        }

        g2.setComposite(oldComposite);
        g2.setColor(oldColor);
        g2.setPaint(oldPaint);
        g2.setStroke(oldStroke);
    }
}