java - Java中的移动球

标签 java graphics awt paint

当我运行它时,重绘不起作用。框架打开并停留2000ms,然后关闭。否则,如果我删除 System.exit(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); 
}   
}

另一个类是

球.java

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);
}
}

最佳答案

  1. 用Swing代替AWT,AWT基本是废话
  2. 不要覆盖顶级容器的paint,您实际上已经进入了不应该覆盖的原因之一
  3. 不要调用 repaint 或任何可能从任何 paint 方法内部调用 repaint 的方法,它会创建一个无限循环,将消耗你的CPU
  4. 绘画在 AWT/Swing 中是被动的。这意味着更新会根据变化(例如鼠标移动或框架大小的变化)以不规则的间隔发生。

现在,进入问题的核心。基本上,您是在框架装饰下绘画。发生这种情况是因为窗口在其可见区域内添加了装饰,而不是添加到它上面。

参见 How to get the EXACT middle of a screen, even when re-sizedHow can I set in the midst?了解更多详情

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

关于java - Java中的移动球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738444/

相关文章:

java - 根据某些条件从 map 返回数组的最佳和最快的方法

java - 线程安全程序的方法

swift - Metal 着色器未按预期工作

graphics - Skia 或 Direct2D 如何使用 GPU 渲染线或多边形?

java - 如何停止 BufferedImage.getGraphics() 打开 X11 设备?

java - 在 headless (headless)服务器上绘制图像

java - 有效 URI Java 的正则表达式

Java Hibernate sql server Express 连接错误

Java:将 Text.Attribute 对象设置为其默认值时出现空指针异常,为什么?

java - char 到 KeyEvent int