import java.applet.*; import java.awt.*; //import java.awt.event.*; import java.util.*; import java.math.*; import java.text.*; import java.awt.event.*; public class RollerPanel extends Panel implements MouseListener { private int stage = 1; private double s = 0.75; //scale for arrowhead private Font f1 = new Font("Helvetica", Font.BOLD, 18); private Font f2 = new Font("Arial", Font.PLAIN, 14); private Font f3 = new Font("Helvetica", Font.PLAIN, 11); private String Header = "RollerFrame Exercise"; private String IntroText1 = "Find the force required to just start the lawn roller rolling over the step of height h."; private String IntroText2 = "Express your results in non-dimensional terms, using R and W, the radius and weight"; private String IntroText3 = "of the roller, as reference quanatities. Sketch how the force varies with h/R."; private String xVal[] = {" 0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", " 1"}; private String yVal[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; DecimalFormat df = new DecimalFormat("###0.000"); Image hr, fw; // stage explanation - // stage 0 = introduction -> not anymore // stage 1 = click on solution // stage 2 = clicked on dot successfully // stage 3 = clicked another step height public RollerPanel() { addMouseListener(this); this.setBackground(Color.white); setSize(600,500); //50 pixels for bottom panel setVisible(true); } public void paint(Graphics g) { Dimension d = this.size(); g.setColor(Color.black); // g.drawRect(0,0,d.width-1,d.height-1); g.setFont(f2); switch (stage) { case 0: { //intro and header g.setFont(f1); g.drawString(Header, d.width/2-60, d.height/6-30); g.setFont(f2); g.drawString(IntroText1, 30,100); g.drawString(IntroText2, 30,120); g.drawString(IntroText3, 30,140); //roller and step g.drawOval(200,200,100,100); g.fillOval(245,245,10,10); g.fillOval(345,245,10,10); g.drawRect(244,244,112,12); g.drawLine(355,250,455,250); g.fillRect(150,300,140,30); g.fillRect(290,280,160,50); //solution box g.drawRect(500,400,70,20); g.setFont(f2); g.drawString("Solution", 510, 415); } break; case 1: { g.setColor(Color.black); g.drawOval(50,50,100,100); g.drawLine(50,151,400,151); g.setColor(Color.red); g.fillOval(142,130,5,5); g.setColor(Color.black); g.drawString("Click to make the step height... try the dot",100,300); } break; case 2: { //code for main part g.setFont(f2); g.setColor(Color.white); g.drawString("Click to make the step height... try the dot", 100,300); //erase instructions g.drawImage(hr,325, 267,this); g.drawImage(fw,325, 292,this); g.setColor(Color.black); g.setFont(f2); g.drawString("Click to try a new step height", 210,50); //draw vectors g.setColor(Color.black); g.drawLine(100,100,200,100); VHead2 head = new VHead2(s,100,100,200,100); //use VHead2 class to get arrow head g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(100,100,100,200); head = new VHead2(s,100,100,100,200); //use VHead2 class to get arrow head g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(200,200,100,100); head = new VHead2(s,200,200,100,100); //use VHead2 class to get arrow head g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(100,100,65,135); //for R head = new VHead2(s,100,100,65,135); g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead //label roller g.drawString("R", 60, 125); g.drawString("W", 95,215); g.drawString("F", 190, 95); g.drawString("N", 200, 215); //draw roller g.setColor(Color.black); g.drawOval(50,50,100,100); g.drawLine(50,151,400,151); //draw graph g.drawLine(100,230,100,430); g.drawLine(100,430,300,430); g.setColor(Color.orange); //draw x grid for (int i=120; i<=300; i=i+20) g.drawLine(i,230,i,435); //draw y grid for (int i=230; i<=410; i=i+20) g.drawLine(95,i,300,i); //label the graph g.setColor(Color.black); g.setFont(f3); g.drawString("F/W", 50 ,310); g.drawString("h/R", 310,435); g.setFont(f3); int j = 0; for (int i=100; i<=300; i=i+20) { g.drawString(xVal[j], i-5, 445); j++; } j = 10; for (int i=240; i<=440; i=i+20) { g.drawString(yVal[j], 85,i-3); j--; } } } } //MouseListener functions public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); switch (stage) { case 0: { if ( (x>=500) & (x<=570) & (y>400) & (y<420) ) { stage = 1; Graphics g = getGraphics(); g.setColor(Color.red); g.drawRect(500,400,70,20); g.setFont(f2); g.drawString("Solution", 510, 415); //erase screen g.setColor(Color.white); g.fillRect(0,0,1000,1000); g.setColor(Color.black); g.drawOval(50,50,100,100); g.drawLine(50,151,400,151); g.setColor(Color.red); g.fillOval(142,130,5,5); g.setColor(Color.black); g.drawString("Click to make the step height... try the dot",100,300); //included next four lines to prevent screen from repainting g.drawImage(hr,325, 267,this); g.drawImage(fw,325, 292,this); } } break; case 1: { Graphics g = getGraphics(); g.setColor(Color.black); g.drawOval(50,50,100,100); g.drawLine(50,151,400,151); g.setColor(Color.red); g.fillOval(142,130,5,5); g.setColor(Color.black); g.setFont(f2); g.drawString("Click to make the step height... try the dot", 100,300); if ((Math.sqrt( (x-144)*(x-144)+(y-132)*(y-132))<6 )) { //see if dot clicked with tolerance of 2 pixels g.setColor(Color.white); g.drawString("Click to make the step height... try the dot", 100,300); g.fillOval(142,130,5,5); g.setColor(Color.black); g.setFont(f2); g.drawString("Click to try a new step height", 210,50); //draw vectors g.setColor(Color.black); g.drawLine(100,100,200,100); VHead2 head = new VHead2(s,100,100,200,100); //use VHead2 class to get arrow head coordinates g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(100,100,100,200); head = new VHead2(s,100,100,100,200); //use VHead2 class to get arrow head coordinates g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(200,200,100,100); head = new VHead2(s,200,200,100,100); //use VHead2 class to get arrow head coordinates g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(100,100,65,135); //for R head = new VHead2(s,100,100,65,135); g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead //label roller g.drawString("R", 60, 125); g.drawString("W", 95,215); g.drawString("F", 190, 95); g.drawString("N", 200, 215); PlotRoller PlotPt = new PlotRoller(x,y); g.drawLine(PlotPt.xcircle(), y, 300, y); g.drawLine(PlotPt.xcircle(), y, PlotPt.xcircle(), 150); g.fillOval(PlotPt.xpoint(), PlotPt.ypoint(), 4,4); g.setFont(f3); g.drawString("" +df.format(PlotPt.xval())+ " ", 445, 280); g.drawString("" +df.format(PlotPt.yval())+ " ", 430, 305); g.drawImage(hr,325, 267,this); g.drawImage(fw,325, 292,this); //draw graph g.drawLine(100,230,100,430); g.drawLine(100,430,300,430); g.setColor(Color.orange); //draw x grid for (int i=120; i<=300; i=i+20) g.drawLine(i,230,i,435); //draw y grid for (int i=230; i<=410; i=i+20) g.drawLine(95,i,300,i); //label the graph g.setColor(Color.black); g.setFont(f3); g.drawString("F/W", 50 ,310); g.drawString("h/R", 310,435); g.setFont(f3); int j = 0; for (int i=100; i<=300; i=i+20) { g.drawString(xVal[j], i-5, 445); j++; } j = 10; for (int i=240; i<=440; i=i+20) { g.drawString(yVal[j], 85,i-3); j--; } stage = 2; } } break; case 2: { //code for main part Graphics g = getGraphics(); g.setFont(f2); g.setColor(Color.white); g.drawString("Click to make the step height... try the dot", 100,300); //erase instructio //draw roller g.setColor(Color.black); g.drawOval(50,50,100,100); g.drawLine(50,151,400,151); //draw graph g.drawLine(100,230,100,430); g.drawLine(100,430,300,430); g.setColor(Color.orange); //draw x grid for (int i=120; i<=300; i=i+20) g.drawLine(i,230,i,435); //draw y grid for (int i=230; i<=410; i=i+20) g.drawLine(95,i,300,i); //label the graph g.setColor(Color.black); g.setFont(f3); g.drawString("F/W", 50 ,310); g.drawString("h/R", 310,435); g.setFont(f3); int j = 0; for (int i=100; i<=300; i=i+20) { g.drawString(xVal[j], i-5, 445); j++; } j = 10; for (int i=240; i<=440; i=i+20) { g.drawString(yVal[j], 85,i-3); j--; } //plots on the graph and draws when user clicks PlotRoller PlotPt = new PlotRoller(x,y); if (PlotPt.isInside()==true) { g.setColor(Color.white); g.drawString("You have clicked in an impossible position",320, 250); //erase impossible note g.fillRect(310, 230, 250, 150); //erase old numbers g.setColor(Color.black); // g.drawLine(x,y,100,100); // VHead2 head = new VHead2(s,x,y,100,100); //use VHead2 class to get arrowhead // g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawLine(100,100,PlotPt.NXVal(),200); //draws line for "N" g.drawLine(PlotPt.xcircle(), y, 300, y); g.drawLine(PlotPt.xcircle(), y, PlotPt.xcircle(), 150); g.fillOval(PlotPt.xpoint(), PlotPt.ypoint(), 4,4); g.drawString("" +df.format(PlotPt.xval())+ " ", 445, 280); g.drawString("" +df.format(PlotPt.yval())+ " ", 430, 305); g.drawImage(hr,325, 267,this); g.drawImage(fw,325, 292,this); g.setFont(f2); //draw "N" and "F" on graph //g.drawString("F", 190, 95); g.drawString("N", PlotPt.NXVal(), 215); g.drawLine(100,100,(int)(100.0+100.0*PlotPt.yval()),100); VHead2 head = new VHead2(s,100,100,(int)(100.0+100.0*PlotPt.yval()),100); //use VHead2 g.fillPolygon(head.xpoints(), head.ypoints(), head.npoints()); //draw arrowhead g.drawString("F",(int)(100.0+100.0*PlotPt.yval())-10,95); } else { g.setColor(Color.white); g.fillRect(310, 230, 250, 150); //erase old numbers g.setColor(Color.black); g.drawString("You have clicked in an impossible position", 320, 250); g.drawString("0 ", 445, 280); g.drawString("NAN ", 430, 305); g.drawImage(hr,325, 267,this); g.drawImage(fw,325, 292,this); } g.setColor(Color.black); g.setFont(f2); g.drawString("Click to try a new step height", 210,50); } } }//end public void mousePressed public void setImages(Image hr, Image fw) { this.hr = hr; this.fw = fw; } }