Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Aritz Lopez
   *
   *    Licensed under the Apache License, Version 2.0 (the "License");
   *    you may not use this file except in compliance with the License.
   *    You may obtain a copy of the License at
   *
   *        http://www.apache.org/licenses/LICENSE-2.0
   *
  *    Unless required by applicable law or agreed to in writing, software
  *    distributed under the License is distributed on an "AS IS" BASIS,
  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
 
 package io.github.aritzhack.aritzh.awt.gameEngine;
 
 
 import javax.swing.*;
 import java.awt.*;
Game engine based on AWT and the Canvas component

Author(s):
Aritz Lopez
 
 public class CanvasEngine extends BasicGame {
 
 	private final InputHandler inputHandler;
 	private final Thread thread;
 	private final Dimension size;
 	private final ILogger logger;
 	private final Canvas canvas = new Canvas();
 	private JFrame frame;
 	private boolean running;
 	private boolean hasFrame;
 	private Graphics graphics;

Create a canvas engine with the specified game and size. A javax.swing.JFrame that holds the canvas will be created.

Parameters:
game The implementation of the game.
width The width of the game
height The height of the game
 
 	public CanvasEngine(IGame gameint widthint height) {
 		this(gamewidthheightfalsenull);
 	}

Create a canvas engine with the specified game and size. Can be specified whether the javax.swing.JFrame should be created or not.

Parameters:
game The implementation of the game.
width The width of the game
height The height of the game
noFrame If true, no javax.swing.JFrame will be created.
logger The logger to use. If null, nothing will be logged.
 
 	public CanvasEngine(IGame gameint widthint heightboolean noFrame, @Nullable ILogger logger) {
 		super(game);
 		Preconditions.checkArgument(width > 0 && height > 0, "Game sizes cannot be negative!");
 
 		this. = !noFrame;
 		this. = new Dimension(widthheight);
 		this. = new Thread(thisgame.getGameName() + "-Thread");
 		this. = new InputHandler();
 		this. = NullLogger.getLogger(logger);
 
 		if (this.this.createFrame();
 	}
 
 	private void createFrame() {
 		this. = new JFrame(this.getGame().getGameName());
 
 		this..setResizable(false);
 		this..add();
 		this..pack();
			public void windowClosing(WindowEvent e) {
			}
		});
	}
	public void setTitle(String title) {
		if (this.this..setTitle(title);
	}

This method must be called to start the engine (and therefore the game).
	public synchronized void start() {
		super.start();
		this. = true;
		if (this.this..setVisible(true);
		this..start();
	}

This method must be called to stop the engine (and therefore the game).
	public synchronized void stop() {
		this..d("Stopping...");
		this.getGame().onStop();
		this. = false;
		if (this.this..dispose();
		try {
			this..join(3000);
catch (InterruptedException e) {
			this..e("Error exiting!"e);
			System.exit(-1);
		}
		this..d("Stopped.");
		System.exit(0);
	}
	public void render() {
		if (!return;
		if (bs == null) {
			return;
		}
		g.fillRect(0, 0, this..this..);
		this. = g;
		this.getGame().onRender();
		this. = null;
		bs.show();
	}

Returns the java.awt.Graphics object into which the game is being rendered.

Returns:
the java.awt.Graphics object into which the game is being rendered.
	public Graphics getGraphics() {
		return ;
	}

		return ;
	}

Returns the size of the game.

Returns:
the size of the game.
	public Dimension getSize() {
		return ;
	}
New to GrepCode? Check out our FAQ X