//******************************************************************************
// Rotate.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;

//==============================================================================
// Main Class for applet Rotate
//
//==============================================================================
public class Rotate extends Applet
{
	// Schieber, um Winkel auszuwaehlen
	Scrollbar sbWinkel = new Scrollbar(Scrollbar.HORIZONTAL,0, 15, 0, 360); 

	// Rechteck in UV-Koordinaten
	final int iXWerte[]={0,0,40,40};
	final int iYWerte[]={0,20,20,0};
	Polygon pUVRechteck = new Polygon(iXWerte, iYWerte,4);
	final static int xm = 30, ym = -10;

	//  Das zu zeichnende Rechteck
	Polygon pXYRechteck = translate(pUVRechteck, 0);
	
	
	// Rotate Class Constructor
	//--------------------------------------------------------------------------
	public Rotate()
	{
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: Rotate\r\n" +
		       "Author: sdteffen\r\n" +
		       "Created with Microsoft Visual J++ Version 1.0";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{
        resize(400, 300);

		setLayout(new BorderLayout());
		// Schieberegler in das Applet einfuegen		
		add("North", sbWinkel);
				
	}
	
	// Neuer Winkel eingestellt
	public boolean handleEvent(Event event) 
	{
       if (event.target == sbWinkel)
	   { 
           	pXYRechteck = translate(pUVRechteck,sbWinkel.getValue());;
			repaint();
		   return true;
       } 
	   else 
	   {
           return super.handleEvent(event);
       }
    }    

	// Rotate Paint Handler
	//--------------------------------------------------------------------------
	public void paint(Graphics g)
	{
		g.drawString("Winkel: "+new Integer(sbWinkel.getValue()).toString()+"°", 10,40);
		//Achsen
		g.drawLine(75,100,125,100);
		g.drawLine(100,75,100,125);
		g.fillPolygon(pXYRechteck);
	}
	
	//Translation von UV- in XY-Koordinaten
	static public Polygon translate(Polygon uvpoly, int Winkel)
	{
		// this we will return
		Polygon xypoly = new Polygon();
		
		// Winkel im Bogenmass
		// Um mathematische Koordinaten (Winkel im Gegenuhrzeigersinn)
		// mit den Computerkoordinaten in Einklang zu bringen "2*Pi -" eingefügt 
		double alpha = 2*Math.PI - 2 * Math.PI * Winkel / 360;
		
		//alle Punkte des Polygons ueberfuehren
		for(int i=0; i < uvpoly.npoints;i++)
		{
		   xypoly.addPoint(new Double(Math.cos(alpha)*(uvpoly.xpoints[i]+xm) - Math.sin(alpha)*(uvpoly.ypoints[i]+ym)+ 100).intValue(),
						   new Double(Math.sin(alpha)*(uvpoly.xpoints[i]+xm) + Math.cos(alpha)*(uvpoly.ypoints[i]+ym)+ 100).intValue());

		}
		return xypoly;
	}
}
