计算机体系结构上机 图形用户界面 哈夫曼编码(画出哈夫曼树) 扩展编码 等长编码
//后面附上两个css文件(和源程序放在同一个java project下即可)
//至于图片文件 ...自己找一张jpg和一张gif,分别命名为1.jpg和1.gif放在java project 下即可
//下面是java源程序
import java.io.File;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//import javafx.scene.layout.HBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.text.DecimalFormat;
//import java.lang.System;
import java.math.BigDecimal;
class MyException extends Exception{//用来抛异常的
/**
*
*/
private static final long serialVersionUID = 1L;
public MyException() {
super();//使用父类的构造函数
}
public MyException(String message) {
super(message);//使用父类的构造函数
}
}
class Node{
public double wei = 0.0;
public int len = 0;
public Node parent = null;
public Node lChild = null;
public Node rChild = null;
public String code = "";
public int forName = 0;
public void setName(int name) {
this.forName = name;
}
public Node() {
}
public Node(double wei ,int len ,Node parent,Node lChild,Node rChild) {
this.wei = wei;
this.len = len;
this.parent = parent;
this.lChild = lChild;
this.rChild = rChild;
}
public Node(double wei ,int len ,Node lChild,Node rChild) {
this.wei = wei;
this.len = len;
this.lChild = lChild;
this.rChild = rChild;
}
public Node(double wei ,Node lChild,Node rChild) {
this.wei = wei;
this.lChild = lChild;
this.rChild = rChild;
}
public Node(Node lChild,Node rChild) {
this.lChild = lChild;
this.rChild = rChild;
}
public Node(double wei) {
this.wei = wei;
this.lChild = null;
this.rChild = null;
}
public void setLChild(Node lChild) {
this.lChild = lChild;
}
public void setRChild(Node rChild) {
this.rChild = rChild;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void setWei(double wei) {
this.wei = wei;
}
public void setLen(int len) {
this.len = len;
}
public void setCode(String s) {
this.code = s;
}
public Node getParent() {
return this.parent;
}
public Node getLChild() {
return this.lChild;
}
public Node getRChild() {
return this.rChild;
}
public double getWei() {
return this.wei;
}
}
public class hgd extends Application{
public static TextField userTextField = new TextField();
public static Text actiontarget = new Text();
public static Stage primaryStage = new Stage();
public static double theH = 0.0;
public static DecimalFormat df = new DecimalFormat( "0.000");
@Override
public void start(Stage pprimaryStage) {
// TODO Auto-generated method stub
HandlerClass handler1 = new HandlerClass();
Label label1 = new Label("Please enter a set of usage frequency in the text box below.");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER); //对齐方式(居中)
//设置grippanel属性
grid.setHgap(10); //水平距离
grid.setVgap(10); //垂直距离
grid.setPadding(new Insets(25,25,25,25)); //设置内边距
Text screenTitle = new Text("Welcome");
screenTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
//设置页面标题id
screenTitle.setId("title");
//grid.add(child, 列索引, 行索引, 跨列数, 跨行数);
grid.add(screenTitle, 0, 0, 2, 1);
//账号
Label freLabel = new Label("Frequency:");
grid.add(freLabel, 0, 1);
grid.add(userTextField, 1, 1);
//提交 按钮
Button btn1 = new Button("Confirm");
Button btn2 = new Button(" Quit ");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT); //对齐方式(底部右侧)
hbBtn.getChildren().addAll(btn2,btn1);
grid.add(hbBtn, 1, 4);
//添加一个文本框(用于显示信息的控制)
//final Text actiontarget = new Text();
actiontarget.setId("actiontarget");
grid.add(actiontarget, 1, 6);
/**
* 3.声明按钮事件
* 点击按钮文本显示信息
* */
btn1.setOnAction(handler1);
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
actiontarget.setText("");
primaryStage.close();
}
});
/**
* 4.把容器添加到场景中 并设置场景大小
* ps:如果不设置场景大小,默认是最小
* */
Scene scene = new Scene(grid);
//场景引入css文件
scene.getStylesheets().add(
hgd.class.getResource("Login.css")
.toExternalForm());
primaryStage.setTitle("JavaFX Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
class HandlerClass implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent e) {
// TODO Auto-generated method stub
String theText = userTextField.getText();
if(theText.length()==0) {
userTextField.setText("");
actiontarget.setText("Error!");
return ;
}
String[] text = theText.split(" ");
double[] doubleText = new double[text.length];
double sum =0.0;
for(int i = 0;i < text.length ;i++) {
doubleText[i] = Double.parseDouble(text[i]);
sum += doubleText[i];
}
double eps = 0.00001;
if(Math.abs((sum -1.0))>eps) {
userTextField.setText("");
actiontarget.setText("Error!");
}
else {
actiontarget.setText("");
A = doubleText;
len = A.length;
c = 1;
createHuffGraph();
primaryStage.close();
//计算H
theH = 0.0;
for(int i = 0;i < doubleText.length;i ++) {
theH += doubleText[i]*(Math.log(doubleText[i])/Math.log(2.0));
}
theH = -theH;
//int theHInteger = Integer.parseInt(String.valueOf(theH*100));
System.out.println(theH);
//System.out.println(Math.log(4)/Math.log(2));
//先等长 哈夫曼 扩展 三种选一
askForWhichCode();
//询问码点方式输入扩展方式还是码长方式输入扩展方式
}
}
}
public static Stage askForWhichCodeStage = new Stage();
public static void askForWhichCode() {
Button btn1 = new Button("等长");
Button btn2 = new Button("哈夫曼");
Button btn3 = new Button("扩展");
Button btn4 = new Button("Back");
HBox hb = new HBox();
//hb.set
hb.getChildren().addAll(btn4,btn1,btn2,btn3);
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
askForWhichCodeStage.close();
isometricView();
}
});
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
askForWhichCodeStage.close();
huffView();
}
});
btn3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
askForWhichCodeStage.close();
askForWhich();
}
});
btn4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public voi