import java.applet.*; //import java.awt.toolkit.*; import java.awt.*; import java.awt.image.*; public class SwtchCnvs extends Canvas { boolean swState, on = true, off = false; Graphics SwCG; static final int lx = 00, cx = 15, rx = 29, wx=30; static final int ty = 00, cy = 15, by = 29, wy=30; // Switch handle constants static final int slx = 9, srx0 = 15, srx1 = 22; // x coords. static final int swt = 1, swc = 15, swb = 29; // y coords. // inner boarder mask constants static final int imx0 = 8, imx1 = 17; // inner mask x coords static final int imyt = 13, imyb = 17; // inner mask y coords // fixed inner boarder constants static final int ibx0 = 8, ibx1 = 6, ibx2 = 6, ibx3 = 8; // left ib xÕs static final int iby0 = 17, iby1 = 17, iby2 = 13, iby3 = 13; // left ib yÕs static final int ibx4 = 17, ibx5 = 18, ibx6 = 18, ibx7 = 17; // right ib xÕs static final int iby4 = 13, iby5 = 13, iby6 = 17, iby7 = 17; // right ib yÕs // outer boarder mask constants static final int omx0 = 9, omx1 = 17, omyt = 10, omyb = 20; // fixed outer boarder constants static final int obx0 = 9, obx1 = 3, obx2 = 3, obx3 = 9; static final int oby0 = 20, oby1 = 20, oby2 = 10, oby3 = 10; static final int obx4 = 17, obx5 = 22, obx6 = 22, obx7 = 17; static final int oby4 = 10, oby5 = 10, oby6 = 20, oby7 = 20; // SwitchÕs title String title = ""; public SwtchCnvs( ) { super( ); // invoke superclasses constructor for a Canvas swState = false; } public SwtchCnvs( String tit ) { super( ); // invoke superclasses constructor for a Canvas title = tit; swState = false; } public SwtchCnvs( boolean state ) { super( ); swState = state; } public SwtchCnvs(String tit, boolean state ) { super( ); title = tit; swState = state; } public Dimension preferredSize( ) // Set size of SwtchCnvs { return new Dimension( wx, wy+15); } public Dimension minimumSize( ) { return new Dimension( wx, wy+15); } public boolean getState( ) { return swState; } public void setState( boolean swv) { swState = swv; repaint( ); } public boolean mouseDown( Event evt, int x, int y) { swState = !swState; update( this.getGraphics( ) ); return true; } public void paint( Graphics swcg) { // Erase changable switch parts swcg.setColor( getBackground ( ) ); swcg.drawLine(slx, swc, slx, swt); // switch handle swcg.drawLine(slx, swt, srx1, swt); swcg.drawLine(srx1, swt, srx0, swc); swcg.drawLine(slx, swc, slx, swb); swcg.drawLine(slx, swb, srx1, swb); swcg.drawLine(srx1, swb, srx0, swc); swcg.drawLine(imx0, imyt, imx1, imyt ); // i. b. masks top swcg.drawLine(imx0, imyb, imx1, imyb ); // i. b. masks bot swcg.drawLine(omx0,omyt, omx1, omyt ); // o. b. masks top swcg.drawLine(omx0,omyb, omx1, omyb ); // o. b. masks bot // Draw fixed switch parts swcg.setColor( getForeground ( ) ); // ctr line swcg.drawLine(slx-3, swc, srx0+3, swc); // fixed parts of inner boarder swcg.drawLine(ibx0, iby0, ibx1, iby1); // left swcg.drawLine(ibx1, iby1, ibx2, iby2); swcg.drawLine(ibx2, iby2, ibx3, iby3); swcg.drawLine(ibx4, iby4, ibx5, iby5); // right swcg.drawLine(ibx5, iby5, ibx6, iby6); swcg.drawLine(ibx6, iby6, ibx7, iby7); // fixed parts of outer boarder swcg.drawLine(obx0, oby0, obx1, oby1); // left swcg.drawLine(obx1, oby1, obx2, oby2); swcg.drawLine(obx2, oby2, obx3, oby3); swcg.drawLine(obx4, oby4, obx5, oby5); // right swcg.drawLine(obx5, oby5, obx6, oby6); swcg.drawLine(obx6, oby6, obx7, oby7); // add switchÕs title swcg.drawString( title, lx+5, by+15 ); if( swState ) { // turn switch on swcg.drawLine(slx, swc, slx, swt); swcg.drawLine(slx, swt, srx1, swt); swcg.drawLine(srx1, swt, srx0, swc); swcg.drawLine(srx0, swc, slx, swc); // draw bottom inner mask swcg.drawLine(imx0, imyb, imx1, imyb ); // draw bottom outer mask swcg.drawLine(omx0, omyb, omx1, omyb ); } else { // turn switch off swcg.drawLine(slx, swc, slx, swb); swcg.drawLine(slx, swb, srx1, swb); swcg.drawLine(srx1, swb, srx0, swc); swcg.drawLine(srx0, swc, slx, swc); // draw top inner mask swcg.drawLine(imx0, imyt, imx1, imyt ); // draw top outer mask swcg.drawLine(omx0, omyt, omx1, omyt ); } } }