Java Code Examples for java.awt.image.BufferStrategy#getDrawGraphics()

The following examples show how to use java.awt.image.BufferStrategy#getDrawGraphics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawReconstruction.java    From deeplearning4j with Apache License 2.0 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 2
Source File: DrawReconstruction.java    From Canova with Apache License 2.0 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 3
Source File: Game.java    From Lunar with MIT 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 4
Source File: DrawReconstruction.java    From DataVec with Apache License 2.0 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 5
Source File: BufferStrategyExceptionTest.java    From openjdk-8-source with GNU General Public License v2.0 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 6
Source File: GameCanvas.java    From design_principles_dojo with MIT License 5 votes vote down vote up
public void draw() throws Exception {
    BufferStrategy strategy = getBufferStrategy();
    Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    graphics.setColor(Color.white);
    graphics.fill(new Rectangle(dimension));

    currentScreen = currentScreen.getNextScreen();
    currentScreen.draw(graphics);

    graphics.dispose();
    strategy.show();
}
 
Example 7
Source File: SimpleImageViewer.java    From javaanpr with Educational Community License v2.0 5 votes vote down vote up
private void start() {
    while (true) {
        BufferStrategy bs = frame.getBufferStrategy();
        if (bs == null) {
            frame.createBufferStrategy(4);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, 800, 600, null);
        g.dispose();
        bs.show();
    }
}
 
Example 8
Source File: BufferStrategyExceptionTest.java    From jdk8u-jdk with GNU General Public License v2.0 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 9
Source File: SplashUpdater.java    From jeveassets with GNU General Public License v2.0 5 votes vote down vote up
private static void paintSplashWindow(boolean sub) {
	synchronized (PAINT_LOCK) {
		if (splashWindow.isVisible()) {
			if (lastPaint != null) {
				delta = delta + (System.currentTimeMillis() - lastPaint);
			}
			boolean paint = sub || (lastPaint == null || (System.currentTimeMillis() - lastPaint) > 16);
			if (delta > UPDATE_DELAY) {
				nextLoadingImage();
				delta = 0;
				paint = true;
			}
			if (paint) {
				lastPaint = System.currentTimeMillis();
				nextLoadingImage();
				BufferStrategy bufferStrategy = splashWindowCanvas.getBufferStrategy();
				if (bufferStrategy == null) {
					splashWindowCanvas.createBufferStrategy(2);
					bufferStrategy = splashWindowCanvas.getBufferStrategy();
				}
				Graphics g = bufferStrategy.getDrawGraphics();
				g.drawImage(splashImage, 0, 0, null);
				paint((Graphics2D) g);
				g.dispose();
				splashWindowCanvas.getBufferStrategy().show();
			}
		}
	}
}
 
Example 10
Source File: BufferStrategyExceptionTest.java    From jdk8u_jdk with GNU General Public License v2.0 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 11
Source File: BufferStrategyExceptionTest.java    From openjdk-8 with GNU General Public License v2.0 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 12
Source File: BufferStrategyExceptionTest.java    From hottub with GNU General Public License v2.0 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 13
Source File: BufferStrategyExceptionTest.java    From jdk8u-jdk with GNU General Public License v2.0 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 14
Source File: BufferStrategyExceptionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 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 15
Source File: BufferStrategyExceptionTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 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 16
Source File: Menu.java    From JavaGame with GNU Affero General Public License v3.0 5 votes vote down vote up
private void render() {
	// frame.getFrame().getContentPane().setBackground(Color.GREEN);
	frame.addMouseMotionListener((MouseMotionListener) Mouse);
	frame.addMouseListener(Mouse);
	frame.addKeyListener(Key);
	BufferStrategy bs = frame.getBufferStrategy();
	if (bs == null) {
		frame.createBufferStrategy(3);
		return;
	}
	Graphics g = bs.getDrawGraphics();
	g.setColor(Color.BLACK);
	g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
	g.setColor(new Color(0xFF660000));
	g.fillRect(0, 0, WIDTH * 3, HEIGHT * 3);
	g.setColor(new Color(0xFFFF9900));
	g.setFont(font.getArial());
	if (isGameOver()) {
		g.drawString("GAME OVER... What will you do now?", 35, 30);
	} else {
		String name = (Game.getJdata_UserName().length() >= 1) ? WordUtils
				.capitalizeFully(Game.getJdata_UserName()).toString()
				: "Player";
		g.drawString("Welcome to JavaGame " + name, 35, 30);
	}
	g.drawLine(0, HEIGHT * 3, 0, 0);
	g.drawLine(0, 0, (WIDTH * 3), 0);
	g.drawLine((WIDTH * 3), 0, (WIDTH * 3), (HEIGHT * 3));
	g.drawLine(0, (HEIGHT * 3), (WIDTH * 3), (HEIGHT * 3));
	// (LEFT,DOWN,WIDTH,HEIGHT)
	paintButtons(isSelectedStart(), isSelectedExit(), g);
	bs.show();
	g.dispose();

}
 
Example 17
Source File: BufferStrategyExceptionTest.java    From openjdk-jdk8u with GNU General Public License v2.0 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 18
Source File: BufferStrategyExceptionTest.java    From jdk8u60 with GNU General Public License v2.0 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 19
Source File: InventoryWindow.java    From JavaGame with GNU Affero General Public License v3.0 5 votes vote down vote up
private void render() {
	BufferStrategy bs = frame.getBufferStrategy();
	if(bs == null){
		frame.createBufferStrategy(3);
		return;
	}

	Graphics g = bs.getDrawGraphics();
	g.setColor(Color.BLACK);
	g.fillRect(0, 0, WIDTH*SCALE+10, HEIGHT*SCALE+10);
	g.setColor(Color.WHITE);
	g.drawString(NAME, 50, 50);
	bs.show();
	g.dispose();
}
 
Example 20
Source File: Game.java    From JavaGame with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
* This method displays the current state of the game. 
*/
public void render() {
	BufferStrategy bs = getBufferStrategy();
	if (bs == null) {
		createBufferStrategy(3);                                        // Creates a new bs with triple buffering, which reduces tearing and cross-image pixelation
		return;
	}

	// Centres the player in the middle of the screen
	int xOffset = (int) getPlayer().getX() - (screen.getWidth() / 2);
	int yOffset = (int) getPlayer().getY() - (screen.getHeight() / 2);

	level.renderTiles(screen, xOffset, yOffset);

	/*
	 * for (int x = 0; x < level.width; x++) { int colour = Colours.get(-1,
	 * -1, -1, 000); if (x % 10 == 0 && x != 0) { colour = Colours.get(-1,
	 * -1, -1, 500); } Font.render((x % 10) + "", screen, 0 + (x * 8), 0,
	 * colour, 1); }
	 */

	level.renderEntities(screen);
	level.renderProjectileEntities(screen);

	// Copies pixel data from the screen into the game
	for (int y = 0; y < screen.getHeight(); y++) {
		for (int x = 0; x < screen.getWidth(); x++) {
			int colourCode = screen.getPixels()[x + y * screen.getWidth()];
			if (colourCode < 255) {                                                     // If it is a valid colour code
				pixels[x + y * WIDTH] = colours[colourCode];                        // Sets the corresponding pixel from the screen to the game
			}
		}
	}

	if (isChangeLevel() == true && getTickCount() % 60 == 0) {
		Game.setChangeLevel(true);
		setChangeLevel(false);
	}

	if (changeLevel == true) {                                                      // If the player is teleporting to a different level
		print.print("Teleported into new world", PrintTypes.GAME);
		if (getMap() == 1) {                                                    // If the player is currently on custom_level
			setMap("/levels/water_level.png");
			if (getDummy() != null) { // Gave nullPointerException(); upon
				// entering new world.
				level.removeEntity(getDummy());
				setNpc(false);
			}
			level.removeEntity(getVendor());                        // When teleporting away from custom_level, remove vendor NPC (always found on custom_level)
			setMap(2);
		} else if (getMap() == 2) {                                     // If the player is currently on water_level
			setMap("/levels/custom_level.png");
			level.removeEntity(getDummy());
			setNpc(false);
			level.addEntity(getVendor());                           // Add a vendor NPC - they are always found on custom_level
			setMap(1);
		}
		changeLevel = false;
	}

	Graphics g = bs.getDrawGraphics();
	g.drawRect(0, 0, getWidth(), getHeight());                       // Creates a rectangle the same size as the screen
	g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null);
	status(g, isDevMode(), isClosing());
	// Font.render("Hi", screen, 0, 0, Colours.get(-1, -1, -1, 555), 1);
	g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null);
	g.setColor(Color.WHITE);
	g.setFont(font.getSegoe());
	g.drawString(
		"Welcome "
			+ WordUtils.capitalizeFully(player
			.getSanitisedUsername()), 3, getHeight() - 17);
	g.setColor(Color.ORANGE);

	if (context.getLocale().getCountry().equals("BE")                                       // If the player resides in Belgium or France (i.e. uses AZERTY keyboard)
		|| context.getLocale().getCountry().equals("FR")) {                             // Displays "Press A to quit" in orange at the bottom-middle portion of the screen
		g.drawString("Press A to quit", (getWidth() / 2)
			- ("Press A to quit".length() * 3), getHeight() - 17);
	} else {                                                                                // If the player resides anywhere else (i.e. uses QWERTY keyboard)
		g.drawString("Press Q to quit", (getWidth() / 2)                        // Displays "Press Q to quit" in orange at the bottom-middle portion of the screen
			- ("Press Q to quit".length() * 3), getHeight() - 17);
	}
	g.setColor(Color.YELLOW);
	g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));                     // Displays the current time in yellow in the bottom right corner of the screen (hh:mm:ss)
	g.setColor(Color.GREEN);
	if (backgroundMusic.getActive()) {                                                      // If music is turned on
		g.drawString("MUSIC is ON ", 3, getHeight() - 3);                    // Displays "MUSIC IS ON" in green in the bottom left corner of the screen.
	}
	g.dispose();                                                                            // Frees up memory and resources for graphics
	bs.show();
}