Desighn Pattern
parent
e6f2dc08a9
commit
a7df6f9de5
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,52 @@
|
||||||
|
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.Singaleton;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* - verhindert, dass von einer Klasse mehr als ein Objekt erstellt werden kann
|
||||||
|
* Das wird dadurch erreicht, dass das gewünschte Objekt in einer Klasse selbst erzeugt dann als statische Instanz abgerufen wird
|
||||||
|
*
|
||||||
|
* Eigenschaften:
|
||||||
|
* - Konstruktor muss als „private“ deklariert.
|
||||||
|
* - Eine öffentliche statische Methode, die die Instanz der Klasse zurückgib
|
||||||
|
* - private static memberVariable
|
||||||
|
*/
|
||||||
|
public class Singleton {
|
||||||
|
|
||||||
|
// privater Konstruktor mit Zugriffsschutz von außen, also die kann nicht mit new erzeugt werden
|
||||||
|
private Singleton() {}
|
||||||
|
|
||||||
|
// vor Zugriff von außen geschützt und statisch
|
||||||
|
private static Singleton erzeuge_Objekt = null;
|
||||||
|
|
||||||
|
|
||||||
|
// öffentliche Methode, Aufruf durch Code
|
||||||
|
// Aber Thread_unsicher
|
||||||
|
public static Singleton getObejekt_Thread_unsicher() {
|
||||||
|
|
||||||
|
if (erzeuge_Objekt == null)
|
||||||
|
erzeuge_Objekt = new Singleton();
|
||||||
|
return erzeuge_Objekt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thread_sicher:
|
||||||
|
public static synchronized Singleton getObejekt_Thread_sicher() {
|
||||||
|
if (erzeuge_Objekt == null) {
|
||||||
|
erzeuge_Objekt = new Singleton();
|
||||||
|
}
|
||||||
|
return erzeuge_Objekt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// test
|
||||||
|
public void print() {
|
||||||
|
System.out.println("Hallo Welt!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// Verwendung Test
|
||||||
|
Singleton objekt = Singleton.getObejekt_Thread_unsicher();
|
||||||
|
objekt.print(); // Hallo Welt!
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package Übungen.Flappy_Bird;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
|
public class DrawImages extends Rectangle {
|
||||||
|
|
||||||
|
Image image;
|
||||||
|
int y_pos;
|
||||||
|
int x_pos;
|
||||||
|
int speed = 5;
|
||||||
|
|
||||||
|
DrawImages(int x, int y, int width, int height, Image image) {
|
||||||
|
super(x, y, width, height);
|
||||||
|
this.image = image;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Graphics g) {
|
||||||
|
g.drawImage(image, x, y, width, height, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move() {
|
||||||
|
y += y_pos;
|
||||||
|
x += x_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_SPACE)
|
||||||
|
set_y(-speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_SPACE)
|
||||||
|
set_y(speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set_y(int y) {
|
||||||
|
y_pos = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set_x(int x) {
|
||||||
|
x_pos = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package Übungen.Flappy_Bird;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GameFrame extends JFrame {
|
||||||
|
|
||||||
|
GameFrame(){
|
||||||
|
this.add(new GamePanel());
|
||||||
|
this.setTitle("Pong Game");
|
||||||
|
this.setResizable(false);
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
this.pack();
|
||||||
|
this.setLocationRelativeTo(null);
|
||||||
|
this.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,101 @@
|
||||||
|
package Übungen.Flappy_Bird;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GamePanel extends JPanel implements Runnable {
|
||||||
|
|
||||||
|
final static int WIDTH_WINDOW = 600;
|
||||||
|
final static int HEIGH_WINDOW = 900;
|
||||||
|
Dimension fensterSize = new Dimension(WIDTH_WINDOW, HEIGH_WINDOW);
|
||||||
|
|
||||||
|
DrawImages world;
|
||||||
|
DrawImages flappy;
|
||||||
|
DrawImages ground1,ground2;
|
||||||
|
|
||||||
|
Thread game;
|
||||||
|
|
||||||
|
boolean gameOver;
|
||||||
|
|
||||||
|
GamePanel() {
|
||||||
|
drawImage();
|
||||||
|
this.setFocusable(true);
|
||||||
|
this.setPreferredSize(fensterSize);
|
||||||
|
this.addKeyListener(new Steuern());
|
||||||
|
this.gameOver = false;
|
||||||
|
game = new Thread(this);
|
||||||
|
game.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawImage() {
|
||||||
|
world = new DrawImages(0, 0, WIDTH_WINDOW, HEIGH_WINDOW, new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Flappy_Bird\\world.png").getImage());
|
||||||
|
flappy = new DrawImages(50, HEIGH_WINDOW / 2, 50, 50, new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Flappy_Bird\\flappy Bird.png").getImage());
|
||||||
|
// Zwei Bodenbilder nebeneinander zeichnen
|
||||||
|
ground1 = new DrawImages(0, HEIGH_WINDOW - 100, WIDTH_WINDOW, 200, new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Flappy_Bird\\ground.png").getImage());
|
||||||
|
ground2 = new DrawImages(WIDTH_WINDOW, HEIGH_WINDOW - 100, WIDTH_WINDOW, 200, new ImageIcon("C:\\Users\\obaya\\git\\Programmierung2\\Programmierung2\\src\\Übungen\\Flappy_Bird\\ground.png").getImage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paint(Graphics g) {
|
||||||
|
super.paint(g);
|
||||||
|
world.draw(g);
|
||||||
|
flappy.draw(g);
|
||||||
|
ground1.draw(g);
|
||||||
|
ground2.draw(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move() {
|
||||||
|
flappy.move();
|
||||||
|
move_ground();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move_ground() {
|
||||||
|
ground1.x -= 3;
|
||||||
|
ground2.x -=3;
|
||||||
|
|
||||||
|
// Sobald das erste Bodenbild komplett aus dem Bildschirm verschwindet
|
||||||
|
if (ground1.x + ground1.width <= 0) {
|
||||||
|
ground1.x = ground2.x + ground2.width ; // Setze es rechts vom zweiten Bild
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sobald das zweite Bodenbild komplett aus dem Bildschirm verschwindet
|
||||||
|
if (ground2.x + ground2.width <= 0) {
|
||||||
|
ground2.x = ground1.x + ground1.width; // Setze es rechts vom ersten Bild
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkKollision() {
|
||||||
|
if(flappy.intersects(ground1))
|
||||||
|
gameOver = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (!gameOver) {
|
||||||
|
try {
|
||||||
|
move();
|
||||||
|
checkKollision();
|
||||||
|
repaint();
|
||||||
|
Thread.sleep(10);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Steuern extends KeyAdapter {
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
flappy.keyPressed(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
flappy.keyReleased(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package Übungen.Flappy_Bird;
|
||||||
|
|
||||||
|
public class GamePlay {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new GameFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
Binary file not shown.
After Width: | Height: | Size: 386 B |
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
Loading…
Reference in New Issue