// plot_waved.java a very basic, yet complete java plot application // y=f(x) x in 0.0 to 1.0, and y in -1.0 to 1.0 import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import javax.swing.*; public class plot_waved extends Frame { int wsize = 1000; int xoff = 20; // frame int yoff = 60; // frame down int ymid = yoff+(wsize-xoff-yoff)/2; double xscale = (double)(wsize-xoff-xoff); double yscale = (double)(wsize-xoff-yoff); double xmin = 0.0; double xmax = 1.0; int nx = 512; double dx = (xmax-xmin)/(double)(nx-1); double x1, y1, x2, y2; int xp1, yp1, xp2, yp2; double f(double x) // y = f(x) 0.0 <= y <= 1.0 { double y = 0.0; double r = 1.0; double Pi = Math.PI; r = 0.1+0.9*Math.cos(x*Pi); y = r*Math.cos(20.0*x*2.0*Pi); return y; } // end y plot_waved() { setTitle("plot_waved"); setSize(wsize,wsize); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); } class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { requestFocus(); System.out.println("press"); // debug print repaint(); System.exit(0); } } public void paint(Graphics g) { g.drawString("r = 0.1+0.9*cos(x*Pi) y = r*cos(20.0*x*2.0*Pi)",50,50); g.drawString("y=f(x) xmin=0, xmax=1, ymin=-1, ymax=1", wsize/2, 50); // draw boundary and y=0 g.setColor(Color.red); g.drawRect(xoff, yoff, wsize-xoff-xoff, wsize-xoff-yoff); g.drawLine(xoff, ymid, wsize-xoff, ymid); g.setColor(Color.black); // draw a wave for(int i=0; i