package practice;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class DrawHistogram {
private DrawFrame Fwin;
static long TimeOfUsingBSTree;
static long TimeOfUsingArrays;
static long TimeOfUsingHashChain;
public long getArraysTime(){
return TimeOfUsingArrays;
}
public long getBSTreeTime(){
return TimeOfUsingBSTree;
}
public long getHashChainTime(){
return TimeOfUsingHashChain;
}
public void setTrueVis(){
Fwin.setVisible(true);
}
public DrawHistogram(int[]set){
long methodAgo = System.nanoTime();
Fwin = new DrawFrame("数组方式求解直方图",set);
Fwin.setLocation(50,0);
long methodEnd = System.nanoTime();
TimeOfUsingArrays = (methodEnd - methodAgo)/1000;
//System.out.println("TimeOfUsingArrays: "+TimeOfUsingArrays);
}
public DrawHistogram(BSTree bst,int[] set){
long methodAgo = System.nanoTime();
Fwin = new DrawFrame("二叉搜索树方式求解直方图",bst,set);
Fwin.setLocation(650,0);
long methodEnd = System.nanoTime();
TimeOfUsingBSTree = (methodEnd - methodAgo)/1000;
//System.out.println("TimeOfUsingBSTree: "+TimeOfUsingBSTree);
}
public DrawHistogram(HashChain hash,int[]set){
long methodAgo = System.nanoTime();
Fwin = new DrawFrame("链表散列方式求解直方图",hash,set);
Fwin.setLocation(30,360);
long methodEnd = System.nanoTime();
TimeOfUsingHashChain = (methodEnd - methodAgo)/1000;
//System.out.println("TimeOfUsingHashChain: "+TimeOfUsingHashChain);
}
public void ShowTime(){
JFrame jf =new JFrame ("三种方法所用时间柱形图比较");
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(600,360);
jf.setVisible(true);
jf.setLocation(640,360);
long[] time = new long[3];
time[1] = TimeOfUsingBSTree;
time[0] = TimeOfUsingArrays;
time[2] = TimeOfUsingHashChain;
DrawComponent dr = new DrawComponent();
dr.setLayout(new BorderLayout());
Panel pl = new Panel(time,3);
dr.add(pl);
jf.add(dr);
}
public static void FinalRandomTest(long[]array,long[]bst,long[]hash){
JFrame jf =new JFrame ("随机数测试");
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(1200,700);
jf.setVisible(true);
jf.setLocation(50,20);
long max1 = Max(array);
long max2 = Max(bst);
long max3 = Max(hash);
long max = Max(max1,max2,max3);
DrawComponent dr = new DrawComponent();
dr.setLayout(new BorderLayout());
Panel pl = new Panel(array,bst,hash,max);
dr.add(pl);
jf.add(dr);
}
public static long Max(long[] set){
long retu = 0;
for(int i=0;i<set.length;i++){
if(retu<set[i])
retu = set[i];
}
return retu;
}
public static long Max(long a,long b,long c){
long retu = a;
if(retu < b)
retu = b;
if(retu < c)
retu = c;
return retu;
}
}
class DrawFrame extends JFrame{//画图窗口类,设置大小,可见性,关闭方式
DrawComponent con;
int color=1;
int precolor = color;
public DrawFrame(String str,int[]set){
setTitle(str);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,360);
setVisible(false);
con = new DrawComponent();
con.setLayout(new BorderLayout());
Panel pl = new Panel(set);
con.add(pl,BorderLayout.CENTER);
this.add(con);
}
public DrawFrame(String str,BSTree bst,int[]set){
setTitle(str);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,360);
setVisible(false);
con = new DrawComponent();
con.setLayout(new BorderLayout());
Panel pl = new Panel(bst,set);
con.add(pl,BorderLayout.CENTER);
this.add(con);
}
public DrawFrame(String str,HashChain hash,int[]set){
setTitle(str);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,360);
setVisible(false);
con = new DrawComponent();
con.setLayout(new BorderLayout());
Panel pl = new Panel(hash,set);
con.add(pl,BorderLayout.CENTER);
this.add(con);
}
}
class DrawComponent extends JComponent{//容器类,画出坐标轴
}
class Panel extends JPanel{
private int x = 0; //矩形的左上点x坐标
private int y = 0; //举行的左上点y坐标
private int width = 0; //矩形的长度(宽度)
private int height = 0; //举行的高度
private int LengthOfEvery = 0; //矩形单位宽度
private int PerHeight =0; //矩形单位高度
int color=1; //颜色设置
int precolor = color;//颜色设置
Rectangle2D [] allRect;//存放所有等待画出来的矩形
String[] allString;//存放所有待画的关键字!!!!!
int[] XofStr;//存放关键字的X Y坐标
int[] YofStr;
String[] freString;
int[] xoffre;
int[] yoffre;
int numberOfRect = 0;
int numberOfLine = 0;
public Panel(HashChain hash,int[] set){ //panel画板的HashChain类定义
for(int i =0;i<set.length;i++){
hash.Add(set[i]);
}
int allDatasnum = hash.getNumOFdatas();//所有关键字的总数,用来声明数组等
int maxfre = hash.getMaxFre();//最大的出现次数,用来计算单位高度
this.setSize(560, 300);
allRect = new Rectangle2D[allDatasnum];
allString = new String[allDatasnum];
XofStr = new int[allDatasnum];
YofStr = new int[allDatasnum];
freString = new String[allDatasnum];
xoffre = new int[allDatasnum];
yoffre = new int[allDatasnum];
for(int i =0;i<allDatasnum;i++){
String str = new String();
allString[i] = str;
}
LengthOfEvery = 530/allDatasnum;
PerHeight = 270/maxfre;
int[] num = new int[allDatasnum];
int[] fre = new int[allDatasnum];
hash.getData();//将链表散列转换为两个数组,一个存储关键字,另一个存储出现的次数
num = hash.getdatas();
fre = hash.getfres();
numberOfRect = allDatasnum;
for(int i =0;i<allDatasnum;i++){//将散列中全部数据转化为待画的矩形,放在数组中
int currentdata = num[i];
int currentFre = fre[i];
x = 20+5*(i+1)+i*LengthOfEvery;
y = 300-(currentFre*PerHeight);
width = LengthOfEvery;
height = PerHeight * currentFre;
Rectangle2D rectan = new Rectangle.Double(x,y,width,height);
allRect[i] = rectan;
String strofdata = String.valueOf(currentdata);
allString[i] = strofdata;
XofStr[i] = x+10;
YofStr[i] =315;
freString[i] = String.valueOf(currentFre);
xoffre[i] = x+10;
yoffre[i] = y-10;
}
}
public Panel(BSTree bst,int[]set){ //panel画板的BSTree类定义
for(int i =0;i<set.length;i++){
bst.Insert(set[i]);
}
this.setSize(560, 300);
int allDatasnum = bst.getNumber();//所有关键字的总数,用来声明数组等
int maxfre = bst.getmaxFre(); //最大出现次数
allRect = new Rectangle2D[allDatasnum];//存放所有待画的矩形
allString = new String[allDatasnum];
XofStr = new int[allDatasnum];
YofStr = new int[allDatasnum];
freString = new String[allDatasnum];
xoffre = new int[allDatasnum];
yoffre = new int[allDatasnum];
for(int i =0;i<allDatasnum;i++){
String str = new String();
allString[i] = str;
}
LengthOfEvery = 530/allDatasnum;
PerHeight = 270/maxfre;
numberOfRect = allDatasnum;
for(int i = 0;i<allDatasnum;i++){
int currentdata = bst.Min();
int currentFre = bst.Delete(currentdata);
x = 20+5*(i+1)+i*LengthOfEvery;
y = 300-(currentFre*PerHeight);
width = LengthO
评论0
最新资源