gpt4 book ai didi

java - 使用 Canvas 和计时器绘图

转载 作者:行者123 更新时间:2023-12-01 17:36:44 24 4
gpt4 key购买 nike

我的 Canvas 有问题,我想在 Canvas 上显示一些移动的球(动画),但除了黑色背景之外我什么也看不到。

有人可以告诉我这段代码中的错误以及它将如何工作吗?

public CopyOfCleanBallPanel2() throws IOException, InterruptedException {

frame = new JFrame("simple gaming loop in java");
frame.setSize(BOX_WIDTH, BOX_WIDTH);
frame.setResizable(false);

displayCanvas = new CustomCanvas();
displayCanvas.setLocation(0, 0);
displayCanvas.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
displayCanvas.setBackground(Color.BLACK);
displayCanvas.setFont(new Font("Arial", Font.BOLD, 14));
displayCanvas.setPreferredSize(new Dimension(CANVAS_WIDTH,CANVAS_HEIGHT));

frame.add(displayCanvas);
displayCanvas.requestFocus();

frame.setLocationRelativeTo(null);

try {
this.aBall = (BallServer) Naming
.lookup("rmi://localhost/BouncingBalls");

} catch (Exception e) {
System.out.println("Exception: " + e);
}


frame.pack();
frame.setVisible(true);

aBall.start();
startFrameTimer();
}

/*
* Initializes the frame (also game update) timer.
*/
private void startFrameTimer() {
frameTimer.schedule(new FrameTimerTask(), 1, GAME_TIMER_COOLDOWN);
}

public void updateSimulation() throws RemoteException {
repaintCanvas();
}
/*
* This method gets called by the timer. It updates the game simulation and
* redraws it.
*/
private void onFrameTimer() throws RemoteException {
updateSimulation();
}

/*
* Causes the whole canvas to get repainted.
*/
private final void repaintCanvas() throws RemoteException {
Graphics g = displayCanvas.getGraphics();
drawworld(g);
}

private class FrameTimerTask extends TimerTask {
public void run() {
try {
onFrameTimer();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/*
* This custom canvas overrides the paint method thus allowing for a custom
* painting on the component.
*/
private class CustomCanvas extends Canvas {
@Override
public void paint(Graphics g) {
// Currently the game message gets drawn over the inner border
// of the canvas so we have to repaint the whole thing.
try {
repaintCanvas();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public void drawworld(Graphics g) throws RemoteException {
g.setColor(Color.BLACK);
g.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

System.out.println("i m in drawworld ");

serBall = aBall.getState1(); ***// here it is remote call and there is thread going on suspension***

for (int i = 0; i < currentNumBalls; i++) {
g.setColor(serBall[i].getBallColor(velocity.getLength()));
g
.fillOval((int) (serBall[i].position.getX() - serBall[i]
.getRadius()),
(int) (serBall[i].position.getY() - serBall[i]
.getRadius()), (int) (2 * serBall[i]
.getRadius()), (int) (2 * serBall[i]
.getRadius()));

// Draw our framerate and ball count
g.setColor(Color.WHITE);
g.drawString("FPS: " + currentFrameRate + " Balls: "
+ currentNumBalls, 15, 15);
}
}

P.S:我认为在调用远程方法并渲染绘图世界时存在一些线程问题,任一线程正在挂起或被阻塞

请帮忙。

吉比拉拉

最佳答案

使用 Swing 时,自定义绘制是通过重写 JPanel(或 JComponent)(而不是 Canvas)的 PaintComponent() 方法来完成的。 Canvas 是一个 AWT 组件,不应与 Swing 一起使用。请参阅 Custom Painting 上的 Swing 教程了解更多信息和示例。

动画应该使用 Swing Timer 来完成,以便在 EDT 上执行代码。 Swing 教程还有“如何使用 Swing 计时器”和“并发”部分,有助于解释这些概念。

repaintCanvas() 方法是不必要的。要重新绘制组件,您只需在组件上调用 repaint() 即可。您永远不应该使用 getGraphics() 方法。所有绘画方法都已接收 Graphics 类作为参数。这是您应该用于绘画的 Graphics 对象。

关于java - 使用 Canvas 和计时器绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com