Search in sources :

Example 1 with BufferStrategy

use of java.awt.image.BufferStrategy in project deeplearning4j by deeplearning4j.

the class DrawReconstruction method start.

public void start() {
    int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    boolean running = true;
    while (running) {
        BufferStrategy bs = frame.getBufferStrategy();
        if (bs == null) {
            frame.createBufferStrategy(4);
            return;
        }
        for (int i = 0; i < width * height; i++) pixels[i] = 0;
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, heightOffset, widthOffset, width, height, null);
        g.dispose();
        bs.show();
    }
}
Also used : BufferStrategy(java.awt.image.BufferStrategy) DataBufferInt(java.awt.image.DataBufferInt)

Example 2 with BufferStrategy

use of java.awt.image.BufferStrategy in project jdk8u_jdk by JetBrains.

the class MultimonFullscreenTest method actionPerformed.

public void actionPerformed(ActionEvent ae) {
    GraphicsDevice dev = deviceMap.get(ae.getSource());
    System.err.println("Setting FS on device:" + dev);
    final Window fsWindow;
    if (useFSWindow) {
        fsWindow = new Window(this, dev.getDefaultConfiguration()) {

            public void paint(Graphics g) {
                renderDimensions(g, getBounds(), this.getGraphicsConfiguration());
            }
        };
    } else if (useFSDialog) {
        fsWindow = new Dialog((Frame) null, "FS Dialog on device " + dev, false, dev.getDefaultConfiguration());
        fsWindow.add(new Component() {

            public void paint(Graphics g) {
                renderDimensions(g, getBounds(), this.getGraphicsConfiguration());
            }
        });
    } else {
        fsWindow = new Frame("FS Frame on device " + dev, dev.getDefaultConfiguration()) {

            public void paint(Graphics g) {
                renderDimensions(g, getBounds(), this.getGraphicsConfiguration());
            }
        };
        if (addHWChildren) {
            fsWindow.add("South", new Panel() {

                public void paint(Graphics g) {
                    g.setColor(Color.red);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
            });
            fsWindow.add("North", new Button("Button, sucka!"));
        }
    }
    fsWindow.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() > 1) {
                done = true;
                fsWindow.dispose();
            }
        }
    });
    fsWindow.addWindowListener(new WindowHandler());
    dev.setFullScreenWindow(fsWindow);
    if (dmChange && dev.isDisplayChangeSupported()) {
        DisplayMode[] dms = dev.getDisplayModes();
        DisplayMode myDM = null;
        for (DisplayMode dm : dms) {
            if (dm.getWidth() == 800 && dm.getHeight() == 600 && (dm.getBitDepth() >= 16 || dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) && (dm.getRefreshRate() >= 60 || dm.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN)) {
                myDM = dm;
                break;
            }
        }
        if (myDM != null) {
            System.err.println("Setting Display Mode: " + myDM.getWidth() + "x" + myDM.getHeight() + "x" + myDM.getBitDepth() + "@" + myDM.getRefreshRate() + "Hz on device" + dev);
            dev.setDisplayMode(myDM);
        } else {
            System.err.println("Can't find suitable display mode.");
        }
    }
    done = false;
    if (runRenderLoop) {
        Thread updateThread = new Thread(new Runnable() {

            public void run() {
                BufferStrategy bs = null;
                if (useBS) {
                    fsWindow.createBufferStrategy(2);
                    bs = fsWindow.getBufferStrategy();
                }
                while (!done) {
                    if (useBS) {
                        Graphics g = bs.getDrawGraphics();
                        renderDimensions(g, fsWindow.getBounds(), fsWindow.getGraphicsConfiguration());
                        bs.show();
                    } else {
                        fsWindow.repaint();
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
                if (useBS) {
                    bs.dispose();
                }
            }
        });
        updateThread.start();
    }
}
Also used : Window(java.awt.Window) Frame(java.awt.Frame) MouseEvent(java.awt.event.MouseEvent) BufferStrategy(java.awt.image.BufferStrategy) MouseAdapter(java.awt.event.MouseAdapter) Graphics(java.awt.Graphics) DisplayMode(java.awt.DisplayMode) Panel(java.awt.Panel) GraphicsDevice(java.awt.GraphicsDevice) Button(java.awt.Button) Dialog(java.awt.Dialog) Component(java.awt.Component)

Example 3 with BufferStrategy

use of java.awt.image.BufferStrategy in project narchy by automenta.

the class TopDownMinicraft method render.

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        requestFocus();
        return;
    }
    int xScroll = player.x - screen.w / 2;
    int yScroll = player.y - (screen.h - 8) / 2;
    if (xScroll < 16)
        xScroll = 16;
    if (yScroll < 16)
        yScroll = 16;
    if (xScroll > level.w * 16 - screen.w - 16)
        xScroll = level.w * 16 - screen.w - 16;
    if (yScroll > level.h * 16 - screen.h - 16)
        yScroll = level.h * 16 - screen.h - 16;
    if (currentLevel > 3) {
        int col = Color.get(20, 20, 121, 121);
        for (int y = 0; y < 14; y++) for (int x = 0; x < 24; x++) {
            screen.render(x * 8 - ((xScroll / 4) & 7), y * 8 - ((yScroll / 4) & 7), 0, col, 0);
        }
    }
    level.renderBackground(screen, xScroll, yScroll);
    level.renderSprites(screen, xScroll, yScroll);
    if (currentLevel < 3) {
        lightScreen.clear(0);
        level.renderLight(lightScreen, xScroll, yScroll);
        screen.overlay(lightScreen, xScroll, yScroll);
    }
    renderGui();
    for (int y = 0; y < screen.h; y++) {
        for (int x = 0; x < screen.w; x++) {
            int cc = screen.pixels[x + y * screen.w];
            if (cc < 255)
                pixels[x + y * WIDTH] = colors[cc];
        }
    }
    Graphics g = bs.getDrawGraphics();
    g.fillRect(0, 0, getWidth(), getHeight());
    int ww = WIDTH * SCALE;
    int hh = HEIGHT * SCALE;
    int xo = (getWidth() - ww) / 2;
    int yo = (getHeight() - hh) / 2;
    g.drawImage(image, xo, yo, ww, hh, null);
    g.dispose();
    bs.show();
}
Also used : BufferStrategy(java.awt.image.BufferStrategy)

Example 4 with BufferStrategy

use of java.awt.image.BufferStrategy in project jdk8u_jdk by JetBrains.

the class RenderingToCachedGraphicsTest method runTest.

private void runTest() {
    // this will cause screen update manager to dump the accelerated surface
    // for this canvas
    renderCanvas.createBufferStrategy(2);
    BufferStrategy bs = renderCanvas.getBufferStrategy();
    do {
        Graphics bsg = bs.getDrawGraphics();
        bsg.setColor(Color.blue);
        bsg.fillRect(0, 0, renderCanvas.getWidth(), renderCanvas.getHeight());
    } while (bs.contentsLost() || bs.contentsRestored());
    // grab the "unaccelerated" onscreen surface
    cachedGraphics = renderCanvas.getGraphics();
    cachedGraphics.setColor(Color.red);
    cachedGraphics.fillRect(0, 0, getWidth(), getHeight());
    bs.dispose();
    bs = null;
    // now the update manager should be able to accelerate onscreen
    // rendering to it again
    cachedGraphics.setColor(Color.green);
    // this causes restoration  of the new accelerated onscreen surface
    // (it is created in "lost" state)
    cachedGraphics.fillRect(0, 0, renderCanvas.getWidth(), renderCanvas.getHeight());
    Toolkit.getDefaultToolkit().sync();
    // and now we should be able to render to it
    cachedGraphics.fillRect(0, 0, renderCanvas.getWidth(), renderCanvas.getHeight());
    Toolkit.getDefaultToolkit().sync();
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
        return;
    }
    Point p = renderCanvas.getLocationOnScreen();
    Rectangle r = new Rectangle(p.x, p.y, renderCanvas.getWidth(), renderCanvas.getHeight());
    BufferedImage bi = robot.createScreenCapture(r);
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            if (bi.getRGB(x, y) != Color.green.getRGB()) {
                System.err.println("Colors mismatch!");
                String name = "RenderingToCachedGraphicsTest.png";
                try {
                    ImageIO.write(bi, "png", new File(name));
                    System.err.println("Dumped grabbed image to: " + name);
                } catch (Exception e) {
                }
                failed = true;
                return;
            }
        }
    }
}
Also used : Graphics(java.awt.Graphics) BufferStrategy(java.awt.image.BufferStrategy) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point)

Example 5 with BufferStrategy

use of java.awt.image.BufferStrategy in project javacv by bytedeco.

the class CanvasFrame method initCanvas.

protected void initCanvas(boolean fullScreen, DisplayMode displayMode, double gamma) {
    canvas = new Canvas() {

        @Override
        public void update(Graphics g) {
            paint(g);
        }

        @Override
        public void paint(Graphics g) {
            // but otherwise seems to work fine.
            try {
                if (canvas.getWidth() <= 0 || canvas.getHeight() <= 0) {
                    return;
                }
                BufferStrategy strategy = canvas.getBufferStrategy();
                do {
                    do {
                        g = strategy.getDrawGraphics();
                        if (color != null) {
                            g.setColor(color);
                            g.fillRect(0, 0, getWidth(), getHeight());
                        }
                        if (image != null) {
                            g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
                        }
                        if (buffer != null) {
                            g.drawImage(buffer, 0, 0, getWidth(), getHeight(), null);
                        }
                        g.dispose();
                    } while (strategy.contentsRestored());
                    strategy.show();
                } while (strategy.contentsLost());
            } catch (NullPointerException e) {
            } catch (IllegalStateException e) {
            }
        }
    };
    if (fullScreen) {
        canvas.setSize(getSize());
        needInitialResize = false;
    } else {
        // mac bug
        canvas.setSize(10, 10);
        needInitialResize = true;
    }
    getContentPane().add(canvas);
    canvas.setVisible(true);
    canvas.createBufferStrategy(2);
// canvas.setIgnoreRepaint(true);
}
Also used : Graphics(java.awt.Graphics) Canvas(java.awt.Canvas) BufferStrategy(java.awt.image.BufferStrategy)

Aggregations

BufferStrategy (java.awt.image.BufferStrategy)8 Graphics (java.awt.Graphics)5 Button (java.awt.Button)1 Canvas (java.awt.Canvas)1 Component (java.awt.Component)1 Dialog (java.awt.Dialog)1 DisplayMode (java.awt.DisplayMode)1 Font (java.awt.Font)1 Frame (java.awt.Frame)1 Graphics2D (java.awt.Graphics2D)1 GraphicsDevice (java.awt.GraphicsDevice)1 Panel (java.awt.Panel)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 Robot (java.awt.Robot)1 Window (java.awt.Window)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BufferedImage (java.awt.image.BufferedImage)1 DataBufferInt (java.awt.image.DataBufferInt)1