[size=18] [/size][u]boum galera tou tendo dificuldade em fazer a rotação de um quadrado, alguem tem uma ideia como faço isso, eu tentei fazer mais ñ tá dando certo[/u]
[code]// função quadrado
import java.awt.Graphics;
import javax.swing.JOptionPane;
public class Translação extends javax.swing.JFrame {
int x1, y1, x2, y2;// variaveis das coordenadas
int t, raio, graus, a, b; //Variaveis de controle
double sen, cos, rad, z1, w1, z2, w2;
//private int vx[];
String dados;//variavel responsavel para pegar os dados
public Translação() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Desenhar_Quadrado = new javax.swing.JButton();
Cima = new javax.swing.JButton();
Baixo = new javax.swing.JButton();
Direita = new javax.swing.JButton();
Esquerdo = new javax.swing.JButton();
Rotaciona = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Desenhar_Quadrado.setText("Desenhar");
Desenhar_Quadrado.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Desenhar_QuadradoActionPerformed(evt);
}
});
Cima.setText("/\");
Cima.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CimaActionPerformed(evt);
}
});
Baixo.setText("\/");
Baixo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BaixoActionPerformed(evt);
}
});
Direita.setText(">");
Direita.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DireitaActionPerformed(evt);
}
});
Esquerdo.setText("<");
Esquerdo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EsquerdoActionPerformed(evt);
}
});
Rotaciona.setText("Rotacionar");
Rotaciona.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RotacionaActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(Desenhar_Quadrado)
.addGap(64, 64, 64)
.addComponent(Rotaciona)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
.addComponent(Esquerdo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Cima)
.addComponent(Baixo))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Direita)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(282, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Cima)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Baixo)
.addComponent(Desenhar_Quadrado)
.addComponent(Rotaciona))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Esquerdo)
.addGap(23, 23, 23))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Direita)
.addGap(23, 23, 23))))
);
pack();
}// </editor-fold>
private void Desenhar_QuadradoActionPerformed(java.awt.event.ActionEvent evt) {
// bloco para pegar os dados que são inserido pelo usuario
dados = JOptionPane.showInputDialog("Digite do Eixo X1: ");
x1 = Integer.parseInt(dados);
//dados="";
dados = JOptionPane.showInputDialog("Digite do Eixo Y1: ");
y1 = Integer.parseInt(dados);
//dados="";
//Usuário entra com o tamanho do Quadrado
dados = JOptionPane.showInputDialog("Digite o Tamanho do Quadrado: ");
t = Integer.parseInt(dados);
//dados="";
x2=x1+t;//atribui o valor para x2
y2=y1+t;//atribui o valor para y2
Quadrado( x1, y1, x2, y2);
}
// dados para translação
private void CimaActionPerformed(java.awt.event.ActionEvent evt) {
Quadrado( x1, y1=y1-10, x2, y2=y2-10);
repaint();
}
private void BaixoActionPerformed(java.awt.event.ActionEvent evt) {
Quadrado( x1, y1=y1+10, x2, y2=y2+10);
repaint();
}
private void DireitaActionPerformed(java.awt.event.ActionEvent evt) {
Quadrado( x1=x1+10, y1, x2=x2+10, y2);
repaint();
}
private void EsquerdoActionPerformed(java.awt.event.ActionEvent evt) {
Quadrado( x1=x1-10, y1, x2=x2-10, y2);
repaint();
}
//função para rotação
private void RotacionaActionPerformed(java.awt.event.ActionEvent evt) {
dados = JOptionPane.showInputDialog("Digite o Angulo da Rotação: ");
graus = Integer.parseInt(dados);
rad = Math.toDegrees(graus);
sen = Math.sin(rad);
cos = Math.cos(rad);
sen = sen*-1;
cos = cos*-1;
z1 = (x1*cos - y1*sen);
z2 = (x2*cos - y2*sen);
w1 = (x1*sen + y1*cos);
w2 = (x2*sen + y2*cos);
x1 = (int) z1;
x2 = (int) z2;
y1 = (int) w1;
y2 = (int) w2;
JOptionPane.showMessageDialog(null, "z1= "+z1+", z2= "+z2+", w1= "+w1+", w2= "+w2+"\n"+
"x1= "+x1+", x2= "+x2+", y1= "+y1+", y2= "+y2+"\nsen= "+sen+", cos= "+cos);
Quadrado(x1,y1,x2,y2);
// repaint();
}
public void Quadrado(int x1, int y1, int x2, int y2){
int x, y;//variaveis auxiliares
Graphics graphics = getGraphics();
for (x=x1;x<=x2;x++){//Desenha reta pixel a pixel rastreando por x
graphics.drawLine(x,y1,x,y1);
graphics.drawLine(x,y2,x,y2);
}
for(y=y1;y<=y2;y++){//Desenha reta pixel a pixel rastreando por y
graphics.drawLine(x1,y,x1,y);
graphics.drawLine(x2,y,x2,y);
}
}
//método que desenha o objeto
public void paint(Graphics g){
super.paint(g);
Quadrado( x1, y1, x2, y2);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Translação().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Baixo;
private javax.swing.JButton Cima;
private javax.swing.JButton Desenhar_Quadrado;
private javax.swing.JButton Direita;
private javax.swing.JButton Esquerdo;
private javax.swing.JButton Rotaciona;
// End of variables declaration
}
[/code]