-2

I try to make a ping pong game but my ball don't move so how to make the ball is move?

This is my code

package test;

import java.awt.*;  
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class pingpong1 extends JFrame implements Runnable  {

 public static void main(String[] args) {
        pingpong1 ping = new pingpong1("PingPong Hard Game");

        new Thread(ping).start();

        ping.setSize(600, 300);

        ping.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        ping.setVisible(true);

    } private int width, height;  

    private User user;          
    private Computer computer;          
    private Ball ball;
    static int UserScore = 0;
    int ComputerScore=0;

    public pingpong1(String title){

    }
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Image img2;
        ImageIcon img = new ImageIcon("pingpong.png");
        img2 = img.getImage();
        g.drawImage(img2,0,0, this);
        ball.paint(g);
    }
    @Override
    public void run(){
        while(true){
            ball.moveBall();
            repaint();

                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    Logger.getLogger( getName()).log(Level.SEVERE,null,e);
                }
    }
    }

    public void paintComponent(Graphics g) {
          if (user == null) {
              width = getWidth();
              height = getHeight();
              user = new User();
              computer = new Computer();
              ball = new Ball();
          }
          ball.draw(g);

      } 
    public class User{

    }
    public class Computer{

    }
    public class Ball{
        private int x,y;
        private int centerX , centerY;
        private Color color;
        boolean go;
        Ball(){
            go=false;
        }
        public void paint(Graphics g) {
            // TODO Auto-generated method stub

        }
        public Ball(int x,int y,Color color){
            this.x=x;
            this.y=y;
            this.color=color;
            this.centerX=5;
            this.centerY=5;
        }
        public int getX(){
            return x;
        }
        public int getY(){
            return y;
        }
        public void moveBall(){
            centerX=5;
            x+=centerX;
            y+=centerY;

        } void draw(Graphics g){
            Image img2;
            ImageIcon img = new ImageIcon("pingpong.png");
            img2 = img.getImage();
            g.drawImage(img2, centerX - -35, centerY -10  , null);
        }
    }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
백형신
  • 1
  • 2
  • why _android_ tag? – M D Jun 07 '16 at 11:42
  • When did you expect this to draw the ball at a different place? I would guess at draw of the Ball class. However you are calling the paint method (which is not implemented) when I would expect the call – Gildraths Jun 07 '16 at 12:02

2 Answers2

1

Firstly your code for moving it, need to have some sort of input to actually move the ball. Right now it doesn't do anything but add x, y and doesnt repaint it, so you basically tell it to do nothing. if you are looking for user controlled something like this will work?

public void moveIt(KeyEvent evt) {
 switch (evt.getKeyCode()) {
        case KeyEvent.VK_DOWN:
            myY += 5;
            break;
        case KeyEvent.VK_UP:
            myY -= 5;
            break;
        case KeyEvent.VK_LEFT:
            myX -= 5;
            break;
        case KeyEvent.VK_RIGHT:
            myX += 5;
            break;
    } 
}

If you are looking for a way to move the ball automatically, then you will need to look at a few things in your code. As you don't take in to consideration the speed/ direction etc...

This is a basic example of a moving ball http://introcs.cs.princeton.edu/java/34nbody/Ball.java.html

I would of made a comment but my rep is under 50.

0

In your code you are correctly updating the ball's position in your game loop:

class pingpong1

    while(true){
        ball.moveBall(); // You update movement here
        repaint();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            System.err.println("Interrupted.");
        }
    }

But you never actually redraw the ball at the updated position:

public void paint(Graphics g){
    super.paint(g);
    Image img2;
    ImageIcon img = new ImageIcon("pingpong.png");
    img2 = img.getImage();
    g.drawImage(img2,0,0, this);
    ball.paint(g); // this method is empty
}

Because the ball method paint is empty:

class Ball

public void paint(Graphics g) {
    // TODO Auto-generated method stub
}

To correct that:

public void paint(Graphics g) {
    ImageIcon icon = new ImageIcon("ball.png");
    Image image = icon.getImage();
    g.drawImage(image, x, y, null);
}

Or just call ball.draw() instead but you still have to correct the x and y because they are currently constant, change:

g.drawImage(img2, centerX + 35, centerY - 10, null);

To:

g.drawImage(img2, x, y, null);
Adel Khial
  • 345
  • 4
  • 15