// Instructions: // (1) Add your code for computing Bernsteins below. // (2) Increase the number of Bezier points (the coeffs a_i, b_i) import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import java.text.*; public class DrawBezier extends JPanel { ///////////////////////////////////////////////////////// // You can ignore this for now ... // What we draw: points, lines. ArrayList points; ArrayList linePoints; Color pointColor = Color.red; Color clickColor = Color.blue; Color lineColor = Color.black; BasicStroke lineStroke = new BasicStroke(1f); double minX=0, maxX=10, minY=0, maxY=10; // Bounds. int numIntervals = 10; // # tick marks. double pointRadius = 0.1; // Size of dot. int numControlPoints = 3; int numCurvePoints = 100; // GUI stuff. Dimension D; // Size of drawing area. int inset=60; // Inset of axes and bounding box. // GUI elements. JFrame frame; JLabel statusLabel = new JLabel (""); BezierPoint clickPoint = null; static DecimalFormat df = new DecimalFormat(); ////////////////////////////////////////////////////////////// // READ from here .... public static void main (String[] argv) { new DrawBezier (); } static double bernstein (int n, int k, double t) { // INSERT YOUR CODE HERE: } public DrawBezier () { // Initialize, build GUI etc. points = new ArrayList(); linePoints = new ArrayList(); buildGUI (); // MODIFY CODE HERE to change the number of Bezier (control) points. numControlPoints = 4; // Four coefficients for x(t) double[] init_a = {1, 2, 3, 6}; // Four coefficients for y(t) double[] init_b = {1, 5, 6, 2}; initializePoints (init_a, init_b); this.repaint (); } ////////////////////////////////////////////////////////////// // Computation of curve void computeBezierCurve () { linePoints = new ArrayList(); double delT = 1.0 / numCurvePoints; int n = numControlPoints-1; for (double t=0; t<=1; t+=delT) { Point2D.Double q = new Point2D.Double (); double x = 0, y = 0; for (int k=0; k<=n; k++) { BezierPoint p = points.get(k); x += p.x * bernstein(n,k,t); y += p.y * bernstein(n,k,t); } q.x = x; q.y = y; linePoints.add (q); } } // ... until here ////////////////////////////////////////////////////////////// public void paintComponent (Graphics g) { //********************************************************* // This is standard paint stuff: draw axes, box etc. // No need to read - skip to **** super.paintComponent (g); Graphics2D g2d = (Graphics2D)g; RenderingHints rh = g2d.getRenderingHints(); rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHints(rh); // Background. D = this.getSize (); g.setColor (Color.WHITE); g.fillRect (0,0, D.width, D.height); Graphics2D g2 = (Graphics2D) g; g2.setStroke (lineStroke); // Axes, bounding box. g.setColor (Color.gray); g.drawLine (inset,D.height-inset, D.width-inset, D.height-inset); g.drawLine (D.width-inset,inset, D.width-inset,D.height-inset); g.drawLine (inset,inset, inset,D.height-inset); g.drawLine (inset,inset, D.width-inset, inset); double xDelta = (maxX-minX) / numIntervals; // X-ticks and labels. for (int i=1; i<=numIntervals; i++) { double xTickd = i*xDelta; int xTick = (int) ( xTickd/(maxX-minX) * (D.width-2*inset)); g.drawLine (inset+xTick,D.height-inset-5, inset+xTick,D.height-inset+5); double x = minX + i*xDelta; g.drawString (df.format(x), xTick+inset-5, D.height-inset+20); } // Y-ticks double yDelta = (maxY-minY) / numIntervals; for (int i=0; i(); for (int n=0; n