/* * ========================================================================= * This program create a frame with a panel and canvas, and draws a simple * sinusoidal graph on the canvas. * * Written By : Mark Austin November 1997 * ========================================================================= */ import java.awt.*; import java.awt.event.*; public class TestGraph extends Frame { // The main method creates an instance of the class. public static void main( String args[] ) { TestGraph t = new TestGraph(); } // Define window frame. public TestGraph() { setLayout (new BorderLayout()); GraphicsScreen gs = new GraphicsScreen(); // Create lower panel and buttons. Panel p1 = new Panel(); Button b1 = new Button("Quit"); b1.addMouseListener(new ControlPanelListener(b1, gs )); p1.add( b1 ); Button b2 = new Button("Clear"); b2.addMouseListener(new ControlPanelListener(b2, gs )); p1.add( b2 ); Button b3 = new Button("Draw Graph"); b3.addMouseListener(new ControlPanelListener(b3, gs )); p1.add( b3 ); // Position canvas and panel within the frame. add("Center", gs ); add("South", p1 ); // Set frame title and size. Display the frame. setTitle("Time-history response of SDOF System"); setSize(600,400); show(); } } class GraphicsScreen extends Canvas { private Dimension size; private Graphics gs; public void paint ( Graphics g ) { size = getSize(); clear(); } public void clear () { int iBorder = 25; // Get graphics and fill in background .... gs = getGraphics(); gs.setColor( Color.black); gs.fillRect( 0, 0, size.width-1, size.height-1 ); // Draw center patch .... gs.setColor( Color.white); gs.fillRect( iBorder, iBorder, size.width-2*iBorder, size.height-2*iBorder ); } public void gsDraw () { int iBorder1 = 25; int iBorder2 = 30; int iScaleX = 10; // Maximum value of x coordinate. int iScaleY = 40; // Range of y-coordinate values. int iMinX = iBorder2 + iBorder1; int iMinY = iBorder2 + iBorder1; int iMaxX = size.width - iBorder2 - iBorder1; int iMaxY = size.height - iBorder2 - iBorder1; int iDeltaX = iMaxX-iMinX; int iDeltaY = iMaxY-iMinY; // Get graphics and draw graph background .... gs = getGraphics(); gs.setColor( Color.red ); gs.fillRect( iBorder1, iBorder1, size.width-2*iBorder1, size.height-2*iBorder1 ); gs.setColor( Color.white ); gs.fillRect( iBorder2, iBorder2, size.width-2*iBorder2, size.height-2*iBorder2 ); // Draw axis in black gs.setColor(Color.black); gs.drawLine( iMinX, iMinY+iDeltaY/2, iMaxX, iMinY+iDeltaY/2 ); gs.drawLine( iMinX, iMinY, iMinX, iMaxY); gs.setColor(Color.red); // Change color to red for plotting // Calculate the original point in pixel system double dYorg = FunctionToPlot(0); int iPrevXpt = iMinX; int iPrevYpt = (int) ((iMinY + (iDeltaY)/2 - (iDeltaY)*dYorg/iScaleY)); // Plot function by looping over x values : problem coordinates are // converted to pixel coordinates for plotting. for (int iXpt = iMinX ; iXpt < iMaxX + 1 ; iXpt++ ) { double dX = (double) ((iXpt - iMinX)*((double) iScaleX / (double) iDeltaX)); double dY = FunctionToPlot( dX ); int iYpt = (int) (iMinY + (iDeltaY)/2 - (iDeltaY)*dY/iScaleY); gs.drawLine( iPrevXpt, iPrevYpt, iXpt, iYpt); iPrevXpt = iXpt; iPrevYpt = iYpt; } // Print title for plot.... gs.setColor(Color.black); // Print title of plot in black. gs.drawString("y(t) = 10.cos(10.t) + 10.sin(10.t)/sqrt(10)", iMinX + iBorder1, iMinY + iBorder1); } // The function to plot public double FunctionToPlot( double dX ) { double dXint = 10.0; double dVint = 10; double dW = Math.sqrt(10.0); return (double)( dXint*Math.cos(dW*dX) + dVint*Math.sin(dW*dX)/dW ); } } class ControlPanelListener extends MouseAdapter { private Button button; private GraphicsScreen graphics; public ControlPanelListener( Button b, GraphicsScreen gs ) { graphics = gs; button = b; } public void mouseReleased(MouseEvent e) { String s = new String(button.getLabel()); if( s.compareTo("Quit") == 0 ) { System.out.println("Quit"); System.exit(0); } if( s.compareTo("Clear") == 0 ) { graphics.clear(); } if( s.compareTo("Draw Graph") == 0 ) { graphics.gsDraw(); } } }