import java.awt.*; import java.awt.event.*; import java.lang.*; import java.lang.Math; import java.text.*; public class Equilibrium2 extends java.applet.Applet implements Runnable { Canvas oscillators; Label beamWeightLabel, blackWeightLabel, blackPosLabel, orangePosLabel, bluePosLabel, bluePosS, beamWeightS, blackWeightS, blackPosS, orangePosS; Thread runner; Scrollbar beamWscroll, blackWscroll, blackXscroll, orangeXscroll, blueXscroll; int beamWeight, blackWeight, blackPos, orangePos, bluePos; double beamW, blackW, blackX, orangeX, blueX, blueForce, redForce; int[] redExes = new int[3]; int[] redWhys = new int[3]; int[] blueExes = new int[3]; int[] blueWhys = new int[3]; Image offscreenImg; Graphics offscreenG; boolean startFlag = false; 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); // beam Weight text buildConstraints(constraints, 0, 0, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label beamWeightLabel = new Label("Weight of the beam (N):", Label.CENTER); gridbag.setConstraints (beamWeightLabel, constraints); add(beamWeightLabel); // beam Weight scrollbar label buildConstraints(constraints, 2, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; beamWeightS = new Label("20", Label.LEFT); beamWeight = 20; gridbag.setConstraints (beamWeightS, constraints); add(beamWeightS); // beam Weight scrollbar buildConstraints(constraints, 1, 0, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; beamWscroll = new Scrollbar(Scrollbar.HORIZONTAL, 20, 1, 0, 50); gridbag.setConstraints (beamWscroll, constraints); add(beamWscroll); // black Weight text buildConstraints(constraints, 0, 1, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label blackWeightLabel = new Label("Weight of the black box (N):", Label.CENTER); gridbag.setConstraints (blackWeightLabel, constraints); add(blackWeightLabel); // black Weight scrollbar label buildConstraints(constraints, 2, 1, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; blackWeightS = new Label("20", Label.LEFT); blackWeight = 20; gridbag.setConstraints (blackWeightS, constraints); add(blackWeightS); // black Weight scrollbar buildConstraints(constraints, 1, 1, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; blackWscroll = new Scrollbar(Scrollbar.HORIZONTAL, 20, 1, 0, 50); gridbag.setConstraints (blackWscroll, constraints); add(blackWscroll); // black position text buildConstraints(constraints, 0, 2, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label blackPosLabel = new Label("Position of the black box (cm):", Label.CENTER); gridbag.setConstraints (blackPosLabel, constraints); add(blackPosLabel); // black position scrollbar label buildConstraints(constraints, 2, 2, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; blackPosS = new Label("80", Label.LEFT); blackPos = 80; gridbag.setConstraints (blackPosS, constraints); add(blackPosS); // black position scrollbar buildConstraints(constraints, 1, 2, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; blackXscroll = new Scrollbar(Scrollbar.HORIZONTAL, 80, 2, 0, 100); gridbag.setConstraints (blackXscroll, constraints); add(blackXscroll); // orange position text buildConstraints(constraints, 0, 3, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label orangePosLabel = new Label("Position of orange box (cm):", Label.CENTER); gridbag.setConstraints (orangePosLabel, constraints); add(orangePosLabel); // orange position scrollbar label buildConstraints(constraints, 2, 3, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; orangePosS = new Label("50", Label.LEFT); orangePos = 50; gridbag.setConstraints (orangePosS, constraints); add(orangePosS); // orange position scrollbar buildConstraints(constraints, 1, 3, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; orangeXscroll = new Scrollbar(Scrollbar.HORIZONTAL, 50, 2, 0, 100); gridbag.setConstraints (orangeXscroll, constraints); add(orangeXscroll); // blue position text buildConstraints(constraints, 0, 4, 1, 1, 20, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.EAST; Label bluePosLabel = new Label("Position of blue support (cm):", Label.CENTER); gridbag.setConstraints (bluePosLabel, constraints); add(bluePosLabel); // blue position scrollbar label buildConstraints(constraints, 2, 4, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; bluePosS = new Label("80", Label.LEFT); bluePos = 80; gridbag.setConstraints (bluePosS, constraints); add(bluePosS); // blue position scrollbar buildConstraints(constraints, 1, 4, 1, 1, 20, 0); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.WEST; blueXscroll = new Scrollbar(Scrollbar.HORIZONTAL, 80, 1, 50, 100); gridbag.setConstraints (blueXscroll, constraints); add(blueXscroll); // canvas buildConstraints(constraints, 0, 5, 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; while (true) { beamW = beamWeight; blackW = blackWeight; blackX = blackPos; orangeX = orangePos; blueX = bluePos; blueForce = (beamW*30+blackW*(blackX-20)+50*(orangeX-20))/(blueX-20); redForce = (beamW*(blueX-50)+blackW*(blueX-blackX)+50*(blueX-orangeX))/(blueX-20); redExes[0]=90; redExes[1]=82; redExes[2]=98; redWhys[0]=297; redWhys[1]=321; redWhys[2]=321; for (index=0; index <=2; index++) { blueExes[index] = redExes[index]+2*(bluePos-20); blueWhys[index] = redWhys[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,199); offscreenG.setColor(Color.white); offscreenG.fillRect(0,200,500,200); // Draw vectors NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(4); offscreenG.setColor(Color.red); offscreenG.drawString("Red support force = " + nf.format(redForce) + " N", 300, 320); offscreenG.drawString("Position of red support = 20 cm" , 300, 340); offscreenG.fillPolygon(redExes,redWhys,3); offscreenG.drawString("WARNING : A negative support force implies" , 50, 360); offscreenG.drawString("that the system is unstable and will tip over." , 50, 375); offscreenG.setColor(Color.blue); offscreenG.drawString("Blue support force = " + nf.format(blueForce) + " N", 300, 360); offscreenG.drawString("Position of blue support = " + bluePos + " cm", 300, 380); offscreenG.fillPolygon(blueExes,blueWhys,3); offscreenG.setColor(Color.gray); offscreenG.fillRect(50,280,200,20); offscreenG.drawString("Beam weight = " + beamWeight + " N", 300, 220); offscreenG.drawString("The beam is 100 cm long", 50, 240); offscreenG.setColor(Color.orange); offscreenG.fillRect(50+2*orangePos-10,260,20,20); offscreenG.drawString("Orange box weighs 50 N", 300, 240); offscreenG.drawString("Position of orange box = " + orangePos + " cm", 300, 260); offscreenG.setColor(Color.black); offscreenG.drawString("Black box weighs " + blackWeight + " N", 300, 280); offscreenG.drawString("Position of black box = " + blackPos + " cm", 300, 300); offscreenG.fillRect(50+2*blackPos-8,264,16,16); 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 == beamWscroll) { beamWeight = ((Scrollbar)evt.target).getValue(); beamWeightS.setText(String.valueOf(beamWeight)); } else if ((Scrollbar)evt.target == blackWscroll) { blackWeight = ((Scrollbar)evt.target).getValue(); blackWeightS.setText(String.valueOf(blackWeight)); } else if ((Scrollbar)evt.target == blackXscroll) { blackPos = ((Scrollbar)evt.target).getValue(); blackPosS.setText(String.valueOf(blackPos)); } else if ((Scrollbar)evt.target == orangeXscroll) { orangePos = ((Scrollbar)evt.target).getValue(); orangePosS.setText(String.valueOf(orangePos)); } else if ((Scrollbar)evt.target == blueXscroll) { bluePos = ((Scrollbar)evt.target).getValue(); bluePosS.setText(String.valueOf(bluePos)); } repaint(); } else return super.handleEvent(evt); return true; } }