import java.awt.*; import java.awt.event.*; import java.lang.*; import java.lang.Math; import java.text.*; public class Rolling2 extends java.applet.Applet implements Runnable { Canvas oscillators; Label radiusLabel, radiusS; Button Start, Reset; Thread runner; Scrollbar radiusscroll; int radius, index, wheelX, wheelY; int[] xpos = new int[21]; int[] ypos = new int[21]; double r, w, theta, t, vRot, vTrans, xDist, vX, vY, wheelR, vPoint; Image offscreenImg; Graphics offscreenG; boolean startFlag; boolean pauseFlag; 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); // Point position text buildConstraints(constraints, 0, 0, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label radiusLabel = new Label("Distance of point from center (cm):", Label.CENTER); gridbag.setConstraints (radiusLabel, constraints); add(radiusLabel); // Point position scrollbar label buildConstraints(constraints, 2, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; radiusS = new Label("40", Label.LEFT); radius = 40; gridbag.setConstraints (radiusS, constraints); add(radiusS); // Point position scrollbar buildConstraints(constraints, 1, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; radiusscroll = new Scrollbar(Scrollbar.HORIZONTAL, 40, 1, 0, 50); gridbag.setConstraints (radiusscroll, constraints); add(radiusscroll); // Start button buildConstraints(constraints, 0, 1 , 1, 1, 0, 10); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Start = new Button(" Start "); gridbag.setConstraints (Start, constraints); add(Start); // Reset button buildConstraints(constraints, 1, 1 , 1, 1, 0, 10); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; Reset = new Button("Reset"); gridbag.setConstraints (Reset, constraints); add(Reset); // canvas buildConstraints(constraints, 0, 2, 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() { double deltat, x, y; deltat=0.125663706144; wheelR = 0.4; wheelY=240-80; wheelX=5; vTrans = 1.0; startFlag = false; pauseFlag = false; while (true) { t = 0.0; theta = 0.0; index = 0; r = radius/100.0; w = vTrans/wheelR; vRot = w*r; vX = vTrans + vRot; vY = 0.0; vPoint = Math.sqrt(vX*vX+vY*vY); xDist = 0.0; wheelX=5+(int)(xDist); x = wheelR*100.0; xpos[0] = 5+(int)(x); y = 100.0*(wheelR+r); ypos[0] = 240-(int)(y); while (pauseFlag) { while (startFlag) { index = index+1; if (index <= 20) { t = t + deltat; theta = w*t; vX = vTrans + vRot*Math.cos(theta); if (Math.abs(vX)<0.0000001) vX = 0.0; vY = -vRot*Math.sin(theta); if (Math.abs(vY)<0.0000001) vY = 0.0; vPoint = Math.sqrt(vX*vX+vY*vY); if (vPoint<0.0000001) vPoint = 0.0; xDist = vTrans*t; wheelX=5+(int)(100.0*xDist); x = wheelR*100.0+100.0*(xDist+r*Math.sin(theta)); xpos[index] = 5+(int)(x); y = 100.0*(wheelR+r*Math.cos(theta)); ypos[index] = 240-(int)(y); } else { index = 20; startFlag = false; } repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } repaint(); try { Thread.sleep(1); } catch (InterruptedException e) { } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { int i; // Draw background offscreenG.setColor(Color.lightGray); offscreenG.fillRect(0,0,600,199); offscreenG.setColor(Color.white); offscreenG.fillRect(0,140,600,260); // Draw mass NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(4); offscreenG.setColor(Color.blue); offscreenG.drawLine(3, 240, 320, 240); offscreenG.setColor(Color.black); offscreenG.fillOval(wheelX,wheelY,80,80); offscreenG.drawString("Wheel radius = 40 cm", 340, 160); offscreenG.drawString("Translational speed = 1.0 m/s to the right", 340, 180); offscreenG.drawString("Angular velocity = " + nf.format(w) + " rad/s", 340, 200); offscreenG.drawString("Distance traveled by wheel = " + nf.format(xDist) + " m", 340, 220); offscreenG.drawString("Time = " + nf.format(t) + " s", 340, 240); offscreenG.drawString("Positive x is to the right ", 40, 320); offscreenG.drawString("Positive y is up ", 40, 340); offscreenG.setColor(Color.white); offscreenG.drawOval(wheelX+30,wheelY+30,20,20); offscreenG.drawLine(wheelX+40,wheelY+40, wheelX+40+(int)(40*Math.sin(theta)),wheelY+40-(int)(40*Math.cos(theta))); offscreenG.setColor(Color.red); for (i=0; i