//TimePanel.java import java.awt.*; import java.awt.event.*; class TimeMouseAdapter extends MouseAdapter implements MouseMotionListener { TimePanel time; int startX=0, startY=0,endX=0,endY=0; public TimeMouseAdapter(TimePanel time) { super(); this.time = time; time.addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { startX = e.getX(); startY = e.getY(); time.EIN.setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 1) { time.offsetX = 0; time.offsetY = 0; } } public void mouseReleased(MouseEvent e) { time.EIN.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); time.repaint(); } public void mouseDragged(MouseEvent e) { time.EIN.setCursor(new Cursor(Cursor.MOVE_CURSOR)); endX = e.getX(); endY = e.getY(); time.offsetX += endX - startX; time.offsetY += endY - startY; startX = endX; startY = endY; time.repaint(); } public void mouseMoved(MouseEvent e) {} } public class TimePanel extends Panel { int top_offset = 30; int offsetX=0,offsetY=0; public Einstein EIN; double margin = .1; double leftmargin = .1; double rightmargin = .1; double divide = 500; public double mapX(double percent) { Rectangle r = getBounds(); double temp = (offsetX+(percent * r.width)); //if (temp > (r.width-3)) return (r.width-3); //if (temp < 3) return 3; return temp; } public double mapY(double percent) { Rectangle r = getBounds(); double temp = (offsetY+top_offset+(r.width - (percent * r.width))); //if (temp > (r.height-top_offset-3)) return (r.height-top_offset-3); //if (temp < (3+top_offset)) return (3+top_offset); //EIN.showStatus(""+temp); return temp; } public TimePanel(Einstein EIN) { this.EIN = EIN; setBackground(Color.white); this.addMouseListener(new TimeMouseAdapter(this)); } public void update(Graphics g) { paint(g); } public String round(double num) { Double d = new Double(num); String sv = d.toString(); int deci = sv.length() - sv.indexOf('.'); if (deci > 2) deci = 2; int end = sv.indexOf('.')+deci; return sv.substring(0,end); } public void Arrow (double x0, double y0, double x1, double y1,Graphics g) { double w, dw = 0.5, r = 5, dx = x1-x0, dy = y0-y1; g.drawLine((int)x0,(int)y0,(int)x1,(int)y1); w = Math.atan2(dy,dx); x0 = x1-r*Math.cos(w+dw)+0.5; y0 = y1+r*Math.sin(w+dw)+0.5; g.drawLine((int)x0,(int)y0,(int)x1,(int)y1); x0 = x1-r*Math.cos(w-dw)+0.5; y0 = y1+r*Math.sin(w-dw)+0.5; g.drawLine((int)x0,(int)y0,(int)x1,(int)y1); } public void drawPoint(double x, double t, Graphics g) { double xdraw = (x/(divide)); double tdraw = (t/(divide)); g.fillOval((int)mapX(margin+xdraw)-2,(int)mapY(margin+tdraw)-2,4,4); } public void drawLine(double x1, double t1, double x2, double t2, Graphics g) { double x1draw = (x1/(divide)); double t1draw = (t1/(divide)); double x2draw = (x2/(divide)); double t2draw = (t2/(divide)); g.drawLine((int)mapX(margin+x1draw),(int)mapY(margin+t1draw),(int)mapX(margin+x2draw),(int)mapY(margin+t2draw)); } public void paint(Graphics gr2) { Rectangle temp = EIN.top_lab.getBounds(); top_offset = temp.height+10; Rectangle r = getBounds(); EIN.offscr2.setColor(Color.white); EIN.offscr2.fillRect(0,0,r.width,r.height); EIN.offscr2.setColor(new Color(207,107,34)); EIN.offscr2.fillRect(0,top_offset,r.width-2,r.width-2); EIN.offscr2.setColor(Color.black); EIN.offscr2.drawLine(2,top_offset+r.width-1,r.width-1,top_offset+r.width-1); EIN.offscr2.drawLine(r.width-1,top_offset+2,r.width-1,top_offset+r.width-2); double scale_obs=0,scale_obj=0; Reality real = EIN.getReality(); String obs_str = "Observer x= "+round(real.myX)+" t= "+round(real.myTime); String obj_str = "Object x= "+round(real.theirX)+" t= "+round(real.theirTime); if (EIN.frame == Reality.OBJECT_FRAME) { real = EIN.mover.getReality(); obs_str = "Observer x= "+round(real.theirX)+" t= "+round(real.theirTime); obj_str = "Object x= "+round(real.myX)+" t= "+round(real.myTime); scale_obs = Math.atan(real.getV()); } else { scale_obj = Math.atan(real.getV()); } EIN.offscr2.setColor(Color.blue); Arrow(mapX(margin),mapY(margin),mapX(margin+(1-2*margin)*Math.cos(scale_obj)),mapY(margin+(1-2*margin)*Math.sin(scale_obj)),EIN.offscr2); Arrow(mapX(margin),mapY(margin),mapX(margin+(1-2*margin)*Math.sin(scale_obj)),mapY(margin+(1-2*margin)*Math.cos(scale_obj)),EIN.offscr2); EIN.offscr2.setColor(Color.red); Arrow(mapX(margin),mapY(margin),mapX(margin+(1-2*margin)*Math.cos(scale_obs)),mapY(margin+(1-2*margin)*Math.sin(scale_obs)),EIN.offscr2); Arrow(mapX(margin),mapY(margin),mapX(margin+(1-2*margin)*Math.sin(scale_obs)),mapY(margin+(1-2*margin)*Math.cos(scale_obs)),EIN.offscr2); EIN.offscr2.setColor(Color.black); EIN.offscr2.drawString("ct",(int)mapX(.05),(int)mapY(.5)); EIN.offscr2.drawString("x",(int)mapX(.5),(int)mapY(.05)); EIN.offscr2.setColor(Color.white); EIN.offscr2.fillRect(0,0,r.width,top_offset); EIN.offscr2.fillRect(0,top_offset+r.width,r.width,r.height - (top_offset+r.width)); EIN.mover.timeDraw(EIN.frame,EIN.offscr2); EIN.offscr2.setColor(Color.white); EIN.offscr2.fillRect(0,0,r.width,top_offset-1); EIN.offscr2.fillRect(0,top_offset+r.width,r.width,r.height-r.width-top_offset); EIN.offscr2.setColor(Color.black); FontMetrics fm = getFontMetrics(EIN.offscr2.getFont()); EIN.offscr2.drawString(obs_str,(r.width-fm.charsWidth(obs_str.toCharArray(),0,obs_str.length()))/2,3+(int)(r.height-(fm.getHeight()/2))); if (EIN.mover == null) obj_str = "Choose experiment or click on Java TA to start"; EIN.offscr2.drawString(obj_str,(r.width-fm.charsWidth(obj_str.toCharArray(),0,obj_str.length()))/2,5+fm.getHeight()); gr2.drawImage(EIN.image2,0,0,this); } }