- import java.applet.*;
- import java.awt.*;
- import java.util.Random;
- public class BallApplet extends Applet implements Runnable {
- Random rd = new Random();
- int x1_pos = 250;
- int trefferanzahl = 5;
- int y1_pos=125;
- int radius = 20;
- int peter =1;
- int peter2 = -1;
- int balken_y = 0;
- int xclick = 0;
- int yclick = 0;
- int appletsize_x = 500;
- int appletsize_y = 250;
- int ball_x_speed = 0;
- int ball_y_speed = 0;
- Image background;
- private Image dbImage;
- private Graphics dbg;
- Button start;
- Button Pause;
- public void init() {
- this.setLayout(null);
- background = getImage(getCodeBase(), "bg.jpg");
- start = new Button("Spiel starten");
- Pause = new Button("Pause");
- start.setBounds(250,125,80,50);
- add(start);
- add(Pause);
- }
- public boolean action(Event e, Object args){
- if(e.target == start ) {
- ball_x_speed = 1+rd.nextInt(trefferanzahl);
- ball_y_speed = 1+rd.nextInt(trefferanzahl);
- start.setVisible(false);
- }
- return false;
- }
- public void start() {
- Thread th = new Thread(this);
- th.start();
- }
- public void stop() {
- }
- public void destroy() {
- }
- public boolean mouseMove (Event e, int x, int y)
- {
- //System.out.println(""+x+" "+y);
- xclick = x;
- yclick = y;
- return true;
- }
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while (true) {
- if(yclick+30 >= y1_pos && yclick-30 <= y1_pos && x1_pos >= 30 - radius && x1_pos <= 30 + radius){
- System.out.println("Ball getroffen!");
- trefferanzahl++;
- if (trefferanzahl >=10){ trefferanzahl = 5; }
- ball_y_speed =1+rd.nextInt(trefferanzahl);
- ball_x_speed = 1+rd.nextInt(trefferanzahl);
- }
- if (x1_pos > appletsize_x - radius) {
- ball_x_speed = ball_x_speed* -1;
- }
- if (y1_pos > appletsize_y - radius) {
- ball_y_speed = ball_y_speed* -1;
- }
- if (x1_pos < radius){
- x1_pos = 250;
- y1_pos = 125;
- }
- if (y1_pos <radius){
- ball_y_speed = ball_y_speed* -1; }
- x1_pos = x1_pos + ball_x_speed;
- y1_pos = y1_pos + ball_y_speed;
- if (x1_pos <= radius){
- x1_pos = radius +2;
- ball_x_speed = ball_x_speed * -1;
- }
- if (y1_pos <= radius){
- y1_pos = radius +2;
- ball_y_speed = ball_y_speed * -1;
- }
- repaint();
- try {
- Thread.sleep(20);
- } catch (InterruptedException ex) {
- }
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- public void paint (Graphics g)
- {
- g.drawImage(background,0,0,this);
- g.setColor (Color.black);
- g.fillOval(x1_pos - radius, y1_pos - radius, 2 * radius, 2 * radius);
- g.setColor(Color.black);
- g.fillRect(30,yclick,20,60);
- }
- public void update(Graphics g) {
- if (dbImage == null) {
- dbImage = createImage(this.getSize().width, this.getSize().height);
- dbg = dbImage.getGraphics();
- }
- dbg.setColor(getBackground());
- dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor(getForeground());
- paint(dbg);
- g.drawImage(dbImage, 0, 0, this);
- }
- }
Undefined
By: Guest | Date: Jul 1 2010 11:34 | Format: None | Expires: never | Size: 4.01 KB | Hits: 919
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago