Foros del Web » Programación para mayores de 30 ;) » Java »

como rotar una figura en java

Estas en el tema de como rotar una figura en java en el foro de Java en Foros del Web. hola a todos , pues miren es que estoy tratando de simular un tangram y hasta el momento he conseguido transladar las difrentes figuras pero ...
  #1 (permalink)  
Antiguo 14/03/2011, 22:34
 
Fecha de Ingreso: octubre-2009
Mensajes: 29
Antigüedad: 14 años, 6 meses
Puntos: 0
como rotar una figura en java

hola a todos , pues miren es que estoy tratando de simular un tangram y hasta el momento he conseguido transladar las difrentes figuras pero no he logrado que roten , si alguien me puede ayudar examinando el programa y ayudandome a rotar se los agrdeceria pues no lo he conseguido

gracias

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

}

Etiquetas: rotar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:22.