【问题标题】:Graphics (java.awt.Graphics) not working on call图形(java.awt.Graphics)不能在通话中工作
【发布时间】:2019-05-03 04:04:21
【问题描述】:

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

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

【问题讨论】:

    标签: java swing graphics awt


    【解决方案1】:
    1. 自定义绘制是通过覆盖paintComponent(...) 而不是paint(...) 来完成的。您还需要调用super.paintComponent(g) 作为第一条语句,以确保绘制背景。

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

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

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

    5. 在 Timer 的 ActionListener 中,您更改变量的值以提供动画,然后调用 repaint(),这将导致您的面板被重新绘制。

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

    7. 阅读 Swing Tutorial 了解 Swing 基础知识。有关于 a) Concurrency in Swing b) How to Use Swing Timers c) Custom Painting 的部分。

    【讨论】:

    • 感谢您的回复,但如果您可以提供代码摘录,以便我可以向他们学习,请正确应用它。谢谢,杰里
    • @JerryPlayz101,我确实提供了代码摘录。本教程有工作示例。您可以通过阅读和玩工作示例来学习。如果您对教程演示有什么不明白的地方,请提出后续问题。您还可以在论坛中搜索其他示例,因为您现在知道要覆盖的正确绘画方法。
    猜你喜欢
    • 2016-10-04
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2015-01-16
    相关资源
    最近更新 更多