图形 (java.awt.Graphics) 无法随叫随到
Graphics (java.awt.Graphics) not working on call
我是 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) 上完成。 Swing Timer会自动执行EDT上的代码。
不要在定时器中使用 while (true)
循环。使用定时器的要点是定时器成为循环。您只需在每次定时器触发时执行您的代码。
在定时器的 ActionListener
中,您更改变量的值以提供动画,然后调用 repaint()
这将导致您的面板重新绘制。
变量名不应以大写字符开头。请注意论坛如何突出显示您的变量名称,因为它认为它们是 class 名称。这令人困惑。学习 Java 惯例并遵守它们。
阅读 Swing Tutorial 了解 Swing 基础知识。上有节 a) Concurrency in Swing
b) How to Use Swing Timers
c) Custom Painting
.
我是 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) 上完成。 Swing Timer会自动执行EDT上的代码。不要在定时器中使用
while (true)
循环。使用定时器的要点是定时器成为循环。您只需在每次定时器触发时执行您的代码。在定时器的
ActionListener
中,您更改变量的值以提供动画,然后调用repaint()
这将导致您的面板重新绘制。变量名不应以大写字符开头。请注意论坛如何突出显示您的变量名称,因为它认为它们是 class 名称。这令人困惑。学习 Java 惯例并遵守它们。
阅读 Swing Tutorial 了解 Swing 基础知识。上有节 a)
Concurrency in Swing
b)How to Use Swing Timers
c)Custom Painting
.