📄 grareader.java
字号:
package graph;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class GraReader{
public Graph graph;
public int count=0;
public int num;
public GraReader(int no){
num=no;
}
public Graph getGraph(String name)throws IOException{
InputStream is=(new File(name+".obj")).toURL().openStream();
return getGraph(is);
}
public Graph getGraph(InputStream is)throws IOException{
graph=new Graph(num);
StreamTokenizer st = new StreamTokenizer(
new BufferedReader(new InputStreamReader(is, "UTF-8")));
st.eolIsSignificant(true);
st.commentChar('#');
scan:
while (true) {
switch (st.nextToken()) {
default:
break scan;
case StreamTokenizer.TT_EOL:
break;
case StreamTokenizer.TT_WORD:
if("size".equals(st.sval)){
if(st.nextToken() == StreamTokenizer.TT_NUMBER){
graph=new Graph((int)st.nval);
}
}else if ("v".equals(st.sval)) {
double x = 0, y = 0;
if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
x = st.nval;
if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
y = st.nval;
}
}
addVert((int) x, (int) y);
while (st.ttype != StreamTokenizer.TT_EOL &&
st.ttype != StreamTokenizer.TT_EOF)
st.nextToken();
} else if ("l".equals(st.sval)) {
int start = 0;
int prev = 0;
int n = 0;
while (true)
if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
n = (int) st.nval;
if (prev >= 0)
addLine(prev , n );
if (start < 0)
start = n;
prev = n;
} else if (st.ttype == '/')
st.nextToken();
else
break;
if (start >= 0)
addLine(start, prev);
if (st.ttype != StreamTokenizer.TT_EOL)
break scan;
} else {
while (st.nextToken() != StreamTokenizer.TT_EOL
&& st.ttype != StreamTokenizer.TT_EOF);
}
}
}
is.close();
return graph;
}
public void addVert(int x,int y){
if(count>graph.labels.length-1)
return;
Point v=new Point(x,y);
graph.setLabel(count,v);
count++;
}
public void addLine(int i,int j){
graph.addEdge(i,j);
}
}
/*class Test{
public static void main(String args[]){
Graph grap=null;
try{
GraReader g=new GraReader();
grap=g.getGraph("graph");
}catch(IOException e){
e.printStackTrace();
}
for(int i=0;i<6;i++){
Point p=(Point)grap.labels[i];
System.out.println("Vertex:<"+p.x+","+p.y+">");
}
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -