import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.LinkedList;
import javax.swing.JOptionPane;
public class PaintPanel extends JPanel implements KeyListener{
public static int[][] matrix;
public final static int MAX_WIDTH = 1000, MAX_HEIGHT = 600;
private final int DELAY = 0;
private Flugzeug flugzeug1, flugzeug2;
private Timer timer1, timer2;
private int fps, fps_alt;
public PaintPanel(){
matrix = new int[MAX_WIDTH][MAX_HEIGHT];
timer1 = new Timer(DELAY, new TimerListener1());
timer2 = new Timer(1000, new TimerListener2());
Point p1 = new Point(80,MAX_HEIGHT/2);
Point p2 = new Point(MAX_WIDTH-80,MAX_HEIGHT/2);
flugzeug1 = new Flugzeug(p1,10000,0,1, matrix);
flugzeug2 = new Flugzeug(p2,20000,180,1, matrix);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
flugzeug1.setGraphics(getGraphics()); // Graphik neu übergeben, erst hier möglich!
flugzeug2.setGraphics(getGraphics()); // Graphik neu übergeben, erst hier möglich!
timer1.start(); // Timer starten (vorher nicht möglich da erst die Graphik übergeben werden muss)
timer2.start(); // Timer starten (vorher nicht möglich da erst die Graphik übergeben werden muss)
setBackground(Color.white);
// Begrenzung
for (int x = 0; x <= (MAX_WIDTH)-1; x++){
matrix[x][0] = 1;
matrix[x][1] = 1;
matrix[x][2] = 1;
matrix[x][3] = 1;
matrix[x][MAX_HEIGHT-1] = 1;
matrix[x][MAX_HEIGHT-2] = 1;
matrix[x][MAX_HEIGHT-3] = 1;
matrix[x][MAX_HEIGHT-4] = 1;
}
for (int y = 0; y <= (MAX_HEIGHT)-1; y++){
matrix[0][y] = 1;
matrix[1][y] = 1;
matrix[2][y] = 1;
matrix[3][y] = 1;
matrix[MAX_WIDTH-1][y] = 1;
matrix[MAX_WIDTH-2][y] = 1;
matrix[MAX_WIDTH-3][y] = 1;
matrix[MAX_WIDTH-4][y] = 1;
}
for (int x = 0; x <= MAX_WIDTH-1; x++){
for (int y = 0; y <= MAX_HEIGHT-1; y++){
int wert = matrix[x][y];
if (wert == 1){
g.setColor(Color.black);
g.drawRect(x,y,0,0);
}
}
}
}
public void keyPressed(KeyEvent event){
if (event.getKeyCode() == KeyEvent.VK_P){
if (timer1.isRunning()){
timer1.stop();
timer2.stop();
} else {
timer1.start();
timer2.start();
}
}
// Flugzeug 1:
if (event.getKeyCode() == KeyEvent.VK_UP){
flugzeug1.setSpeed_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_DOWN){
flugzeug1.setBreak_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_LEFT){
flugzeug1.setLeft_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_RIGHT){
flugzeug1.setRight_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_SPACE){
flugzeug1.setFire1_key_pressed(true);
}
// Flugzeug 2:
if (event.getKeyCode() == KeyEvent.VK_W){
flugzeug2.setSpeed_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_S){
flugzeug2.setBreak_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_A){
flugzeug2.setLeft_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_D){
flugzeug2.setRight_key_pressed(true);
}
if (event.getKeyCode() == KeyEvent.VK_CONTROL){
flugzeug2.setFire1_key_pressed(true);
}
}
public void keyReleased(KeyEvent event){
// Flugzeug 1:
if (event.getKeyCode() == KeyEvent.VK_UP){
flugzeug1.setSpeed_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_DOWN){
flugzeug1.setBreak_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_LEFT){
flugzeug1.setLeft_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_RIGHT){
flugzeug1.setRight_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_SPACE){
flugzeug1.setFire1_key_pressed(false);
}
// Flugzeug 2:
if (event.getKeyCode() == KeyEvent.VK_W){
flugzeug2.setSpeed_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_S){
flugzeug2.setBreak_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_A){
flugzeug2.setLeft_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_D){
flugzeug2.setRight_key_pressed(false);
}
if (event.getKeyCode() == KeyEvent.VK_CONTROL){
flugzeug2.setFire1_key_pressed(false);
}
}
public void keyTyped(KeyEvent event){}
private class TimerListener1 implements ActionListener{
public void actionPerformed(ActionEvent event){
fps++;
flugzeug1.moveFlugzeug(); // Flugzeug 1 bewegen
flugzeug2.moveFlugzeug(); // Flugzeug 2 bewegen
double schaden1 = flugzeug1.moveWeapon1(); // Geschosse von Flugzeug1 bewegen und angerichteten Schaden speichern
if (schaden1 != 0){
flugzeug2.damaged(schaden1);
}
double schaden2 = flugzeug2.moveWeapon1(); // Geschosse von Flugzeug2 bewegen und angerichteten Schaden speichern
if (schaden2 != 0){
flugzeug1.damaged(schaden2);
}
flugzeug1.paintStatus(new Point(20,20)); // Status Flugzeug 1 zeichnen
flugzeug2.paintStatus(new Point(MAX_WIDTH-100,20)); // Status Flugzeug2 zeichnen
}
}
private class TimerListener2 implements ActionListener{
public void actionPerformed(ActionEvent event){
// fps:
Graphics g = getGraphics();
if (fps != fps_alt){
g.setColor(Color.white);
g.drawString("fps: " + fps_alt,20,MAX_HEIGHT-20);
fps_alt = fps;
}
g.setColor(Color.black);
g.drawString("fps: " + fps,20,MAX_HEIGHT-20);
fps = 0;
}
}
}