上传试试看 不知道 是不是一定成功的

源代码在线查看: visual.java

软件大小: 63 K
上传用户: xx87293767
关键词:
下载地址: 免注册下载 普通下载 VIP

相关代码

				/**
				 *
				 */
				package k_means;
				
				import java.awt.BorderLayout;
				import java.awt.Dimension;
				import java.awt.event.ActionEvent;
				import java.awt.event.ActionListener;
				import java.io.DataOutputStream;
				import java.io.File;
				import java.io.FileOutputStream;
				import java.io.IOException;
				import java.io.OutputStream;
				
				import javax.swing.BorderFactory;
				import javax.swing.JButton;
				import javax.swing.JFileChooser;
				import javax.swing.JFrame;
				import javax.swing.JLabel;
				import javax.swing.JMenu;
				import javax.swing.JMenuBar;
				import javax.swing.JMenuItem;
				import javax.swing.JPanel;
				import javax.swing.JTextField;
				import javax.swing.JOptionPane;
				
				/**
				 * @author yu
				 *
				 */
				public class Visual implements ActionListener
				{
					private JFrame MainFrame  = null;
					private JMenuBar mb = null;
					private JMenu Menu;
				
					private JFileChooser FileChooser;
					private JFileChooser SaveFile;
					private JButton OpenFile;
					private JButton BtnNumCluster;
					private JButton BtnSave;
				
					private JTextField DataCut;
					private JLabel LabCut;
				
					private JPanel MainPanel;
					private JPanel ButtonPanel;
					private DataPanel VisualPanel;
				
				
				
				
					private File file;
					private String FileName;
					public static int ClusterNum;
				
					public static  PointCluster[] NewData;
				
					public Visual()
					{
				
						JMenuItem MItem_Exit;
						JMenuItem MItem_Clustering;
						JMenuItem MItem_Int;
						MainFrame = new JFrame("K-Means算法可视化");
						//////////////////////////////
						mb = new JMenuBar();
						Menu = new JMenu("文件");
				
						MItem_Exit = new JMenuItem("关闭");
						MItem_Exit.addActionListener(this);
				
						MItem_Clustering = new JMenuItem("Clustering From File");
						MItem_Clustering.addActionListener(this);
				
						MItem_Int = new JMenuItem("可视化聚类");
						MItem_Int.addActionListener(this);
				
						mb.add(Menu);
						Menu.add(MItem_Clustering);
						Menu.add(MItem_Int);
						Menu.add(MItem_Exit);
				
						MainFrame.setJMenuBar(mb);
						///////////////////////////////
						FileChooser = new JFileChooser("C://");
						SaveFile = new JFileChooser("C://");
				
						OpenFile = new JButton("选择源数据文件");
						OpenFile.addActionListener(this);
						BtnSave = new JButton("保存文件");
						BtnSave.addActionListener(this);
						BtnNumCluster = new JButton("输入聚类数目");
						BtnNumCluster.addActionListener(this);
				
				
						LabCut = new JLabel("输入数据分隔符");
						DataCut = new JTextField(6);
						///////////////////////////////
						MainPanel = new JPanel(new BorderLayout(1,1));
				        ButtonPanel = new JPanel(new BorderLayout(1,1));
						VisualPanel = new DataPanel();
				
						ButtonPanel.setBorder(BorderFactory.createLoweredBevelBorder());
						ButtonPanel.add(OpenFile,BorderLayout.WEST);
						ButtonPanel.add(BtnSave,BorderLayout.CENTER);
						ButtonPanel.add(BtnNumCluster,BorderLayout.EAST);
				
						MainPanel.setBorder(BorderFactory.createLoweredBevelBorder());
						MainPanel.add(LabCut,BorderLayout.WEST);
						MainPanel.add(DataCut,BorderLayout.CENTER);
						MainPanel.add(ButtonPanel,BorderLayout.EAST);
				
				
						MainFrame.add(MainPanel,BorderLayout.SOUTH);
					    MainFrame.add(VisualPanel);
				
					    Dimension   screen   =   MainFrame.getToolkit().getScreenSize();
						MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						MainFrame.setLocation(screen.height/4,screen.width/7);
						MainFrame.setResizable(false);
						MainFrame.setSize(600,480);
						MainFrame.setVisible(true);
				
					}
					public void actionPerformed(ActionEvent e)
					{
						// TODO Auto-generated method stub
				
						int result;
						if(e.getActionCommand().equals("选择源数据文件"))
						{
							FileChooser.setDialogTitle("选择输入的数据集");
							FileChooser.setApproveButtonText("确定");
							result = FileChooser.showOpenDialog(MainFrame);
							if (result == JFileChooser.APPROVE_OPTION)
				            {
								file = FileChooser.getSelectedFile();
								FileName = file.getParent()+"\\"+file.getName();
				            }
				
						}
						if(e.getActionCommand().equals("保存文件"))
						{
							SaveFile.setDialogTitle("保存数据集");
							SaveFile.setApproveButtonText("确定");
							result = SaveFile.showSaveDialog(MainFrame);
							if (result == JFileChooser.APPROVE_OPTION)
				            {
								try
								{
									OutputStream fos = new FileOutputStream(SaveFile.getSelectedFile().getPath());
									DataOutputStream dos = new DataOutputStream(fos);
									for(int i = 0; i < NewData.length; i++)
									{
										dos.writeBytes(String.valueOf(i) + "------cluster\n" );
										for(int j = 0; j < NewData[i].getCluData().size(); j++)
										{
											dos.writeBytes(String.valueOf(NewData[i].getCluData().get(j).x) + "," + String.valueOf(NewData[i].getCluData().get(j).y) + "\n");
										}
				
									}
								}
								catch(IOException es)
								{
									JOptionPane.showMessageDialog(MainFrame, "haha");
								}
				            }
						}
						if(e.getActionCommand().equals("Clustering From File"))
						{
							try
							{
								if(FileName == null)
								{
									JOptionPane.showMessageDialog(MainFrame, "the name of the file can not null ");
								}
								else
								{
									try
									{
										Clustering c = new Clustering(ClusterNum,FileName,DataCut.getText());
										c.output();
									}
									catch(Exception e2)
									{
										JOptionPane.showMessageDialog(MainFrame, "未输入聚类数目或者数据分隔符错误" + "error" + e2.getMessage());
									}
				
								}
							}
							catch (Exception e1)
							{
								JOptionPane.showMessageDialog(MainFrame, e1.getMessage());
							}
						}
						if(e.getActionCommand().equals("可视化聚类"))
						{
							try
							{
								PointClustering p = new PointClustering(ClusterNum,VisualPanel.getList());
								NewData = p.Clu;
								p.output();
								JOptionPane.showMessageDialog(MainFrame, "聚类结束,请点击output clustering查看结果");
							}
							catch(Exception e2)
							{
								JOptionPane.showMessageDialog(MainFrame, "未输入聚类数目或者数据未空" + "error" + e2.getMessage());
							}
						}
						if(e.getActionCommand().equals("输入聚类数目"))
						{
							try
							{
								ClusterNum = Integer.parseInt(JOptionPane.showInputDialog(MainFrame,"请输入聚类的数目"));
							}
							catch(NumberFormatException nume)
							{
								JOptionPane.showMessageDialog(MainFrame, "数据格式错误"+ nume.getMessage());
							}
						}
						if(e.getActionCommand().equals("Exit"))
						{
							System.exit(0);
						}
				
					}
					public static void main(String[] args) throws IOException
					{
						new Visual();
					}
				
				}
							

相关资源