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();
}
}
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();
}
}
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();
}
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;
}
}
}
}
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);
}
Aggregations