import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JLabel;
class LayoutManager extends Panel implements Runnable, KeyListener {
// Initialisierung der Variablen
private static final long serialVersionUID = 1L;
boolean running = true;
int appletsize_x = 1200; // Größe des Applets in x - Richtung
int appletsize_y = 1200; // Größe des Applets in y - Richtung
int v1 = 11;
int v2 = 17;
int v3 = 25;
int v4 = 13;
double t;
int Radius = 47;
int radius = 27;
int kradius = 10;
int kRadius = 20;
int radiusComet = 5;
double ex_a_e = 0.5;
double ex_b_e = 0.25;
double ex_a_m = 0.4;
double ex_b_m = 0.15;
double ex_a_s = 0.25;
double ex_b_s = 0.5;
int mittelP = 0;
int Schweif = 1;
int anzahlC = 25 * radiusComet;
int[] x1Comet = new int[anzahlC];
int[] y1Comet = new int[anzahlC];
int[] x2Comet = new int[anzahlC];
int[] y2Comet = new int[anzahlC];
int[] x3Comet = new int[anzahlC];
int[] y3Comet = new int[anzahlC];
int[] x4Comet = new int[anzahlC];
int[] y4Comet = new int[anzahlC];
int xm;
int ym;
int xs;
int ys;
int[] x = new int[5];
int[] y = new int[5];
double[] xd = new double[5];
double[] yd = new double[5];
BufferedImage buffer;
Graphics2D bufferG;
// Variablen für die Doppelpufferung
private Image dbImage;
private Graphics dbg;
Planets Merkur = new Planets(v3, t, kradius, ex_b_m, ex_a_m, 0);
Planets Venus = new Planets(v2, t, kRadius, ex_a_s, ex_a_s, 1);
Planets Earth = new Planets(v4, t, radius, ex_a_e, ex_b_e, 2);
Planets Mars = new Planets(v1, t, Radius, ex_a_m, ex_b_m, 3);
JButton button = new JButton("Tastaturereignisse");
JLabel l = new JLabel();
Thread th = new Thread(this);
BirdsEyeView bird;
// Side side;
Color braun;
int f = 0;
Panel centerPanel;
Panel buttons;
public LayoutManager() {
bird = new BirdsEyeView(appletsize_x, appletsize_y, v1, v2, v3, v4, t,
Radius, radius, kradius, kRadius, radiusComet, ex_a_e, ex_b_e,
ex_a_m, ex_b_m, ex_a_s, ex_b_s, mittelP, Schweif, anzahlC, xm,
ym, xs, ys, x, y, xd, yd);
bird.setSize(300, 300);
setLayout(new BorderLayout());
// add the two canvases
buttons = new Panel();
buttons.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
Color farbebg = new Color(0, 0, 0);
bird.setBackground(farbebg);
add(bird);
add(buttons, BorderLayout.NORTH);
Button SunB = new Button("Sun");
Button MerkurB = new Button("Merkur");
Button VenusB = new Button("Venus");
Button EarthB = new Button("Earth");
Button MarsB = new Button("Mars");
buttons.add(SunB);
buttons.add(MerkurB);
buttons.add(VenusB);
buttons.add(EarthB);
buttons.add(MarsB);
enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
enableEvents(AWTEvent.FOCUS_EVENT_MASK);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.KEY_EVENT_MASK);
// pack();
// setBackground(Color.black);
setPreferredSize(new Dimension(appletsize_x, appletsize_y));
// Thread th = new Thread(this);
th.start();
addKeyListener(this);
}
public boolean isFocusable() {
return true;
}
public void actionPerformed(ActionEvent event){
if (event.getActionCommand().equals("Sonne")){
System.out.println("SunButton");
}
if (event.getActionCommand().equals("Merkur")){
}
if (event.getActionCommand().equals("Venus")){
}
if (event.getActionCommand().equals("Erde")){
}
if (event.getActionCommand().equals("Mars")){
}
}
public void keyPressed(KeyEvent event) {
// Schweife
if (event.getKeyChar() == 'q') {
Schweif = 0;
System.out.println("q");
for (int k = 0; k <= anzahlC; k++) {
x1Comet[k] = 0;
y1Comet[k] = 0;
x2Comet[k] = 0;
y2Comet[k] = 0;
x3Comet[k] = 0;
y3Comet[k] = 0;
x4Comet[k] = 0;
y4Comet[k] = 0;
}
}
if (event.getKeyChar() == 'w') {
Schweif = 1;
System.out.println("w");
for (int k = 0; k < anzahlC; k++) {
x1Comet[k] = 0;
y1Comet[k] = 0;
x2Comet[k] = 0;
y2Comet[k] = 0;
x3Comet[k] = 0;
y3Comet[k] = 0;
x4Comet[k] = 0;
y4Comet[k] = 0;
}
}
if (event.getKeyChar() == 'e') {
Schweif = 2;
System.out.println("e");
}
// Thread Soppen und starten
if (event.getKeyChar() == 'f') {
// th.interrupt();
running = false;
System.out.println("finish");
}
if (event.getKeyChar() == 's') {
running = true;
th = new Thread(this);
th.start();
}
// Zentrum setzen
if (event.getKeyChar() == '0') {
running = false;
mittelP = 0;
while (th.isAlive()) {
System.out.print("");
}
running = true;
th = new Thread(this);
th.start();
}
if (event.getKeyChar() == '1') {
running = false;
mittelP = 1;
while (th.isAlive()) {
System.out.print("alive ");
}
running = true;
th = new Thread(this);
th.start();
}
if (event.getKeyChar() == '2') {
running = false;
mittelP = 2;
while (th.isAlive()) {
System.out.print("alive ");
}
running = true;
th = new Thread(this);
th.start();
}
if (event.getKeyChar() == '3') {
running = false;
mittelP = 3;
while (th.isAlive()) {
System.out.print("alive ");
}
running = true;
th = new Thread(this);
th.start();
}
this.bird.Schweif = this.Schweif;
System.out.println(event.getKeyChar());
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
}
public void run() {
// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// BufferedImage
this.buffer = new BufferedImage(appletsize_x, appletsize_y,
Transparency.TRANSLUCENT);
this.bufferG = buffer.createGraphics();
this.bufferG.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
setPreferredSize(new Dimension(appletsize_x, appletsize_y));
// add("Center", centerPanel);
// add("South", side);
for (int j = 0; j <= anzahlC - 1; j++) {
x1Comet[j] = 0;
y1Comet[j] = 0;
x2Comet[j] = 0;
y2Comet[j] = 0;
x3Comet[j] = 0;
y3Comet[j] = 0;
x4Comet[j] = 0;
y4Comet[j] = 0;
}
// Solange true ist läuft der Thread weiter
while (running) {
t += 0.02;
// System.out.println(Schweif);
if (Schweif == 2) {
comet();
}
center(mittelP);
// System.out.println(t);
// Neuzeichnen des Applets
repaint();
bird.repaint();
try {
// Stoppen des Threads für in Klammern angegebene Millisekunden
Thread.sleep(20);
} catch (InterruptedException ex) {
// do nothing
}
// Zurücksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
if (th.interrupted()) {
break;
}
}
}
public void Input(int p) {
running = false;
mittelP = p;
while (th.isAlive()) {
System.out.print("alive ");
}
running = true;
th = new Thread(this);
th.start();
System.out.println("Input end");
}
public void center(int n) {
xd[1] = Merkur.OvalX(t);
yd[1] = Merkur.OvalY(t);
xd[2] = Venus.OvalX(t);
yd[2] = Venus.OvalY(t);
xd[3] = Earth.OvalX(t);
yd[3] = Earth.OvalY(t);
xd[4] = Mars.OvalX(t);
yd[4] = Mars.OvalY(t);
x[1] = (int) (Merkur.OvalX(t) + xd[n]);
y[1] = (int) (Merkur.OvalY(t) + yd[n]);
x[2] = (int) (Venus.OvalX(t) + xd[n]);
y[2] = (int) (Venus.OvalY(t) + yd[n]);
x[3] = (int) (Earth.OvalX(t) + xd[n]);
y[3] = (int) (Earth.OvalY(t) + yd[n]);
x[4] = (int) (Mars.OvalX(t) + xd[n]);
y[4] = (int) (Mars.OvalY(t) + yd[n]);
x[0] = (int) (xd[n] / 2);
y[0] = (int) (yd[n] / 2);
x[n] = 0;
y[n] = 0;
}
public void comet() {
for (int i = anzahlC - 1; 0 < i; i--) {
x1Comet[i] = x1Comet[i - 1];
y1Comet[i] = y1Comet[i - 1];
x2Comet[i] = x2Comet[i - 1];
y2Comet[i] = y2Comet[i - 1];
x3Comet[i] = x3Comet[i - 1];
y3Comet[i] = y3Comet[i - 1];
x4Comet[i] = x4Comet[i - 1];
y4Comet[i] = y4Comet[i - 1];
}
x1Comet[0] = x[1];
y1Comet[0] = y[1];
x2Comet[0] = x[2];
y2Comet[0] = y[2];
x3Comet[0] = x[3];
y3Comet[0] = y[3];
x4Comet[0] = x[4];
y4Comet[0] = y[4];
}
/**
* Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des
* Bildschirmflackerns
*/
public void update(Graphics g) {
// Initialisierung des DoubleBuffers
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// Bildschirm im Hintergrund löschen
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// Auf gelöschten Hintergrund Vordergrund zeichnen
dbg.setColor(getForeground());
paint(dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm
// anzeigen
g.drawImage(dbImage, 0, 0, this);
paint(g);
}
public void paint(Graphics g) {
/*
* g.setColor (braun);
*
* g.fillOval (appletsize_x/2 - 1, appletsize_y / 2 - 1, 20, 20);
*
*
* // BufferedImage bufferG.setColor (Color.red);
*
* bufferG.fillOval (x[1] + appletsize_x/2 - 1, y[1] + appletsize_y / 2
* - 1 -100, 2, 2);
*
* bufferG.setColor (Color.red);
*
* bufferG.fillOval (x[2] + appletsize_x/2 - 1, y[2] + appletsize_y / 2
* - 1 - 100, 2, 2);
*
* bufferG.setColor (Color.red);
*
* bufferG.fillOval (x[3] + appletsize_x/2 - 1, y[3] + appletsize_y / 2
* - 1 - 100, 2, 2);
*
* bufferG.setColor (Color.red);
*
* bufferG.fillOval (x[4] + appletsize_x/2 - 1, y[4] + appletsize_y / 2
* - 1 - 100, 2, 2);
*
* bufferG.setColor (Color.red);
*
* bufferG.fillOval (x[0] + appletsize_x/2 - 1, y[0] + appletsize_y / 2
* - 1 - 100, 2, 2);
*
*
* if (Schweif == 0) { Color braun = new Color(142,43,43);
*
* g.setColor (braun);
*
* g.fillOval (x[1] + appletsize_x/2 - 1, y[1] + appletsize_y / 2 - 1
* -100, 2, 2);
*
* g.setColor (Color.orange);
*
* g.fillOval (x[2] + appletsize_x/2 - 1, y[2] + appletsize_y / 2 - 1 -
* 100, 2, 2);
*
* g.setColor (Color.blue);
*
* g.fillOval (x[3] + appletsize_x/2 - 1, y[3] + appletsize_y / 2 - 1 -
* 100, 2, 2);
*
* g.setColor (Color.red);
*
* g.fillOval (x[4] + appletsize_x/2 - 1, y[4] + appletsize_y / 2 - 1 -
* 100, 2, 2);
*
* g.setColor (Color.yellow);
*
* g.fillOval (x[0] + appletsize_x/2 - 1, y[0] + appletsize_y / 2 - 1 -
* 100, 2, 2);
*
* } if (Schweif == 1) { Color braun = new Color(142,43,43);
*
* g.drawImage(buffer,0,0,null);
*
*
* g.setColor (braun);
*
* g.fillOval (x[1] + appletsize_x/2 - 2, y[1] + appletsize_y / 2 - 2
* -100, 4, 4);
*
* g.setColor (Color.orange);
*
* g.fillOval (x[2] + appletsize_x/2 - 2, y[2] + appletsize_y / 2 - 2 -
* 100, 4, 4);
*
* g.setColor (Color.blue);
*
* g.fillOval (x[3] + appletsize_x/2 - 2, y[3] + appletsize_y / 2 - 2 -
* 100, 4, 4);
*
* g.setColor (Color.red);
*
* g.fillOval (x[4] + appletsize_x/2 - 2, y[4] + appletsize_y / 2 - 2 -
* 100, 4, 4);
*
* g.setColor (Color.yellow);
*
* g.fillOval (x[0] + appletsize_x/2 - 2, y[0] + appletsize_y / 2 - 2 -
* 100, 4, 4); } if (Schweif == 2) { for(int i = 1; i < anzahlC; i++) {
* Color farbe = new Color(255 - (10/radiusComet) * i,0,(10/
* radiusComet) * i);
*
* g.setColor (farbe);
*
* //System.out.println(farbe);
*
* g.fillOval (x1Comet[i] - radiusComet + appletsize_x/2, y1Comet[i] -
* radiusComet + appletsize_y/2 + i/20 - 100, 2 * radiusComet - i/10, 2
* * radiusComet - i/10);
*
* g.fillOval (x2Comet[i] - radiusComet + appletsize_x/2, y2Comet[i] -
* radiusComet + appletsize_y/2 + i/20 - 100, 2 * radiusComet - i/10, 2
* * radiusComet - i/10);
*
* g.fillOval (x3Comet[i] - radiusComet + appletsize_x/2, y3Comet[i] -
* radiusComet + appletsize_y/2 + i/20 - 100, 2 * radiusComet - i/10, 2
* * radiusComet - i/10);
*
* g.fillOval (x4Comet[i] - radiusComet + appletsize_x/2, y4Comet[i] -
* radiusComet + appletsize_y/2 + i/20 - 100, 2 * radiusComet - i/10, 2
* * radiusComet - i/10);
*
* g.setColor (Color.yellow);
*
* g.fillOval (appletsize_x/2 - 5, appletsize_y / 2 - 5 - 100, 10, 10);
*
* }
*
*
*
* }
*/
}
}