?? curveapplet.java
字號:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.event.MouseInputAdapter;
public class CurveApplet extends JApplet {
public void init(){
pane = new CurvePane();
Container content = getContentPane();
//Add the pane displaying the curves to the content pane for the applet
content.add(pane);
MouseHandler handler = new MouseHandler(); //Create the listener
pane.addMouseListener(handler); //Monitor mouse button presses
pane.addMouseMotionListener(handler); //as well as movement
}
//class define a pane on which to draw
class CurvePane extends JComponent {
public CurvePane(){
quadCurve = new QuadCurve2D.Double(
startQ.x, startQ.y,
control.x, control.y,
endQ.x, endQ.y);
cubicCurve = new CubicCurve2D.Double(
startC.x, startC.y,
controlStart.x,controlStart.y,
controlEnd.x, controlEnd.y,
endC.x, endC.y);
}
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
//Update the curves with the current control point positions
quadCurve.ctrlx = ctrlQuad.getCenter().x;
quadCurve.ctrly = ctrlQuad.getCenter().y;
cubicCurve.ctrlx1 = ctrlCubic1.getCenter().x;
cubicCurve.ctrly1 = ctrlCubic1.getCenter().y;
cubicCurve.ctrlx2 = ctrlCubic2.getCenter().x;
cubicCurve.ctrly2 = ctrlCubic2.getCenter().y;
//Draw the curves
g2D.setPaint(Color.blue);
g2D.draw(quadCurve);
g2D.draw(cubicCurve);
//Create and draw the markers showing the control points
g2D.setPaint(Color.red);
ctrlQuad.draw(g2D);
ctrlCubic1.draw(g2D);
ctrlCubic2.draw(g2D);
//draw tangents from the curve end points to the control marker centers
Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(endQ, ctrlQuad.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(startC, ctrlCubic1.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(endC, ctrlCubic2.getCenter());
g2D.draw(tangent);
}
}
class Marker {
public Marker(Point2D.Double control) {
center = control;
//Create circle around control point
circle = new Ellipse2D.Double(control.x - radius, control.y - radius, 2.0*radius, 2.0*radius);
}
public void draw (Graphics2D g2D){
g2D.draw(circle);
}
//get center of marker - the control point position
Point2D.Double getCenter(){
return center;
}
public boolean contains(double x, double y){
return circle.contains(x, y);
}
public void setLocation(double x, double y){
center.x = x; //Update control points coordinates
center.y = y;
circle.x = x - radius; //Change circle position correspondingly
circle.y = y - radius;
}
Point2D.Double center;
Ellipse2D.Double circle;
static final double radius = 3;
}
class MouseHandler extends MouseInputAdapter {
public void mousePressed(MouseEvent e){
//check if the cursor inside any marker
if(ctrlQuad.contains(e.getX(), e.getY()))
selected = ctrlQuad;
else if(ctrlCubic1.contains(e.getX(), e.getY()))
selected = ctrlCubic1;
else if( ctrlCubic2.contains(e.getX(), e.getY()))
selected = ctrlCubic2;
}
public void mouseReleased(MouseEvent e){
selected = null;
}
public void mouseDragged(MouseEvent e){
if(selected != null){
//Set the marker to current cursor position
selected.setLocation(e.getX(), e.getY());
pane.repaint();
}
}
Marker selected = null;
}
//Points for quadratic curve
Point2D.Double startQ = new Point2D.Double(50, 150);
Point2D.Double endQ = new Point2D.Double(150, 150);
Point2D.Double control = new Point2D.Double(80, 100);
//Points for cubic curve
Point2D.Double startC = new Point2D.Double(50, 300);
Point2D.Double endC = new Point2D.Double(150, 300);
Point2D.Double controlStart = new Point2D.Double(80, 250);
Point2D.Double controlEnd = new Point2D.Double(160, 250);
//Markers for control points
Marker ctrlQuad = new Marker(control);
Marker ctrlCubic1 = new Marker(controlStart);
Marker ctrlCubic2 = new Marker(controlEnd);
QuadCurve2D.Double quadCurve;
CubicCurve2D.Double cubicCurve;
CurvePane pane = new CurvePane();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -