/* Archimedes experiment */ import java.awt.*; import java.applet.*; public class archi extends Applet { public MediaTracker tracker, trackerBg; public Image offScreenImage; public Image lWater, lAlcohol, theBack; public boolean copyRight = false, glasup = false, dragSlider = false; public boolean water = true; public int thex = 290, they = 115, xdist = 0; public double force = 2.0; public int substance = 1; // (1: titanium, or 2: tungsten) public void init() { super.init(); offScreenImage = createImage(500,360); //{{INIT_CONTROLS setLayout(null); resize(500,350); //}} trackerBg = new MediaTracker(this); theBack = this.getImage(getCodeBase(),"bg.jpg"); trackerBg.addImage(theBack,0); try { trackerBg.waitForID(0); } catch (InterruptedException e) { } lWater = this.getImage(getCodeBase(),"glaspurple.jpg"); trackerBg.addImage(lWater,0); try { trackerBg.waitForID(0); } catch (InterruptedException e) { } lAlcohol = this.getImage(getCodeBase(),"glasblue2.jpg"); trackerBg.addImage(lAlcohol,0); try { trackerBg.waitForID(0); } catch (InterruptedException e) { } repaint(); } public void update(Graphics g){ Graphics offg = offScreenImage.getGraphics(); paint(offg); g.drawImage(offScreenImage, 0, 0, this); } public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,500,350); g.drawImage(theBack,0,0,theBack.getWidth(this),theBack.getHeight(this),this); if (water) { if (glasup) { g.drawImage(lWater,34,140,lWater.getWidth(this),lWater.getHeight(this),this); } else { g.drawImage(lWater,34,235,lWater.getWidth(this),lWater.getHeight(this),this); } } else { if (glasup) { g.drawImage(lAlcohol,34,140,lAlcohol.getWidth(this),lAlcohol.getHeight(this),this); } else { g.drawImage(lAlcohol,34,235,lAlcohol.getWidth(this),lAlcohol.getHeight(this),this); } } // Physics section: double Volume = 75.8; // cm^3 = assumed volume of object double rho_liquid = 1.0; // g/cm^3 if water if (!water) rho_liquid = 0.7; // if alcohol double rho_metal = 4.54; // g/cm^3 if titanium if (substance == 2) rho_metal = 19.3; // if tungsten double weight = 0.001 * rho_metal * Volume * 9.81; // in N if (glasup) weight = weight - 0.001 * rho_liquid * Volume * 9.81; g.drawString("Weight = "+weight,200,30); g.setColor(Color.blue); double needleL = 23; double needleT = (weight - force) * Math.PI / 3.; if (needleT < -0.8*Math.PI) needleT = -0.8*Math.PI; if (needleT > 0.8*Math.PI) needleT = 0.8*Math.PI; int endx = 65 + (int)(needleL * Math.sin(needleT)); int endy = 63 - (int)(needleL * Math.cos(needleT)); g.drawLine(65,63,endx,endy); g.drawLine(64,63,endx-1,endy); g.drawLine(66,63,endx+1,endy); // sliding weight g.setColor(Color.black); g.fillRect(thex-15,they,31,21); g.setColor(Color.lightGray); g.fillRect(thex-2,they+1,5,19); g.setColor(Color.green); g.drawLine(thex,they+2,thex,they+17); // rope and object if (!glasup) { g.setColor(Color.black); g.fillOval(45,215,40,18); g.fillRect(45,209,40,15); g.setColor(Color.darkGray); g.fillOval(45,200,40,18); } else { g.setColor(Color.darkGray); g.fillOval(45,215,40,18); g.fillRect(45,209,40,15); g.setColor(Color.gray); g.fillOval(45,200,40,18); } g.setFont(new Font("Helvetica", Font.BOLD, 14)); g.setColor(Color.red); if (glasup) g.setColor(Color.pink); g.drawString(""+substance,62,230); g.setColor(Color.gray); g.fillRect(64,136,3,73); if (glasup) { g.setColor(Color.lightGray); g.fillRect(64,186,3,23); } // labels: active ones are red, passive are gray g.setFont(new Font("Helvetica", Font.BOLD, 14)); if (glasup) { g.setColor(Color.red); g.drawString("Up",453,306); g.setColor(Color.black); g.drawString("Down",440,340); } else { g.setColor(Color.black); g.drawString("Up",453,306); g.setColor(Color.red); g.drawString("Down",440,340); } if (water) { g.setColor(Color.red); g.drawString("Water",356,306); g.setColor(Color.black); g.drawString("Alcohol",356,340); } else { g.setColor(Color.black); g.drawString("Water",366,306); g.setColor(Color.red); g.drawString("Alcohol",352,340); } // select the substance labels g.setColor(Color.gray); g.fillOval(347,239,12,12); g.fillOval(347,259,12,12); g.setColor(Color.black); g.drawOval(347,239,12,12); g.drawOval(347,259,12,12); if (substance == 1) { g.setColor(Color.red); g.fillOval(349,241,9,9); g.drawString("Substance 1",375,250); g.setColor(Color.black); g.drawString("Substance 2",375,270); } else { g.setColor(Color.red); g.fillOval(349,261,9,9); g.drawString("Substance 2",375,270); g.setColor(Color.black); g.drawString("Substance 1",375,250); } g.setColor(Color.yellow); g.setFont(new Font("Helvetica", Font.BOLD, 18)); g.drawString("F = "+outString(""+force,5)+" N",380,30); // copyright notice: if (copyRight) { g.setColor(Color.green); g.fillRect(100,100,240,70); g.setColor(Color.white); g.setFont(new Font("Helvetica", Font.PLAIN, 18)); g.drawString("This applet was written by:",110,120); g.drawString("Wolfgang Bauer",110,140); g.drawString("Copyright: WB 1999",110,160); } } public boolean handleEvent(Event event) { if (event.id == Event.KEY_PRESS){ String thePressedKey = ""+(char)event.key; if (thePressedKey.equalsIgnoreCase("w")) { copyRight = !copyRight; repaint(); } } if (event.key == Event.DOWN || event.key == Event.LEFT){ if (event.shiftDown()){ if (force > 0.0) force = force - 0.02; thex = (int)(10*force+270.5); } else { if (thex > 270) thex--; force = 0.1 * (thex - 270); } repaint(); } if (event.key == Event.UP || event.key == Event.RIGHT){ if (event.shiftDown()){ force = force + 0.02; thex = (int)(10*force+270.5); } else { if (thex < 440) thex++; force = 0.1 * (thex - 270); } repaint(); } if (event.target == this && event.id == Event.MOUSE_UP) { a_MouseUp(event); return true; } if (event.target == this && event.id == Event.MOUSE_DOWN) { a_MouseDown(event); return true; } if (event.target == this && event.id == Event.MOUSE_DRAG) { a_MouseDrag(event); return true; } return super.handleEvent(event); } public String outString(String inString, int l){ // returns the first l chars of string int ls = (int)Math.min(inString.length(),l); return inString.substring(0,ls); } void a_MouseDown(Event event) { int xx = event.x; int yy = event.y; xdist = xx-thex; if (Math.abs(xdist) < 10 && yy > they && yy < they + 22) { dragSlider = true; } repaint(); } void a_MouseDrag(Event event) { int xx = event.x; int yy = event.y; if (dragSlider) thex = Math.min(440,Math.max(270,xx - xdist)); force = 0.1 * (thex - 270); repaint(); } void a_MouseUp(Event event) { int xx = event.x; int yy = event.y; dragSlider = false; if (xx > 345 && xx < 360) { if (yy > 237 && yy < 252) { substance = 1; } else if (yy > 257 && yy < 272) { substance = 2; } } if (xx > 423) { if (yy > 285 && yy < 316) glasup = true; if (yy > 319) glasup = false; } else if (xx > 347) { if (yy > 285 && yy < 316) water = true; if (yy > 319) water = false; } repaint(); } //{{DECLARE_CONTROLS //}} }