q1:eventlistener_thread

Dies ist eine alte Version des Dokuments!


Zunächst wieder die Hauptklasse mit der MAIN (UFO) <Code JAVA> package ufo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Ufo extends JFrame{
	Container c;           
	Zeichenbrett z;   
	public Ufo(){
		super("Ufo");
		 c = getContentPane();      
		 z = new Zeichenbrett(); //Alles was passiert, passiert im Zeichenbrett, 
		    // ist ne saubere Lösung oder?  
		 z.setFocusable(true);
		 c.add(z);
		 setTitle("Ufo");
		 setSize(830,700);
		 setVisible(true);
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 
	}
 
  public static void main(String[] args) {
	  new Ufo();
  }
}

</Code>

Das eigentliche Programm. <Code JAVA> package ufo;

import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JPanel;

public class Zeichenbrett extends JPanel {

static int x = 100;
static int y = 100;
static double ax = 0;
static double ay = 0;
Zeichenbrett z = this;
Zeichenbrett(){
	super();
	this.addKeyListener(new TastaturEingabe());
	Thread g = new Thread(new Gravity());
	g.start();
	
}
public void paintComponent(Graphics g) { 
	super.paintComponent(g);
	zeichneUfo(g, x, y);
}	
void zeichneUfo(Graphics g, int xUfo, int yUfo)
{
	g.drawRect(xUfo,yUfo,30,15);
	g.drawLine(xUfo+15,yUfo-10,xUfo+15,yUfo);
	g.drawLine(xUfo+10,yUfo+15,xUfo+5,yUfo+25);
	g.drawLine(xUfo+20,yUfo+15,xUfo+25,yUfo+25);
	g.drawLine(0,625,800,625);
}
class TastaturEingabe implements KeyListener{
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			ax=ax+10; 
		}
		if (e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			ax=ax-10;   
		}
		if (e.getKeyCode() == KeyEvent.VK_UP)
		{
			ay=ay-1;   
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			ay=ay+1;
		}
	 
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			ax=0; 
		}
		if (e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			ax=0;   
		}
		if (e.getKeyCode() == KeyEvent.VK_UP)
		{
			ay=0;   
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			ay=0;
		}
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
}
class Gravity implements Runnable{
	@Override
	public void run() {
		

		double yk = (double)y;	
		double xk = (double)x;
		double vy = 0 ;
		double vx = 0 ;
		long t;
		while (y<1000) {
		t = System.currentTimeMillis();
		try {
			Thread.sleep(20);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		t = System.currentTimeMillis()-t;
		double ayk = 3 + ay;
		vy += ayk*0.005*t; 
		yk+=(vy*t*0.005);
		vx += ax*0.005*t; 
		xk+=(vx*t*0.005);
		if (yk>600) {
			vy=-vy/1.5;
			yk=599;
			vx=-vx/2;
		}
		if (xk>800) xk=0;
		if (xk<0)  yk=800;
		
		y = (int)yk;
		x = (int)xk;
		repaint(); 
	}
   }
}

} </Code>

  • /var/www/infowiki/data/attic/q1/eventlistener_thread.1510646660.txt.gz
  • Zuletzt geändert: 2017/11/14 08:04
  • von admin03