javafx Hanoi
Netbean java8 source code :http://files.cnblogs.com/files/rojas/HNT.zip
screenshot:
1 model
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hnt.model;
- /**
- *
- * @author yang
- */
- public class Pan {
- private String ptag;
- private double psize;
- public Pan(String ptag, double psize) {
- this.ptag = ptag;
- this.psize = psize;
- }
- /**
- * @return the ptag
- */
- public String getPtag() {
- return ptag;
- }
- /**
- * @param ptag the ptag to set
- */
- public void setPtag(String ptag) {
- this.ptag = ptag;
- }
- /**
- * @return the psize
- */
- public double getPsize() {
- return psize;
- }
- /**
- * @param psize the psize to set
- */
- public void setPsize(double psize) {
- this.psize = psize;
- }
- }
2 common logic
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hnt.control;
- import hnt.model.Pan;
- import java.util.Stack;
- /**
- *
- * @author yang
- */
- public class Logic {
- public void setNewPole(Stack<Pan> pole, int capcity) {
- int i = capcity - ;
- while (i >= ) {
- pole.push(new Pan("p" + i, i * + ));
- i--;
- }
- }
- public int MovePan(Stack<Pan> soruce, Stack<Pan> target) {
- if (soruce.size() == ) {
- return ;
- }
- if (target.size() == ) {
- target.push(soruce.pop());
- return ;
- } else {
- Pan pans = soruce.peek();
- Pan pant = target.peek();
- if (CheckSize(pans, pant)) {
- target.push(soruce.pop());
- return ;
- }
- return ;
- }
- }
- private boolean CheckSize(Pan pans, Pan pant) {
- boolean canMove = false;
- if (pans.getPsize() < pant.getPsize()) {
- canMove = true;
- }
- return canMove;
- }
- }
3 call point
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hnt;
- import java.io.IOException;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Scene;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- /**
- *
- * @author yang
- */
- public class HNT extends Application {
- @Override
- public void start(Stage primaryStage) throws IOException {
- FXMLLoader loader=new FXMLLoader();
- loader.setLocation(HNT.class.getResource("/hnt/showHNT.fxml"));
- BorderPane bp= loader.load();
- ShowHNTController shnt= loader.getController();
- //shnt.doinitialize();
- StackPane root = new StackPane();
- root.getChildren().add(bp);
- Scene scene = new Scene(root, , );
- primaryStage.setTitle("Hello World!");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
4 control
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hnt;
- import hnt.control.Logic;
- import hnt.model.Pan;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.ResourceBundle;
- import java.util.Stack;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.ListView;
- import javafx.scene.control.TextField;
- /**
- * FXML Controller class
- *
- * @author yang
- */
- public class ShowHNTController implements Initializable {
- Logic l = new Logic();
- Stack<Pan> source = new Stack<>();
- Stack<Pan> target = new Stack<>();
- private int capcity = ;
- @FXML
- TextField tfcapcity = new TextField("" + getCapcity());
- @FXML
- public void handleTextChange(final ActionEvent event) {
- TextField tmptf = (TextField) event.getSource();
- //System.out.println("----------------- "+tmptf.getText());
- setCapcity(Integer.parseInt(tmptf.getText()));
- refreshListView();
- }
- public void refreshListView() {
- Stack<Pan> pole = new Stack();
- l.setNewPole(pole, getCapcity());
- obsA = translate2SomeSymbol(pole);
- poleA.setItems(obsA);
- selecteditem.add("A");
- obsB.clear();
- obsC.clear();
- mapAear.put("obsA", obsA);
- mapAear.put("obsB", obsB);
- mapAear.put("obsC", obsC);
- poleA.setItems(mapAear.get("obsA"));
- poleB.setItems(mapAear.get("obsB"));
- poleC.setItems(mapAear.get("obsC"));
- //clear status
- selecteditem = new ArrayList<>();
- source.removeAllElements();
- target.removeAllElements();
- System.out.println(" come to refreshListView()");
- }
- List<String> selecteditem = new ArrayList<>();
- int countMoveTimes = ;
- @FXML
- Label MoveTimes = new Label("MoveTimes :" + countMoveTimes);
- @FXML
- Button A;
- @FXML
- Button B;
- @FXML
- Button C;
- static ObservableList<Button> obsA = FXCollections.observableArrayList();
- @FXML
- ListView<Button> poleA = new ListView<>(obsA);
- static ObservableList<Button> obsB = FXCollections.observableArrayList();
- @FXML
- ListView<Button> poleB = new ListView<>(obsB);
- static ObservableList<Button> obsC = FXCollections.observableArrayList();
- @FXML
- ListView<Button> poleC = new ListView<>(obsC);
- // String Aear[]={"obsA","obsB","obsC"};
- static Map<String, ObservableList> mapAear = new HashMap<>();
- /**
- * Initializes the controller class.
- */
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // String Aear[]={"obsA","obsB","obsC"};
- //MoveTimes = new Label("MoveTimes");
- Stack<Pan> pole = new Stack();
- l.setNewPole(pole, getCapcity());
- obsA = translate2SomeSymbol(pole);
- poleA.setItems(obsA);
- selecteditem.add("A");
- mapAear.put("obsA", obsA);
- mapAear.put("obsB", obsB);
- mapAear.put("obsC", obsC);
- System.out.println(" come to ShowHNTController");
- }
- //to button
- private ObservableList translate2SomeSymbol(Stack<Pan> pole) {
- ObservableList<Button> obs = FXCollections.observableArrayList();
- int pdelay=pole.size();
- for (Pan pole1 : pole) {
- Button bt = new Button();
- bt.setText(pole1.getPtag());
- bt.setRotate();
- bt.setMaxWidth(pole1.getPsize());
- bt.setTranslateX();
- obs.add(bt);
- }
- return obs;
- }
- // to SomeSymbol
- private Stack<Pan> retranslate2SomeSymbol(ObservableList<Button> bts) {
- // ObservableList<Button> obs=FXCollections.observableArrayList();
- Stack<Pan> pole = new Stack<>();
- for (Button bt : bts) {
- Pan p = new Pan(bt.getText(), bt.getWidth());
- pole.add(p);
- }
- return pole;
- }
- String buttoncolor = "-fx-base: #b6e7c9;";
- public void ChangeColor(String which, String colorstr) {
- // String buttoncolor="-fx-base: #b6e7c9;";
- for (String key : mapAear.keySet()) {
- if (key.contains(which) && (mapAear.get(key).size() > )) {
- Button bt = (Button) mapAear.get(key).get(mapAear.get(key).size() - );
- bt.setStyle(colorstr);
- }
- }
- }
- @FXML
- public void handleClick(final ActionEvent event) {
- Button bt = (Button) event.getSource();
- //System.out.println(" from which "+bt.getText()+" width "+bt.getWidth());
- if (selecteditem.isEmpty()) {
- selecteditem.add(bt.getText());
- ChangeColor(bt.getText(), buttoncolor);
- return;
- } else {
- if (selecteditem.size() >= ) {
- selecteditem.remove();
- selecteditem.add(bt.getText());
- ChangeColor(bt.getText(), buttoncolor);
- } else {
- selecteditem.add(bt.getText());
- ChangeColor(bt.getText(), buttoncolor);
- }
- }
- // move action
- if ((selecteditem.size() > ) && (!selecteditem.get().equals("")) && (!selecteditem.get().equals(selecteditem.get()))) {
- // System.out.println(" selected name " +obsA.getClass().getName());
- for (String key : mapAear.keySet()) {
- if (key.contains(selecteditem.get())) {
- source = retranslate2SomeSymbol(mapAear.get(key));
- }
- if (key.contains(selecteditem.get())) {
- target = retranslate2SomeSymbol(mapAear.get(key));
- }
- }
- ////===============
- l.MovePan(source, target);
- for (String key : mapAear.keySet()) {
- if (key.contains(selecteditem.get())) {
- mapAear.replace(key, translate2SomeSymbol(source));
- }
- if (key.contains(selecteditem.get())) {
- mapAear.replace(key, translate2SomeSymbol(target));
- }
- }
- ChangeColor(bt.getText(), "");
- poleA.setItems(mapAear.get("obsA"));
- poleB.setItems(mapAear.get("obsB"));
- poleC.setItems(mapAear.get("obsC"));
- //MoveTimes =new Label("MoveTimes");
- countMoveTimes += ;
- MoveTimes.setText("MoveTimes:" + countMoveTimes);
- //clear status
- selecteditem = new ArrayList<>();
- source.removeAllElements();
- target.removeAllElements();
- }
- }
- /**
- * @return the capcity
- */
- public int getCapcity() {
- return capcity;
- }
- /**
- * @param capcity the capcity to set
- */
- public void setCapcity(int capcity) {
- this.capcity = capcity;
- }
- }
5 FXML
- <?xml version="1.0" encoding="UTF-8"?>
- <?import javafx.geometry.*?>
- <?import javafx.scene.text.*?>
- <?import java.lang.*?>
- <?import java.util.*?>
- <?import javafx.scene.*?>
- <?import javafx.scene.control.*?>
- <?import javafx.scene.layout.*?>
- <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hnt.ShowHNTController">
- <bottom>
- <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="100.0" BorderPane.alignment="CENTER">
- <children>
- <Button fx:id="A" minHeight="20.0" minWidth="20.0" mnemonicParsing="false" onAction="#handleClick" prefHeight="23.0" prefWidth="100.0" text="A" />
- <Button fx:id="B" mnemonicParsing="false" onAction="#handleClick" prefHeight="23.0" prefWidth="100.0" text="B" />
- <Button fx:id="C" mnemonicParsing="false" onAction="#handleClick" prefHeight="23.0" prefWidth="100.0" text="C" />
- </children>
- </HBox>
- </bottom>
- <center>
- <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
- <children>
- <ListView fx:id="poleA" prefHeight="200.0" prefWidth="200.0" rotate="180.0" />
- <ListView fx:id="poleB" prefHeight="200.0" prefWidth="200.0" rotate="180.0" />
- <ListView fx:id="poleC" prefHeight="200.0" prefWidth="200.0" rotate="180.0" />
- </children>
- </HBox>
- </center>
- <top>
- <VBox prefHeight="52.0" prefWidth="600.0" BorderPane.alignment="CENTER">
- <children>
- <Label alignment="CENTER" prefHeight="15.0" prefWidth="600.0" text="HNT" textAlignment="CENTER">
- <font>
- <Font size="27.0" />
- </font>
- </Label>
- <HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0">
- <children>
- <Label fx:id="MoveTimes" prefHeight="15.0" prefWidth="365.0" text="Move Times">
- <padding>
- <Insets left="20.0" />
- </padding>
- </Label>
- <Label prefHeight="15.0" prefWidth="42.0" text="Capcity:" />
- <TextField fx:id="tfcapcity" onAction="#handleTextChange" text="">
- <HBox.margin>
- <Insets />
- </HBox.margin>
- <padding>
- <Insets bottom="10.0" />
- </padding>
- </TextField>
- </children>
- </HBox>
- </children>
- </VBox>
- </top>
- </BorderPane>
javafx Hanoi的更多相关文章
- Hanoi问题java解法
用什么语言解法都差不多,思路都是一样,递归,这其中只要注重于开始和结果的状态就可以了,对于中间过程,并不需要深究.(我细细思考了一下,还是算了.=_=) 代码其实很简单注重的是思路. 问题描述:有一个 ...
- 问题记录:JavaFx 鼠标滑轮滚动事件监听!
问题描述: 在listview的item里面添加鼠标拖拽排序功能.代码如下: setOnMouseDragged(event -> { //设定鼠标长按0.3秒后才可拖拽 防止误操作 isCan ...
- JavaFx客户端服务器C/S架构搭建
客户端获取服务器端软件更新版本方法: package com.platform.ui.update; import java.io.BufferedInputStream; import java.i ...
- JavaFX 教程资料收集
1. JavaFX中文资料 http://www.javafxchina.net/blog/docs/tutorial1/ 2. JavaFX入门教程 http://www.xrpmoon.com/c ...
- HDU1329 Hanoi Tower Troubles Again!——S.B.S.
Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...
- 在 linux 上部署并运行 JavaFX 项目
环境 redhat 6.4.eclipse安装JavaFX插件 项目详情及代码参见 在linux上配置并创建JavaFX项目 ,该部署即此文章中项目的部署 配置build.fxbuild 生成buil ...
- 在linux上配置并创建JavaFX项目
本环境为linux配置,因为这里的JavaFX项目是为定制Oracle监控工具而写的.现Oracle已收购Java好几年,用它自己的产生监控自己的东西还是很兼容的.此处Eclipse 为4.5版本. ...
- javafx之登陆界面的跳转
界面布局用到的是fxml而非纯java代码,工具是javafx sence builder 账号:account 密码:password 登陆成功: 可以点击退出登陆返回到登陆页面 工程目录: pac ...
随机推荐
- ViewPager设置不能滚动
设置ViewPager不能滑动 1:设置当前选中的页面 public void setCurrentItem(int item) { mPopulatePending = false; setCurr ...
- <Sicily>Funny Game
一.题目描述 Two players, Singa and Suny, play, starting with two natural numbers. Singa, the first player ...
- OGG切换步骤
步骤描述 提前准备好切换方案:以及其他相关人员的配合 切换至容灾数据库: (1)停止前端业务,确认目标端数据已经追平 (2)数据校验,确认数据一致 (3)停止生产库OGG进程(停止后可以直接删除) ( ...
- NetBios, NetBios over TCP/IP, SMB 之间的关系
首先提到的是NetBios,NetBios是Network Basic Input/Output System的缩写,提供了一种允许局域网内不同电脑能够通信的功能.严格来说,NetBios是一套API ...
- 使用jemdoc制作个人主页
jemdoc官网说明: http://jemdoc.jaboc.net/index.html 作者的个人主页:https://jemnz.com/ 将下载的jemdoc.py文件和需要转化的xxx.j ...
- HDU 4939 Stupid Tower Defense 简单DP
题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...
- 【Round #36 (Div. 2 only) C】Socks Pairs
[题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...
- HDFS 文件系统流程图。PB级文件存储时序图。
大小文件通吃, 热点hash功能. 全局唯一KV索引. 百度网盘模式.断点续传功能.MR分析功能. 来自为知笔记(Wiz)
- cocos2d-x 2.2.0 怎样在lua中注冊回调函数给C++
cocos2d-x内部使用tolua进行lua绑定.可是引擎并没有提供一个通用的接口让我们能够把一个lua函数注冊给C++层面的回调事件. 翻看引擎的lua绑定代码,我们能够仿照引擎中的方法来做. 值 ...
- HDU 5389 Zero Escape(DP + 滚动数组)
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...