gpt4 book ai didi

java - 图形 (java.awt.Graphics) 无法在调用时工作

转载 作者:行者123 更新时间:2023-12-02 10:31:58 25 4
gpt4 key购买 nike

我是 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) {}
}

任何帮助将不胜感激。另外,你们提供的任何提示也会有所帮助。感谢您提前提供的任何帮助。

最佳答案

  1. 自定义绘制是通过重写paintComponent(...)而不是paint(...)来完成的。您还需要调用 super.paintComponent(g) 作为第一条语句,以确保绘制背景。

  2. Java 区分大小写,因此您需要确保重写正确的方法。您应该始终在您重写的方法之前的行中使用@Override。如果您输入错误,编译器会告诉您。

  3. 您应该使用 Swing Timer 来制作动画。 Swing 组件的更新应该在事件调度线程 (EDT) 上完成。 Swing Timer 将自动执行 EDT 上的代码。

  4. 不要在计时器中使用 while (true) 循环。使用定时器的要点是定时器变成了循环。您只需在每次计时器触发时执行代码即可。

  5. 在计时器的 ActionListener 中,您更改变量的值以提供动画,然后调用 repaint() 这将使您的面板重新粉刷。

  6. 变量名称不应以大写字符开头。请注意论坛如何突出显示您的变量名称,因为它认为它们是类名称。这很令人困惑。学习 Java 约定并遵循它们。

  7. 阅读Swing Tutorial Swing 基础知识。其中有以下部分:a) Swing 中的并发 b) 如何使用 Swing 计时器 c) 自定义绘画

关于java - 图形 (java.awt.Graphics) 无法在调用时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567030/

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