import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.*;
public class MyFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Rotation eines Dreiecks");
MyPanel panel = new MyPanel();
frame.getContentPane().add(panel);
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Dreieck d;
public MyPanel()
{
super();
d = new Dreieck(this);
d.start();
}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, 500, 500);
if(d != null)
{
double[] xkoords = d.getXkoords();
double[] ykoords = d.getYkoords();
Polygon polygonen = new Polygon();
polygonen.addPoint((int)xkoords[0], (int)ykoords[0]);
polygonen.addPoint((int)xkoords[1], (int)ykoords[1]);
polygonen.addPoint((int)xkoords[2], (int)ykoords[2]);
g.setColor(Color.RED);
g.fillPolygon(polygonen);
}
}
}
class Dreieck extends Thread
{
private double winkel= 90;
private double xkoordSpitze;
private double xkoordLinks;
private double xkoordRechts;
private double ykoordSpitze;
private double ykoordLinks;
private double ykoordRechts;
//========================================= Original-Koordinaten
private double xkoordSpitzeOrig;
private double xkoordLinksOrig;
private double xkoordRechtsOrig;
private double ykoordSpitzeOrig;
private double ykoordLinksOrig;
private double ykoordRechtsOrig;
private int speed = 0;
private MyPanel panel;
public Dreieck(MyPanel panel)
{
this.panel = panel;
//========================================= Original-Koordinaten bekommen Werte
xkoordSpitzeOrig = 65;
xkoordLinksOrig = 45;
xkoordRechtsOrig = 85;
ykoordSpitzeOrig = 50;
ykoordLinksOrig = 70;
ykoordRechtsOrig = 70;
xkoordSpitze = xkoordSpitzeOrig;
xkoordLinks = xkoordLinksOrig;
xkoordRechts = xkoordRechtsOrig;
ykoordSpitze = ykoordSpitzeOrig;
ykoordLinks = ykoordLinksOrig;
ykoordRechts = ykoordRechtsOrig;
}
/**
* Gibt die x-Koordinate des Schiffes zurueck
*
* @return xkoord double
*/
public double[] getXkoords()
{
double[] xkoords = new double[3];
xkoords[0] = xkoordSpitze;
xkoords[1] = xkoordLinks;
xkoords[2] = xkoordRechts;
return xkoords;
}
/**
* Gibt die y-Koordinate des Schiffes zurueck
*
* @return ykoord double
*/
public double[] getYkoords()
{
double[] ykoords = new double[3];
ykoords[0] = ykoordSpitze;
ykoords[1] = ykoordLinks;
ykoords[2] = ykoordRechts;
return ykoords;
}
public void turnLeft()
{
winkel -= 5;
rotate();
panel.repaint();
}
public void rotate()
{
//========================================= Polygon auf Basis der Original-Koordinaten erstellen
Polygon polygon = new Polygon();
polygon.addPoint((int)xkoordSpitzeOrig, (int)ykoordSpitzeOrig);
polygon.addPoint((int)xkoordLinksOrig, (int)ykoordLinksOrig);
polygon.addPoint((int)xkoordRechtsOrig, (int)ykoordRechtsOrig);
double x = (polygon.xpoints[0] + polygon.xpoints[1] + polygon.xpoints[2])/3.0;
double y = (polygon.ypoints[0] + polygon.ypoints[1] + polygon.ypoints[2])/3.0;
AffineTransform at = new AffineTransform();
at.setToRotation(Math.toRadians(winkel), x,y);
for (int i=0; i<polygon.npoints; i++)
{
Point2D.Double p = new Point2D.Double(polygon.xpoints[i], polygon.ypoints[i]);
at.transform(p,p);
polygon.xpoints[i] = (int)p.x;
polygon.ypoints[i] = (int)p.y;
}
//========== Das, was gezeichnet wird, sind nach wie vor die xkoordSpitze usw.
// Die Original-Koordinaten werden immer nur verwendet, um am Anfang das un-gedrehte Polygon zu erstellen
xkoordSpitze = polygon.xpoints[0];
xkoordLinks = polygon.xpoints[1];
xkoordRechts = polygon.xpoints[2];
ykoordSpitze = polygon.ypoints[0];
ykoordLinks = polygon.ypoints[1];
ykoordRechts = polygon.ypoints[2];
}
public void turnRight()
{
winkel += 5;
rotate();
panel.repaint();
}
public void erhoeheSpeed()
{
speed += 1;
}
public void run()
{
try
{
Thread.sleep(2000);
for(int i = 0; i < 20; i++)
{
turnRight();
Thread.sleep(200);
}
Thread.sleep(3000);
for(int j = 0; j < 20; j++)
{
turnLeft();
Thread.sleep(200);
}
}
catch(InterruptedException ie)
{
}
}
}