import java.awt.*; //============================================================================== // WgtTri Class // // AGG - Alexander Gee // // 041497 - code converted to Java //============================================================================== public class WgtTri extends Canvas { private Dimension offsize; // off screen buffer size private Graphics offgraphics; // off screen buffer private Image offimage; // off screen buffer image private boolean redrawall; // redraw state private int width; // window width private int height; // window height private int triptx[]; private int tripty[]; private int inset; private int tx; private int ty; private int size; private int diam; public int Wmin; public int Wmid; public int Wmax; // WgtTri Class Constructor 1: //-------------------------------------------------------------------------- public WgtTri() { width = 180; height = 180; resize( width, height ); inset = 40; triptx = new int[4]; tripty = new int[4]; redrawall = true; setCanvasSize(); } // WgtTri Class Constructor 2: //-------------------------------------------------------------------------- public WgtTri( int width, int height ) { this.width = width; this.height = height; resize( this.width, this.height ); inset = 40; triptx = new int[4]; tripty = new int[4]; redrawall = true; setCanvasSize(); } public void update( Graphics g ) { paint( g ); } public void paint( Graphics g ) { if( offgraphics == null ) { offsize = size(); offimage = createImage( offsize.width, offsize.height ); offgraphics = offimage.getGraphics(); } if( redrawall ) { offgraphics.setColor( getBackground() ); offgraphics.fillRect( 0, 0, width, height ); offgraphics.setColor( Color.white ); offgraphics.drawRect( 5, 5, width-10, height-10 ); offgraphics.setColor( Color.black ); offgraphics.drawLine( triptx[0], tripty[0], triptx[1], tripty[1] ); offgraphics.drawLine( triptx[1], tripty[1], triptx[2], tripty[2] ); offgraphics.drawLine( triptx[2], tripty[2], triptx[3], tripty[3] ); offgraphics.drawString( "Wmax", tx-40, ty+size/2+3 ); offgraphics.drawString( "Wmin", tx+size/2-12, ty+size+20 ); offgraphics.drawString( "1", tx-13, ty+3 ); offgraphics.drawString( "0", tx-13, ty+size+15 ); offgraphics.drawString( "1", tx+size-1, ty+size+15 ); } g.drawImage( offimage, 0, 0, this ); int x = tx + Wmin*size/1000; int y = ty + size - Wmax*size/1000; int d = diam/2; g.drawArc( x-d, y-d, diam, diam, 0, 360 ); g.drawLine( x, y-d, x, y+d ); g.drawLine( x-d, y, x+d, y ); } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown( Event evt, int x, int y ) { int px = x - tx; int py = ty + size - y; Wmax = (py*1000/size + 5)/10*10; Wmin = (px*1000/size + 5)/10*10; adjustWeights(); redrawall = false; repaint(); return false; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp( Event evt, int x, int y ) { int px = x - tx; int py = ty + size - y; Wmax = (py*1000/size + 5)/10*10; Wmin = (px*1000/size + 5)/10*10; adjustWeights(); redrawall = false; repaint(); return false; } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held. //-------------------------------------------------------------------------- public boolean mouseDrag( Event evt, int x, int y ) { int px = x - tx; int py = ty + size - y; Wmax = (py*1000/size + 5)/10*10; Wmin = (px*1000/size + 5)/10*10; adjustWeights(); redrawall = false; repaint(); return false; } private void setCanvasSize() { tx = inset+10; ty = inset; if( width < height ) { size = width - 2 * inset; } else { size = height - 2 * inset; } diam = size/10 - 1; triptx[0] = tx; tripty[0] = ty; triptx[1] = triptx[0]; tripty[1] = tripty[0] + size; triptx[2] = triptx[1] + size; tripty[2] = tripty[1]; triptx[3] = triptx[2] - size; tripty[3] = tripty[2] - size; } private void adjustWeights() { if( Wmax < 1 ) { Wmax = 1; } if( Wmin < 0 ) { Wmin = 0; } if( Wmax > 1000 ) { Wmax = 1000; } if( Wmin > 1000 ) { Wmin = 1000; } if( Wmax + Wmin > 1000 ) { Wmin = 1000 - Wmax; } if( Wmax < 1000 && Wmin == 0 ) { Wmin = 1; } Wmid = 1000 - Wmax - Wmin; repaint(); } // public access methods //------------------------------------------------------------------------- public int getWminValue() { return Wmin; } public int getWmidValue() { return Wmid; } public int getWmaxValue() { return Wmax; } public void setWminValue( int wmin ) { Wmin = wmin; adjustWeights(); repaint(); } public void setWmidValue( int wmid ) { Wmid = wmid; adjustWeights(); repaint(); } public void setWmaxValue( int wmax ) { Wmax = wmax; adjustWeights(); repaint(); } public void setValues( int wmin, int wmid, int wmax ) { Wmin = wmin; Wmid = wmid; Wmax = wmax; adjustWeights(); repaint(); } }