java - Access one class from another -


i have class defined publishes method allowing access private object within:

    public class hexboard {  [...]          public hexboard(int width, int height, boolean wrap) {             setsize(width, height); // inherently calls reset()             setwrap(wrap);         } // hexboard constructor           public polygon gethexagon(int cellindex) {              polygon p = new polygon();             (int = 0; < 6; i++) {                 p.addpoint((int) (hexcentres.x(cellindex) + hexpoints.x(i)), (int) (hexcentres.y(cellindex) + hexpoints.y(i)));             }              return p;         } // gethexagon          public int cells() { return cellcount; }      } // hexboard 

you can see method creates polygon , returns it. bit works well. now, have class, publishes extended jpanel in order draw hexagon-based playfield consisting of lots of hexagons.

import java.awt.*; import javax.swing.*;  public class pcinterface extends jpanel {      public void paintcomponent(graphics g) {         super.paintcomponent(g);         int cellcount = hexboard.cells();         (int = 0; < hexboard.cells(); i++) {             g.drawpolygon(hexboard.gethexagon(i));         }     } // paintboard  } // pcinterface 

the problem pcinterface knows nothing of hexboard, can't access hexboard.cells() or hexboard.gethexagon().

when following executed

public class test {      public static void main(string args[]) {          breadboard board = new breadboard(12,12,false);          board.setcellradius(25);          jframe frame = new jframe();         frame.settitle("breadboard");         frame.setsize(600, 600);         frame.addwindowlistener(new windowadapter() {            public void windowclosing(windowevent e) {               system.exit(0);            }         });         container contentpane = frame.getcontentpane();         contentpane.add(new pcinterface());         frame.setvisible(true); */     } // main  } // test 

i hope open window , draw hexagons, can see hexagon based board created in main using hexboard, doesn't exist in context of pcinterface.

i can see tat readily include pcinterface in main , solve problem: i'm trying develop multiple platforms , had hoped appropriate way separate platform dependant classes.

how can employ data held in breadboard within pcinterface class, please?

you need instance of hexboard. might add 1 pcinterface

public class pcinterface extends jpanel {      public hexboard hexboard      public pcinterface(hexboard board)     {         this.hexboard = board;     }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         int cellcount = this.hexboard.cells();         (int = 0; < this.hexboard.cells(); i++) {             g.drawpolygon(this.hexboard.gethexagon(i));         }     } // paintboard  } // pcinterface 

assuming type of board, breadboard extends hexboard, can pass constructor this

contentpane.add(new pcinterface(board)); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -