GeoTest.java 2.16 KB
Newer Older
Juon Kawakami's avatar
init  
Juon Kawakami committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package firesimulator.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 * @author tn
 *
 */
public class GeoTest extends JFrame implements MouseListener{

    private static final long serialVersionUID = 1L;
    float p[];
	float[] cross;
	int current=0;
	Point c;
	Point d;

	public static void main(String[] args) {
		GeoTest gt=new GeoTest();
		gt.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		gt.setVisible(true);
	}
	
	public GeoTest(){
		super("geotest");
		p=new float[]{50f,50f,300f,300f,100f,100f,200f,200f};
		addMouseListener(this);
		setSize(500,500);
		cross=null;
	}
	
	public void update(Graphics g){
		g.setColor(Color.BLACK);
		g.fillRect(0,0,getWidth(),getHeight());
		g.setColor(Color.RED);
		g.drawLine((int)p[0],(int)p[1],(int)p[2],(int)p[3]);
		g.setColor(Color.GREEN);
		g.drawLine((int)p[4],(int)p[5],(int)p[6],(int)p[7]);		
		if(d!=null&&c!=null){
			g.setColor(Color.YELLOW);
			g.drawLine(c.x,c.y,d.x,d.y);
		}
		if(cross!=null){
			g.setColor(Color.CYAN);
			g.drawLine((int)cross[0]-10,(int)cross[1]-10,(int)cross[0]+10,(int)cross[1]+10);
			g.drawLine((int)cross[0]-10,(int)cross[1]+10,(int)cross[0]+10,(int)cross[1]-10);
		}
		if(c!=null){
			g.setColor(Color.YELLOW);
			g.drawOval(c.x-5,c.y-5,10,10);
		}
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON1){		
			if(current>7)current=0;
			p[current]=(float)e.getX();
			p[++current]=(float)e.getY();
			if(e.isControlDown()) p[current-1]=200f;
			if(e.isShiftDown()) p[current-1]=400f;
			current++;
		}
		findPoint();
		d=Geometry.getRndPoint(c,100d);
		findIntersect();
		update(getGraphics());
	}

	private void findPoint() {
		Point a=new Point((int)p[0],(int)p[1]);
		Point b=new Point((int)p[2],(int)p[3]);
		c=Geometry.getRndPoint(a,b);
	}

	private void findIntersect() {
		cross=Geometry.intersect(p);
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
		
	}
	
	
	
	
	
}