/** Programmers: Y.Miretskiy & M.Sabaev Date : July 18-23, 1996; **/ import java.applet.Applet; import java.awt.*; import java.lang.*; public class main1 extends Applet { private int r1 = 0; private int r2 = 0; private int r3 = 0; private int x1,x2,y1,y2; private Station st1,st2,st3; private Circle c1,c2,c3; private TextField tf1,tf2,tf3,tf4; private Label lb1,lb2,lb3,lb4,lb5,lb6,lb7,lb8; private Panel p1,p2,p3,p4,p5; private Rectangle r; private Dimension d; private GridBagLayout gbl; private Button det; private LaDetails details; public void init() { gbl = new GridBagLayout(); this.setLayout(gbl); p1 = new Panel(); p1.setLayout(gbl); lb1 = new Label("(S-P) arrival time "); lb2 = new Label("(S-P) arrival time "); lb3 = new Label("(S-P) arrival time "); constrain(p1,lb1,0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(p1,lb2,1,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(p1,lb3,2,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); d = this.size(); r = new Rectangle(0,60,d.width, d.height-60); p2 = new Panel(); p2.setLayout(gbl); tf1 = new TextField(5); Label lb4 = new Label("sec "); constrain(p2,tf1,0,0,1,1); constrain(p2,lb4,1,0,1,1); p3 = new Panel(); p3.setLayout(gbl); tf2 = new TextField(5); Label lb5 = new Label("sec "); constrain(p3,tf2,0,0,1,1); constrain(p3,lb5,1,0,1,1); p4 = new Panel(); p4.setLayout(gbl); tf3 = new TextField(5); lb6 = new Label("sec "); constrain(p4,tf3,0,0,1,1); constrain(p4,lb6,1,0,1,1); det = new Button(" Details "); constrain(p1,p2,0,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(p1,p3,1,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(p1,p4,2,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(p1,det,3,1,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE, GridBagConstraints.EAST,1.0,1.0,0,0,0,0); constrain(this,p1,0,0,GridBagConstraints.REMAINDER,1, GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTH,1.0,1.0,0,0,0,0); st1 = new Station(Color.blue); st1.resize(21,21); st2 = new Station(Color.gray); st2.resize(21,21); st3 = new Station(Color.green); st3.resize(21,21); constrain(this,st1,0,1,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE, GridBagConstraints.CENTER,1.0,1.0,0,0,0,0); constrain(this,st2,0,2,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE, GridBagConstraints.WEST,1.0,1.0,0,0,0,0); constrain(this,st3,0,3,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE, GridBagConstraints.EAST,1.0,1.0,0,0,0,0); this.setFont(new Font("TimesRoman",Font.PLAIN,10)); lb7 = new Label("Scale:"); lb8 = new Label("km:grid"); tf4 = new TextField("15",5); // tf4.setEditable(false); p5 = new Panel(); p5.setLayout(gbl); constrain(p5,lb7,1,0,1,1); constrain(p5,tf4,2,0,1,1); constrain(p5,lb8,3,0,1,1); constrain(this,p5,0,4,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE, GridBagConstraints.SOUTHWEST,1.0,1.0,0,0,0,0); } public void drawMap(Graphics g, Rectangle r) { g.setColor(Color.white); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(Color.black); for(int x=r.x; x<=(r.x+r.width);x+=20) g.drawLine(x, r.y, x, r.y+r.height); for(int y=r.y; y<=(r.y+r.height);y+=20) g.drawLine(r.x, y, r.x+r.width, y); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { drawMap(g,r); c1 = new Circle(st1.center(),r1,st1.getColor()); c2 = new Circle(st2.center(),r2,st2.getColor()); c3 = new Circle(st3.center(),r3,st3.getColor()); c1.draw(g,r.y,1); c2.draw(g,r.y,1); c3.draw(g,r.y,1); if((c1.intersects(c2)) && (c2.intersects(c3)) && (c3.intersects(c1))) { Point[] p1 = new Point[2]; p1 = c2.intersections(c3); float m1 = ((float)(p1[0].y-p1[1].y))/(p1[0].x-p1[1].x); Point[] p2 = new Point[2]; p2 = c1.intersections(c2); float m2 = ((float)(p2[0].y-p2[1].y))/(p2[0].x-p2[1].x); Point[] p3 = new Point[2]; p3= c3.intersections(c1); float m3 = ((float)(p3[0].y-p3[1].y))/(p3[0].x-p3[1].x); float temp = ((m1*p1[0].x+p2[0].y-p1[0].y-m2*p2[0].x)/(m1-m2)); y1 = (int)(m1*temp-m1*p1[0].x+p1[0].y); x1=(int)temp; temp = ((m1*p1[0].x+p3[0].y-p1[0].y-m3*p3[0].x)/(m1-m3)); y2 = (int)(m1*temp-m1*p1[0].x+p1[0].y); x2=(int)temp; if((equals(x1,x2,2)) && insideCir(x1,y1) && onMap(x1,y1)) { g.setColor(Color.red); drawChord(g, p1[0].x, p1[0].y, p1[1].x, p1[1].y, r.y, m1); drawChord(g, p2[0].x, p2[0].y, p2[1].x, p2[1].y, r.y, m2); drawChord(g, p3[0].x, p3[0].y, p3[1].x, p3[1].y, r.y, m3); g.fillOval(x1-4,(y1+y2)/2-4,8,8); g.setFont(new Font("Helvetica",Font.ITALIC,10)); if(x1+54<=r.x+r.width) g.drawString("Epicenter",x1+7,y1+4); else g.drawString("Epicenter",x1-54,y1+4); } } } private boolean insideCir(int x, int y) { return Math.sqrt(Math.pow(x-c1.getCenter().x,2)+ Math.pow(y-c1.getCenter().y,2))<= c1.getRadius(); } private boolean equals(int x, int y, int num) { if (Math.abs(x-y)<=num) return true; else return false; } private void drawChord(Graphics g, int x1, int y1, int x2, int y2, int yBound, float m) { if((y1>=yBound) && (y2>=yBound)) g.drawLine(x1,y1,x2,y2); else if(y10) c.insets=new Insets(top,left,bottom,right); ((GridBagLayout)container.getLayout()).setConstraints(component,c); container.add(component); } private void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height) { constrain(container,component,grid_x,grid_y, grid_width,grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0,0.0,0,0,0,0); } private void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int top, int left, int bottom, int right) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST,0.0,0.0,top,left, bottom,right); } private boolean onMap(int x, int y) { if (((bounds().x+4<= x) && (bounds().x+bounds().width-4>= x) && (bounds().y+64<= y) && (bounds().y+bounds().height-4>= y)) && ((x>161) || (y<315))) return true; else return false; } public boolean handleEvent(Event evt) { if (evt.id == Event.MOUSE_DRAG) { if (((st1.inside(evt.x,evt.y)) || (st2.inside(evt.x,evt.y)) || (st3.inside(evt.x,evt.y))) && ((r1!=0) || (r2!=0) || (r3!=0))) { r1=0; r2=0; r3=0; repaint(); } if ((st1.inside(evt.x,evt.y)) && (onMap(evt.x+st1.bounds().width/2, evt.y+st1.bounds().height/2)) && (onMap(evt.x-st1.bounds().width/2, evt.y-st1.bounds().height/2))) { st1.move(evt.x-st1.bounds().width/2,evt.y-st1.bounds().height/2); return true; } else if ((st2.inside(evt.x,evt.y)) && (onMap(evt.x+st2.bounds().width/2, evt.y+st2.bounds().height/2)) && (onMap(evt.x-st2.bounds().width/2, evt.y-st2.bounds().height/2))) { st2.move(evt.x-st2.bounds().width/2,evt.y-st2.bounds().height/2); return true; } else if ((st3.inside(evt.x,evt.y)) && (onMap(evt.x+st3.bounds().width/2, evt.y+st3.bounds().height/2)) && (onMap(evt.x-st3.bounds().width/2, evt.y-st3.bounds().height/2))) { st3.move(evt.x-st3.bounds().width/2,evt.y-st3.bounds().height/2); return true; } else return super.handleEvent(evt); } else return super.handleEvent(evt); } public boolean action(Event evt, Object what) { if ((evt.target == tf1) || (evt.target == tf2) || (evt.target == tf3) || (evt.target == tf4)) { try { float scale = Float.valueOf(tf4.getText()).floatValue(); float time1 = Float.valueOf(tf1.getText()).floatValue(); r1 = (int)(time1*180f/scale); float time2 = Float.valueOf(tf2.getText()).floatValue(); r2 = (int)(time2*180f/scale); float time3 = Float.valueOf(tf3.getText()).floatValue(); r3 = (int)(time3*180f/scale); repaint(); } catch (NumberFormatException e) { r1=0; r2=0; r3=0; repaint(); return true; } return true; } else if(evt.target==det) { if((det.getLabel()).equals("Close")) { remove(details); showStations(); det.setLabel("Details"); tf1.setEditable(true); tf2.setEditable(true); tf3.setEditable(true); tf4.setEditable(true); return true; } else { details = new LaDetails(scaleIt(), onMap(x1,y1), insideCir(x1, y1)); if (details.getFlag() && details.getE()) details.reshape(340,70,225,135); else details.reshape(340,70,180,90); constrain(this,details,0,0,1,1); hideStations(); det.setLabel("Close"); tf1.setEditable(false); tf2.setEditable(false); tf3.setEditable(false); tf4.setEditable(false); return true; } } //end handling det button else return super.action(evt,what); } private void hideStations() { if(( insideIt(st1.bounds().x,st1.bounds().y)) || insideIt(st1.bounds().x+st1.bounds().width, st1.bounds().y+st1.bounds().height)) st1.hide(); if(( insideIt(st2.bounds().x,st2.bounds().y)) || insideIt(st2.bounds().x+st2.bounds().width, st2.bounds().y+st2.bounds().height)) st2.hide(); if(( insideIt(st3.bounds().x,st3.bounds().y)) || insideIt(st3.bounds().x+st3.bounds().width, st3.bounds().y+st3.bounds().height)) st3.hide(); } private boolean insideIt(int x, int y) { if( (details.bounds().x < x) && (details.bounds().x+details.bounds().width > x) && (details.bounds().y < y) && (details.bounds().y+details.bounds().height > y)) return true; else return false; } //end inside private void showStations() { if(!st1.isVisible()) st1.show(); if(!st2.isVisible()) st2.show(); if(!st3.isVisible()) st3.show(); } public float[] scaleIt() { float[] result = new float[3]; try { float scale=Float.valueOf(tf4.getText()).floatValue(); result[0]=((float)c1.getRadius())*scale/20f; result[1]=((float)c2.getRadius())*scale/20f; result[2]=((float)c3.getRadius())*scale/20f; return result; } catch(NumberFormatException e) return null; } } //end main1