Example usage for java.awt Graphics2D getTransform

List of usage examples for java.awt Graphics2D getTransform

Introduction

In this page you can find the example usage for java.awt Graphics2D getTransform.

Prototype

public abstract AffineTransform getTransform();

Source Link

Document

Returns a copy of the current Transform in the Graphics2D context.

Usage

From source file:Java2DUtils.java

@SuppressWarnings("unchecked")
public static void setTransform(Graphics2D g2, AffineTransform transform) {
    AffineTransform current;//ww w  .  j a  v  a 2 s  .c  o  m

    current = g2.getTransform();
    transforms.push(current);
    g2.setTransform(transform);
}

From source file:Main.java

/**
 * Draws a shape with the specified rotation about <code>(x, y)</code>.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param shape  the shape (<code>null</code> not permitted).
 * @param angle  the angle (in radians).
 * @param x  the x coordinate for the rotation point.
 * @param y  the y coordinate for the rotation point.
 *///ww w. j  a  v a 2 s  .c om
public static void drawRotatedShape(final Graphics2D g2, final Shape shape, final double angle, final float x,
        final float y) {

    final AffineTransform saved = g2.getTransform();
    final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
    g2.transform(rotate);
    g2.draw(shape);
    g2.setTransform(saved);

}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ???//from w  ww  . j  a va2  s .co m
 * ?????
 * 3: 180
 * 6: 90
 * 8: 27090
 * @param img_fn
 * @param orient
 * @throws IOException 
 */
public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException {
    double radian = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        break;
    case 6:
        radian = 90.0;
        break;
    case 8:
        radian = 270.0;
        break;
    default:
        return false;
    }
    BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn));
    int width = old_img.getWidth();
    int height = old_img.getHeight();

    BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = new_img.createGraphics();

    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform) (origXform.clone());
    // center of rotation is center of the panel
    double xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        xRot = width / 2.0;
        yRot = xRot;
        break;
    default:
        return false;
    }
    newXform.rotate(Math.toRadians(radian), xRot, yRot);

    g2d.setTransform(newXform);
    // draw image centered in panel
    g2d.drawImage(old_img, 0, 0, null);
    // Reset to Original
    g2d.setTransform(origXform);

    FileOutputStream out = new FileOutputStream(dest_fn);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * Paints a chart with scaling options/*from   ww  w.  ja v  a2  s . com*/
 * 
 * @param chart
 * @param info
 * @param out
 * @param width
 * @param height
 * @param resolution
 * @return BufferedImage of a given chart with scaling to resolution
 * @throws IOException
 */
private static BufferedImage paintScaledChartToBufferedImage(JFreeChart chart, ChartRenderingInfo info,
        OutputStream out, int width, int height, int resolution, int bufferedIType) throws IOException {
    Args.nullNotPermitted(out, "out");
    Args.nullNotPermitted(chart, "chart");

    double scaleX = resolution / 72.0;
    double scaleY = resolution / 72.0;

    double desiredWidth = width * scaleX;
    double desiredHeight = height * scaleY;
    double defaultWidth = width;
    double defaultHeight = height;
    boolean scale = false;

    // get desired width and height from somewhere then...
    if ((scaleX != 1) || (scaleY != 1)) {
        scale = true;
    }

    BufferedImage image = new BufferedImage((int) desiredWidth, (int) desiredHeight, bufferedIType);
    Graphics2D g2 = image.createGraphics();

    if (scale) {
        AffineTransform saved = g2.getTransform();
        g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
        g2.setTransform(saved);
        g2.dispose();
    } else {
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), info);
    }
    return image;
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int[] x = { 0, 100, 100, 0, 0, 75, 75, 125, 25, 50 };
    int[] y = { 0, 10, 10, 100, 25, 125, 75, 75, 510, 50 };
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform at0 = g2d.getTransform();
    g2d.scale(size / 100, size / 100);/*  ww  w  .j  av  a2 s  .  c  o m*/
    g.drawPolyline(x, y, x.length);
    g2d.setTransform(at0);
}

From source file:Main.java

public void paint(Graphics g, JComponent c) {

    JLabel label = (JLabel) c;
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return;//from   w w  w.j  av  a2  s . c  om
    }

    FontMetrics fm = g.getFontMetrics();
    paintViewInsets = c.getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;

    // Use inverted height & width
    paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform tr = g2.getTransform();
    if (clockwise) {
        g2.rotate(Math.PI / 2);
        g2.translate(0, -c.getWidth());
    } else {
        g2.rotate(-Math.PI / 2);
        g2.translate(-c.getHeight(), 0);
    }

    if (icon != null) {
        icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
    }

    if (text != null) {
        int textX = paintTextR.x;
        int textY = paintTextR.y + fm.getAscent();

        if (label.isEnabled()) {
            paintEnabledText(label, g, clippedText, textX, textY);
        } else {
            paintDisabledText(label, g, clippedText, textX, textY);
        }
    }

    g2.setTransform(tr);
}

From source file:TransformDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect = new Rectangle(5, 5, 200, 200);

    int w = getSize().width;
    int h = getSize().height;

    AffineTransform saveXform = g2.getTransform();
    AffineTransform toCenterAt = new AffineTransform();
    toCenterAt.translate(w / 2 - (rect.width / 2), h / 2 - (rect.height / 2));
    g2.transform(toCenterAt);/*from w  ww .  ja  v a  2  s  .c o  m*/

    g2.fill(rect);

    g2.transform(saveXform);
}

From source file:juicebox.track.EigenvectorTrack.java

private void drawRotatedString(Graphics2D g2, String string, float x, float y) {
    AffineTransform orig = g2.getTransform();
    g2.rotate(0);/*w w w.j a v  a  2  s  .c  o m*/
    g2.setColor(Color.BLUE);
    g2.translate(x, 0);
    g2.scale(-1, 1);
    g2.translate(-x, 0);
    g2.drawString(string, x, y);
    g2.setTransform(orig);
}

From source file:gda.plots.BlockWrapper.java

/**
 * JFreeChart will actually call this from its drawTitle method expecting the Block to draw itself at this point.
 * Our JComponent will already have been drawn as a consequence of being a simple child of the ChartPanel (which
 * extends JPanel). However we can use the information passed to the Block here to get the JComponents bounds set
 * correctly./* www .  j ava  2s .c  o m*/
 * 
 * @param g2
 *            the Graphics2D into which to draw (not used here)
 * @param area
 *            the Rectangle2D into which the object should draw itself)
 * @param params
 *            some parameters not yet understood
 * @return an Object else null
 */
@Override
public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
    logger.debug("BW area is " + area);
    logger.debug("BW params is " + params);
    logger.debug("BW g2 is " + g2);
    logger.debug("BW g2.transform is " + g2.getTransform());
    jc.setBounds((int) Math.round(area.getX()), (int) Math.round(area.getY()),
            (int) Math.round(area.getWidth()), (int) Math.round(area.getHeight()));

    return null;
}

From source file:WorldMapGraphDemo.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features./*ww  w  .  jav a2  s .  c om*/
 * 
 */
public WorldMapGraphDemo() {
    setLayout(new BorderLayout());

    map.put("TYO", new String[] { "35 40 N", "139 45 E" });
    map.put("PEK", new String[] { "39 55 N", "116 26 E" });
    map.put("MOW", new String[] { "55 45 N", "37 42 E" });
    map.put("JRS", new String[] { "31 47 N", "35 13 E" });
    map.put("CAI", new String[] { "30 03 N", "31 15 E" });
    map.put("CPT", new String[] { "33 55 S", "18 22 E" });
    map.put("PAR", new String[] { "48 52 N", "2 20 E" });
    map.put("LHR", new String[] { "51 30 N", "0 10 W" });
    map.put("HNL", new String[] { "21 18 N", "157 51 W" });
    map.put("NYC", new String[] { "40 77 N", "73 98 W" });
    map.put("SFO", new String[] { "37 62 N", "122 38 W" });
    map.put("AKL", new String[] { "36 55 S", "174 47 E" });
    map.put("BNE", new String[] { "27 28 S", "153 02 E" });
    map.put("HKG", new String[] { "22 15 N", "114 10 E" });
    map.put("KTM", new String[] { "27 42 N", "85 19 E" });
    map.put("IST", new String[] { "41 01 N", "28 58 E" });
    map.put("STO", new String[] { "59 20 N", "18 03 E" });
    map.put("RIO", new String[] { "22 54 S", "43 14 W" });
    map.put("LIM", new String[] { "12 03 S", "77 03 W" });
    map.put("YTO", new String[] { "43 39 N", "79 23 W" });

    cityList = new ArrayList<String>(map.keySet());

    // create a simple graph for the demo
    graph = new DirectedSparseMultigraph<String, Number>();
    createVertices();
    createEdges();

    ImageIcon mapIcon = null;
    String imageLocation = "/images/political_world_map.jpg";
    try {
        mapIcon = new ImageIcon(getClass().getResource(imageLocation));
    } catch (Exception ex) {
        System.err.println("Can't load \"" + imageLocation + "\"");
    }
    final ImageIcon icon = mapIcon;

    Dimension layoutSize = new Dimension(2000, 1000);

    Layout<String, Number> layout = new StaticLayout<String, Number>(graph,
            new ChainedTransformer(new Transformer[] { new CityTransformer(map),
                    new LatLonPixelTransformer(new Dimension(2000, 1000)) }));

    layout.setSize(layoutSize);
    vv = new VisualizationViewer<String, Number>(layout, new Dimension(800, 400));

    if (icon != null) {
        vv.addPreRenderPaintable(new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform oldXform = g2d.getTransform();
                AffineTransform lat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.LAYOUT).getTransform();
                AffineTransform vat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.VIEW).getTransform();
                AffineTransform at = new AffineTransform();
                at.concatenate(g2d.getTransform());
                at.concatenate(vat);
                at.concatenate(lat);
                g2d.setTransform(at);
                g.drawImage(icon.getImage(), 0, 0, icon.getIconWidth(), icon.getIconHeight(), vv);
                g2d.setTransform(oldXform);
            }

            public boolean useTransform() {
                return false;
            }
        });
    }

    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    add(panel);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse();
    vv.setGraphMouse(graphMouse);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    //        vv.scaleToLayout(scaler);

    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());
        }
    });

    JButton reset = new JButton("reset");
    reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity();
            vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).setToIdentity();
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    add(controls, BorderLayout.SOUTH);
}