Java中的移动球

Java中的移动球,java,graphics,awt,paint,Java,Graphics,Awt,Paint,当我运行它时,重新绘制不起作用。框架打开并保持2000ms,然后关闭。否则,如果我删除系统。退出(0);声明,它没有关闭,但彩球仍然没有出现 java import java.awt.*; import javax.swing.JFrame; public class BallWorld extends Frame { public static void main(String[] args) { BallWorld bw1 = new BallWorld(Color.red);

当我运行它时,重新绘制不起作用。框架打开并保持2000ms,然后关闭。否则,如果我删除系统。退出(0);声明,它没有关闭,但彩球仍然没有出现

java

import java.awt.*;
import javax.swing.JFrame;

public class BallWorld extends Frame {

public static void main(String[] args) {

    BallWorld bw1 = new BallWorld(Color.red);
    bw1.show();


}

public static final int framewidth = 600;
public static final int frameheight = 400;

private Ball aball;
private int counter = 0;

private BallWorld(Color ballColor){

    setSize(framewidth,frameheight);
    setTitle("Ball World");

    aball = new Ball(10, 15, 5);
    aball.setColor(ballColor);
    aball.setMotion(3.0, 6.0);

}


public void paint(Graphics g){

    aball.paint1(g);

    aball.move();


    if((aball.x() < 0) || (aball.x() > framewidth))

        aball.setMotion(-aball.xMotion(), aball.yMotion());

    if((aball.yMotion() < 0) || (aball.yMotion() > frameheight))

        aball.setMotion(aball.xMotion(),-aball.yMotion());

    //redraw  the frame

    counter = counter + 1;

    if(counter < 2000)
    repaint();
    else
        System.exit(0); 
}   
}
  • 使用Swing代替AWT,AWT基本上是迟钝的
  • 不要覆盖顶级容器的
    绘制
    ,实际上您已经进入了不应该覆盖的原因之一
  • 不要从任何
    paint
    方法内部调用
    repaint
    或任何可能调用
    repaint
    的方法,它将创建一个无限循环,消耗您的CPU
  • 在AWT/Swing中,绘画是被动的。这意味着更新会根据更改(如鼠标移动或帧大小的更改)以不规则的间隔进行
  • 现在,谈谈你问题的核心。基本上,你是在画框架下的装饰。这是因为窗口的可见区域内添加了装饰,而不是添加到其上

    有关详细信息,请参阅和


    这就是为什么建议不要覆盖顶层容器的绘制。您需要创建一个组件,该组件可以添加到框架中,然后可以在其上绘制

    我已将BallWorld类扩展到JPanel,并已添加到JFrame中。现在它显示的是一条直线,而不是一个移动的球。repaint()还是有问题吗?请帮忙!!你没有打电话给super.paintcomponent,假设你的paintcomponent被超越了谢谢你,这帮了大忙!
    import java.awt.*;
    import java.awt.Rectangle;
    
    public class Ball {
    
    protected Rectangle location;
    protected double dx,dy;
    protected Color color;
    
    
    
    public Ball(int x,int y, int r){
    
        location =new Rectangle(x-r, y-r, 2*r, 2*r);
    
        dx=0;   //initially no motion
        dy=0;
        color = Color.blue;
    }
    
    // Setters
    public void  setColor(Color newColor){
        color = newColor;
    }
    
    public void setMotion(double new_dx, double new_dy){
        dx = new_dx;
        dy = new_dy;
    }
    
    
    //Getters
    
    public int radius(){    
        return location.width/2;
    }
    
    public int x(){
        return location.x + radius();
    }
    
    public int y(){
        return location.y + radius();
    }
    
    public double xMotion(){
        return dx;
    }
    
    public double yMotion(){    
        return dy;
    }
    
    public Rectangle region(){
        return location;
    }
    
    //Methods that change attributes of the ball
    
    public void moveTo(int x, int y){
        location.setLocation(x, y);
    }
    
    public void move(){
        location.translate((int) dx,(int) dy);
    }
    
    public void paint1(Graphics g){
    
        g.setColor(color);
        g.fillOval(location.x, location.y,location.width,location.height);
    }
    }