graphic.java

来自「Java实现的常用数据结构算法」· Java 代码 · 共 192 行

JAVA
192
字号
package Graphics;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class Graphic extends Panel implements  Runnable{
	public Vertex[] vertex;
	public Line[] line;
	public int count=0;
	public int ct=0;
	public Graphic(){
		vertex=new Vertex[5];
		line=new Line[5];
	/*	this.addMouseListener(this);
		vertex=new Vertex[5];
		line=new Line[5];
		for(int i=0;i<5;i++){
			vertex[i]=new Vertex(i*70,i*70);
			vertex[i].setColor(i);
			if(i>0)
			line[i]=new Line(vertex[i],vertex[i-1]);
		}
		line[0]=new Line(vertex[0],vertex[4]);*/
	 
	}
	public void run(){}
	public Graphic(InputStream is)throws IOException, FileFormatException{
		this();
		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 ("v".equals(st.sval)) {
		    double x = 0, y = 0, z = 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 ("f".equals(st.sval) || "fo".equals(st.sval) || "l".equals(st.sval)) {
		    int start = -1;
		    int prev = -1;
		    int n = -1;
		    while (true)
			if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
			    n = (int) st.nval;
			    if (prev >= 0)
				addLine(prev - 1, n - 1);
			    if (start < 0)
				start = n;
			    prev = n;
			} else if (st.ttype == '/')
			    st.nextToken();
			else
			    break;
		    if (start >= 0)
			addLine(start - 1, prev - 1);
		    if (st.ttype != StreamTokenizer.TT_EOL)
			break scan;
		} else {
		    while (st.nextToken() != StreamTokenizer.TT_EOL
			    && st.ttype != StreamTokenizer.TT_EOF);
		}
	    }
	}
	is.close();
	if (st.ttype != StreamTokenizer.TT_EOF)
	    throw new FileFormatException(st.toString());
	}
	public void addVert(int x,int y){
		if(count>4)
		return;
		Vertex v=new Vertex(x,y);
		 vertex[count]=v;
		 count++;
		 System.out.println("added vertex x"+v.position.x +"y:"+v.position.y);
		
	}
	public void addLine(int i,int j){
		if(i<5&&j<5){
		Line l=new Line(vertex[i],vertex[j]);
		line[ct]=l;
		ct++;
		System.out.println("added Line x"+vertex[i].position.x+ "y:"+vertex[i].position.y+
		vertex[j].position.x + "y:"+vertex[j].position.y);	
		}
		
	}

/*		public void mousePressed(MouseEvent evt){
				for(int i=0;i<5;i++){
					Point p=evt.getPoint();
					if(vertex[i].isSelected(evt.getPoint())){
					  vertex[i].setColor(1);
					  vertex[i].setPosition(p.x,p.y);
					}
					
				}
			}
			public void mouseClicked(MouseEvent evt){
				for(int i=0;i<5;i++){
					//if(vertex[i].isSelected(evt.getPoint()))
					vertex[i].state=2;
				}
			}
			public void mouseExited(MouseEvent evt){}
			public void mouseEntered(MouseEvent evt){}
			public void mouseReleased(MouseEvent evt){}
		*/	
	public void Paint(Graphics g) {
		g.drawString("helo",30,30);
		while(true){
			 
			for(int i=0;i<line.length;i++){
			line[i].draw(g);
		    }
		   try{
		    	new Thread().sleep(2000);
		    	
		    }	catch(InterruptedException e){}		
		}
		
	}
	public void display(){
		for(int i=0;i<5;i++){
			System.out.println("the "+i+"st X->"+vertex[i].position.x+" Y->"+ vertex[i].position.y);
		}
		for(int i=0;i<4;i++){
		  System.out.println("added Line x->"+line[i].v1.position.x+ "y->"+line[i].v1.position.y+"x->"
		  +line[i].v2.position.x + "y->"+line[i].v2.position.y);	
		}
	}
	public void Paint(Dragable obj){
		Graphics g=this.getGraphics();
		obj.draw(g);
		
	}

	class FileFormatException extends Exception {
    public FileFormatException(String s) {
	super(s);
    }
}
//	public void paint(Dragable obj) {}
	public void searchPath(int startVertex, int distVertex) {
	}
	public static void main(String args[]){
		Frame f=new Frame();
		f.setSize(400,400);
		f.setVisible(true);
		Graphic gra=null;//new Graphic();
		InputStream is =null;
		File fr;
		/**/	try {
		   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		    is= (fr=new File("source.txt")).toURL().openStream();
		    System.out.println(fr.toURL());
		    gra = new Graphic (is);
		    gra.setBackground(Color.black);		    
		 } catch(Exception e) {
		    e.toString();
		 }
		 try {
		    if (is != null)
			is.close();
		 } catch(Exception e) {
		 }
		 if(gra!=null){
		 	f.add(gra);	
		 	System.out.println("ok");
		 	//gra.display();	 
		 }
		 
		 
		 	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?