Java Code Examples for java.awt.image.BufferStrategy

The following are top voted examples for showing how to use java.awt.image.BufferStrategy. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Lunar   File: Game.java   View source code 6 votes vote down vote up
/**
 * Draw all game objects.
 */

private void onDraw() {
    BufferStrategy frameStrategy = frame.getBufferStrategy();
    if (frameStrategy == null) {
        // create the buffer strategy, its null.
        frame.createBufferStrategy(3);
        frameStrategy = frame.getBufferStrategy();
    }
    // clear and get our graphics object.
    graphics = frameStrategy.getDrawGraphics();
    graphics.clearRect(0, 0, width, height);

    if (showFPS) {
        graphics.setColor(Color.red);
        graphics.drawString(Integer.toString(fps) + " fps", 50, 50);
    }

    // update stack.
    stack.forEach(state -> state.onDraw(graphics));

    graphics.dispose();
    frameStrategy.show();

}
 
Example 2
Project: KingdomKing   File: Game.java   View source code 6 votes vote down vote up
public void render() {
	//creates place for image to be rendered behind screen then shown
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3); //3 "screens"
		return;
	}

	screen.clear();
	int xScroll = player.x - screen.width / 2;
	int yScroll = player.y - screen.height / 2;
	level.render(xScroll, yScroll, screen);
	player.render(screen);

	for (int i = 0; i < pixels.length; i++) {
		pixels[i] = screen.pixels[i];
	}

	Graphics g = bs.getDrawGraphics(); //linking buffer with graphics
	g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
	g.setColor(Color.WHITE);
	g.setFont(new Font("Verdena", 0, 20));
	g.drawString("X: " + (player.x >> 4) + ", Y: " + (player.y >> 4), 0, 16);
	g.dispose(); //remove graphics that are not used
	bs.show(); //swapping out the buffers
}
 
Example 3
Project: jvb   File: MergedScreen.java   View source code 6 votes vote down vote up
@Override
public void update(RenderedFrame left, RenderedFrame right, DebugDrawer debugDrawer) {
    this.requestFocusInWindow();
    BufferStrategy bufferStrategy = getBufferStrategy();
    if (bufferStrategy == null) {
        createBufferStrategy(2);
        bufferStrategy = getBufferStrategy();
    }
    for (int row = 0; row < Screen.HEIGHT; row++) {
        for (int col = 0; col < Screen.WIDTH; col++) {
            imageData[col + Screen.WIDTH * row] = (left.getPixel(row, col) & 0xff) << 16 | (right.getPixel(row, col) & 0xff);
        }
    }
    Graphics g = bufferStrategy.getDrawGraphics();
    g.drawImage(bufferedImage, 0, 0, Screen.WIDTH * SCALE, Screen.HEIGHT * SCALE, null);
    if (debugDrawer != null) {
        debugDrawer.drawDebug(g, SCALE);
    }
    g.dispose();
    bufferStrategy.show();
}
 
Example 4
Project: openjdk-jdk10   File: BufferStrategyExceptionTest.java   View source code 6 votes vote down vote up
public void render() {
    ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
    ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
    BufferCapabilities bufCap =
        new BufferCapabilities(imgFrontBufCap,
            imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
    try {

        createBufferStrategy(2, bufCap);
    } catch (AWTException ex) {
        createBufferStrategy(2);
    }

    BufferStrategy bs = getBufferStrategy();
    do {
        Graphics g =  bs.getDrawGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.red);
        g.drawString("Rendering test", 20, 20);

        g.drawImage(bi, 50, 50, null);

        g.dispose();
        bs.show();
    } while (bs.contentsLost()||bs.contentsRestored());
}
 
Example 5
Project: AutoDrivingCarUsingNeuronNetwork   File: CarDemo.java   View source code 6 votes vote down vote up
private void render() {
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3);
		return;
	}
	Graphics2D g = (Graphics2D) bs.getDrawGraphics();
	screen.setGraphic(g);
	entityManager.renderByPixels(screen);
	for (int i = 0; i < pixels.length; i++) {
		pixels[i] = screen.getPixels()[i];
	}
	g.setStroke(new BasicStroke(2));
	g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
	entityManager.renderByGraphics(screen);
	screen.dispose();
	bs.show();
}
 
Example 6
Project: DungeonRun2   File: Game.java   View source code 6 votes vote down vote up
private void render() {
	BufferStrategy bs = this.getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3);
		return;
	}
	screen.clear(ColorUtil.toARGBColor(Color.BLACK));
	gsm.render(screen);

	for (int i = 0; i < screen.getWidth() * screen.getHeight(); i++) {
		pixels[i] = screen.getPixels()[i];
	}

	Graphics g = bs.getDrawGraphics();
	g.setColor(Color.BLACK);
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	if (settings.fullscreen) {
		g.drawImage(img, fullScreenXOff, fullScreenYOff, fullScreenImageWidth, fullScreenImageHeight, null);
	} else {
		g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
	}
	g.dispose();
	bs.show();
}
 
Example 7
Project: PhET   File: OffscreenCanvasExample.java   View source code 6 votes vote down vote up
/**
 * Render offscreen graphics into the frame.
 */
private void render() {
    final BufferStrategy bufferStrategy = frame.getBufferStrategy();
    do {
        do {
            final Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
            // canvas is not opaque, so fill with background color
            graphics.setPaint(background);
            graphics.fillRect(0, 0, 400, 400);
            // then let canvas render into graphics
            canvas.render(graphics);
            graphics.dispose();
        } while (bufferStrategy.contentsRestored());
        bufferStrategy.show();
    } while (bufferStrategy.contentsLost());
}
 
Example 8
Project: DataVec   File: DrawReconstruction.java   View source code 6 votes vote down vote up
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();

        }
    }
 
Example 9
Project: marshmallow   File: Game.java   View source code 6 votes vote down vote up
private void render(){
	BufferStrategy bs = this.getBufferStrategy();
	if(bs == null){
		this.createBufferStrategy(3);
		return;
	}
	Graphics g = bs.getDrawGraphics();
	drawBackground(g); //TODO: fix it up? Background Images
	handler.render(g);
	if(gameState == STATE.Game){
		hud.render(g);	
		gun.render(g);
	}else if (gameState != STATE.Game){
		handler.clear();
		menu.render(g);
	}
	g.dispose();
	bs.show();
}
 
Example 10
Project: OriginalSpaceShooter   File: Game.java   View source code 6 votes vote down vote up
private void render() {
    BufferStrategy gameBS = this.getBufferStrategy();
    if (gameBS == null) {
        this.createBufferStrategy(3);
        return;
    }
    Graphics gameG = gameBS.getDrawGraphics();

    ///////////////DRAW GRAPHICS///////////////////
    gameG.setColor(Color.black); //TODO Replace Background Color with a Background Picture
    gameG.fillRect(0, 0, getWidth(), getHeight()); //background size
    handler.render(gameG);

    gameG.setColor(Color.white);
    gameG.drawString("| SCORE: " +
            handler.getScore() + " | HEALTH: " +
            handler.getHealth() + " | LEVEL: " +
            handler.getLevel() + " | MONEY: " +
            handler.getMoney() + " |", 15, 15);
    /////////////////////////////////////////////

    gameG.dispose();
    gameBS.show();
}
 
Example 11
Project: Firestorm   File: Game.java   View source code 6 votes vote down vote up
private void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    // At least on Ubuntu we seem not to need to translate the canvas
    if(!Util.isUnix())
        g2d.translate(-6, -28);
    ////////////////////////////////////////////////

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, WIDTH, HEIGHT);

    stateManager.render(g2d);

    ////////////////////////////////////////////////
    g.dispose();
    bs.show();
}
 
Example 12
Project: piccolo2d.java   File: OffscreenCanvasExample.java   View source code 6 votes vote down vote up
/**
 * Render offscreen graphics into the frame.
 */
private void render() {
    final BufferStrategy bufferStrategy = frame.getBufferStrategy();
    do {
        do {
            final Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
            // canvas is not opaque, so fill with background color
            graphics.setPaint(background);
            graphics.fillRect(0, 0, 400, 400);
            // then let canvas render into graphics
            canvas.render(graphics);
            graphics.dispose();
        } while (bufferStrategy.contentsRestored());
        bufferStrategy.show();
    } while (bufferStrategy.contentsLost());
}
 
Example 13
Project: JBomberman   File: GameCanvas.java   View source code 6 votes vote down vote up
/**
 * Kümmert sich um die BufferStrategy und delegiert das 
 * zeichnen der Sprites direkt an die Sprites selbst.
 * 
 */
public void render() {
	BufferStrategy bs = getBufferStrategy();
	if(bs == null) {
		try {
		createBufferStrategy(3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return;
	}
	
	Graphics g = bs.getDrawGraphics();
	game.drawAll(image.getGraphics());
	g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
	
	g.dispose();
	bs.show();
}
 
Example 14
Project: aritzh   File: CanvasEngine.java   View source code 6 votes vote down vote up
@Override
public void render() {
	if (!running) return;

	BufferStrategy bs = canvas.getBufferStrategy();
	if (bs == null) {
		canvas.createBufferStrategy(3);
		return;
	}
	Graphics g = bs.getDrawGraphics();

	g.setColor(Color.BLACK);
	g.fillRect(0, 0, this.size.width, this.size.height);

	this.graphics = g;

	this.getGame().onRender();
	this.getGame().onPostRender();

	this.graphics = null;

	g.dispose();
	bs.show();
}
 
Example 15
Project: StickJumper   File: Game.java   View source code 6 votes vote down vote up
private void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    if (state == GameState.LOADING) LoadScreen.render(g);
    if (state == GameState.MENU) menu.render(g);
    if (state == GameState.CREDITS) credits.render(g);
    if (state == GameState.ABOUT) about.render(g);
    if (state == GameState.CONTROLS) controlScreen.render(g);
    if (state == GameState.GAMEOVER) endScreen.render(g);
    renderGame(g);
    g.dispose();
    bs.show();
}
 
Example 16
Project: Dystopia   File: DystopiaCanvas.java   View source code 6 votes vote down vote up
public void render() {
	BufferStrategy bs = this.getBufferStrategy();
	
	if (bs == null) {
		this.createBufferStrategy(DystopiaCanvas.BUFFERS);
		this.requestFocus();
		return;
	}

	Graphics2D g = (Graphics2D) bs.getDrawGraphics();
	
	g.setColor(new Color(0xEEEEEE));
	g.fillRect(0, 0, DisplayCarrier.getCanvas().getWidth(), DisplayCarrier.getCanvas().getHeight());
	GridDisplay.drawGrid(g);
	Rectangle2D rect = new Rectangle2D.Double(DisplayCarrier.getCanvas().getWidth() / 2 - Tile.getTileSize(), DisplayCarrier.getCanvas().getHeight() / 2 - Tile.getTileSize(), Tile.getTileSize() * 2, Tile.getTileSize() * 2);
	g.draw(rect);
	PlayerCamera.drawPlayer(g);
	
       g.dispose();
       
       if (!bs.contentsLost()) {
       	bs.show();
       }
       Toolkit.getDefaultToolkit().sync();
}
 
Example 17
Project: org.dayflower   File: ProgressUpdateFutureImpl.java   View source code 6 votes vote down vote up
private void doPaint(String status, int percent) {
	try {
		int width = (int)(jFrame.getWidth() / 1.5D);
		int height = 30;
		int x = (jFrame.getWidth() - width) / 2;
		int y = jFrame.getHeight() - (50 + height);
		
		BufferStrategy bufferStrategy = jFrame.getBufferStrategy();
		
		Graphics graphics = bufferStrategy.getDrawGraphics();
		
		Graphics2D graphics2D = Graphics2D.class.cast(graphics);
		graphics2D.setColor(Color.BLACK);
		graphics2D.fillRect(0, 0, jFrame.getWidth(), jFrame.getHeight());
		
		doPaintBackground(graphics2D, x, y, width, height);
		doPaintForeground(graphics2D, x, y, width, height, status, percent);
		
		graphics2D.dispose();
	} catch(IllegalStateException e) {
		
	}
}
 
Example 18
Project: rabbit-escape   File: SwingGraphics.java   View source code 6 votes vote down vote up
public DrawFrame(
    BufferStrategy strategy,
    java.awt.Canvas canvas,
    Renderer<SwingBitmap, SwingPaint> renderer,
    SoundPlayer soundPlayer,
    SpriteAnimator animator,
    int frameNum,
    World world,
    WaterAnimation waterAnimation
)
{
    super( strategy );
    this.canvas = canvas;
    this.renderer = renderer;
    this.soundPlayer = soundPlayer;
    this.animator = animator;
    this.frameNum = frameNum;
    this.world = world;
    this.waterAnimation = waterAnimation;
}
 
Example 19
Project: SuperSquirrel   File: Game.java   View source code 6 votes vote down vote up
private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    Graphics2D g2d = (Graphics2D) g;
    
    /////////// Drawing begins;
    g.setColor(new Color(25, 191, 224));  // background color
    g.fillRect(0, 0, getWidth(), getHeight()); // Fill
    for (int i = 0; i < (level.getWidth() * scale) / (tex.cloud.getWidth() / 2); i++) { // Clouds
        g.drawImage(tex.cloud, (int)-cam.getX()/2 + ((tex.cloud.getWidth()/3) * i), (i % 3) * (i % 4) * 10, tex.cloud.getWidth()/2, tex.cloud.getHeight()/2, null);
    }
    g2d.translate(-cam.getX(), -cam.getY()); // Cam Begins 
    handler.render(g); // Object rendering
    g2d.translate(cam.getX(), cam.getY()); // Cam Ends

    hud.render(g); // Draw HUD
    /////////// Drawing ends;
    
    g.dispose();
    bs.show();
}
 
Example 20
Project: Project--H   File: game.java   View source code 6 votes vote down vote up
/**render**/
public void render() {
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3);
		return; 
	}
	Screen.clear();
	Screen.render(x, y);
	
	for (int i = 0; i < pixels.length; i++) {
		pixels[i] = Screen.pixels[i];
		
	}
	
	Graphics g = bs.getDrawGraphics();
	g.drawImage(image,0, 0, getWidth(), getHeight(), null);
	g.dispose();
	bs.show();
	
}
 
Example 21
Project: Canova   File: DrawReconstruction.java   View source code 6 votes vote down vote up
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();

		}
	}
 
Example 22
Project: deeplearning4j   File: DrawReconstruction.java   View source code 6 votes vote down vote up
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();

        }
    }
 
Example 23
Project: consulo   File: DialogWrapperPeerImpl.java   View source code 6 votes vote down vote up
@Override
public void dispose() {
  if (isShowing()) {
    hide();
  }

  if (myWindowListener != null) {
    myWindowListener.saveSize();
    removeWindowListener(myWindowListener);
    myWindowListener = null;
  }

  DialogWrapper.cleanupWindowListeners(this);

  final BufferStrategy strategy = getBufferStrategy();
  if (strategy != null) {
    strategy.dispose();
  }
  super.dispose();

  removeAll();
  DialogWrapper.cleanupRootPane(rootPane);
  rootPane = null;

}
 
Example 24
Project: spacewar-2d   File: Game.java   View source code 6 votes vote down vote up
public void render(){
	BufferStrategy bs = this.getBufferStrategy();
	if(bs == null){
		createBufferStrategy(3);
		return;
	}
	
	Graphics g = bs.getDrawGraphics();
	/////////////////////////////////
	
	g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
	player.render(g);
	bullets.render(g);
	
	////////////////////////////////
	g.dispose();
	bs.show();
}
 
Example 25
Project: 11-Zain   File: Game.java   View source code 6 votes vote down vote up
public void render() {
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		/** Increasing number could improve speed but would slow down performance **/
		createBufferStrategy(3);
		return;
	}
	
	screen.clear();
	int xScroll = player.x - screen.width / 2;
	int yScroll = player.y - screen.height / 2;
	level.render(xScroll, yScroll, screen);
	player.render(screen);
	
	for (int i = 0; i < pixels.length; i++) {
		pixels[i] = screen.pixels[i];
	}

	Graphics g = bs.getDrawGraphics();
	g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
	g.setColor(Color.WHITE);
	g.setFont(new Font("Verdana", 0, 50));
	g.dispose();
	bs.show();
}
 
Example 26
Project: AutonomousCar   File: LooiWindow.java   View source code 5 votes vote down vote up
private void paint()
{
    
    BufferStrategy bs = canvas.getBufferStrategy();
    if(bs == null)
        {
            canvas.createBufferStrategy(3);
            return;
        }
    g = bs.getDrawGraphics();

    g.clearRect(0,0,(int)getWindowWidth(),(int)getWindowHeight());
    
    for(int i = paintActiveLooiObjects.size()-1; i > -1 ; i--)
        {
            paintActiveLooiObjects.get(i).setGraphics(g);
            g.setColor(Color.black);//set default color here! omg it used to be yellow and I had no idea... and i was so confused y everything was YELLOW
            paintActiveLooiObjects.get(i).looiPaint();
        }



    g.setColor(Color.black);
    g.fillRect(0,0,getWindowWidth(),viewVerticalOffset);
    g.fillRect(0,viewHeight+viewVerticalOffset,getWindowWidth(),getWindowHeight());
    g.fillRect(0,0,viewHorizontalOffset,getWindowHeight());
    g.fillRect(viewWidth+viewHorizontalOffset,0,getWindowWidth(),getWindowHeight());

    bs.show();
    g.dispose();
    //g.finalize();//This line gives the program a TINY performance boost
}
 
Example 27
Project: The-Mysterious-Mind-Of-Jack   File: GameCanvas.java   View source code 5 votes vote down vote up
public void draw() {
	requestFocus();
       BufferStrategy bs = getBufferStrategy();
       if (bs == null) {
           createBufferStrategy(3);
           return;
       }

       Graphics g = currentFrame.getGraphics();
       GameCanvas.g = g;
       g.setColor(Color.GRAY);
       g.fillRect(0, 0, getWidth(), getHeight());
       
       if (Game.paused) {
       	if (!isBlurred) {
        	if (StateHandler.pausedGame != null) {
	        	StateHandler.pausedGame.render(g);
	        	blurred = Tools.blur(currentFrame);
	        	isBlurred = true;
        	}
       	}
       } else isBlurred = false;
       
       StateHandler.render(g);
       
       bs.getDrawGraphics().drawImage(currentFrame, 0, 0, StateHandler.WIDTH, StateHandler.HEIGHT, null);
       
       g.dispose();
       bs.show();
}
 
Example 28
Project: OpenJSharp   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
/**
 * Lets the manager know that this buffer strategy is no longer interested
 * in being v-synced.
 */
public static synchronized void releaseVsync(BufferStrategy bs) {
    VSyncedBSManager bsm = getInstance(false);
    if (bsm != null) {
        bsm.relinquishVsync(bs);
    }
}
 
Example 29
Project: OpenJSharp   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy current = strategy.get();
        if (current != null) {
            return (current == bs);
        }
    }
    strategy = new WeakReference<BufferStrategy>(bs);
    return true;
}
 
Example 30
Project: OpenJSharp   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy b = strategy.get();
        if (b == bs) {
            strategy.clear();
            strategy = null;
        }
    }
}
 
Example 31
Project: VTerminal   File: Panel.java   View source code 5 votes vote down vote up
/** Draws every character of every row onto the canvas. */
public void draw() {
    final BufferStrategy bs = this.getBufferStrategy();

    do {
        do {
            final Graphics2D gc;

            try {
                gc = (Graphics2D) bs.getDrawGraphics();
            } catch (final NullPointerException | IllegalStateException e) {
                // BufferStrategy may not have been created on the first call,
                // so just do a recursive call until it works.
                // This may be a bad idea.
                draw();
                return;
            }

            gc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
            gc.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
            gc.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            gc.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

            // Font characters are pre-rendered images, so no need for AA.
            gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

            // No-need for text rendering related options.
            gc.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
            gc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

            // If alpha is used in the character images, we want computations related to drawing them to be fast.
            gc.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);

            screen.draw(gc, imageCache);
            gc.dispose();
        } while (bs.contentsRestored()); // Repeat render if drawing buffer contents were restored.

        bs.show();
    } while (bs.contentsLost()); // Repeat render if drawing buffer was lost.
}
 
Example 32
Project: snowstorm   File: AwtCanvasPainter.java   View source code 5 votes vote down vote up
@Override
public void drawScene(Snowflake[] scene, ScreenParameters screenParameters) {
    if (DEBUG_SHOW_FPS) fpsCounter.recordFrame();

    BufferStrategy strategy = getBufferStrategy();
    do {
        do {
            Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();
            SnowFlake3DRenderer snowFlake3DRenderer = new AwtSnowflakeRenderer(g2d, screenParameters);

            drawBackground(g2d, screenParameters);
            for (Snowflake snowflake : scene) {
                snowFlake3DRenderer.renderSnowflake(snowflake.x + xoffset, snowflake.y, snowflake.size, snowflake.z, screenParameters);
            }

            if (DEBUG_SHOW_FPS) {
                g2d.setColor(Color.white);
                g2d.drawString(String.format("%.2f min FPS", fpsCounter.getMinFps()), 20, 20);
                g2d.drawString(String.format("%.2f max FPS", fpsCounter.getMaxFps()), 20, 35);
            }

            g2d.dispose();

        } while (strategy.contentsRestored());

        strategy.show();
        DEFAULT_TOOLKIT.sync(); // Seems like this is necessary on Ubuntu for smooth animation
    } while (strategy.contentsLost());
}
 
Example 33
Project: jdk8u-jdk   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
/**
 * Lets the manager know that this buffer strategy is no longer interested
 * in being v-synced.
 */
public static synchronized void releaseVsync(BufferStrategy bs) {
    VSyncedBSManager bsm = getInstance(false);
    if (bsm != null) {
        bsm.relinquishVsync(bs);
    }
}
 
Example 34
Project: jdk8u-jdk   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy current = strategy.get();
        if (current != null) {
            return (current == bs);
        }
    }
    strategy = new WeakReference<BufferStrategy>(bs);
    return true;
}
 
Example 35
Project: jdk8u-jdk   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy b = strategy.get();
        if (b == bs) {
            strategy.clear();
            strategy = null;
        }
    }
}
 
Example 36
Project: jdk8u-jdk   File: BufferStrategyExceptionTest.java   View source code 5 votes vote down vote up
public void render() {
    ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
    ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
    BufferCapabilities bufCap =
        new BufferCapabilities(imgFrontBufCap,
            imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
    try {

        createBufferStrategy(2, bufCap);
    } catch (AWTException ex) {
        createBufferStrategy(2);
    }

    BufferStrategy bs = getBufferStrategy();
    do {
        Graphics g =  bs.getDrawGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.red);
        g.drawString("Rendering test", 20, 20);

        g.drawImage(bi, 50, 50, null);

        g.dispose();
        bs.show();
    } while (bs.contentsLost()||bs.contentsRestored());
}
 
Example 37
Project: openjdk-jdk10   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
/**
 * Lets the manager know that this buffer strategy is no longer interested
 * in being v-synced.
 */
public static synchronized void releaseVsync(BufferStrategy bs) {
    VSyncedBSManager bsm = getInstance(false);
    if (bsm != null) {
        bsm.relinquishVsync(bs);
    }
}
 
Example 38
Project: openjdk-jdk10   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy current = strategy.get();
        if (current != null) {
            return (current == bs);
        }
    }
    strategy = new WeakReference<BufferStrategy>(bs);
    return true;
}
 
Example 39
Project: openjdk-jdk10   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
    if (strategy != null) {
        BufferStrategy b = strategy.get();
        if (b == bs) {
            strategy.clear();
            strategy = null;
        }
    }
}
 
Example 40
Project: openjdk9   File: VSyncedBSManager.java   View source code 5 votes vote down vote up
/**
 * Lets the manager know that this buffer strategy is no longer interested
 * in being v-synced.
 */
public static synchronized void releaseVsync(BufferStrategy bs) {
    VSyncedBSManager bsm = getInstance(false);
    if (bsm != null) {
        bsm.relinquishVsync(bs);
    }
}