Created
January 13, 2016 13:39
-
-
Save jakobkogler/b4b1cdebab9890869b9e to your computer and use it in GitHub Desktop.
PK Adhoc Spiel Gruppe Markus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.RenderingHints; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.Timer; | |
import java.awt.event.*; | |
@SuppressWarnings("serial") | |
public class Game extends JPanel implements ActionListener, KeyListener { | |
int x = 0; | |
int y = 0; | |
Timer timer; | |
public Game() | |
{ | |
addKeyListener(this); | |
timer = new Timer(1000, this); | |
timer.start(); | |
setFocusable(true); | |
requestFocusInWindow(); | |
} | |
private void moveBall() { | |
x = x + 30; | |
y = y; | |
} | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2d.fillOval(x, y, 30, 30); | |
} | |
@Override | |
public void keyTyped(KeyEvent e){} | |
@Override | |
public void keyPressed(KeyEvent e){ | |
switch (e.getKeyCode()){ | |
case KeyEvent.VK_LEFT: | |
System.out.println("left"); | |
break; | |
case KeyEvent.VK_RIGHT: | |
System.out.println("right"); | |
break; | |
case KeyEvent.VK_UP: | |
System.out.println("up"); | |
break; | |
case KeyEvent.VK_DOWN: | |
System.out.println("down"); | |
break; | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e){} | |
public void actionPerformed(ActionEvent e) | |
{ | |
if (e.getSource() == timer) | |
{ | |
this.moveBall(); | |
this.repaint(); | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
JFrame frame = new JFrame("Sample Frame"); | |
Game game = new Game(); | |
frame.add(game); | |
frame.setSize(300, 400); | |
frame.setVisible(true); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment