Java不会将图形渲染到帧

时间:2016-07-06 16:00:05

标签: java eclipse

使用以下代码,无论我尝试什么,图形都不会在屏幕上呈现。

package local.ryan.grid;

import java.awt.BufferCapabilities;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game implements Runnable {

    public boolean isRunning = false;

    private long lastTime;
    private double fps;
    public int gamestage = 0;
    public Frame frame = new Frame(2, 300, 300, "Ok");

    public static void main(String[] args) throws InterruptedException {

        Thread game = new Thread(new Game());
        game.start();

    }

    public void run() {

        start();

        while(isRunning) {

            lastTime = System.nanoTime();

            try {

                // Limit Each Frame, to 60 fps.
                // Prevents performance issues with multiple objects.

            Thread.sleep(1000 / 60); // 1000 miliseconds (1 second) / 60 frames-per-second ~ 17 ms.



            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            render();

            fps = 1000000000D / (System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish
            lastTime = System.nanoTime();

            // Change to integer, to remove decimals.
            System.out.println("FPS: " + (int) fps + ".");

        }

    }

    public void render(){

           BufferStrategy bs = frame.getBufferStrategy();

           if (bs== null){

           frame.createBufferStrategy(3);
           return;

           }

           Graphics2D g = (Graphics2D)bs.getDrawGraphics();

            //Draws a background and a line for testing
            g.setColor(Color.GRAY);
            g.drawRect(0, 0, 500, 500);
            g.setColor(Color.BLACK);
            g.drawLine(50, 50, 200, 50);

            //Displays the graphics to the frame
            frame.update(g);
            g.dispose();
            bs.show();

        }

    public void start() {

        if(isRunning)
            return;

        isRunning = true;


    }

    public void stop() {

        if(!isRunning)
            return;

        isRunning = false;


    }

}

我还有一个名为Frame的类来创建一个框架:

package local.ryan.grid;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Frame extends JFrame {

    public Frame(int scale, int x, int y, String title) {

        setTitle(title);
        setSize(x, y);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);

    }

    public JFrame getFrame() {

        return this;

    }

}

1 个答案:

答案 0 :(得分:1)

package local.ryan.grid;

import java.awt.BufferCapabilities;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game implements Runnable {

    public boolean isRunning = false;

    private long lastTime;
    private double fps;
    public int gamestage = 0;
    public JFrame frame = new Frame(2, 300, 300, "Ok");

    public static void main(String[] args) throws InterruptedException {

        Thread game = new Thread(new Game());
        game.start();

    }

    public void run() {

        start();

        while(isRunning) {

            lastTime = System.nanoTime();

            try {

                // Limit Each Frame, to 60 fps.
                // Prevents performance issues with multiple objects.

            Thread.sleep(1000 / 60); // 1000 miliseconds (1 second) / 60 frames-per-second ~ 17 ms.



            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            render(frame.getGraphics());

            fps = 1000000000D / (System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish
            lastTime = System.nanoTime();

            // Change to integer, to remove decimals.
            System.out.println("FPS: " + (int) fps + ".");

        }

    }

    public void render(Graphics g) {    

        Shape circle = new Ellipse2D.Float(10,10,100f,100f);
        Graphics2D ga = (Graphics2D)g;
        ga.draw(circle);

    }  

    public void start() {

        if(isRunning)
            return;

        isRunning = true;


    }

    public void stop() {

        if(!isRunning)
            return;

        isRunning = false;


    }

}

可能不是最好的方法,但它有效!