/** * sets all <i>Actor</i> fields, guaranteeing values within the specified range. Later, it will be * treated as a virtual method, and subclasses will call this (the superclass method) to perform * its work. */ public void inputAllFields() { setName( Input.instance.getString( getClass().getSimpleName() + ":Current Name:" + name + " New Name:")); setStrength( Input.instance.getDouble( String.format("Strength:%.1f", strength), MIN_STRENGTH, MAX_STRENGTH)); setHealth( Input.instance.getDouble(String.format("Health:%.1f", health), MIN_HEALTH, MAX_HEALTH)); setSpeed(Input.instance.getDouble(String.format("Speed:%.1f", speed), MIN_SPEED, MAX_SPEED)); } // end void inputAllFields()
/** * <i>Actor</i> constructor is used when building <i>Actor</i> objects automatically: * <i>strength</i>, <i>health</i>, <i>speed</i> fields are given randomly generated values within * their range; <i>name</i> is given a sequentially numbered name: <i>Auto:<b>n</b></i> where * <i><b>n</b></i> is the sequence number. The <i>name</i> can be edited to create an unique * <i>Actor</i>. * * @param subclassCount used to support automatic naming (which includes a unique serial number). * @param armyAllegiance used to support the <i>Army</i>-specific <i>DropShadow</i> glow around * this Actor object. */ public Actor(int subclassCount, Army armyAllegiance) { this.armyAllegiance = armyAllegiance; ++actorSerialNumber; // static class-oriented variable. There is one-and-only-one instance of // this variable regardless of the number of Actor objects in existence // (from none to infinity). setName( String.format( "%d:%s:%d:", actorSerialNumber, getClass().getSimpleName(), subclassCount)); // An alternate way to assemble a String to use as a name. Because of // polymorphism "getClass().getName()" will return the subclass name // when they exist. setStrength(SingletonRandom.instance.getNormalDistribution(MIN_STRENGTH, MAX_STRENGTH, 2.0)); setHealth(SingletonRandom.instance.getNormalDistribution(MIN_HEALTH, MAX_HEALTH, 2.0)); setSpeed(SingletonRandom.instance.getNormalDistribution(MIN_SPEED, MAX_SPEED, 2.0)); createAvatar(); tooltip = new Tooltip(toString()); Tooltip.install(getAvatar(), tooltip); tt = new TranslateTransition(); tt.setNode(getAvatar()); // reuse } // end Actor constructor
/** * createTable is static to allow Army to define a table without having any Actor objects present. */ public static TableView<Actor> createTable() { TableView<Actor> table = new TableView<Actor>(); final double PREF_WIDTH_DOUBLE = 80.0; table.setPrefWidth( PREF_WIDTH_DOUBLE * 7.5); // 7.0 because there are 6 individual columns, but one of those is DOUBLE-WIDTH, // and there is some inter-column spacing table.setEditable(true); TableColumn<Actor, String> nameCol = new TableColumn<>("Name"); nameCol.setCellValueFactory(new PropertyValueFactory<Actor, String>("name")); nameCol.setPrefWidth(PREF_WIDTH_DOUBLE * 2.0); TableColumn<Actor, Number> healthCol = new TableColumn<>("Health"); healthCol.setCellValueFactory(cell -> cell.getValue().health); healthCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> strengthCol = new TableColumn<>("Strength"); strengthCol.setCellValueFactory(cell -> cell.getValue().strength); strengthCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> speedCol = new TableColumn<>("Speed"); speedCol.setCellValueFactory(cell -> cell.getValue().speed); speedCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> locationXCol = new TableColumn<>("X"); locationXCol.setCellValueFactory(cell -> cell.getValue().getAvatar().translateXProperty()); locationXCol.setPrefWidth(PREF_WIDTH_DOUBLE); TableColumn<Actor, Number> locationYCol = new TableColumn<>("Y"); locationYCol.setCellValueFactory(cell -> cell.getValue().getAvatar().translateYProperty()); locationYCol.setPrefWidth(PREF_WIDTH_DOUBLE); ObservableList<TableColumn<Actor, ?>> c = table.getColumns(); c.add(nameCol); c.add(healthCol); c.add(strengthCol); c.add(speedCol); c.add(locationXCol); c.add(locationYCol); // Compare line ABOVE with line BELOW: The BELOW line looks cleaner and does actually work . . . // but the compiler spits out a warning. The ABOVE line accomplishes the same thing, less // elegantly, but without warnings. // table.getColumns().addAll(nameCol, healthCol, strengthCol, speedCol, locationXCol, // locationYCol); // The following code makes each cell in the selected columns editable (Name, Health, Strength, // Speed) // We CANNOT implement edit capabilities on the X/Y columns since they are READ-ONLY. nameCol.setCellFactory(TextFieldTableCell.<Actor>forTableColumn()); nameCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setName(event.getNewValue()); a.resetAvatarAttributes(); }); healthCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); healthCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setHealth((Double) event.getNewValue()); a.resetAvatarAttributes(); }); strengthCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); strengthCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setStrength((Double) event.getNewValue()); a.resetAvatarAttributes(); }); speedCol.setCellFactory( TextFieldTableCell.<Actor, Number>forTableColumn(new NumberStringConverter())); speedCol.setOnEditCommit( event -> { Actor a = (event.getTableView().getItems().get(event.getTablePosition().getRow())); a.setSpeed((Double) event.getNewValue()); a.resetAvatarAttributes(); }); return table; } // end createTable()