簡體   English   中英

drawString()沒有在我的游戲上繪制字符串

[英]drawString() isn't drawing the String on my game

我正在使用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. 不要覆蓋繪畫,而是覆蓋paintComponent ,您已經繞過了繪畫過程,這意味着您可能會遇到各種討厭的故障
  2. 呼叫super.paintComponent

看一下AWT中的繪畫以及“搖擺執行自定義繪畫” ,以了解有關如何完成繪畫的更多詳細信息。

Swing也不是線程安全的,並且在更改繪制過程用來更新UI的任何內容時都需要小心,因為繪制可能會在任何時間以任何原因發生。

考慮使用Swing Timer而不是Thread 計時器在事件調度線程的上下文中執行它的滴答事件,這使得從內部更新UI更加安全。 請參閱使用JFC / Swing創建GUI

您有幾件事順序錯誤。 繪畫時,需要以正確的順序繪畫,否則將在其他對象的頂部繪畫。

因此,在您的代碼中, paint()方法的前5行應如下所示(注意相同的代碼,但順序不同)

    g.clearRect(0, 0, WIDTH, HEIGHT);

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

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

您需要先繪制背景色。 然后,您可以在其頂部繪制字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM