0

I was following a java game programming tutorial which had different states to deal with. Right now we just aren't doing anything about them, just instantiating them in our main class . The super class which defines states is as given below :

package com.arjav.tilegame.states;

import java.awt.Graphics;

import com.arjav.tilegame.Game;

public abstract class State {

protected Game game ;

private static State currentState = null ;

public State(Game game) {
    this.game = game ;
}

public static void setState(State state){
    currentState = state ;
}

public static State getState(){
    return currentState ;
}

public abstract void tick();

public abstract void render(Graphics g);
}

The main class where I am encountering a problem:

package com.arjav.tilegame;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import com.arjav.tilegame.display.Display;
import com.arjav.tilegame.gfx.Assets;
import com.arjav.tilegame.states.GameState;
import com.arjav.tilegame.states.MenuState;
import com.arjav.tilegame.states.State;

public class Game implements Runnable{

private Display display ;
public int width , height ;
private BufferStrategy bs ;

public String title ;
private Graphics g ;

private boolean running = false ;
private Thread thread ;

//States

private State gameState ;
private State menuState ;

public Game(String title , int width , int height){

    this.width = width ;
    this.height = height ;
    this.title = title ;

}


  private void init() {

    display = new Display(title , width , height);
    Assets.init();


      gameState = new State(this);
      gameState = new State(this);
      State.setState(gameState);
  }


private void tick(){
    if(State.getState() != null) State.getState().tick();
}

private void render(){
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
     }

    g = bs.getDrawGraphics();

    g.clearRect(0, 0, width, height);

    if(State.getState() != null) State.getState().render(g);


    bs.show();
    g.dispose();
 }

  public void run() {

    init();

    int fps = 60 ;
    double timePerTick = 1000000000 / fps ;
    double delta = 0 ;
    long now ; 
    long lastTime = System.nanoTime();
    long timer = 0 ;
    int ticks = 0;

    while(running){
        now = System.nanoTime();;
        delta += (now - lastTime) / timePerTick ;
        timer += now - lastTime ;
        lastTime = now ;

        if(delta >= 1){

        tick();
        render();
        ticks++ ;
        delta-- ;
        }
        if(timer >= 1000000000){
            System.out.println("Ticks and Frame : " + ticks);
            ticks = 0 ;
            timer = 0;
        }
    }

    stop();
}

public synchronized void start(){
    if(running) return;
    running = true ;
    thread = new Thread(this);
    thread.start();
 }

public synchronized void stop(){
    if(!running) return ;
    running = false ;
    try{
        thread.join(); 
     }
    catch (InterruptedException e){
        e.printStackTrace();
     }
  }
 }

In the tutorial it was fine but it's giving some problem here D:

Perdomoff
  • 938
  • 2
  • 7
  • 26
Arjav Garg
  • 49
  • 10
  • 2
    Please, specify the problems you're facing. – Andrea Oct 28 '15 at 15:13
  • 2
    You cannot instantiate an abstract class in java. You have to extend it and implement the abstract method. – yogidilip Oct 28 '15 at 15:14
  • It simply says cannot instantiate type State at the instantiating of the both states in the main class in the init() method – Arjav Garg Oct 28 '15 at 15:14
  • see http://stackoverflow.com/questions/4579305/can-we-instantiate-an-abstract-class?lq=1 and http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class – James Wierzba Oct 28 '15 at 15:17

1 Answers1

7

You cannot create an instance of an abstract class directly. Instead, you must create an-abstract subclass, which includes an implementation for every abstract method

public class MyState extends State {
  MyState(Game g) {
    super(g);
  }

  public void tick() {
    System.out.println("tick");
  }

  public void render(Graphics g) {
    System.out.println("I am rendering!");
    //render the MyState
  }
}

State s = new MyState(myGame);
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80