diff --git a/Main.java b/Main.java index bab7114..502eeae 100644 --- a/Main.java +++ b/Main.java @@ -26,7 +26,7 @@ public static void main(String[] args) { HttpGet getHomePage = new HttpGet("http://www.zhihu.com/"); try { - //填充登陆请求中基本的参数 + //填充登陆请求中基本的参数。 CloseableHttpResponse response = httpClient.execute(getHomePage); String responseHtml = EntityUtils.toString(response.getEntity()); String xsrfValue = responseHtml.split("")[0]; diff --git "a/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/CalcWeightAndDoc.java" "b/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/CalcWeightAndDoc.java" deleted file mode 100644 index 494444b..0000000 --- "a/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/CalcWeightAndDoc.java" +++ /dev/null @@ -1,338 +0,0 @@ -package org.geekgao.one; - -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.DocumentHelper; -import org.dom4j.Element; -import org.dom4j.io.OutputFormat; -import org.dom4j.io.SAXReader; -import org.dom4j.io.XMLWriter; - -import java.io.*; -import java.util.*; - -public class CalcWeightAndDoc { - //这三个常量是训练文章的存储的地方 - private final String positiveArticlePath = "/home/geekgao/practice/positive"; - private final String negativeArticlePath = "/home/geekgao/practice/negative"; - private final String unsureArticlePath = "/home/geekgao/practice/unsure"; - - //这两个是词典的位置 - private final String posiDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-positive.txt"; - private final String negaDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-negative.txt"; - - private Map positiveWord;//存储积极词汇的map - private Map negativeWord;//存储消极词汇的map - private Map unsureWord;//存储不确定词汇的map - - //这两个存储词典中的词语 - private Set positiveDict; - private Set negativeDict; - - //需要的全局变量 - private boolean isGroup = false; - String strTemp;//从xml文件解析词语时用到的临时变量 - - public static void main(String[] args) { - new CalcWeightAndDoc().launch(); - } - - public void launch() { - positiveDict = new HashSet(); - negativeDict = new HashSet(); - - readEmotionWord(positiveDict,posiDictPath); - readEmotionWord(negativeDict,negaDictPath); - - //这里两个地址是目标地址,生成的文件就在下面两个地址里 - calcDoc("/home/geekgao/doc.xml"); - calcWeight("/home/geekgao/weight.xml"); - - System.out.println("执行完毕!"); - } - - public void readEmotionWord(Set Dict, String dictPath) { - File file = new File(dictPath); - BufferedReader reader = null; - try { - String t; - reader = new BufferedReader(new FileReader(file)); - while ((t = reader.readLine()) != null) { - Dict.add(t); - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - - } - } - } - } - - //参数是生成的xml文件的路径与名字 - public void calcDoc(String resultPath) { - File negative[] = new File(negativeArticlePath).listFiles(); - File positive[] = new File(positiveArticlePath).listFiles(); - File unsure[] = new File(unsureArticlePath).listFiles(); - double negCount = 0; - double posCount = 0; - double unsCount = 0; - - try { - for (File file : negative) { - if (file.isFile()) { - negCount++; - } - } - - for (File file : positive) { - if (file.isFile()) { - posCount++; - } - } - - for (File file : unsure) { - if (file.isFile()) { - unsCount++; - } - } - } catch(NullPointerException e){ - System.out.println("程序因为空引用结束!"); - System.exit(1); - } - - //建立document对象 - try { - Document document = DocumentHelper.createDocument(); - - Element root = document.addElement("root");//添加文档根 - Element request = root.addElement("prior"); //添加root的子节点 - request.addAttribute("pNegative", String.valueOf(negCount/(negCount + posCount + unsCount))); - request.addAttribute("pPositive", String.valueOf(posCount/(negCount + posCount + unsCount))); - request.addAttribute("pUnsure", String.valueOf(unsCount/(negCount + posCount + unsCount))); - - OutputFormat format = OutputFormat.createPrettyPrint(); - format.setEncoding("UTF-8");//根据需要设置编码 - // 输出全部原始数据,并用它生成新的我们需要的XML文件 - XMLWriter writer2 = new XMLWriter(new FileWriter(new File(resultPath)), format); - writer2.write(document); //输出到文件 - writer2.close(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - //参数是生成的xml文件的路径与名字 - public void calcWeight(String resultPath) { - positiveWord = new HashMap(); - negativeWord = new HashMap(); - unsureWord = new HashMap(); - - //计算各自类别所有文章中每个词汇出现的次数 - getWordMap(positiveWord,positiveArticlePath); - getWordMap(negativeWord,negativeArticlePath); - getWordMap(unsureWord,unsureArticlePath); - - //存储计算后验概率公式中的分母的第一部分,第二部分等于1 - double allPosWeight = 0; - double allNegWeight = 0; - double allUnsWeight = 0; - - //保留各个Map的情感词汇 - keepEmotionWord(positiveWord); - keepEmotionWord(negativeWord); - keepEmotionWord(unsureWord); - - /*System.out.println(positiveWord); - System.out.println(negativeWord); - System.out.println(unsureWord);*/ - - /* - (1)遍历positiveWord这个Map,得到里面的各个词语在积极词汇中的次数,再在其他两个Map中查看是否有这个词语,有,就把其他的那个 - 次数加到当前Map的当前词语的value上,并且删除那个Map中的当前词语;没有这个词的话,那么在那个;类别中出现的次数就是0. - (2)遍历negativeWord,不用看positiveWord了,只需看unsureWord,处理方法同上。 - (3)遍历unsureWord,这些词在其他两个类别中都是0,直接得到在当前类别中的值 - */ - - try { - Document xmlFile = DocumentHelper.createDocument();//建立一个xml文档 - Element root = xmlFile.addElement("root"); - - Set word = positiveWord.keySet(); - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - Integer count = positiveWord.get(tmp); - allPosWeight += count; - } - - word = negativeWord.keySet(); - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - Integer count = negativeWord.get(tmp); - allNegWeight += count; - } - - word = unsureWord.keySet(); - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - Integer count = unsureWord.get(tmp); - allUnsWeight += count; - } - - word = positiveWord.keySet(); - for (Iterator it = word.iterator(); it.hasNext(); ) { - Element wd = root.addElement("word");//建立新的词语节点 - String tmp = (String) it.next(); - wd.addAttribute("data",tmp); - Integer count; - - count = positiveWord.get(tmp); - wd.addAttribute("pPositive",String.valueOf(count / (allPosWeight + 1))); - - if (negativeWord.containsKey(tmp)) { - count = negativeWord.get(tmp); - negativeWord.remove(tmp); - wd.addAttribute("pNegative",String .valueOf(count / (allNegWeight + 1))); - } else { - wd.addAttribute("pNegative","0"); - } - - if (unsureWord.containsKey(tmp)) { - count = unsureWord.get(tmp); - unsureWord.remove(tmp); - wd.addAttribute("pUnsure",String.valueOf(count / (allUnsWeight + 1))); - } else { - wd.addAttribute("pUnsure","0"); - } - } - - word = negativeWord.keySet(); - for (Iterator it = word.iterator(); it.hasNext(); ) { - Element wd = root.addElement("word");//建立新的词语节点 - String tmp = (String) it.next(); - wd.addAttribute("data",tmp); - Integer count; - - wd.addAttribute("pPositive","0"); - count = negativeWord.get(tmp); - wd.addAttribute("pNegative",String .valueOf(count / (allNegWeight + 1))); - - if (unsureWord.containsKey(tmp)) { - count = unsureWord.get(tmp); - unsureWord.remove(tmp); - wd.addAttribute("pUnsure",String.valueOf(count / (allUnsWeight + 1))); - } else { - wd.addAttribute("pUnsure","0"); - } - } - - word = unsureWord.keySet(); - for (Iterator it = word.iterator(); it.hasNext(); ) { - Element wd = root.addElement("word");//建立新的词语节点 - String tmp = (String) it.next(); - wd.addAttribute("data",tmp); - Integer count; - - wd.addAttribute("pPositive","0"); - wd.addAttribute("pNegative","0"); - count = unsureWord.get(tmp); - wd.addAttribute("pUnsure",String.valueOf(count / (allUnsWeight + 1))); - } - - //输出全部原始数据,在编译器中显示 - OutputFormat format = OutputFormat.createPrettyPrint(); - format.setEncoding("UTF-8");//根据需要设置编码 - // 输出全部原始数据,并用它生成新的我们需要的XML文件 - XMLWriter writer2 = new XMLWriter(new FileWriter(new File(resultPath)), format); - writer2.write(xmlFile); //输出到文件 - writer2.close(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void getWordMap(Map wordMap,String articlePath) { - File articleArray[] = new File(articlePath).listFiles();//将文件夹中的文件都读取进来,下面就一个个的分析 - - for (int i = 0;i < articleArray.length;i++) { - calcFreauency(wordMap,articleArray[i]); - } - } - - //解析出文章中的词语,并且映射上频数 - public void calcFreauency(Map wordMap,File article) { - try { - //取得dom4j的解析器 - SAXReader reader = new SAXReader(); - //取得代表文档的Document对象 - Document document = reader.read(article); - //取得根结点 - Element root = document.getRootElement();//取得根节点 - - List list1 = root.elements();//取得的子节点 - List sentence_list = ((Element)list1.get(0)).elements();//下的集合 - - List tok_list;//下的集合 - //Dom4jDemo t = new Dom4jDemo(); - //遍历节点 - for (int i = 0; i < sentence_list.size(); i++) { - tok_list = ((Element)sentence_list.get(i)).elements();//获得每个sentence的tok集合 - for (int j = 0;j < tok_list.size();j++) { - setWordMap((Element)tok_list.get(j),wordMap); - } - } - } catch (DocumentException e) { - e.printStackTrace(); - } - } - - public void setWordMap(Element tok,Map wordMap) { - String type,text; - List list; - - if (!(tok.getName().equals("tok"))) {//如果不是tok节点,那么就不用处理了 - return ; - } - //获取属性type - type = tok.attributeValue("type"); - //只访问原子节点 - if (type.equals("atom") && isGroup) { - text = tok.getText(); - text = text.replace("\t", ""); - text = text.replace("\n", ""); - /*System.out.print(text + " ");*/ - strTemp = strTemp + text; - } else if (type.equals("group")) { - isGroup = true; - strTemp = ""; - list = tok.elements(); - for (int k = 0,size3 = list.size();k < size3;k++) { - tok = (Element)list.get(k); - setWordMap(tok,wordMap); - } - Integer count = wordMap.get(strTemp);//计算当前map里面的当前text对应的次数 - wordMap.put(strTemp,count == null?1:count + 1); - isGroup = false; - } - } - - public void keepEmotionWord(Map wordMap) { - Set word = wordMap.keySet(); - - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - //两个情感词典都不包含这个词语,那么就把这个词语去掉 - if (!positiveDict.contains(tmp) && !negativeDict.contains(tmp)) { - it.remove(); - } - } - } -} diff --git "a/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/EmotionJudge.java" "b/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/EmotionJudge.java" deleted file mode 100644 index bb7994c..0000000 --- "a/\346\226\207\346\234\254\346\203\205\346\204\237\345\210\206\346\236\220/EmotionJudge.java" +++ /dev/null @@ -1,235 +0,0 @@ -package org.geekgao.one; - -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.*; - -public class EmotionJudge { - private double priorPositive;//积极先验概率 - private double priorNegative;//消极先验概率 - private double priorUnsure;//不确定先验概率 - - private Map backPositive;//词语的后验概率 - private Map backNegative;//同上 - private Map backUnsure;//同上 - - private boolean isGroup = false; - private String strTemp; - private Map articleWordMap; - - //这两个是词典的位置 - private final String posiDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-positive.txt"; - private final String negaDictPath = "/home/geekgao/朴素贝叶斯/台湾大学情感词典/ntusd-negative.txt"; - - //这两个存储词典中的词语 - private Set positiveDict; - private Set negativeDict; - - public static void main(String [] args) { - new EmotionJudge().launch(); - } - - public void launch() { - getPrior(); - getBack(); - - positiveDict = new HashSet(); - negativeDict = new HashSet(); - readEmotionWord(positiveDict, posiDictPath); - readEmotionWord(negativeDict, negaDictPath); - calc(); - } - - //获得先验概率 - public void getPrior() { - SAXReader sax = new SAXReader(); - try { - //从这读取doc的值 - Document document = sax.read(new File("/home/geekgao/doc.xml")); - Element root = document.getRootElement(); - List prior = root.elements(); - - priorPositive = Double.valueOf(prior.get(0).attributeValue("pPositive")); - priorNegative = Double.valueOf(prior.get(0).attributeValue("pNegative")); - priorUnsure = Double.valueOf(prior.get(0).attributeValue("pUnsure")); - - } catch (DocumentException e) { - e.printStackTrace(); - } - } - - //获得后验概率 - public void getBack() { - SAXReader sax = new SAXReader(); - try { - //从这读取weight的值 - Document document = sax.read(new File("/home/geekgao/weight.xml")); - Element root = document.getRootElement(); - List back = root.elements(); - - backNegative = new HashMap(); - backPositive = new HashMap(); - backUnsure = new HashMap(); - - double backPos;//积极后验概率 - double backNeg;//消极后验概率 - double backUns;//不确定后验概率 - String word; - - for (int i = 0;i < back.size();i++) { - backPos = Double.valueOf(back.get(i).attributeValue("pPositive")); - backNeg = Double.valueOf(back.get(i).attributeValue("pNegative")); - backUns = Double.valueOf(back.get(i).attributeValue("pUnsure")); - word = back.get(i).attributeValue("data"); - - backPositive.put(word,backPos); - backNegative.put(word,backNeg); - backUnsure.put(word,backUns); - } - } catch (DocumentException e) { - e.printStackTrace(); - } - } - - public void calc() { - articleWordMap = new HashMap(); - - //读取文章 - calcFreauency(articleWordMap,new File("/home/geekgao/朴素贝叶斯/500trainblogxml/positiveout/1377331000713.txt")); - keepEmotionWord(articleWordMap); - - double allBackPos = 1; - double allBackNeg = 1; - double allBackUns = 1; - - Set word = articleWordMap.keySet(); - - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - double back; - if (backPositive.containsKey(tmp)) { - back = backPositive.get(tmp); - allBackPos = Math.pow(back,articleWordMap.get(tmp)) * allBackPos; - } - - if (backNegative.containsKey(tmp)) { - back = backNegative.get(tmp); - allBackNeg= Math.pow(back,articleWordMap.get(tmp)) * allBackNeg; - } - - if (backUnsure.containsKey(tmp)) { - back = backUnsure.get(tmp); - allBackUns = Math.pow(back,articleWordMap.get(tmp)) * allBackUns; - } - } - - double resultPositive; - double resultNegative; - double resultUnsure; - - resultPositive = priorPositive * allBackPos; - resultNegative = priorNegative * allBackNeg; - resultUnsure = priorUnsure * allBackUns; - - System.out.println("积极:" + resultPositive); - System.out.println("消极:" + resultNegative); - System.out.println("不确定:" + resultUnsure); - } - - //解析出文章中的词语,并且映射上频数 - public void calcFreauency(Map wordMap,File article) { - try { - //取得dom4j的解析器 - SAXReader reader = new SAXReader(); - //取得代表文档的Document对象 - Document document = reader.read(article); - //取得根结点 - Element root = document.getRootElement();//取得根节点 - - List list1 = root.elements();//取得的子节点 - List sentence_list = ((Element)list1.get(0)).elements();//下的集合 - - List tok_list;//下的集合 - //遍历节点 - for (int i = 0; i < sentence_list.size(); i++) { - tok_list = ((Element)sentence_list.get(i)).elements();//获得每个sentence的tok集合 - for (int j = 0;j < tok_list.size();j++) { - setWordMap((Element)tok_list.get(j),wordMap); - } - } - } catch (DocumentException e) { - e.printStackTrace(); - } - } - - public void setWordMap(Element tok,Map wordMap) { - String type,text; - List list; - - if (!(tok.getName().equals("tok"))) {//如果不是tok节点,那么就不用处理了 - return ; - } - //获取属性type - type = tok.attributeValue("type"); - //只访问原子节点 - if (type.equals("atom") && isGroup) { - text = tok.getText(); - text = text.replace("\t", ""); - text = text.replace("\n", ""); - /*System.out.print(text + " ");*/ - strTemp = strTemp + text; - } else if (type.equals("group")) { - isGroup = true; - strTemp = ""; - list = tok.elements(); - for (int k = 0,size3 = list.size();k < size3;k++) { - tok = (Element)list.get(k); - setWordMap(tok,wordMap); - } - Integer count = wordMap.get(strTemp);//计算当前map里面的当前text对应的次数 - wordMap.put(strTemp,count == null?1:count + 1); - isGroup = false; - } - } - - public void keepEmotionWord(Map wordMap) { - Set word = wordMap.keySet(); - - for (Iterator it = word.iterator();it.hasNext();) { - String tmp = (String)it.next(); - //两个情感词典都不包含这个词语,那么就把这个词语去掉 - if (!positiveDict.contains(tmp) && !negativeDict.contains(tmp)) { - it.remove(); - } - } - } - - public void readEmotionWord(Set Dict, String dictPath) { - File file = new File(dictPath); - BufferedReader reader = null; - try { - String t; - reader = new BufferedReader(new FileReader(file)); - while ((t = reader.readLine()) != null) { - Dict.add(t); - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - - } - } - } - } -} diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/data.txt" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/data.txt" deleted file mode 100644 index 79fcf96..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/data.txt" +++ /dev/null @@ -1,68 +0,0 @@ -1 114 486 2 45 -2 114 441 1 45 3 39 4 71 -3 153 441 2 39 -4 114 370 2 71 5 38 -5 76 370 4 38 6 56 -6 76 314 5 56 7 13 9 73 -7 63 314 6 13 -8 63 241 9 13 -9 76 241 6 73 8 13 10 26 68 13 -10 76 215 9 26 11 70 -11 146 215 10 70 12 36 -12 172 241 11 36 13 43 -13 211 222 64 58 34 32 12 43 -14 211 109 64 55 15 55 -15 266 109 16 45 19 56 14 55 -16 311 109 17 61 20 56 15 45 -17 372 109 16 61 21 56 26 44 -18 224 53 19 42 -19 266 53 18 42 20 45 15 56 -20 311 53 16 56 19 45 21 61 -21 372 53 17 56 20 61 22 37 -22 409 53 21 37 23 36 -23 445 53 22 36 24 32 -24 445 85 23 32 25 91 27 24 -25 536 85 24 91 -26 416 109 17 44 27 29 28 19 -27 445 109 24 24 26 29 29 19 -28 416 128 33 47 26 19 29 29 -29 445 128 27 19 28 29 30 55 -30 500 128 29 55 31 47 -31 500 175 32 28 38 47 30 47 -32 528 175 31 28 -33 416 175 67 50 36 47 28 47 -34 243 222 35 65 40 106 13 32 -35 308 222 34 65 66 32 36 108 41 93 -36 416 222 33 47 35 108 37 67 43 93 -37 483 222 36 67 38 17 44 62 -38 500 222 37 17 39 25 31 47 -39 525 222 38 25 -40 205 321 34 106 53 128 -41 308 315 48 46 35 93 42 49 -42 357 315 49 27 41 49 43 59 -43 416 315 36 93 42 59 45 26 -44 458 279 37 62 45 39 -45 442 315 43 26 44 39 46 30 -46 428 342 49 71 45 30 47 21 -47 419 361 50 62 52 27 46 21 -48 308 361 50 49 41 46 -49 357 342 50 19 42 27 46 71 -50 357 361 48 49 49 19 51 25 47 62 -51 357 386 50 25 52 51 54 44 -52 408 386 51 51 55 48 56 93 47 27 -53 267 433 40 128 57 67 -54 357 430 51 44 55 31 57 34 -55 388 430 52 48 54 31 58 36 -56 492 428 52 93 63 71 -57 331 453 53 67 54 34 59 38 -58 402 464 55 36 59 46 62 69 -59 358 480 57 38 58 46 60 27 -60 358 507 59 27 61 67 -61 291 507 60 67 -62 447 517 58 69 63 80 -63 523 492 56 71 62 80 -64 211 164 65 34 13 58 14 55 -65 245 164 64 34 -66 308 190 35 32 -67 366 175 33 50 -68 89 241 9 13 \ No newline at end of file diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/view.txt" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/view.txt" deleted file mode 100644 index 2311a03..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\225\260\346\215\256/view.txt" +++ /dev/null @@ -1,31 +0,0 @@ -¥ 61 һ - 65 ö -ѧ¥ 5 ѧ¥ľ - 12 ָ˺ü - 3 ܺĴ - 66 -ҽԺ 39 ñȶãҽԺҲͦãܱDzõ -һʵ¥ 47 ʵ¥ -ʦԢ 10 ֪Щʦס -ͼ 42 ţȥ -10-16¥ 15 ¥ö -˺ 41 ¾ۼ - 8 ö - 25 Ҳ⣡ - 60 붫űĸأ - 53 ¾ۼ -ѧ¥ 52 Ľѧ¥ - 67 - 33 ٳͼ汾Ͼɺٺ -ѧ 7 ǵ -ʳ㳡 32 Ҳȶ -Ȫ 54 ׳ˮӣ -ʳ 68 ʳζãأ - 22 ȥУ -Է 26 ˱ȶ -ѧ 48 ѧ -ʵ¥ 45 ʵ¥ - 11 -ʵ¥ 44 ʵ¥ -1-6¥ 24 7-9أ - 1 С diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/icon.png" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/icon.png" deleted file mode 100644 index 870d503..0000000 Binary files "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/icon.png" and /dev/null differ diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/map.jpg" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/map.jpg" deleted file mode 100644 index 5ea423b..0000000 Binary files "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/images/map.jpg" and /dev/null differ diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideAlgorithm.java" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideAlgorithm.java" deleted file mode 100644 index c1fb243..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideAlgorithm.java" +++ /dev/null @@ -1,169 +0,0 @@ -package org.geekgao.guide; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; -import java.util.Stack; - - -/** - * - * @author geekgao - * ṩ̬㷨 - * - */ - -public class GuideAlgorithm { - - /** - * - * @param start ʼ - * @param end - * @param count ĸ - * @param map ͼ - * @return - */ - public static Integer[] Dijkstra(int start,int end,int count,Map map) { - if (start == end) { - return null; - } - - class LengthAndRoad{ - int length; - List road = new LinkedList(); - } - LengthAndRoad[] temp= new LengthAndRoad[count + 1];//㷨ʱõǸ,±Ӧڵ - Set alreadyFind = new HashSet();//Ѿҵ·Щļ - -// =============================================== -// ʼ - Vertex tmpVex = map.get(start); - Set point = tmpVex.pointNum.keySet();//ʼָЩ - for (Integer i:point) { - temp[i] = new LengthAndRoad(); - temp[i].length = tmpVex.pointNum.get(i); - temp[i].road = new LinkedList(); - temp[i].road.add(start); - temp[i].road.add(i); - } - alreadyFind.add(start); -// =============================================== - int currentStart = 0;//ÿѭʱʼ,ѭȥһѡ㣨·̵Ǹ㣩 - int MAX = 2147483647; - for (int k = 1;k <= count;k++) { - long endTime = System.currentTimeMillis(); - - int minLength = MAX; - int n = -1;//temp - for (LengthAndRoad l:temp) { - n++; - if (l == null || alreadyFind.contains(n)) { - continue; - } - if (minLength > l.length) { - minLength = l.length; - currentStart = n; - } - } - if (currentStart == end) { - break; - } - alreadyFind.add(currentStart); - - Vertex currentVex = map.get(currentStart);//ǰʼ - List currentStartRoad = new LinkedList(temp[currentStart].road);//ǰ·(ʼ·ƹ) - - Set currentPoint = currentVex.pointNum.keySet();//õǰָЩ - for (Integer i:currentPoint) { - if (alreadyFind.contains(i)) { - continue; - } - if ((temp[i] == null) || (temp[currentStart].length + currentVex.pointNum.get(i) < temp[i].length)) { - LengthAndRoad newRoad = new LengthAndRoad(); - newRoad.length = temp[currentStart].length + currentVex.pointNum.get(i); - newRoad.road = new LinkedList(currentStartRoad); - newRoad.road.add(i); - temp[i] = newRoad; - } - } - } - - if (currentStart != end) { - return null; - } - - Integer[] result = new Integer[temp[currentStart].road.size()]; - int n = 0; - for (Integer i:temp[currentStart].road) { - result[n++] = i; - } - return result; - } - - public static Integer[] Bfs(int startNum, Map map, - Map view, Map viewNumNameMap) { - - List resultList = new LinkedList(); - Queue q = new LinkedList(); - - Set alreadyFind = new HashSet();//洢Ѿʹĵ - q.offer(startNum);//ʼ - alreadyFind.add(startNum); - while (!q.isEmpty()) { - Integer num = q.poll(); - if (viewNumNameMap.containsKey(num)) { - resultList.add(num); - } - Set pointNum = map.get(num).pointNum.keySet(); - for (Integer i:pointNum) { - if (!alreadyFind.contains(i)) { - q.offer(i); - alreadyFind.add(i); - } - } - } - - Integer[] result = new Integer[resultList.size()]; - for (int i = 0;i < resultList.size();i++) { - result[i] = resultList.get(i); - } - - return result; - } - - public static Integer[] Dfs(int startNum, Map map, - Map view, Map viewNumNameMap) { - - List resultList = new LinkedList();//㷨洢ս - Stack s = new Stack();//㷨õջ - Set alreadyFind = new HashSet();//洢Ѿʹĵ - - s.push(startNum); - alreadyFind.add(startNum); - while (!s.isEmpty()) { - int num = s.pop(); - if (viewNumNameMap.containsKey(num)) { - resultList.add(num); - } - Map pointNum = map.get(num).pointNum; - for (Iterator it = pointNum.keySet().iterator();it.hasNext();) { - int nextNum = it.next(); - if (!alreadyFind.contains(nextNum)) { - s.push(nextNum); - alreadyFind.add(nextNum); - } - } - } - Integer[] result = new Integer[resultList.size()]; - for (int i = 0;i < resultList.size();i++) { - result[i] = resultList.get(i); - } - - return result; - } - -} diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideSystem.java" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideSystem.java" deleted file mode 100644 index 803d70f..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideSystem.java" +++ /dev/null @@ -1,849 +0,0 @@ -package org.geekgao.guide; - -import java.awt.BasicStroke; -import java.awt.BorderLayout; -import java.awt.CardLayout; -import java.awt.Color; -import java.awt.Container; -import java.awt.FlowLayout; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.GridLayout; -import java.awt.Image; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import javax.swing.DefaultListModel; -import javax.swing.ImageIcon; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTextField; - -public class GuideSystem extends JFrame{ - - //Ͻͼ - private Image icon = GuideUtil.getImage("images/icon.png"); - final private JButton firstPanelDownBut1 = new JButton("·"); - final private JButton firstPanelDownBut2 = new JButton("·"); - final private JButton rightBut1 = new JButton("·ѯ"); - final private JButton rightBut2 = new JButton("Ϣ"); - final private JButton rightBut3 = new JButton("ӽڵ"); - final private JButton rightBut4 = new JButton("ɾڵ"); - final private JButton rightBut5 = new JButton("Ӿ"); - final private JButton rightBut6 = new JButton("ɾ"); - final private JButton rightBut7 = new JButton("ͼ㷨"); - private JPanel cardPanel;//ɺܶpanelĿƬpanel - private CardLayout card;//Ƭֹ - JComboBox firstPanelDownBox1; - JComboBox firstPanelDownBox2; - - /** - * 洢һ·е㣬֮仭߱ʾ· - * 洢һ·ϵĽڵ - */ - private Integer[] paintVertex; - - /** - * ͼƬԭ - * windows7ǣ3,25 - * LinuxûУƽ̨ - */ - private static final int borderWidth = 3; - private static final int borderHeight = 25; - - private Map map;//ͼ - private Map view;//ľĵźͼӦ - private Map viewNumNameMap;//źƶӦ - private JLabel secondPanelLab; - private JList secondPanelList; - private JLabel thirdPanelUpMapLabel; - private JLabel thirdPanelDownLab2; - private JTextField thirdPanelDownText1; - private JButton thirdPanelDownButton1; - private JPanel thirdPanelUp; - private JTextField fourthPanelDownText; - private JButton fourthPanelDownButton;//ȷɾڵ㰴ť - - private JButton clickedButton;//DZµĸť - private JTextField fifthPanelDownText1; - private JTextField fifthPanelDownText2; - private JButton fifthPanelDownButton; - private JTextField fifthPanelDownText3; - private JTextField sixthPanelDownText; - private JButton sixthPanelDownButton; - - private static final String vertexPath = "C:/Users/geekgao/Desktop/γ/data.txt"; - private static final String viewPath = "C:/Users/geekgao/Desktop/γ/view.txt"; - private JButton sevenPanelBfsButton; - private JButton sevenPanelDfsButton; - - public static void main(String[] args) { - - GuideSystem guide = new GuideSystem(); - guide.paintGuideWindow(); - - } - - public GuideSystem() { - - map = GuideUtil.getVertex(vertexPath);//ڵϢ - view = GuideUtil.getView(viewPath);//뾰Ϣ - - Set viewNameSet = view.keySet(); - viewNumNameMap = new HashMap(); - for (String name:viewNameSet) { - viewNumNameMap.put(Integer.valueOf(view.get(name).split(" ")[0]),name); - } - - } - - @Override - /** - * · - */ -public void paint(Graphics g) { - - super.paint(g); - Color c = g.getColor(); - g.setColor(Color.RED); - float lineWidth = 4.0f;// - ((Graphics2D)g).setStroke(new BasicStroke(lineWidth)); - - //µ·ѯ - if (clickedButton == rightBut1 || - clickedButton == firstPanelDownBut1) { - if (paintVertex == null) { - return; - } - - for (int i = 0;i + 1 < paintVertex.length;i++) { - g.drawLine( map.get(paintVertex[i]).x + borderWidth, - map.get(paintVertex[i]).y + borderHeight, - map.get(paintVertex[i+1]).x + borderWidth, - map.get(paintVertex[i+1]).y + borderHeight); - } - g.setFont(new Font("΢ź", Font.BOLD, 15)); - g.setColor(Color.YELLOW); - g.drawString("", map.get(paintVertex[0]).x + borderWidth, - map.get(paintVertex[0]).y + borderHeight); - g.drawString("յ", map.get(paintVertex[paintVertex.length - 1]).x + borderWidth, - map.get(paintVertex[paintVertex.length - 1]).y + borderHeight); - } else if ( clickedButton == rightBut3 || - clickedButton == rightBut4 || - clickedButton == rightBut5 || - clickedButton == rightBut6 || - clickedButton == fourthPanelDownButton || - clickedButton == thirdPanelDownButton1 || - clickedButton == fifthPanelDownButton || - clickedButton == sixthPanelDownButton) { - Set vexNum = map.keySet(); - Set viewName = view.keySet(); - Set viewNum = new HashSet();//洢ıţĵɫҪֳ - - for (String s:viewName) { - String introduce = view.get(s); - viewNum.add(Integer.valueOf(introduce.split(" ")[0])); - } - - Vertex t; - int r = 3;//Բİ뾶 - //ͼĽڵ㣬ÿڵϻһ - for (Integer i:vexNum) { - t = map.get(i); - if (viewNum.contains(i)) { - g.setColor(Color.YELLOW); - } else { - g.setColor(Color.RED); - } - g.fillOval(t.x + borderWidth - r, t.y + borderHeight - r, 2 * r, 2 * r); - } - g.setFont(new Font("΢ź", Font.BOLD, 12)); - g.setColor(Color.BLACK); - - //ӡ - for (Integer i:vexNum) { - t = map.get(i); - g.drawString(String.valueOf(i), t.x + borderWidth, t.y + borderHeight); - } - } - - g.setColor(c); - - } - - public void paintGuideWindow() { - - setTitle("ʵϵͳ - geekgao"); - setIconImage(icon); - /** - * ڹر - */ - addWindowListener(new WindowAdapter() { - - @Override - public void windowClosing(WindowEvent e) { - System.exit(0); - } - - }); - -// =============================================================== - /** - * ƴ - */ - Container c = getContentPane();//ȡ - card = new CardLayout(); - cardPanel = new JPanel(card);//ĿƬpanel - JPanel controlPanel = new JPanel(new GridLayout(10,1,0,5));//ҲĿƿƬpanelмŰť - c.add(cardPanel,BorderLayout.WEST); - c.add(controlPanel,BorderLayout.EAST); - - /** - * ưť - */ - controlPanel.add(rightBut1); - controlPanel.add(rightBut2); - controlPanel.add(rightBut3); - controlPanel.add(rightBut4); - controlPanel.add(rightBut5); - controlPanel.add(rightBut6); - controlPanel.add(rightBut7); - rightBut1.addActionListener(new MyButtonActionListener()); - rightBut2.addActionListener(new MyButtonActionListener()); - rightBut3.addActionListener(new MyButtonActionListener()); - rightBut4.addActionListener(new MyButtonActionListener()); - rightBut5.addActionListener(new MyButtonActionListener()); - rightBut6.addActionListener(new MyButtonActionListener()); - rightBut7.addActionListener(new MyButtonActionListener()); - - /** - * ණÿpanelٸpanelӶ - */ - - /** - * һƬҳ - */ - JPanel firstPanel = new JPanel(new BorderLayout()); - cardPanel.add(firstPanel);//ǰpanelӵcardPanel - - JPanel firstPanelUp = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); - firstPanel.add(firstPanelUp,BorderLayout.NORTH); - JLabel firstPanelMapLabel = new JLabel(new ImageIcon("src/images/map.jpg")); - firstPanelUp.add(firstPanelMapLabel); - - /** - * ѡصһ - */ - JPanel firstPanelDown = new JPanel(new FlowLayout(FlowLayout.LEFT,10,5)); - firstPanel.add(firstPanelDown,BorderLayout.SOUTH); - JLabel firstPanelDownlab1 = new JLabel(""); - firstPanelDown.add(firstPanelDownlab1); - firstPanelDownBox1 = new JComboBox(); - firstPanelDown.add(firstPanelDownBox1); - JLabel firstPanelDownlab2 = new JLabel("յ"); - firstPanelDown.add(firstPanelDownlab2); - firstPanelDownBox2 = new JComboBox(); - firstPanelDown.add(firstPanelDownBox2); - setViewBox(); - - firstPanelDown.add(firstPanelDownBut1); - firstPanelDown.add(firstPanelDownBut2); - firstPanelDownBut1.addActionListener(new MyButtonActionListener()); - firstPanelDownBut2.addActionListener(new MyButtonActionListener()); - - /** - * ڶƬҳ - */ - JPanel secondPanel = new JPanel(new BorderLayout()); - cardPanel.add(secondPanel); - - secondPanelList = new JList(); - secondPanelList.setModel(new DefaultListModel()); - JScrollPane scrollPane = new JScrollPane(secondPanelList);//б - secondPanel.add(scrollPane,BorderLayout.WEST); - - secondPanelLab = new JLabel(); - secondPanelLab.setHorizontalAlignment(JLabel.CENTER); - secondPanel.add(secondPanelLab,BorderLayout.CENTER); - - setViewNameList(); - - //List굥¼ - secondPanelList.addMouseListener(new MyMouseListener()); - - /** - * Ƭҳ - */ - JPanel thirdPanel = new JPanel(new BorderLayout()); - cardPanel.add(thirdPanel); - - thirdPanelUp = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); - thirdPanel.add(thirdPanelUp); - thirdPanelUpMapLabel = new JLabel(new ImageIcon("src/images/map.jpg")); - thirdPanelUp.add(thirdPanelUpMapLabel,FlowLayout.LEFT); - thirdPanelUpMapLabel.addMouseListener(new MyMouseListener());//ͼƬLabel¼ - - JPanel thirdPanelDown = new JPanel(new FlowLayout(FlowLayout.LEFT,10,5)); - thirdPanel.add(thirdPanelDown,BorderLayout.SOUTH); - JLabel thirdPanelDownLab1 = new JLabel("ͼѡ:"); - thirdPanelDown.add(thirdPanelDownLab1); - thirdPanelDownLab2 = new JLabel(); - thirdPanelDown.add(thirdPanelDownLab2); - JLabel thirdPanelDownLab3= new JLabel("Щϵ(Ÿ):"); - thirdPanelDown.add(thirdPanelDownLab3); - thirdPanelDownText1 = new JTextField(14); - thirdPanelDown.add(thirdPanelDownText1); - thirdPanelDownButton1 = new JButton("ȷ"); - thirdPanelDown.add(thirdPanelDownButton1); - thirdPanelDownButton1.addActionListener(new MyButtonActionListener()); - - /** - * ĸƬҳ - */ - JPanel fourthPanel = new JPanel(new BorderLayout()); - cardPanel.add(fourthPanel); - - JPanel fourthPanelUp = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); - fourthPanel.add(fourthPanelUp,BorderLayout.NORTH); - JLabel fourthPanelUpMapLabel = new JLabel(new ImageIcon("src/images/map.jpg")); - fourthPanelUp.add(fourthPanelUpMapLabel,FlowLayout.LEFT); - JPanel fourthPanelDown = new JPanel(new FlowLayout(FlowLayout.LEFT,10,5)); - fourthPanel.add(fourthPanelDown,BorderLayout.SOUTH); - JLabel fourthPanelDownLabel = new JLabel("ҪɾĽڵ(Ÿ):"); - fourthPanelDown.add(fourthPanelDownLabel); - fourthPanelDownText = new JTextField(15); - fourthPanelDown.add(fourthPanelDownText); - fourthPanelDownButton = new JButton("ȷɾ"); - fourthPanelDown.add(fourthPanelDownButton); - fourthPanelDownButton.addActionListener(new MyButtonActionListener()); - - /** - * Ƭҳ - */ - JPanel fifthPanel = new JPanel(new BorderLayout()); - cardPanel.add(fifthPanel); - - JPanel fifthPanelUp = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); - fifthPanel.add(fifthPanelUp,BorderLayout.NORTH); - JLabel fifthPanelUpMapLabel = new JLabel(new ImageIcon("src/images/map.jpg")); - fifthPanelUp.add(fifthPanelUpMapLabel,FlowLayout.LEFT); - JPanel fifthPanelDown = new JPanel(new FlowLayout(FlowLayout.LEFT,10,5)); - fifthPanel.add(fifthPanelDown,BorderLayout.SOUTH); - JLabel fifthPanelDownLabel1 = new JLabel("ڵ:"); - fifthPanelDown.add(fifthPanelDownLabel1); - fifthPanelDownText1 = new JTextField(2); - fifthPanelDown.add(fifthPanelDownText1); - JLabel fifthPanelDownLabel2 = new JLabel(":"); - fifthPanelDown.add(fifthPanelDownLabel2); - fifthPanelDownText2 = new JTextField(7); - fifthPanelDown.add(fifthPanelDownText2); - JLabel fifthPanelDownLabel3 = new JLabel(":"); - fifthPanelDown.add(fifthPanelDownLabel3); - fifthPanelDownText3 = new JTextField(10); - fifthPanelDown.add(fifthPanelDownText3); - fifthPanelDownButton = new JButton("ȷ"); - fifthPanelDown.add(fifthPanelDownButton); - fifthPanelDownButton.addActionListener(new MyButtonActionListener()); - - /** - * Ƭҳ - */ - JPanel sixthPanel = new JPanel(new BorderLayout()); - cardPanel.add(sixthPanel); - - JPanel sixthPanelUp = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); - sixthPanel.add(sixthPanelUp,BorderLayout.NORTH); - JLabel sixthPanelUpMapLabel = new JLabel(new ImageIcon("src/images/map.jpg")); - sixthPanelUp.add(sixthPanelUpMapLabel,FlowLayout.LEFT); - JPanel sixthPanelDown = new JPanel(new FlowLayout(FlowLayout.LEFT,10,5)); - sixthPanel.add(sixthPanelDown,BorderLayout.SOUTH); - JLabel sixthPanelDownLabel = new JLabel("Ҫɾľ(ɫ)(Ÿ):"); - sixthPanelDown.add(sixthPanelDownLabel); - sixthPanelDownText = new JTextField(15); - sixthPanelDown.add(sixthPanelDownText); - sixthPanelDownButton = new JButton("ȷɾ"); - sixthPanelDown.add(sixthPanelDownButton); - sixthPanelDownButton.addActionListener(new MyButtonActionListener()); - - /** - * ߸Ƭҳ - * 㷨ҳ - */ - JPanel seventhPanel = new JPanel(new FlowLayout()); - cardPanel.add(seventhPanel); - - sevenPanelBfsButton = new JButton("BFS㷨"); - sevenPanelDfsButton = new JButton("DFS㷨"); - - seventhPanel.add(sevenPanelDfsButton); - seventhPanel.add(sevenPanelBfsButton); - - MyButtonActionListener buttonListener = new MyButtonActionListener(); - sevenPanelBfsButton.addActionListener(buttonListener); - sevenPanelDfsButton.addActionListener(buttonListener); - - -// =============================================================== - - setResizable(false);//ɸĴС - setVisible(true); - pack(); - setLocationRelativeTo(null);//仰pack()棬ʼڴСΪ0ϽĻе - - } - - public int getStartNum() { - String viewNameStart = (String) firstPanelDownBox1.getSelectedItem(); - String viewLastStart = view.get(viewNameStart); - return Integer.parseInt((viewLastStart.split(" "))[0]); - } - - public int getEndNum() { - String viewNameEnd = (String) firstPanelDownBox2.getSelectedItem(); - String viewLastEnd = view.get(viewNameEnd); - return Integer.parseInt((viewLastEnd.split(" "))[0]); - } - - public int getMaxNum() { - /** - * Ľڵ - */ - Set vexNum = map.keySet(); - int max = 0;//Ŵ1ʼ - for (Integer i:vexNum) { - if (max < i) { - max = i; - } - } - - return max; - } - - public void setViewBox() { - - firstPanelDownBox1.removeAllItems(); - firstPanelDownBox2.removeAllItems(); - - Set viewName = view.keySet(); - for (String s:viewName) { - firstPanelDownBox1.addItem(s); - firstPanelDownBox2.addItem(s); - } - } - - private void setViewNameList() { - - DefaultListModel model = (DefaultListModel) secondPanelList.getModel(); - model.clear(); - - Set viewNameSet = view.keySet(); - for (String viewName:viewNameSet) { - model.addElement(viewName); - } - secondPanelLab.setText("");//ܾɾԭȲ鿴ǸݣҪɾLabelе - } - - class MyButtonActionListener implements ActionListener{ - - @Override - public void actionPerformed(ActionEvent e) { - - clickedButton = (JButton) e.getSource(); - if (clickedButton == firstPanelDownBut1) { - /** - * ·İť - */ - int numStart = getStartNum(); - int numEnd = getEndNum(); - int count = getMaxNum();//Ϊ±·ӦԽĴСӦð - paintVertex = GuideAlgorithm.Dijkstra(numStart, numEnd, count,map); - - if (paintVertex == null) { - JOptionPane.showMessageDialog(null, "û·Եյͬȷнڵ㶼ͨ"); - } - - repaint(); - } else if (clickedButton == firstPanelDownBut2) { - /** - * ·߰ť - */ - paintVertex = null; - repaint(); - } else if (clickedButton == thirdPanelDownButton1) { - /** - * ȷӽڵİť - */ - - String xy = thirdPanelDownLab2.getText(); - String relationNum = thirdPanelDownText1.getText(); - if (xy.equals("") || relationNum.equals("")) { - JOptionPane.showMessageDialog(null, "δ!"); - return; - } - - Set relationNumSet = new HashSet(); - String[] relationNumArray = relationNum.split(","); - for (int i = 0;i < relationNumArray.length;i++) { - try { - relationNumSet.add(Integer.valueOf(relationNumArray[i])); - } catch (NumberFormatException e1) { - JOptionPane.showMessageDialog(null, "벻ȷֻ!"); - return; - } - } - - //ûдĵ㲻ڣôʹ֮ӳɹ - for (Integer i:relationNumSet) { - if (!map.containsKey(i)) { - JOptionPane.showMessageDialog(null, "˵ͼϲڵĵ!"); - return; - } - } - thirdPanelDownText1.setText(""); - - int vexNum;//ӵͼеĽڵ - for (vexNum = 1;map.containsKey(vexNum);vexNum++);//ѡһСIJδͼĽڵ - - Vertex newVex = new Vertex(); - newVex.num = vexNum; - newVex.x = Integer.parseInt(xy.split(",")[0]); - newVex.y = Integer.parseInt(xy.split(",")[1]); - newVex.pointNum = new HashMap(); - - //¼ĵϢ - for (Integer i:relationNumSet) { - int x = map.get(i).x; - int y = map.get(i).y; - int distance = (int) Math.sqrt((x-newVex.x) * (x-newVex.x) + (y-newVex.y) * (y-newVex.y)); - newVex.pointNum.put(i, distance); - } - map.put(newVex.num, newVex); - - //޸¼ӵĵйϵĵϢЩҲ¼ӵĵйϵ - for (Integer i:relationNumSet) { - Vertex t = map.get(i); - t.pointNum.put(newVex.num, newVex.pointNum.get(i)); - map.put(i, t); - } - - GuideUtil.setVertex(map, vertexPath); - repaint(); - } else if (clickedButton == fourthPanelDownButton) { - /** - * ȷɾڵ㰴ť - */ - - if (fourthPanelDownText.getText().equals("")) { - return; - } - - String[] deleteNumStr = fourthPanelDownText.getText().split(","); - Set deleteNum = new HashSet(); - for (String s:deleteNumStr) { - try{ - deleteNum.add(Integer.valueOf(s)); - } catch (NumberFormatException e1) { - JOptionPane.showMessageDialog(null, "벻ȷֺֻͶ!"); - return; - } - } - fourthPanelDownText.setText(""); - -// ɾϢ,ɾĵŵļ - for (Integer i:deleteNum) { - //ͼɾ - if (map.containsKey(i)) { - map.remove(i); - } - } - - Set vexNum = map.keySet();//ͼеĵ - Map pointNum;//ijڵָʲô - for (Integer i:vexNum) { - pointNum = map.get(i).pointNum; - for (Integer j:deleteNum) { - if (pointNum.containsKey(j)) { - pointNum.remove(j); - } - } - } - GuideUtil.setVertex(map, vertexPath); - - //ڵһ㣬нϢҲҪɾ - Set viewName = view.keySet(); - String t; - for (Iterator it = viewName.iterator();it.hasNext();) { - t = view.get(it.next()).split(" ")[0];//õ - if (deleteNum.contains(Integer.valueOf(t))) { - it.remove(); - } - } - GuideUtil.setView(view, viewPath); - - setViewBox();//ʾBOX - setViewNameList();//ʾѡص - - paintVertex = null;//ɾڵͲʾ·ˣʱԭѡ·յ㻹 - - repaint(); - } else if (clickedButton == fifthPanelDownButton) { - /** - * ȷӾ㰴ť - */ - - String viewNum = fifthPanelDownText1.getText(); - String viewName = fifthPanelDownText2.getText(); - String viewIntroduce = fifthPanelDownText3.getText(); - if (viewNum.equals("") || viewName.equals("") || viewIntroduce.equals("")) { - JOptionPane.showMessageDialog(null, "δ!"); - return; - } - - //㲻ڲ - try { - if (!map.containsKey(Integer.valueOf(viewNum))) { - JOptionPane.showMessageDialog(null, "ڵ㲻!"); - return; - } - } catch (NumberFormatException e1) { - JOptionPane.showMessageDialog(null, "ڵ벻ȷֻ!"); - return; - } - - Set viewNameSet = view.keySet(); - for (String name:viewNameSet) { - if (view.get(name).split(" ")[0].equals(viewNum)) { - JOptionPane.showMessageDialog(null, "˽ڵǾ㣬Ҫɾ!"); - return; - } - } - - fifthPanelDownText1.setText(""); - fifthPanelDownText2.setText(""); - fifthPanelDownText3.setText(""); - - view.put(viewName, viewNum + " " +viewIntroduce); - - GuideUtil.setView(view, viewPath); - - setViewBox();//ʾBOX - setViewNameList();//ʾѡص - - repaint(); - } else if (clickedButton == sixthPanelDownButton) { - /** - * ȷɾ㰴ť - */ - - String inputText = sixthPanelDownText.getText(); - if (inputText.equals("")) { - JOptionPane.showMessageDialog(null, "δ!"); - return; - } - - Set deleteNumSet = new HashSet(); - String[] deleteNumArray = inputText.split(","); - for (int i = 0;i < deleteNumArray.length;i++) { - try { - deleteNumSet.add(Integer.valueOf(deleteNumArray[i])); - } catch (NumberFormatException e1) { - JOptionPane.showMessageDialog(null, "ֺֻͶ!"); - return; - } - } - - Set viewNumSet = new HashSet();//洢ı,жǷҪɾĵǷһ - Set viewNameSet = view.keySet(); - for (String viewName:viewNameSet) { - viewNumSet.add(Integer.valueOf(view.get(viewName).split(" ")[0])); - } - for (Integer i:deleteNumSet) { - if (!viewNumSet.contains(i)) { - JOptionPane.showMessageDialog(null, "ĵаǾĵ!"); - return; - } - } - sixthPanelDownText.setText(""); - - for (Iterator it = viewNameSet.iterator();it.hasNext();) { - if (deleteNumSet.contains(Integer.valueOf(view.get(it.next()).split(" ")[0]))) { - it.remove(); - } - } - - setViewBox(); - setViewNameList(); - GuideUtil.setView(view, viewPath); - - repaint(); - } else if ( clickedButton == sevenPanelBfsButton || - clickedButton == sevenPanelDfsButton) { - /** - * 㷨һ - */ - - JFrame newWindow = new JFrame(); - Container c = newWindow.getContentPane(); - - JLabel roadText = new JLabel(); - JScrollPane scrollPanel = new JScrollPane(roadText); - c.add(scrollPanel); - - int startNum = 1;//Сĵ㿪ʼȱ - int i; - for (i = 1;i <= getMaxNum() && !map.containsKey(i);i++); - startNum = i; - Integer[] roadVexNum = null; - if (clickedButton == sevenPanelBfsButton) { - roadVexNum = GuideAlgorithm.Bfs(startNum, map, view,viewNumNameMap); - newWindow.setTitle("ȱ"); - } else if (clickedButton == sevenPanelDfsButton) { - roadVexNum = GuideAlgorithm.Dfs(startNum, map, view,viewNumNameMap); - newWindow.setTitle("ȱ"); - } - - StringBuffer roadStr = new StringBuffer(); - roadStr.append(""); - roadStr.append(viewNumNameMap.get(roadVexNum[0])); - int length = 0; - for (i = 1;i < roadVexNum.length;i++) { - roadStr.append("==>" + viewNumNameMap.get(roadVexNum[i])); - length += viewNumNameMap.get(roadVexNum[i]).length(); - - if (length > 30) { - length = 0; - roadStr.append("
"); - } - } - roadStr.append(""); - roadText.setText(roadStr.toString()); - - newWindow.pack(); - newWindow.setLocationRelativeTo(null);//仰pack()棬ʼڴСΪ0ϽĻе - newWindow.setVisible(true); - newWindow.setResizable(false); - } else if (clickedButton == rightBut1) { - /** - * ·ѯť - */ - card.first(cardPanel); - for (int i = 0;i < 0;i++) { - card.next(cardPanel); - } - repaint(); - } else if (clickedButton == rightBut2) { - /** - * Ϣť - */ - card.first(cardPanel); - for (int i = 0;i < 1;i++) { - card.next(cardPanel); - } - } else if (clickedButton == rightBut3) { - /** - * ӽڵ㰴ť - */ - card.first(cardPanel); - for (int i = 0;i < 2;i++) { - card.next(cardPanel); - } - repaint(); - } else if (clickedButton == rightBut4) { - /** - * ɾڵ㰴ť - */ - card.first(cardPanel); - for (int i = 0;i < 3;i++) { - card.next(cardPanel); - } - repaint(); - } else if (clickedButton == rightBut5) { - /** - * Ӿ㰴ť - */ - card.first(cardPanel); - for (int i = 0;i < 4;i++) { - card.next(cardPanel); - } - repaint(); - } else if (clickedButton == rightBut6) { - /** - * ɾ㰴ť - */ - card.first(cardPanel); - for (int i = 0;i < 5;i++) { - card.next(cardPanel); - } - repaint(); - } else if (clickedButton == rightBut7) { - /** - * ɾ㰴ť - */ - card.first(cardPanel); - for (int i = 0;i < 6;i++) { - card.next(cardPanel); - } - } - } - - } - - class MyMouseListener implements MouseListener { - - @Override - public void mouseClicked(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mousePressed(MouseEvent e) { - if (e.getSource() == thirdPanelUpMapLabel) { - String x = String.valueOf(e.getX()); - String y = String.valueOf(e.getY()); - thirdPanelDownLab2.setText(x + "," + y); - } - - } - - @Override - public void mouseReleased(MouseEvent e) { - if (e.getSource() == secondPanelList) { - if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) { - String viewName = (String) secondPanelList.getSelectedValue(); - String introduction = view.get(viewName).split(" ")[1]; - secondPanelLab.setText(introduction); - } - } - } - - @Override - public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseExited(MouseEvent e) { - // TODO Auto-generated method stub - - } - - } -} diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideUtil.java" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideUtil.java" deleted file mode 100644 index 48b3ad1..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/GuideUtil.java" +++ /dev/null @@ -1,168 +0,0 @@ -package org.geekgao.guide; - -import java.awt.Image; -import java.awt.image.BufferedImage; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import javax.imageio.ImageIO; - -/** - * - * @author geekgao - * - * ͼƬ,ȡļ - * - */ -public class GuideUtil { - - private GuideUtil(){} - - /** - * - * @param path - * @return - * ͼƬ - */ - public static Image getImage(String path) { - URL u = GuideUtil.class.getClassLoader().getResource(path); - BufferedImage img = null; - - try { - img = ImageIO.read(u); - } catch (IOException e) { - e.printStackTrace(); - } - - return img; - } - - /** - * - * @param path - * @return - * ڵϢ - */ - public static Map getVertex(String path) { - Map map = new HashMap(); - BufferedReader fileIn = null; - String lineStr; - - try { - fileIn = new BufferedReader(new FileReader(path)); - while ((lineStr = fileIn.readLine()) != null) { - Vertex vex = new Vertex(); - String[] everyNum = lineStr.split(" "); - vex.num = Integer.parseInt(everyNum[0]); - vex.x = Integer.parseInt(everyNum[1]); - vex.y = Integer.parseInt(everyNum[2]); - - vex.pointNum = new HashMap(); - for (int i = 3;i < everyNum.length;i+=2) { - vex.pointNum.put(Integer.valueOf(everyNum[i]), Integer.valueOf(everyNum[i+1])); - } - map.put(vex.num, vex); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - fileIn.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return map; - - } - - public static void setVertex(Map map,String outPath) { - BufferedWriter outFile = null; - Set vertexNum = map.keySet(); - - try { - outFile = new BufferedWriter(new FileWriter(outPath)); - for (Integer v:vertexNum) { - Vertex tmpVex = map.get(v); - outFile.write(v + " " + tmpVex.x + " " + tmpVex.y); - Map tmpLength = tmpVex.pointNum; - Set pointNum = tmpLength.keySet(); - for (Integer i:pointNum) { - outFile.write(" " + i + " " + tmpLength.get(i)); - } - outFile.newLine(); - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - outFile.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - public static Map getView(String path) { - Map result = new HashMap(); - BufferedReader fileIn = null; - - try { - fileIn = new BufferedReader(new FileReader(path)); - String all,first,second; - String[] tmp; - while ((all = fileIn.readLine()) != null) { - tmp = all.split(" ", 2); - first = tmp[0]; - second = tmp[1]; - result.put(first, second); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - try { - fileIn.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return result; - } - - public static void setView(Map view,String outPath) { - BufferedWriter outFile = null; - Set viewName = view.keySet(); - - try { - outFile = new BufferedWriter(new FileWriter(outPath)); - for (String v:viewName) { - outFile.write(v + " " + view.get(v)); - outFile.newLine(); - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - outFile.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - -} \ No newline at end of file diff --git "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/Vertex.java" "b/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/Vertex.java" deleted file mode 100644 index 62e29d9..0000000 --- "a/\350\245\277\351\202\256\345\257\274\346\270\270\347\263\273\347\273\237\346\272\220\347\240\201/\346\272\220\347\240\201/org/geekgao/guide/Vertex.java" +++ /dev/null @@ -1,18 +0,0 @@ -package org.geekgao.guide; - -import java.util.Map; - -/** - * - * @author geekgao - * ϵͳݽṹ - * ʾͼеһ - * Ȩ - * - */ - -class Vertex { - int num;// - int x,y;// - Map pointNum;//ָЩ(ıźweight) -}