通过客户端 - 服务器javafx渲染形状

时间:2016-02-24 00:59:13

标签: javafx hexagonal-tiles

我是初学者并且已经开始创建一个十六进制游戏了我让我的董事会独立工作但是我希望把它变成一个客户端服务器,因为它是基于转向的游戏并且永远在服务器。服务器将传输对象,客户端将呈现它,但是当我尝试时,我得到一个空白屏幕。如果我在客户端上创建相同的对象,它会呈现正常。我可以确认客户端正在从服务器拉出对象,因为我创建的像素坐标和径向坐标正在打印到控制台。

我没有包含HexTile类,但是它扩展了多边形并且是序列化的。 HexGrouping只是一个数组。在HexTile中,我使用setLayoutX和setLayoutY来确定板上的位置。

服务器

public class ServerGame{


private static Properties prop = new Properties();



/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
     prop = loadSettings();
     HexGrouping hexeServer = new HexGrouping(Integer.parseInt(prop.getProperty("players")));
     System.out.println("Sender Start");

        ServerSocketChannel ssChannel = ServerSocketChannel.open();
        ssChannel.configureBlocking(true);
        int port = Integer.parseInt(prop.getProperty("port"));
        ssChannel.socket().bind(new InetSocketAddress(port));

        while (true) {
            SocketChannel sChannel = ssChannel.accept();

            ObjectOutputStream  oos = new 
                      ObjectOutputStream(sChannel.socket().getOutputStream());
            oos.writeObject(hexeServer);
            oos.close();

            System.out.println("Connection ended");
        }

}

客户端

public class ClientGameBoard extends Application{

//check the screen dimensions to make a full screen layout
Rectangle2D screen = Screen.getPrimary().getVisualBounds();
Properties prop = new Properties();



/**
 * @param args
 */
public static void main(String[] args) {
    launch(args);

}

@Override
public void start(Stage myStage) throws Exception {
    prop = loadSettings(); //all server settings will be located here


    myStage.setTitle("TI3");
    Pane rootNode = new Pane();
    Pane boardnode = new Pane();
    ScrollPane sp = new ScrollPane();
    Scene myScene = new Scene(rootNode, screen.getWidth(), screen.getHeight(), HexGrouping.BACKGROUND);


    myStage.setScene(myScene);

    HexGrouping hexes = getBoard();

    for (int i = 0; i < hexes.table.length; i++){
            boardnode.getChildren().add(hexes.table[i]);
            System.out.println(hexes.table[i].getPixel());

    }
    sp.setPrefSize(screen.getWidth(), screen.getHeight());


    //had to do some kludge to prevent it from resizing for linux
    myStage.setMaxHeight(myScene.getHeight());
    myStage.setMaxWidth(myScene.getWidth());
    myStage.setMinHeight(myScene.getHeight());
    myStage.setMinWidth(myScene.getWidth());

    sp.setContent(boardnode);
    rootNode.getChildren().add(sp);

    myStage.show();

}

private Properties loadSettings(){

       Properties p = new Properties();

       try {
         FileInputStream in = new FileInputStream("client.conf");
         p.load(in);
         in.close();
        }
        catch (FileNotFoundException e){
           System.out.println("No Client Config File.. creating");
           //create a file, change this to a better input
           createProp(prop, "localhost", "19566");
           p = prop;
        }
        catch (IOException ea){
            ea.printStackTrace();
        }
       return p;
    }
private void createProp(Properties p, String ip, String port){
        p.setProperty("IP", ip);
        p.setProperty("Port", port);

        try {
            FileOutputStream out = new FileOutputStream("client.conf");
            p.store(out, "--Server Information--");
            out.close();
        }
        catch (IOException ea){
            ea.printStackTrace();

        }
}
private HexGrouping getBoard() throws IOException, ClassNotFoundException {
    HexGrouping hg = null;
    System.out.println("Receiver Start");

    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(true);
    if (sChannel.connect(new InetSocketAddress(prop.getProperty("IP"), Integer.parseInt(prop.getProperty("Port"))))); {

        ObjectInputStream ois = 
                 new ObjectInputStream(sChannel.socket().getInputStream());

        hg = (HexGrouping)ois.readObject();
    }
    return hg;

}

包含所有六边形并确定位置的HexGrouping。还有一些其他方法,但我认为这些是相关的。

public class HexGrouping implements Serializable {
  public HexTile[] table;
  private Hashtable<Point,Integer> radialLookup = new Hashtable<Point,Integer>();
  //All constants for the game
  public final static Color BACKGROUND = Color.LIGHTGRAY;
  public final static Color OUTLINE = Color.BLACK;
  public final static Color HEXFILL = Color.WHITE;
  public final static Color HOMEOUTLINE = Color.YELLOW;
  protected final static double SIZE = 220; //pixel size of the tile
  protected final static int BORDER = 4;
  protected final static boolean FLAT = true; // flat orientation or pointy
  private final int players;


public HexGrouping(int players){
//Constructor

    HexMath.init(); // intiate the HexMath Static Class for later calculations
    double playWidth;
    double playHeight;
    if (players <= 6){
        table = new HexTile[37];
        playWidth = 6 * SIZE; // something happens here the whole data isn't in scope
        playHeight = 6 * SIZE;

    }
    else {
        table = new HexTile[61];
        playWidth = 8.5 * SIZE;
        playHeight = 8 * SIZE;
    }
    for (int i = 0; i < table.length; i++){
           table[i] = new HexTile(); // initialize the array with HexTiles

    }      
    this.players = players;

    setAllRadial(players);
    setCoordinates(playWidth, playHeight);
    setBoard(players);


}
private void setAllRadial(int player){
/* Give the hexes radial coordinates so they can be referenced and distance can be measured, these coordinates then determine the
 * cartisiaun coordinates for each hex so it can be arranged and connected
 */ 
    int q0;
    int r0 = 0;
    int max;
    int counter = 1;
    int q0start = 0;

    if (player > 6) {
      q0 = 4;
      q0start = 4;
      max = 9;
    }
    else {
      q0 = 3;
      q0start = 3;
      max = 7;
    }

    //Provides the radial coordinates for each of the hextiles, brain hurts on this math
    for (int i = 0; i < table.length; i++){
        Point p = new Point(-q0,r0);
        table[i].setRadial(p);
        radialLookup.put(p, i);
        r0++;
    if (++counter > max - Math.abs(q0)) {
         q0--;
         counter = 1;
         if (q0 < 0)
            r0 = -q0start;
         else
            r0 = (q0start + 1) - (max-Math.abs(q0));

    }

    }
}
private void setCoordinates(double w, double h){
/* provide coordinates for the graphics to draw for each of the hexes. This is only used for rendering and is based on the radial position
 * it takes the width and height of the render environment to place the center hex in the middle
 */

    for (int i = 0; i < table.length; i++){
           table[i].setDrawXandY(HexMath.Radialto1stPix(table[i].getRadial(), w/2+SIZE, h/2));


    }


}

0 个答案:

没有答案