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; int imagewidth, imageheight; // The constructor makes the curve object with default values. // They can be resized. bendCanvas() { this.setBackground(Color.white); Dimension d = this.getSize(); imagewidth = d.width; imageheight = d.height; curve = new bendGraphic(650,450,0.0,0.0); // imagewidth = 650; // imageheight = 450; } // 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 setTorsion_R(double torque_R) { curve.setTorsion_R(torque_R); this.drawCanvas(); } public void setTorsion_L(double torque_L) { curve.setTorsion_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. Dimension d = this.getSize(); 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); // 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(); paint(g); } }