java - Am I calling the other classes correctly? I feel like the SnakeBoardDisplay should only be created once -


i have 3 separate classes in java 1 logic, 1 painting, , last window.

window class, class creates board display , adds window class

public snakewindow() {     this.settitle("snake");     this.setdefaultcloseoperation(jframe.exit_on_close);     this.setsize(win_wid, win_hei);      snakeboarddisplay dpanel = new snakeboarddisplay();     this.add(dpanel);     inintmenu();     this.setvisible(true);     setresizable(false); } public static void main(string[] args) {      new snakewindow();   } 

board display class, class creates new board display

where proper place class called , executed?

public snakeboarddisplay() {      game = new snakegame();      tm.start();     addkeylistener(this);     setfocusable(true);     setfocustraversalkeysenabled(false); } public static void main(string[] args) {      new snakeboarddisplay();   } 

game class

public snakegame() {     init();  }  public void init() {     for(int = 0; < tokens; i++)     {         int q = i;          q *= seperationdis;           tokensx[i] = crdx-q;         tokensy[i] = crdy;      }     sidecollision();     applecollision();     snakemover();     bodycollision();     apple(); } 

declaring classes

public class snakegame extends snakewindow {  public class snakeboarddisplay extends jpanel implements actionlistener, keylistener{  public class snakewindow extends jframe  { 

your main class snakeboarddisplay. class has main method instantiates snakegame in snakeboarddisplay.

you mentioned:

snakegame extends snakewindow 

which means when snakegame instantiated, snakewindow created. need call super class constructor in snakegame constructor.

public snakegame() {   super();    init(); }  

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -