The QuadCurve2D class provides a quadratic parametric curve segments. We are providing you an example which shows different curves.
Draw different curves with QuadCurve2D
This section illustrates you how to draw different curves with the class
QuadCurve2D.
The QuadCurve2D class provides a quadratic parametric curve segments. We are providing you an
example which shows different curves.
In the given example, we have used Vector class to implement an array of
objects. The components of this class can be accessed using an index. The
method vector.size() of Vector class returns the number of
components in the vector. The method vector.elementAt(k)
returns the component at the specified index, in this example we have used 'k'
as index.
Here is the code of QuadCurveExample.java
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
public class QuadCurveExample extends JApplet {
Canvas1 canvas;
public static void main(String[] args){
JFrame frame = new JFrame("QuadCurve Example");
QuadCurveExample curve = new QuadCurveExample();
curve.init();
frame.getContentPane().add(curve);
frame.setSize(500,250);
frame.setVisible(true);
}
public void init() {
Container container = getContentPane();
canvas = new Canvas1();
container.add(canvas);
}
class Canvas1 extends Canvas {
Vector vector;
QuadCurve2D quadCurve = null;
Rectangle2D rec = null;
public Canvas1() {
setBackground(Color.white);
setSize(500, 250);
vector = new Vector();
vector.addElement(new QuadCurve2D.Float(30, 30, 90,
170, 130,30));
vector.addElement(new QuadCurve2D.Float(130, 110, 170,
50, 210,190));
vector.addElement(new QuadCurve2D.Float(250, 30, 230,
70, 270,130));
vector.addElement(new QuadCurve2D.Float(260, 170, 270,
150,290, 190));
vector.addElement(new QuadCurve2D.Float(280, 180, 280,
160,300, 190));
vector.addElement(new QuadCurve2D.Float(310, 190, 350,
50, 390,130));
vector.addElement(new QuadCurve2D.Float(30, 190, 90,
180, 130,200));
vector.addElement(new QuadCurve2D.Float(140, 40, 100,
190, 140,200));
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int k = 0; k < vector.size(); k++) {
g2.draw((QuadCurve2D) vector.elementAt(k));
}
}
}
} |
Output will be displayed as:
Download Source Code