Results 1 to 3 of 3
  1. #1
    hawx is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default how to rotate a figure in java

    hi all, they look at is that I'm trying to simulate a tangram and so far I've gotten difrentes transladar the figures but I failed to rotate, if anyone can help me review the program and is helping me to rotate the agrdeceria because I have not achieved

    thanks


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Panel;
    import java.awt.Point;
    import java.awt.Polygon;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    
    public class Figura extends JPanel {
    
    private Polygon[] polygons = {};
    private Polygon selected = null;
    private Point last = null;
    private float rot;
    
    public Figura() {
    
    rot = (float)Math.PI/2;
    polygons = new Polygon[3];
    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    onMouseDown(e);
    Graphics2D g = (Graphics2D)getGraphics();
    int x = e.getX(); // x-coordinate where user clicked.
    int y = e.getY();
    if(e.isMetaDown()){
    polygons[0].contains(x, y);
    g.rotate(5);
    //g.setColor(Color.blue);
    //g.fillOval( x - 25, y - 15, 60, 30 );
    //g.setColor(Color.black);
    //g.drawOval( x - 25, y - 15, 60, 30 );
    
    }
    }
    public void mouseReleased(MouseEvent e) {
    onMouseUp(e);
    }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
    onMouseDragged(e);
    }
    });
    }
    
    protected void onMouseDown(MouseEvent e) {
    last = e.getPoint();
    for (int i = 0; i < polygons.length; i++) {
    if (polygons[i].contains(last)) {
    selected = polygons[i];
    return;
    }
    }
    selected = null;
    }
    
    protected void onMouseDragged(MouseEvent e) {
    Point now = e.getPoint();
    if (last != null) {
    int xtrans = now.x - last.x;
    int ytrans = now.y - last.y;
    if (selected != null) {
    selected.translate(xtrans, ytrans);
    repaint();
    }
    last = now;
    }
    }
    
    protected void onMouseUp(MouseEvent e) {
    last = null;
    selected = null;
    repaint();
    }
    
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.blue);
    
    for (int i = 0; i < polygons.length; i++) {
    g2.drawPolygon(polygons[i]);
    //g2.rotate(rot, this.getWidth()/2, this.getHeight()/2);
    }
    }
    
    public Polygon[] getPolygons() {
    return polygons;
    }
    
    public void setPolygons(Polygon[] polygons) {
    this.polygons = polygons;
    }
    
    
    public void triangulo(){
    int xVals[] = { 108, 80,135};
    int yVals[] = { 340, 380, 380 };
    //Polygon[] polys = new Polygon[1];
    //for (int i = 0; i < polygons.length; i++) {
    polygons[0] = new Polygon(xVals, yVals, 3);
    
    //}
    
    }
    
    public void cuadrado(){
    int polx[] = {208, 208,258,258,258};
    int poly[] = {340, 390, 340, 390,390};
    //Polygon[] polys = new Polygon[1];
    // for (int i = 0; i < polygons.length; i++) {
    polygons[1] = new Polygon(polx, poly, 4);
    // }
    
    }
    
    
    public void paralelogramo(){
    
    int polx[]={330, 380 , 480, 430};
    int poly[]={100, 280, 280 ,330};
    polygons[2]= new Polygon(polx, poly, 4);
    }
    
    
    
    public static void main(String[] args) {
    Figura panel = new Figura();
    
    panel.triangulo();
    panel.cuadrado();
    panel.paralelogramo();
    panel.setPolygons(panel.polygons);
    
    
    JPanel panelImagenes = new JPanel();
    TitledBorder border = BorderFactory.createTitledBorder( "Resultados" );
    panelImagenes.setLayout(new BorderLayout());
    panel.add(panelImagenes);
    
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800, 500);
    frame.setContentPane(panel);
    frame.show();
    }
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Check out AffineTransform and its method createTransformedShape

    db

  3. #3
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    The next example will rotate a picture 5 degrees at a time. We are using the Java2D package (and Swing).

    --------------------
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class RotatePanel extends JPanel {
    private Image image;
    private double currentAngle;

    public RotatePanel(Image image) {
    this.image = image;
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 0);
    try {
    mt.waitForID(0);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void rotate() {
    //rotate 5 degrees at a time
    currentAngle+=5.0;
    if (currentAngle >= 360.0) {
    currentAngle = 0;
    }
    repaint();
    }
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform)(origXform.clone());
    //center of rotation is center of the panel
    int xRot = this.getWidth()/2;
    int yRot = this.getHeight()/2;
    newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
    g2d.setTransform(newXform);
    //draw image centered in panel
    int x = (getWidth() - image.getWidth(this))/2;
    int y = (getHeight() - image.getHeight(this))/2;
    g2d.drawImage(image, x, y, this);
    g2d.setTransform(origXform);
    }

    public Dimension getPreferredSize() {
    return new Dimension (image.getWidth(this), image.getHeight(this));
    }


    public static void main(String[] args) {
    JFrame f = new JFrame();
    Container cp = f.getContentPane();
    cp.setLayout(new BorderLayout());
    Image testImage =
    Toolkit.getDefaultToolkit().getImage("c:/temp/gumby.gif");
    final RotatePanel rotatePanel = new RotatePanel(testImage);
    JButton b = new JButton ("Rotate");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    rotatePanel.rotate();
    }
    });
    cp.add(rotatePanel, BorderLayout.CENTER);
    cp.add(b, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
    }
    }
    ------------

Similar Threads

  1. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 04:12 AM
  2. Basic Java applet error, cant figure it out!
    By alacn in forum Java Applets
    Replies: 3
    Last Post: 08-03-2010, 06:25 PM
  3. Java rotate(); problem
    By Kasp3r in forum Java 2D
    Replies: 4
    Last Post: 06-03-2010, 10:58 AM
  4. Image rotate
    By Micromani in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2010, 01:33 PM
  5. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •