有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇让球弹起,它留下了痕迹?

我是java新手,正在写一个乒乓球游戏。 我让球移动(这是我的首要任务),让它在屏幕上成为一个焦点。 但由于某些原因,它在后面留下了一条黑线,我不知道我是否应该对此进行勘误,但这是我的两个类的代码(Ball类只是一个保存球信息的类)

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class Main extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;

    public static int Width=650;
    public static int Height=600;
    public boolean Running=false;
    public Thread thread;
    public Ball ball = new Ball();

    public static void main(String[] args){
      Main game = new Main();
      JFrame frame = new JFrame();
      frame.setSize(Width+25, Height+49);
      frame.setTitle("Pong By Poo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
      frame.add(game);
      game.start();
    }


    public void start(){
      if(Running==true){
        return;
      } else {
        Running=true;
        thread = new Thread(this);
        thread.start();
      }
    }

    public void run(){
      while(Running==true){
        try{
          Thread.sleep(5);
          Draw();
          Update();
          ball.YVelocity();
          ball.XVelocity();
        } catch(Exception e){}
      }
    }

    public void Draw(){
      BufferStrategy bs = this.getBufferStrategy();
      if(bs==null){
        createBufferStrategy(2);
      } else {
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillOval(ball.BallLocationX, ball.BallLocationY, 20, 20);
        g.dispose();
        bs.show();
      }
    }

    public void Update(){
      if(ball.BallLocationX==0) {
        ball.BallMovementX=true;
        System.out.println("Ball has hit the Left");
      }
      if(ball.BallLocationX==Width) {
        ball.BallMovementX=false;
        System.out.println("Ball has hit the Right");
      }
      if(ball.BallLocationY==0){
        ball.BallMovementY=true;
        System.out.println("Ball has hit the Top");
      }
      if(ball.BallLocationY==Height){
        ball.BallMovementY=false;
        System.out.println("Ball has hit the bottom");
      }
    }
  }



import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Ball extends JPanel{
  public int BallLocationX;
  public int BallLocationY;
  public boolean BallMovementY; //true makes the ball go up, false makes it go down
  public boolean BallMovementX; //True makes the ball go right, false makes it go left.

  public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillOval(BallLocationX, BallLocationY, 10, 10);
  }

  //moves the ball left to right
  public int YVelocity(){
    if(BallMovementY==true){
      BallLocationY++;
    } else {
      BallLocationY--;
    }

    return BallLocationY;
  }

  //Moves the ball up and down
  public int XVelocity(){
    if(BallMovementX==true){
      BallLocationX+=2;
    } else {
      BallLocationX-=2;
    }

    return BallLocationX;
    }
}

请帮忙


共 (1) 个答案

  1. # 1 楼答案

    基本上,你的球移动的任何地方都会永久地停留在你的Color.BLACK上。如果你想摆脱这一点,你需要在每次球移动时刷新,并在再次绘制球的位置之前,将Canvas重新绘制到Color.WHITE(或任何东西)

    具体来说,请看下面的代码:

    public void Draw(){
      BufferStrategy bs = this.getBufferStrategy();
      if(bs==null){
        createBufferStrategy(2);
      } else {
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillOval(ball.BallLocationX, ball.BallLocationY, 20, 20);
        g.dispose();
        bs.show();
      }
    }
    

    这里没有逻辑覆盖您之前对指示球位置的Canvas所做的更改

    此外,作为一个附带的细节,Java标准在camelCase中有方法名

    回答评论中的问题:Canvas API中没有任何东西可以自动理解Canvas的默认背景,并可以将所有其他图形属性重置为该背景。然而,要获得这个功能,你需要做的就是在绘制球的位置之前,重新绘制默认布局(无论是一种颜色,还是基本背景图像,或者其他任何东西)