【问题标题】:Why doesn't my screen turn red?为什么我的屏幕没有变红?
【发布时间】:2015-03-12 01:54:00
【问题描述】:

好的,所以我是编程新手,我正在关注 Youtube 上的教程来构建我自己的游戏。我的问题是我的屏幕没有变红,它只是保持灰色。我确定我做错了什么,但 eclipse 上没有错误。代码如下:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = 1L;

    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;

    private Thread thread;
    private JFrame frame;
    private boolean running = false;

    public Game() {
       Dimension size = new Dimension(width * scale, height * scale);
       setPreferredSize(size);

       frame = new JFrame();
    }

    public synchronized void start() {
        running = true;
        thread = new Thread("Display");
        thread.start();

    }

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

    public void run() {
        while (running) {
            update();
            render();
        }

    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.RED);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.dispose();
        bs.show();
    }

    public static void main(String[] args){
        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle("JJF");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);

        game.start();


    }

}

【问题讨论】:

  • 为什么会变黑?
  • 确实,为什么?我在您的源代码中看到的唯一颜色是Color.RED
  • 图形 g = bs.getDrawGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, getWidth(), getHeight()); g.dispose(); bs.show();对不起,我改变了它
  • 你在任何地方都没有绘画方法,也没有典型的游戏面板。
  • 好吧,我不确定那是什么,但在我正在观看的教程中,我遵循了所有步骤,他的屏幕改变了颜色?我的不会

标签: java


【解决方案1】:

你从来没有真正为Thread 提供运行的东西......

public synchronized void start() {
    running = true;
    // Not the reference to this...
    thread = new Thread(this, "Display");
    thread.start();

}

通过传递this(这是实现RunnableGame 类的一个实例),Thread 将能够调用您的run 方法

注意:

可视区域的大小应该由组件定义,而不是框架。这可以通过覆盖getPreferredSize 方法并返回您希望组件成为的首选可视尺寸来实现。否则,可视区域将是框架的大小减去它的装饰插图,这可能不符合您的期望。

在你的“游戏循环”中,你应该考虑在循环之间有一个小的延迟,以便系统有时间实际更新屏幕,这需要Thread 的一些压力

可运行示例

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;

    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;

    private Thread thread;
    private JFrame frame;
    private boolean running = false;

    public Game() {
        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);

        frame = new JFrame();
    }

    public synchronized void start() {
        running = true;
        thread = new Thread(this, "Display");
        thread.start();

    }

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

    public void run() {
        while (running) {
            update();
            render();
//          try {
//              Thread.sleep(40);
//          } catch (InterruptedException ex) {
//          }
        }

    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.RED);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle("JJF");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);

        game.start();

    }

}

【讨论】:

  • 好的,谢谢您的回答。我添加了,但是结果还是和以前一样,当我运行它时,它似乎没有改变颜色
  • 对我来说很好......确保你已经重新编译它并且没有使用旧的/肮脏的构建
  • 没关系,我再次运行它并得到它,谢谢你的帮助,我一定是在教程中忘记了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 2022-11-19
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多