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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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   Source Code and License 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 41
Project: openjdk9   File: VSyncedBSManager.java   Source Code and License 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 42
Project: openjdk9   File: VSyncedBSManager.java   Source Code and License 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 43
Project: openjdk9   File: BufferStrategyExceptionTest.java   Source Code and License 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 44
Project: retronix   File: Screen.java   Source Code and License 5 votes vote down vote up
public void draw(State state) {
    BufferStrategy buffer = getBufferStrategy();
    Graphics2D g2 = (Graphics2D) buffer.getDrawGraphics();
    if (g2 == null) return;
    try {
        g2.scale(3, 3);
        g2.setBackground(Color.BLACK);
        g2.clearRect(0, 0, 320, 180);
        state.render(new Graphics2DRenderer(g2));
        buffer.show();
    } finally {
        g2.dispose();
    }
}
 
Example 45
Project: PhET   File: FullscreenSwingTest.java   Source Code and License 5 votes vote down vote up
public void run() {
    boolean done = false;

    while ( !done ) {
        BufferStrategy strategy = frame.getBufferStrategy();
        Graphics g = strategy.getDrawGraphics();

        g.setColor( Color.black );
        g.fillRect( 0, 0, 800, 600 );
        g.setColor( Color.white );
        g.fillRect( 350, 250, 100, 100 );

        g.dispose();
        strategy.show();
        Toolkit.getDefaultToolkit().sync();

        // --- Dialog doesn't need that Graphics ---
        if ( dialog != null ) {
            dialog.render( null );
        }

        try {
            Thread.sleep( 200 );
        }
        catch ( InterruptedException e ) {
        }
    }


    frame.setVisible( false );
    frame.dispose();
}
 
Example 46
Project: Gymnasiearbete   File: Gyarb.java   Source Code and License 5 votes vote down vote up
/**
 * Renders everything in the game
 */
private void render() {
	if(window.shouldClose())//TODO
           return;
	BufferStrategy buffer = getBufferStrategy();
	Graphics g = buffer.getDrawGraphics();
	
	GameStateManager.render(screen, (Graphics2D) g);
	
	g.dispose();
	buffer.show();
}
 
Example 47
Project: SpaceInvaders   File: Game.java   Source Code and License 5 votes vote down vote up
private void Render(){
    BufferStrategy bs = getBufferStrategy();
    if(bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics graphics = bs.getDrawGraphics();
    Graphics2D graphics2D = (Graphics2D) graphics;

    graphics.setColor(COLOR_BACKGROUND);
    graphics.fillRect(0, 0, getWidth(), getHeight());

    Stream
        .of(
            allInvaderShips,
            allInvaderProjectiles,
            allHeroProjectiles,
            allExplosionVFX,
            Arrays.asList(heroShip, statusRibbon))
        .<GraphicalShape>flatMap(dynamicElements -> dynamicElements.stream())
        .forEach(shape -> shape.Paint(graphics2D));

    if(this.IsGameOver)
        gameOverScreenOverlay.Paint(graphics2D);

    bs.show();
    graphics2D.dispose();
}
 
Example 48
Project: jdk8u_jdk   File: VSyncedBSManager.java   Source Code and License 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 49
Project: jdk8u_jdk   File: VSyncedBSManager.java   Source Code and License 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 50
Project: jdk8u_jdk   File: VSyncedBSManager.java   Source Code and License 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 51
Project: jdk8u_jdk   File: BufferStrategyExceptionTest.java   Source Code and License 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 52
Project: lookaside_java-1.8.0-openjdk   File: VSyncedBSManager.java   Source Code and License 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 53
Project: lookaside_java-1.8.0-openjdk   File: VSyncedBSManager.java   Source Code and License 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 54
Project: lookaside_java-1.8.0-openjdk   File: VSyncedBSManager.java   Source Code and License 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 55
Project: lookaside_java-1.8.0-openjdk   File: BufferStrategyExceptionTest.java   Source Code and License 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 56
Project: snowstorm   File: AwtCanvasPainter.java   Source Code and License 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 57
Project: neo   File: Neo.java   Source Code and License 5 votes vote down vote up
/**
 * <strong><em>render</em></strong><br /><br />
 * 
 * &emsp;Main rendering function. Not to be overriden.
 * 
 * @since NEO.1
 */
private void render(){
	BufferStrategy bs = this.getBufferStrategy();
	if(bs == null){
		this.createBufferStrategy(3);
		return;
	}
	
	Graphics g = bs.getDrawGraphics();
	Graphics2D g2d = (Graphics2D) g;
	
	//////////////////////////////////
	
	//If resizable, it automatically scales the graphics.
	if(window.getShouldScale())
		g2d.scale((double)getWidth()/window.getWidth(), (double)getHeight()/window.getHeight());
	
	//Clears the screen, to change, call setColor.
	g.fillRect(0, 0, (int)window.getWidth(), (int)window.getHeight());
	
	scene.render(g);
	
	
	//////////////////////////////////
	
	g2d.dispose();
	g.dispose();
	bs.show();
}
 
Example 58
Project: javify   File: Window.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a buffering strategy that manages how this window is
 * repainted.  This method attempts to create the optimum strategy
 * based on the desired number of buffers.  Hardware or software
 * acceleration may be used.
 *
 * createBufferStrategy attempts different levels of optimization,
 * but guarantees that some strategy with the requested number of
 * buffers will be created even if it is not optimal.  First it
 * attempts to create a page flipping strategy, then an accelerated
 * blitting strategy, then an unaccelerated blitting strategy.
 *
 * Calling this method causes any existing buffer strategy to be
 * destroyed.
 *
 * @param numBuffers the number of buffers in this strategy
 *
 * @throws IllegalArgumentException if requested number of buffers
 * is less than one
 * @throws IllegalStateException if this window is not displayable
 *
 * @since 1.4
 */
public void createBufferStrategy(int numBuffers)
{
  if (numBuffers < 1)
    throw new IllegalArgumentException("Window.createBufferStrategy: number"
                                       + " of buffers is less than one");

  if (!isDisplayable())
    throw new IllegalStateException("Window.createBufferStrategy: window is"
                                    + " not displayable");

  BufferStrategy newStrategy = null;

  // try a flipping strategy
  try
    {
      newStrategy = new WindowFlipBufferStrategy(numBuffers);
    }
  catch (AWTException e)
    {
    }

  // fall back to an accelerated blitting strategy
  if (newStrategy == null)
    newStrategy = new WindowBltBufferStrategy(numBuffers, true);

  bufferStrategy = newStrategy;
}
 
Example 59
Project: javify   File: Canvas.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a buffering strategy that manages how this canvas is
 * repainted.  This method attempts to create the optimum strategy
 * based on the desired number of buffers.  Hardware or software
 * acceleration may be used.
 *
 * createBufferStrategy attempts different levels of optimization,
 * but guarantees that some strategy with the requested number of
 * buffers will be created even if it is not optimal.  First it
 * attempts to create a page flipping strategy, then an accelerated
 * blitting strategy, then an unaccelerated blitting strategy.
 *
 * Calling this method causes any existing buffer strategy to be
 * destroyed.
 *
 * @param numBuffers the number of buffers in this strategy
 *
 * @throws IllegalArgumentException if requested number of buffers
 * is less than one
 * @throws IllegalStateException if this canvas is not displayable
 *
 * @since 1.4
 */
public void createBufferStrategy(int numBuffers)
{
  if (numBuffers < 1)
    throw new IllegalArgumentException("Canvas.createBufferStrategy: number"
                                       + " of buffers is less than one");

  if (!isDisplayable())
    throw new IllegalStateException("Canvas.createBufferStrategy: canvas is"
                                    + " not displayable");

  BufferStrategy newStrategy = null;

  // try a flipping strategy
  try
    {
      newStrategy = new CanvasFlipBufferStrategy(numBuffers);
    }
  catch (AWTException e)
    {
    }

  // fall back to an accelerated blitting strategy
  if (newStrategy == null)
    newStrategy = new CanvasBltBufferStrategy(numBuffers, true);

  bufferStrategy = newStrategy;
}
 
Example 60
Project: hi   File: Game.java   Source Code and License 5 votes vote down vote up
public void render() {
	BufferStrategy bs = getBufferStrategy();
	if(bs == null) {
		createBufferStrategy(3);
		return;
	}
	Graphics g = bs.getDrawGraphics();
	g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
	bs.show();
}