PongGame
parent
a3080bf514
commit
353aed9497
|
@ -0,0 +1,125 @@
|
|||
package Übungen;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class PongGame {
|
||||
|
||||
static int xRect1 = 10;
|
||||
static int yRect1 = 10;
|
||||
static int bewegung = 1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame window = new JFrame();
|
||||
window.setSize(700, 700);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setTitle("PongGame");
|
||||
window.setLayout(null);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JPanel rect1 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g); // Sicherstellen, dass das Panel korrekt gezeichnet wird
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
}
|
||||
};
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
|
||||
|
||||
|
||||
Timer timer = new Timer(700, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() ==KeyEvent.VK_DOWN) {
|
||||
// Bewege das Rechteck
|
||||
yRect1 += bewegung ;
|
||||
|
||||
// Aktualisiere dann die Position des Panels
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
// Starte den Timer
|
||||
timer.start();
|
||||
|
||||
|
||||
|
||||
// Setze den Fokus auf das Fenster, um die KeyListener zu aktivieren
|
||||
window.setFocusable(true);
|
||||
window.requestFocusInWindow();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JPanel rect2 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
}
|
||||
};
|
||||
rect2.setBounds(650, 10, 10, 90);
|
||||
|
||||
window.add(rect1);
|
||||
window.add(rect2);
|
||||
|
||||
/*
|
||||
* JFrame besteht aus mehreren Schichten: Eine davon ist das ContentPane, welches der Bereich ist, in den die meisten Komponenten wie JPanel, JButton, etc.
|
||||
* eingefügt werden. Die Methode getContentPane() gibt dieses ContentPane zurück, das dann bearbeitet werden kann.
|
||||
* Hintergrundfarbe setzen: Wenn du die Hintergrundfarbe des gesamten Fensters auf Schwarz setzen möchtest,
|
||||
* ohne die JPanel-Komponenten selbst zu beeinflussen, setzt du die Hintergrundfarbe des ContentPane.
|
||||
*/
|
||||
window.getContentPane().setBackground(Color.black);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue