Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 11 Dec 2012
Offline Last Active Apr 03 2016 12:14 PM

#5166007 Is this memory leakage?

Posted by on 10 July 2014 - 07:48 AM

Yes you are right you need to call close.

Another approach:

public class InputManager {
   private final Scanner scanner;

   public InputManager() {
      scanner = new Scanner(System.in);

   //This should be called when you don't want to read input anymore.
   public void shutdown() {

   public String menu(){
      String menuoption = scanner.next();
      //Do some stuff...

      return menuoption;
   public void info(){
      String info = scanner.next();

      //Do some stuff... 

You could then use your class like this:

public static void main(String[] args) {
   InputManager inputManager = new InputManager();

   //Do whatever you want with the user input.
   String option = inputManager.menu();
   System.out.println("Option: " + option);

   //Before exiting the application you free resources

#5054992 Check if objects collide in 2d, without specifying which objects?

Posted by on 19 April 2013 - 01:38 PM

You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.

public class CollisionDetection {
    private static ArrayList<Entity> entities = new ArrayList<Entity>();

    public static boolean collides(Entity e) {
        return collidesWith(e) != null;

    public static Entity collidesWith(Entity e) {
        Entity obstacle = null;

        for(Entity entity: entities) {
            //If its not the same entity
            if(entity!= e) {
                if(e.getRect().intersects(entity.getRect()) {
                    obstacle = entity;

        return obstacle;

    //Add obstacle to detect
    public static void addEntity(Entity entity) {

    //Remove obstacle to detect
    public static void removeEntity(Entity entity) {

You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:


Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.


//Detect if the ball (this) collides with any other entities
boolean collision = CollisionDetection.collides(this);

//Detect with which entity the ball collides
//If its not colliding with anything it will return null.
//Its your job to ensure that it will not collide with more than one entity at the time
//by reacting on collision.
Entity obstacle = CollisionDetection.collidesWith(this);



#5011780 Chess Movement Using Mouse Listeners (Java)

Posted by on 17 December 2012 - 11:48 AM

I've already worked on something similar, it's just mathematics. MouseEvent can provide you the mouse position of the click (MouseEvent.getX(), MouseEvent.getY()).
We're lucky that you are making a chess game because of chessboard. To get the selected square location you can simply loop through all rows and column and check if the position of the mouse is within the square.

I wrote a little method which would find a square , I didn't test it but I think it can help you out.

	private final int NUMBER_OF_ROWS = 8;
	private final int NUMBER_OF_COLUMNS = 8;
	private final int SQUARE_SIZE = 32;

	public Square getSquareAt(int x, int y) {
		for(int col = 0; col < NUMBER_OF_COLUMNS ; ++col ) {
			for(int row = 0; row < NUMBER_OF_ROWS; ++row ) {
				int squareX = row * SQUARE_SIZE;
				int squareY = col * SQUARE_SIZE;
				Rectangle rectangle = new Rectangle(squareX,squareY, SQUARE_SIZE, SQUARE_SIZE);
				if(rectangle.contains(x, y)) {
					return chessBoard[col][row];
		return null;

I let java.awt detect if the coordinate of the mouse is within the bounds of the square.

Edit: That will work if there is one unique mouse listener listening on the chessboard panel. (container of all squares).

#5009671 Java game Garbage Collection

Posted by on 11 December 2012 - 09:11 PM

I don't think the black screen is a good idea. At the initialization of the game you should load alll necessary objects that you will need. Then, depending on the game, you could use a cache system to access to your preloaded objects which should eliminate hiccups if you do it right. If you have to load objects at runtime, you can load it into the cache and then always access it from there. Though, you need to release objects that you don't need anymore from the cache afterward.

And by "cache" I don't mean anything complicated. It can be a hashmap of objects with names as key.

Think about it, if you have hiccups it's surely because you are loading something at each loop pass.