Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.*;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Main extends JFrame implements KeyListener, Runnable, WindowListener{
- JPanel panel;
- int bright = 1200;
- int bleft = 0;
- int btop = 40;
- int bbottom = 1025;
- double vx = 1.0;
- double vy = 1.0;
- int cx = 0;
- int cy = 0;
- int speed = 10;
- double angle = 15;
- boolean yeet = false;
- Main(){
- panel = new JPanel();
- this.setLayout(null);
- this.getContentPane().add(panel);
- this.setBounds(0, 0, 1250, 1250);
- this.setVisible(true);
- this.setResizable(false);
- new Thread(this).start();
- this.addKeyListener(this);
- this.addWindowListener(this);
- }
- public void paint(Graphics g){
- super.paint(g);
- g.setColor(Color.BLACK);
- g.drawOval(this.cx, this.cy, 50, 50);
- }
- public static void main(String[] args) {
- new Main();
- }
- @Override
- public void keyPressed(KeyEvent arg0) {
- // TODO Auto-generated method stub
- System.out.println(arg0.getKeyCode());
- if (arg0.getKeyCode() == 107){
- this.vx = this.vx * 1.05;
- this.vy = this.vy * 1.05;
- }
- else if(arg0.getKeyCode() == 109){
- this.vx = this.vx * 0.95;
- this.vy = this.vy * 0.95;
- }
- else if(arg0.getKeyCode() == 38){
- double tmpvx = this.vx;
- double tmpvy = this.vy;
- this.vx = Math.cos(Math.toRadians(angle)) * tmpvx - Math.sin(Math.toRadians(angle)) * tmpvy;
- this.vy = Math.sin(Math.toRadians(angle)) * tmpvx + Math.cos(Math.toRadians(angle)) * tmpvy;
- }
- else if(arg0.getKeyCode() == 40){
- double tmpvx = this.vx;
- double tmpvy = this.vy;
- this.vx = Math.cos(Math.toRadians(angle * (-1))) * tmpvx - Math.sin(Math.toRadians(angle * (-1))) * tmpvy;
- this.vy = Math.sin(Math.toRadians(angle * (-1))) * tmpvx + Math.cos(Math.toRadians(angle * (-1))) * tmpvy;
- }
- }
- public void moveX(){
- int distance = (int)(this.vx * this.speed);
- this.cx = this.cx + distance;
- if(this.cx >= this.bright){
- this.vx = this.vx * (-1);
- yeet = true;
- }
- if(this.cx >= 51){
- yeet = true;
- }
- if((this.cx <= this.bleft) && yeet){
- this.vx = this.vx * (-1);
- System.out.println("echt");
- }
- }
- public void moveY(){
- int distance = (int)(this.vy * this.speed);
- this.cy = this.cy + distance;
- if(this.cy >= this.bbottom){
- this.vy = this.vy * (-1);
- yeet = true;
- }
- if(this.cy >= 50){
- yeet = true;
- }
- if((this.cy <= this.btop) && yeet){
- this.vy = this.vy * (-1);
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(true){
- moveX();
- moveY();
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- this.repaint();
- }
- }
- @Override
- public void windowActivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowClosed(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowClosing(WindowEvent arg0) {
- // TODO Auto-generated method stub
- System.exit(0);
- }
- @Override
- public void windowDeactivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowDeiconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowIconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowOpened(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement