我是Java编程的新手,但我用其他语言进行过编码。我遇到了一个问题,无法调用包含一些绘图说明的Paint()
方法。我希望能够在计时器函数中调用它。代码如下:
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Canvas;
import javax.swing.JPanel;
public class Player extends JPanel implements KeyListener, ActionListener{
// does the same as inheritiing methods and attributes from "JPanel" class type.
private static final long serialVersionUID = 1L;
private static long UUID;
// Time to set game-state values
@SuppressWarnings("unused")
private boolean isPlaying = false;
private int startingScore = 0;
@SuppressWarnings("unused")
private int currScore = startingScore;
// currScore should equal startingScore for correct start score when starting each game.
@SuppressWarnings("unused")
private int TotalBricks = 21;
private static Timer timer = new Timer();
private int delay = 5;
// Player Start Pos
private int PlayerX = 310;
private int PlayerY = 550; // TODO Change PlayerY Value
// Player Dimensions from Start Coords
private int PlayerMinX = PlayerX - 50;
private int PlayerMaxX = PlayerX + 50;
private int PlayerMinY = PlayerY - 4;
private int PlayerMaxY = PlayerY + 4;
// Ball Start Pos
@SuppressWarnings("unused")
private int BallX = 120;
@SuppressWarnings("unused")
private int BallY = 350;
// Ball Velocities
@SuppressWarnings("unused")
private int BallVelX = -1;
@SuppressWarnings("unused")
private int BallVelY = -2;
public Player(){
super();
this.setBackground(Color.white);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
setVisible(true);
MyTimer();
//TODO Get the bricks to display on screen
}
public void MyTimer() {
TimerTask timerTask;
timerTask = new TimerTask() {
@Override
public void run() {
while (true) {
// TODO Get Paint() function working in here
}
}
};
timer.schedule(timerTask, 0, delay);
}
public void Paint(Graphics g){
// linear functions - colour precedes draw, can be overriden without affecting previous statements
// background
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
// border of window
g.setColor(Color.yellow);
g.fillRect(0,0,3,592);
g.fillRect(0,0,692,3);
g.fillRect(691,0,3,592);
// no underside border
// paddle settings
g.setColor(Color.green);
g.fillRect(PlayerMinX, PlayerMinY, PlayerMaxX, PlayerMaxY);
//TODO Check if this works
// ball settings
g.setColor(Color.yellow);
g.fillRect(BallX, BallY, 20, 20);
}
public void actionPerformed(ActionEvent arg0) {
}
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
}
else if (true){
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
}
如有任何帮助,我们将不胜感激。此外,你们能提供的任何提示也会有所帮助。感谢您提前提供的任何帮助。
-
自定义绘制是通过重写
paintComponent(...)
而不是paint(…(来完成的。您还需要调用super.paintComponent(g)
作为第一个语句,以确保绘制背景。 -
Java是区分大小写的,因此您需要确保覆盖正确的方法。您应该始终在重写方法之前的行中使用
@Override
。如果你犯了一个打字错误,编译器会告诉你。 -
您应该使用
Swing Timer
制作动画。Swing组件的更新应该在事件调度线程(EDT(上完成。回转计时器将自动执行EDT上的代码。 -
不要在计时器中使用
while (true)
循环。使用计时器的意义在于计时器成为循环。您只需在每次定时器启动时执行代码。 -
在计时器的
ActionListener
中,更改变量的值以提供动画,然后调用repaint()
,这将导致面板重新绘制。 -
变量名称不应以大写字符开头。注意论坛如何突出显示您的变量名,因为它认为它们是类名。这令人困惑。学习Java惯例并遵守它们。
-
阅读Swing基础知识的Swing教程。有关于a(
Concurrency in Swing
b(How to Use Swing Timers
c(Custom Painting
的部分。