# Java Ping Pong



## Li-La-Launebaer (6. Apr 2010)

Hey mag mir jemand helfen?
Das Programm funktioniert soweit, aber ich hab ein kleines Problem, wenn der Ball aus dem Bildschirmrand fliegt,* kommt er in 10sekunden wieder rein*, eig soll das Spiel beendet werden und da soll "Verloren" stehen und wenn ich einmal den Ball treffe, soll oben rechts ein Punkt hinzukommen.. i.wie krieg ich das net wirklich hin 

*Hat jemand ne Seite wo alle java begriffe erklärt sind?*


```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MyPong extends javax.swing.JFrame {
    
    private Graphics grp = null;
    private boolean running = true;
    private Thread t = null;
    private Image screen  = null;
    private Graphics2D gra = null;
    
    private Kugel k = null;
    
    int x = 0;
    int y = 0;
       
    /** Creates new form MyPong */
    public MyPong() {
        initComponents();
        initGame();
    }
    
    public void initGame(){if (!running){
        while(true) {
            try{
                Thread.currentThread().sleep(100);
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }
        }
    }
    
    grp = jPanel1.getGraphics();
    screen = createImage(400,300);
    gra = (Graphics2D)screen.getGraphics();
    k = new Kugel(150,150, 1, 1);
    t = new Thread(new Game());
    t.start();
    }
    
    

    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();

        setTitle("MyPong");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel1MouseMoved(evt);
            }
        });

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
    
    private void jPanel1MouseMoved(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
        
        y = evt.getY();
        
        
    }
    
    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
        
        running = running ^ true;
        //System.out.println(running);
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    

    public static void main(String args[]) {
        new MyPong().show();
    }
    
    private void drawField(){
        gra.drawRect(5,5, 394, 294);
        gra.drawOval(175, 125, 50, 50);
        gra.drawLine(200,5, 200, 294);
        gra.drawLine(5, 150, 394, 150);
        
    }
    
    private void toScreen(){
        grp.drawImage(screen,0,0,400,300,this);
    }
    
    private void drawKugel(){
        
        k.koord.x += 2 * k.xricht;
        k.koord.y += 2 * k.yricht;
        
        if (( ( k.koord.x >= 380 ) || ( k.koord.x <= 390 ) ) &&  ( Math.abs(y - k.koord.y ) < 20 )){ //Treffer!
            
            if( k.koord.x + 10 > 390 )
                k.xricht *= -1;
        }
        
        if (k.koord.x + 5 < 23)
            k.xricht *= -1;
        
        if( k.koord.y + 4 > 300 )
            k.yricht *= -1;
        else if (k.koord.y + 4 < 14)
            k.yricht *= -1;
        
        
        gra.fillOval(k.koord.x - 3, k.koord.y - 3, 5,5);
        
    }
    
    private void drawPlayer(){
        gra.fillRect( 380 , ( ( y < 278 ) && (y > 10) ) ? y : (y == 278) ? 278 : 10,  10, 20);
    }
    
    private void drawComputer(){
        gra.fillRect(10, k.koord.y - 10 , 10,20);
    }
    
class Game implements Runnable{
        
        public void run(){
            
            while(true){ 
                
                if (!running){
                    while(!running) {
                        try{
                            Thread.currentThread().sleep(100);
                        }catch(InterruptedException ie){
                            ie.printStackTrace();
                        }
                    }
                } else {
                    
                    while(running){
                        
                        
                        
                        gra.clearRect(0,0,400,300);
                        
                        drawField();
                        
                        drawPlayer();
                        
                        drawKugel();
                        
                        drawComputer();
                        
                        toScreen();
                        
                        try{
                            Thread.currentThread().sleep(10);
                        }catch(InterruptedException ie){
                            System.out.println(ie);
                            ie.printStackTrace();
                        }
                    }
                }
            }
        }
        
    }
    
    class Kugel{
        
        public Point koord = null;
        public int xricht = 0; // -1, 0 , 1
        public int yricht = 0; // -1, 0 , 1
        
        public Kugel(int x, int y, int xr, int yr){
            koord = new Point(x,y);
            xricht = xr;
            yricht = yr;
            
        }
        
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
    
}
```


----------



## Quurks (6. Apr 2010)

Hast du irgendein genaueres Problem?

Was sind alle Java-Begriffe?
Galileo Computing :: Java ist auch eine Insel (8. Auflage)
Java Platform SE 6 <- Hier sollte "alles" erklärt sein


----------

