viernes, 31 de agosto de 2018

Rotacion de una Figura en Java con netbeans

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 *
 * @author Duraznilla
 */
    public class Aspas extends JPanel {
 
        //En esta parte son las posiciones guardadas que van a ir colocadas en startAngle
  int x0[] = new int[]{70,-110,-25,-200};
   
 
  public void paint(Graphics g){
    
       super.paint(g);
         Graphics2D Dibujo=(Graphics2D)g;
         AffineTransform at = Dibujo.getTransform();
           int x = getWidth() / 2;
           int y= getHeight() / 2;
           
          
       super.paint(Dibujo);
               //En esta parte puede moverse la figura donde sea cuando movemos nuestra ventana
              Dibujo.setTransform(at);
              at.rotate(Math.toRadians(180), x,y);
              Dibujo.setTransform(at);
        
      
       
            
        
        
         //Para hacer girar la figura le damos en el startAngle la posición que va a moverse con el siguiente Hilo
        
        
         try{
            
            Thread.sleep(100);
            g.setColor(Color.BLACK);
            //x,y,Width,Height,startAngle,ang
            g.fillArc(200, 200, 180,200, x0[0], 45);
            g.fillArc(200, 200, 180,200, x0[1], 45);
            //x,y,Width,Height,starangle,ang
            g.setColor(Color.MAGENTA);
            g.fillArc(200, 200, 180,200, x0[2],45);
            g.fillArc(200, 200, 180,200, x0[3],45);

            //Pociones, si lo ponemos negativo el valor de 10 o cualquier numero que quieran va a cambiar   la dirección de giro de la figura, en este código la figura gira a la izquierda.
            x0[0] += 10;
            x0[1] += 10;
            x0[2] += 10;
            x0[3] += 10;
            
          
            
         }catch(Exception ex){}
        
        
         //El repaint nos sirve para volver iniciar el pain
         repaint();
    }
   

  
   
    
public static void main(String[] args) {
         
  
      
       Aspas dibujo=new Aspas();
        JFrame ventana=new JFrame();
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ventana.add(dibujo);
        ventana.setSize(600,600);
        ventana.setVisible(true);
    
   
     }}