java - drawString() 没有在我的游戏中绘制字符串

标签 java swing graphics

我正在使用drawString()在字符串上写入文本,但没有显示任何内容。我将颜色设置为白色并将坐标设置为(100,100)。我还有什么做错的吗?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 800, HEIGHT = 800;
    private Thread thread;
    private boolean running = false;

    private BodyPart b;
    private ArrayList<BodyPart> snake;

    private Apple apple;
    private ArrayList<Apple> apples;

    private Random r;

    private int xCoor = 20, yCoor = 20;
    private int size = 10;
    private int score = 0;

    private boolean right = true, left = false, up = false, down = false;

    private int ticks = 0;

    private Key key;

    public Screen() {
        setFocusable(true);
        key = new Key();
        addKeyListener(key);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));

        r = new Random();

        snake = new ArrayList<BodyPart>();
        apples = new ArrayList<Apple>();

        start();
    }

    public void reset() {
        snake.clear();
        apples.clear();
        xCoor = 20;
        yCoor = 20;
        size = 10;
        score = 0;
        running = true;
    }

    public void tick() {
        if(snake.size() == 0) {
            b = new BodyPart(xCoor, yCoor, 20);
            snake.add(b);
        }

        if(apples.size() == 0) {
            int xCoor = r.nextInt(40);
            int yCoor = r.nextInt(40);

            apple = new Apple(xCoor, yCoor, 20);
            apples.add(apple);
        }

        for(int i = 0; i < apples.size(); i++) {
            if(xCoor == apples.get(i).getxCoor() && yCoor ==  apples.get(i).getyCoor()) {
                size++;
                apples.remove(i);
                score += 10;
                i--;
            }
        }

        for(int i = 0; i < snake.size(); i++) {
            if(xCoor == snake.get(i).getxCoor() && yCoor ==  snake.get(i).getyCoor()) {
                if(i != snake.size() - 1) {
                    reset();
                }
            }
        }

        if(xCoor < 0) xCoor = 40;
        if(xCoor > 40) xCoor = 0;
        if(yCoor < 0) yCoor = 40;
        if(yCoor > 40) yCoor = 0;

        ticks++;

        if(ticks > 250000) {
            if(right) xCoor++;
            if(left) xCoor--;
            if(up) yCoor--;
            if(down) yCoor++;

            ticks = 185000;

            b = new BodyPart(xCoor, yCoor, 20);
            snake.add(b);

            if(snake.size() > size) {
                snake.remove(0);
            }
        }
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, WIDTH, HEIGHT);

        g.drawString("Score: " + score, 100, 100);
            g.setColor(Color.WHITE);

        g.setColor(new Color(20, 50, 0));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.BLACK);
        for(int i = 0; i < WIDTH / 20; i++) {
            g.drawLine(i * 20, 0, i * 20, HEIGHT);
        }

        for(int i = 0; i < HEIGHT / 20; i++) {
            g.drawLine(0, i * 20, WIDTH, i * 20);
        }

        for(int i = 0; i < snake.size(); i++) {
            snake.get(i).draw(g);
        }
        for(int i = 0; i < apples.size(); i++) {
            apples.get(i).draw(g);
        }

    }

    public void start() {
        running = true;
        thread = new Thread(this, "Game Loop");
        thread.start();
    }

    public void stop() {
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while(running) {
            tick();
            repaint();
        }
    }

    private class Key implements KeyListener {

        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();

            if(key == KeyEvent.VK_RIGHT && !left) { 
                up = false;
                down = false;
                right = true;
            }

            if(key == KeyEvent.VK_LEFT && !right) { 
                up = false;
                down = false;
                left = true;
            }

            if(key == KeyEvent.VK_UP && !down) {
                left = false;
                right = false;
                up = true;
            }

            if(key == KeyEvent.VK_DOWN && !up) {
                left = false;
                right = false;
                down = true;
            }

        }



    }

最佳答案

执行操作的顺序非常重要,请记住,在 Swing 中绘画就像在纸上绘画一样,如果您先绘制文本,然后再绘制它,它将不再可见...

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(new Color(20, 50, 0));
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setColor(Color.BLACK);
    for (int i = 0; i < WIDTH / 20; i++) {
        g.drawLine(i * 20, 0, i * 20, HEIGHT);
    }

    for (int i = 0; i < HEIGHT / 20; i++) {
        g.drawLine(0, i * 20, WIDTH, i * 20);
    }

    for (int i = 0; i < snake.size(); i++) {
        snake.get(i).draw(g);
    }
    for (int i = 0; i < apples.size(); i++) {
        apples.get(i).draw(g);
    }

    g.setColor(Color.WHITE);
    g.drawString("Score: " + score, 100, 100);
}
  1. 不要重写paint,重写paintComponent,你已经绕过了绘画过程,这意味着你最终可能会遇到各种令人讨厌的故障
  2. 调用super.paintComponent

看看Painting in AWT and SwingPerforming Custom Painting有关如何完成绘画的更多详细信息。

Swing 也不是线程安全的,每次更改绘制过程用于更新 UI 的任何内容时都需要小心,因为绘制可能会因任何原因随时发生。

考虑使用 Swing Timer 而不是 Thread。计时器在事件调度线程的上下文中执行其滴答事件,从而使从内部更新 UI 更加安全。请参阅Creating a GUI With JFC/Swing

关于java - drawString() 没有在我的游戏中绘制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315759/

相关文章:

java - 使用 SharedPreferences 保存/加载时忽略 String ArrayList?

Java KeyEvent方 block 移动

java - 如何模拟 JButton 数组中特定 JButton 的移动

java - 如何将 JtextArea 中的所有文本输出到保存文件?

graphics - 球与球的碰撞 - 检测和处理

java - 更多关于 equals 和 hashCode 的内容

Java JTable 不显示记录

Java:从一个类调用另一个类的方法时遇到问题

c# - C# Graphics 对象的 Java 等价物是什么? (包括更多详细信息)

java - Spring Security 4中的方法defineTargetUrl未被调用