import java.awt.*; import java.awt.event.*; import java.lang.*; import java.lang.Math; public class SecondLaw1 extends java.applet.Applet implements Runnable { Canvas oscillators; Label forceLabel, forceAngleLabel, muLabel, forceS, forceAngleS; Thread runner; TextField mu; Scrollbar forcescroll, forceAnglescroll; int force, forceAngle; int[] xpos = new int[30]; int[] ypos = new int[30]; double[] x = new double[30]; double[] y = new double[30]; double coeff, appliedF, forceA, fX, fY, fNormal, fFriction, accel; double pi = 3.141592653589793238468; Image offscreenImg; Graphics offscreenG; void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) { gbc.gridx = gx; gbc.gridy = gy; gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty = wy; } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridbag); // Force text buildConstraints(constraints, 0, 0, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label forceLabel = new Label("Applied force (N):", Label.CENTER); gridbag.setConstraints (forceLabel, constraints); add(forceLabel); // Force scrollbar label buildConstraints(constraints, 2, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; forceS = new Label("50", Label.LEFT); force = 50; gridbag.setConstraints (forceS, constraints); add(forceS); // force scrollbar buildConstraints(constraints, 1, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; forcescroll = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 20, 100); gridbag.setConstraints (forcescroll, constraints); add(forcescroll); // force angle text buildConstraints(constraints, 0, 1, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label forceAngleLabel = new Label("Angle of force (degrees):", Label.CENTER); gridbag.setConstraints (forceAngleLabel, constraints); add(forceAngleLabel); // force Angle scrollbar label buildConstraints(constraints, 2, 1, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; forceAngleS = new Label("0", Label.LEFT); forceAngle = 0; gridbag.setConstraints (forceAngleS, constraints); add(forceAngleS); // force Angle scrollbar buildConstraints(constraints, 1, 1, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; forceAnglescroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, -90, 90); gridbag.setConstraints (forceAnglescroll, constraints); add(forceAnglescroll); // coefficient of friction label buildConstraints(constraints, 0, 2, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label mulabel = new Label("Coefficient of friction (between 0 and 2.0):", Label.RIGHT); gridbag.setConstraints (mulabel, constraints); add(mulabel); // coefficient of friction textbox buildConstraints(constraints, 1, 2, 1, 1, 0, 0); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; mu = new TextField("0.5", 6); coeff = 0.5; gridbag.setConstraints (mu, constraints); add(mu); // canvas buildConstraints(constraints, 0, 3, 3, 1, 0, 80); constraints.fill = GridBagConstraints.HORIZONTAL; oscillators = new Canvas(); gridbag.setConstraints (oscillators, constraints); add(oscillators); offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); } public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { int index, temp; double scale, kAngle, arrowScale, tempA; Double tempText; scale = 0.5; arrowScale = 9.0; kAngle = pi / 180.0; while (true) { appliedF = force; forceA = kAngle*forceAngle; fX = appliedF*Math.cos(forceA); if (Math.abs(fX)<0.000001) fX = 0.0; fY = appliedF*Math.sin(forceA); if (Math.abs(fY)<0.000001) fY = 0.0; fNormal = 100.0-fY; tempText = Double.valueOf(mu.getText()); coeff = tempText.doubleValue(); if (coeff < 0.0) coeff = 0.0; if (coeff > 2.0) coeff = 2.0; fFriction = coeff*fNormal; if (fFriction > fX) fFriction = fX; accel = (fX - fFriction)*0.098; x[0] = 30.0; x[1] = 80.0; temp=180-30; tempA = kAngle*temp; x[2] = x[1]+arrowScale*Math.cos(tempA); temp=180+30; tempA = kAngle*temp; x[3] = x[1]+arrowScale*Math.cos(tempA); x[4] = 30.0; temp=90-30; tempA = kAngle*temp; x[5] = x[4]+arrowScale*Math.cos(tempA); temp=90+30; tempA = kAngle*temp; x[6] = x[4]+arrowScale*Math.cos(tempA); x[7] = 200.0; x[8] = x[7]+scale*appliedF*Math.cos(forceA); temp=180-forceAngle-30; tempA = kAngle*temp; x[9] = x[8]+arrowScale*Math.cos(tempA); temp=180-forceAngle+30; tempA = kAngle*temp; x[10] = x[8]+arrowScale*Math.cos(tempA); x[11] = x[7]+scale*fX; temp=180-30; tempA = kAngle*temp; x[12] = x[11]+arrowScale*Math.cos(tempA); temp=180+30; tempA = kAngle*temp; x[13] = x[11]+arrowScale*Math.cos(tempA); x[14] = x[7]; temp=90-30; tempA = kAngle*temp; x[15] = x[14]+arrowScale*Math.cos(tempA); temp=90+30; tempA = kAngle*temp; x[16] = x[14]+arrowScale*Math.cos(tempA); x[17] = 150.0; x[18] = x[17]; temp=180-270-30; tempA = kAngle*temp; x[19] = x[18]+arrowScale*Math.cos(tempA); temp=180-270+30; tempA = kAngle*temp; x[20] = x[18]+arrowScale*Math.cos(tempA); x[21] = 150.0; x[22] = x[21]; temp=90-30; tempA = kAngle*temp; x[23] = x[22]+arrowScale*Math.cos(tempA); temp=90+30; tempA = kAngle*temp; x[24] = x[22]+arrowScale*Math.cos(tempA); x[25] = 100.0; x[26] = x[25]-scale*fFriction; temp= -30; tempA = kAngle*temp; x[27] = x[26]+arrowScale*Math.cos(tempA); temp= 30; tempA = kAngle*temp; x[28] = x[26]+arrowScale*Math.cos(tempA); y[0] = 220.0; y[1] = 220.0; temp=180-30; tempA = kAngle*temp; y[2] = y[1]+arrowScale*Math.sin(tempA); temp=180+30; tempA = kAngle*temp; y[3] = y[1]+arrowScale*Math.sin(tempA); y[4] = 170.0; temp=90-30; tempA = kAngle*temp; y[5] = y[4]+arrowScale*Math.sin(tempA); temp=90+30; tempA = kAngle*temp; y[6] = y[4]+arrowScale*Math.sin(tempA); y[7] = 305.0; y[8] = y[7]-scale*appliedF*Math.sin(forceA); temp=180-forceAngle-30; tempA = kAngle*temp; y[9] = y[8]+arrowScale*Math.sin(tempA); temp=180-forceAngle+30; tempA = kAngle*temp; y[10] = y[8]+arrowScale*Math.sin(tempA); y[11] = y[7]; temp=180-30; tempA = kAngle*temp; y[12] = y[11]+arrowScale*Math.sin(tempA); temp=180+30; tempA = kAngle*temp; y[13] = y[11]+arrowScale*Math.sin(tempA); y[14] = y[7]-scale*fY; temp=90-30; if (fY<0.0) temp = -90-30; tempA = kAngle*temp; y[15] = y[14]+arrowScale*Math.sin(tempA); temp=90+30; if (fY<0.0) temp = -90+30; tempA = kAngle*temp; y[16] = y[14]+arrowScale*Math.sin(tempA); y[17] = 320.0; y[18] = y[17]+50.0; temp=180-270-30; tempA = kAngle*temp; y[19] = y[18]+arrowScale*Math.sin(tempA); temp=180-270+30; tempA = kAngle*temp; y[20] = y[18]+arrowScale*Math.sin(tempA); y[21] = 290.0; y[22] = y[21]-scale*fNormal; temp=90-30; tempA = kAngle*temp; y[23] = y[22]+arrowScale*Math.sin(tempA); temp=90+30; tempA = kAngle*temp; y[24] = y[22]+arrowScale*Math.sin(tempA); y[25] = 305.0; y[26] = y[25]; temp= -30; tempA = kAngle*temp; y[27] = y[26]+arrowScale*Math.sin(tempA); temp= 30; tempA = kAngle*temp; y[28] = y[26]+arrowScale*Math.sin(tempA); for (index=0; index <=28; index++) { xpos[index] = (int)(x[index]); ypos[index] = (int)(y[index]); } repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { int i, yVal; // Draw background offscreenG.setColor(Color.lightGray); offscreenG.fillRect(0,0,500,149); offscreenG.setColor(Color.white); offscreenG.fillRect(0,150,500,250); // Draw vectors offscreenG.setColor(Color.black); offscreenG.drawLine(10,341,290,341); offscreenG.drawLine(xpos[0],ypos[0],xpos[1],ypos[1]); offscreenG.drawLine(xpos[1],ypos[1],xpos[2],ypos[2]); offscreenG.drawLine(xpos[1],ypos[1],xpos[3],ypos[3]); offscreenG.drawLine(xpos[0],ypos[0],xpos[4],ypos[4]); offscreenG.drawLine(xpos[4],ypos[4],xpos[5],ypos[5]); offscreenG.drawLine(xpos[4],ypos[4],xpos[6],ypos[6]); offscreenG.drawString("x", 84, 220); offscreenG.drawString("y", 30, 164); offscreenG.drawString("Acceleration = " + accel + " m/s^2", 320, 300); offscreenG.setColor(Color.darkGray); offscreenG.drawRect(100,270,100,70); offscreenG.drawLine(xpos[17],ypos[17],xpos[18],ypos[18]); offscreenG.drawLine(xpos[18],ypos[18],xpos[19],ypos[19]); offscreenG.drawLine(xpos[18],ypos[18],xpos[20],ypos[20]); offscreenG.drawString("Weight = 100 N ", 320, 180); offscreenG.drawString("mg", xpos[18]-18, ypos[18]-11); offscreenG.setColor(Color.red); offscreenG.drawLine(xpos[7],ypos[7],xpos[8],ypos[8]); offscreenG.drawLine(xpos[8],ypos[8],xpos[9],ypos[9]); offscreenG.drawLine(xpos[8],ypos[8],xpos[10],ypos[10]); if (fX > 0.0) { offscreenG.drawLine(xpos[7],ypos[7],xpos[11],ypos[11]); offscreenG.drawLine(xpos[11],ypos[11],xpos[12],ypos[12]); offscreenG.drawLine(xpos[11],ypos[11],xpos[13],ypos[13]); if (fY > 0.0 ) { offscreenG.drawString("Fx", xpos[11], ypos[11]+10); } else { offscreenG.drawString("Fx", xpos[11], ypos[11]-2); } } if (Math.abs(fY)>0.0) { offscreenG.drawLine(xpos[7],ypos[7],xpos[14],ypos[14]); offscreenG.drawLine(xpos[14],ypos[14],xpos[15],ypos[15]); offscreenG.drawLine(xpos[14],ypos[14],xpos[16],ypos[16]); offscreenG.drawString("Fy", xpos[14]-14, ypos[14]+2); } if (fY > 0.0) { offscreenG.drawString("F", xpos[8]+1, ypos[8]-4); } else { offscreenG.drawString("F", xpos[8]+1, ypos[8]+12); } offscreenG.drawString("Applied force", 320, 200); offscreenG.drawString("x-component = " + fX + " N", 330, 220); offscreenG.drawString("y-component = " + fY + " N", 330, 240); offscreenG.setColor(Color.blue); if (fNormal > 0.0) { offscreenG.drawLine(xpos[21],ypos[21],xpos[22],ypos[22]); offscreenG.drawLine(xpos[22],ypos[22],xpos[23],ypos[23]); offscreenG.drawLine(xpos[22],ypos[22],xpos[24],ypos[24]); offscreenG.drawString("FN", xpos[22]-6, ypos[22]-3); } offscreenG.drawString("Normal force = " + fNormal + " N", 320, 260); if (fFriction > 0.0) { offscreenG.drawLine(xpos[25],ypos[25],xpos[26],ypos[26]); offscreenG.drawLine(xpos[26],ypos[26],xpos[27],ypos[27]); offscreenG.drawLine(xpos[26],ypos[26],xpos[28],ypos[28]); offscreenG.drawString("Ff", xpos[26]-6, ypos[26]-5); } offscreenG.drawString("Frictional force = " + fFriction + " N", 320, 280); g.drawImage(offscreenImg,0,0,this); } public void destroy() { offscreenG.dispose(); } public boolean action (Event evt, Object arg) { if (evt.target instanceof Button) { } else return super.action(evt, arg); return true; } public boolean handleEvent(Event evt) { if (evt.target instanceof Scrollbar) { if ((Scrollbar)evt.target == forcescroll) { force = ((Scrollbar)evt.target).getValue(); forceS.setText(String.valueOf(force)); } else if ((Scrollbar)evt.target == forceAnglescroll) { forceAngle = ((Scrollbar)evt.target).getValue(); forceAngleS.setText(String.valueOf(forceAngle)); } repaint(); } else return super.handleEvent(evt); return true; } }