import java.awt.*; // to get Graphics object. public class bendCanvas extends Canvas { Image offscreen; // This is the offscreen image so we can double buffer. bendGraphic curve = null; Dimension d = this.getSize(); int imagewidth = d.width, imageheight = d.height; // The constructor makes the curve object with default values. // They can be resized. bendCanvas() { this.setBackground(Color.white); curve = new bendGraphic(imagewidth,imageheight,0.0,0.0); System.out.println("created of bendGraphic of size " + imagewidth + " x " + imageheight); } // A public function to allow people to change the torque_R // of the longer wavelength. We pass this on to our curve object. public void setTorque_R(double torque_R) { curve.setTorque_R(torque_R); this.drawCanvas(); } public void setTorque_L(double torque_L) { curve.setTorque_L(torque_L); this.drawCanvas(); } // These two inform the layout what our minimum requirements are. // Without these, you may get a zero size object. public Dimension getPreferredSize() { Dimension d = new Dimension(200,50); return d; } public Dimension getMinimumSize() { Dimension d = new Dimension(150,50); return d; } // This method draws the background and text at its current position. public void paint(Graphics g) { //Dimension d = this.size(); update(g); } public void update( Graphics g ) { // If the screen is invalid, don't redraw. Just copy from the // offscreen buffer. if (offscreen != null) { g = this.getGraphics(); g.drawImage(offscreen, 0, 0, this); } else { g.clearRect(0,0,imagewidth,imageheight); } } public void processMouseEvent(java.awt.event.MouseEvent e) { if (e.getSource()==this) { this.drawCanvas(); } } public void drawCanvas() { // Make sure the offscreen image is created and is the right size. if ((offscreen == null) || ((imagewidth != d.width) || (imageheight != d.height))) { // if (offscreen != null) offscreen.flush(); // Sometimes the image sizes are zero or negative before the // frame is drawn. if (d.width > 1 && d.height > 1) { imagewidth = d.width; imageheight = d.height; } // Create an image from the size of this canvas using a method // from the component class, I think. offscreen = createImage(imagewidth, imageheight); System.out.println("created image of size " + imagewidth + " x " + imageheight); // Make our curve agree to the size. curve.setSize(imagewidth,imageheight); } // Get the offscreen image. Graphics g; try { g = offscreen.getGraphics(); } catch (java.lang.NullPointerException e) { System.out.println("java.lang.NullPointerException: " + e); g = null; } // Draw into the off-screen image. g.clearRect(0,0,d.width,d.height); curve.draw(g); // Request that the system call paint sometime soon. repaint(); } }