// // Bezier curves // // Mark Maloof // July 1998 // import java.awt.*; import java.awt.event.*; /*=======================================================================*/ public class pierre extends Frame { public pierre() { super("COSC 286 Bezier Curves"); // Create the window this.setSize(300, 300); // Specify the frame size this.setResizable(false); // Prevent resizing (supposedly) // Create a ScrollPane ScrollPane pane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER); pane.setSize(300, 300); // Specify its size this.add(pane, "Center"); // Add it to the frame DrawingArea scribble; scribble = new DrawingArea(this); // Create a drawing area pane.add(scribble); // Add it to the ScrollPane MenuBar menubar = new MenuBar(); // Create a menubar this.setMenuBar(menubar); // Add it to the frame Menu file = new Menu("File"); // Create a File menu menubar.add(file); // Add to menubar // Create a quit menu item with a shortcut, and add it to the menu MenuItem q; file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q))); // Create and register an action listener object for the menu item. q.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Create and register action listener for the window closing event. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Set the window size and pop it up. this.pack(); this.show(); } // pierre public static void main(String[] args) { new pierre(); } // main } // pierre class /*=======================================================================*/ class DrawingArea extends Component { protected Frame frame; public DrawingArea(Frame frame) { this.frame = frame; } // DrawingArea public void setPixel(Graphics g, int x, int y) { g.drawLine(x, y, x, y); } // setPixel public void paint(Graphics g) { Dimension d = getSize(); // Dimensions of the drawing area g.setColor(Color.black); // Sets the drawing color to black Vertex[] controls = new Vertex[3]; controls[0] = new Vertex(50,150,0); controls[1] = new Vertex(150,100,0); controls[2] = new Vertex(200,150,0); Bezier b = new Bezier(3, controls); b.plotControlPoints(g); b.plot(g); } // paint } // DrawingArea class /*=======================================================================*/ class Bezier { int[] c; Vertex[] controls; int nControls; // Le constructeur Bezier(int nControls, Vertex[] controls) { this.nControls = nControls; this.controls = new Vertex[nControls]; this.controls = controls; this.c = new int[nControls]; computeCoefficients(); } // Bezier private void computeCoefficients() { int n = nControls - 1; for (int k = 0; k <= n; k++) { c[k] = 1; for (int i = n; i >= k+1; i--) c[k] *= i; for (int i = n - k; i >= 2; i--) c[k] /= i; } // for } // computeCoefficients public void plotControlPoints(Graphics g) { Color currentColor = g.getColor(); g.setColor(Color.red); for (int k = 0; k < nControls; k++) g.drawOval(controls[k].ix(), controls[k].iy(), 2, 2); g.setColor(currentColor); } // plotControlPoints public void plot(Graphics g) { for (double u = 0.0; u <= 1.0; u += 0.01) { Vertex p = new Vertex(); int n = nControls - 1; for (int k = 0; k < nControls; k++) { double blend = c[k] * Math.pow(u, k) * Math.pow(1-u, n-k); p.x(p.x() + controls[k].x() * blend); p.y(p.y() + controls[k].y() * blend); p.z(p.z() + controls[k].z() * blend); } // for g.drawLine(p.ix(), p.iy(), p.ix(), p.iy()); } // for } // plot } // Bezier class /*=======================================================================*/ class Vertex { private static final int N = 4; private double m[]; /*-----------------------------------------------------------------------*/ // // Vertex constructor // public Vertex(double x, double y, double z) { m = new double[N]; this.m[0] = x; this.m[1] = y; this.m[2] = z; this.m[3] = 1.0; } // Vertex /*-----------------------------------------------------------------------*/ // // Vertex constructor: assigns the values to 0.0. Used in the transform // method below. // public Vertex() { m = new double[N]; this.m[0] = 0.0; this.m[1] = 0.0; this.m[2] = 0.0; this.m[3] = 0.0; } // Vertex /*-----------------------------------------------------------------------*/ // // x, y, z, ix, iy, iz: accessor methods. // public double x() { return this.m[0]; } // x public double y() { return this.m[1]; } // y public double z() { return this.m[2]; } // z public int ix() { return (int) Math.round(this.m[0]); } // ix public int iy() { return (int) Math.round(this.m[1]); } // iy public int iz() { return (int) Math.round(this.m[2]); } // iz /*-----------------------------------------------------------------------*/ // // x, y, and z: used to set x and y and z. // public void x(double x) { this.m[0] = x; } // x public void y(double y) { this.m[1] = y; } // y public void z(double z) { this.m[2] = z; } // z } // Vertex class