Java缓冲策略多驱动程序显示

时间:2017-09-30 00:18:10

标签: java driver multiple-monitors bufferstrategy

我正在使用缓冲策略在java中创建一个应用程序。唯一的问题是当我在我的集​​成显卡上使用最大化的缓冲区策略时,它会因此错误崩溃:

Exception in thread "Thread-2" java.lang.IllegalStateException: Buffers have not been created

我认为它崩溃的原因是我在多显示器上使用AMD显卡和集成显卡,换句话说,两台显示器正在使用AMD显卡的图形和一台显示器正在使用集成显卡。

结果,我无法在igpu运行监视器上最大化我的应用程序,但可以最大化我在视频卡操作的监视器上的窗口。

你有解释为什么Java会给我废话吗?另外,有没有解决方案,所以我可以在我的igpu跑监视器上最大化我的窗口?

这是缓冲区的创建和使用方式:

旁注,每当使用Graphics对象时,我都使用g.drawImage(BufferedImage, x, y, null);

    private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null) {
        this.createBufferStrategy(3); //creation
                                      //this being a class that implements Runnable
        return;
    }

    Graphics g = bs.getDrawGraphics(); //bufferstrategy is converted to graphics here

    g.setColor(Color.black);
    g.fillRect(0, 0, mutableWidth, mutableHeight);

    handler.render(g); //Graphics is transported to hundreds of objects to get displayed

    g.dispose();
    bs.show();
}

我相信你不需要阅读实现runnable的整个类,但以防你这样做:

package GenRuntime;

import GenRuntime.GameObjects.Block.Block;
import GenRuntime.Handler.Handler;
import UI.UIInput.KeyInput;
import Resources.Initializer;

import java.awt.*;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = -240840600533728354L;

    public static final int WIDTH = 800, HEIGHT = WIDTH /12 * 9;

    private int mutableWidth, mutableHeight;
    private int windowXLoc, windowYLoc;

    private Thread thread;
    private boolean running = false;

    private Handler handler;

    Window window;

    public Game() {
        handler = new Handler();
        this.addKeyListener((new KeyInput(handler)));

        window = new Window(WIDTH, HEIGHT, "Survior", this);
        mutableHeight = HEIGHT;
        mutableWidth = WIDTH;
    }

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

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

    public int Frames;

    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            long now = System.nanoTime();
            delta += (now-lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                tick();
                delta--;
            }

            if (running)
                render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                Frames = frames;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
    }

    private void tick() {
        handler.tick();
    }

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

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, mutableWidth, mutableHeight);

        handler.render(g);

        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        Initializer.init();

        Block b = new Block();
        new Game();
    }

    //The rest of the code is getter and setter methods. Don't worry about them

    public void setSize(int width, int height) {
        mutableHeight = height;
        mutableWidth = width;
    }

    public void setSize(Dimension dim) {
        mutableWidth = dim.width;
        mutableHeight = dim.height;
    }

    public void setWindowLoc(int x, int y) {
        windowXLoc = x;
        windowYLoc = y;
    }

    public void setWindowLoc(Point p) {
        windowXLoc = p.x;
        windowYLoc = p.y;
    }
    public int getMutableWidth() {
        return mutableWidth;
    }

    public int getMutableHeight() {
        return mutableHeight;
    }

    public int getWindowXLoc() {
        return windowXLoc;
    }

    public int getWindowYLoc() {
        return windowYLoc;
    }
}

1 个答案:

答案 0 :(得分:0)

答案发布在以下链接:java: IllegalStateException - Buffers have not been created

我有一个多驱动程序问题,通过将 Rect b = drwable.getBounds(); canvas.save(); Path path = new Path(); int w = b.right; int h = b.bottom; path.addCircle(w/2, h/2, w/2, Path.Direction.CW); canvas.clipPath(path); drawable.draw(canvas); canvas.restore(); 添加到我的java命令行(或VM参数)来修复