Question: import java.util. * ; import javax.swing. * ; import java.awt.image. * ; import java.awt. * ; class Bezier extends JApplet { BufferedImage image = new

import java.util.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
class Bezier extends JApplet {
BufferedImage image = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
private final double controlPoints[][];
private final double steps;
Bezier(double controPoints[][], int steps){
this.controlPoints = controPoints;
this.steps = steps;
}
public void init(){
setSize(1280,720);
repaint();
}
int[] computePoint(float u){
float c0,c1,c2,c3;
c0=1-3*u +3*u*u - u*u*u;
c1=3*u -6*u*u +3*u*u*u;
c2=3*u*u -3*u*u*u;
c3= u*u*u;
int ptx =(int)(c0* controlPoints[0][0]+ c1* controlPoints[1][0]+ c2* controlPoints[2][0]+ c3* controlPoints[3][0]);
int pty =(int)(c0* controlPoints[0][1]+ c1* controlPoints[1][1]+ c2* controlPoints[2][1]+ c3* controlPoints[3][1]);
int pts[]={ptx, pty};
return pts;
}
public void paint(Graphics g){
int prevPointx =0;
int prevPointy =0;
int color[]={255,0,0,255};
for(int i =0; i steps; i++){
int[] pts = computePoint(i/(float)steps);
int x = pts[0];
int y = pts[1];
if(i !=0)
g.drawLine(prevPointx, prevPointy, x, y);
prevPointx = x;
prevPointy = y;
raster.setPixel(x, y,color);
}
g.drawImage(image,0,0, null);
}
}
public class Main {
public static void main(String[] args){
Scanner input = new java.util.Scanner(System.in);
double points[][]= new double[4][2];
System.out.println("Enter p1, p1', p2, p2' x and y coordinates respectively");
for(int i =0; i points.length; i++){
points[i][0]= input.nextInt();
points[i][1]= input.nextInt();
}
System.out.print("Enter how many steps to do: ");
int steps = input.nextInt();
JFrame frame = new JFrame("Bezier Cubic Spline");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Bezier curve = new Bezier(points, steps);
curve.setPreferredSize(new Dimension(600,600));
frame.add(curve);
frame.pack();
frame.setVisible(true);
}
}
based on the prev code we need to do this:
1.Write a program to draw curves sing Beizer curve for P1(200250), P1`(300350) control points and P2(550600), P2`(500450) tangent vectors .
2.Modify the above program ,along with the curve add an object shape of the ball at the end of the point P2 using 2D graphics.
import java.util. * ; import javax.swing. * ;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!