java g2d绘制点_Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)...

1 import javax.swing.*;2 import java.awt.*;3 import java.awt.geom.*;4 importjavax.swing.event.MouseInputAdapter;5 importjava.awt.event.MouseEvent;6

7 @SuppressWarnings("serial")8 public class CurveApplet extendsJApplet {9 //Initialize the applet

10 @Override11 public voidinit() {12 pane = new CurvePane(); //Create pane containing curves

13 Container content = getContentPane(); //Get the content pane14

15 //Add the pane displaying the curves to the content pane for the applet

16 content.add(pane); //BorderLayout.CENTER is default position

17 MouseHandler handler = new MouseHandler(); //Create the listener

18 pane.addMouseListener(handler); //Monitor mouse button presses

19 pane.addMouseMotionListener(handler); //as well as movement

20

21 }22

23 //Class defining a pane on which to draw

24 class CurvePane extendsJComponent {25 //Constructor

26 publicCurvePane() {27 quadCurve = new QuadCurve2D.Double( //Create quadratic curve

28 startQ.x, startQ.y, //Segment start point

29 control.x, control.y, //Control point

30 endQ.x, endQ.y); //Segment end point

31

32 cubicCurve = new CubicCurve2D.Double( //Create cubic curve

33 startC.x, startC.y, //Segment start point

34 controlStart.x, controlStart.y, //Control pt for start

35 controlEnd.x, controlEnd.y, //Control point for end

36 endC.x, endC.y); //Segment end point

37 }38

39 @Override40 public voidpaint(Graphics g) {41 Graphics2D g2D = (Graphics2D)g; //Get a 2D device context42

43 //Update the curves with the current control point positions

44 quadCurve.ctrlx =ctrlQuad.getCenter().x;45 quadCurve.ctrly =ctrlQuad.getCenter().y;46 cubicCurve.ctrlx1 =ctrlCubic1.getCenter().x;47 cubicCurve.ctrly1 =ctrlCubic1.getCenter().y;48 cubicCurve.ctrlx2 =ctrlCubic2.getCenter().x;49 cubicCurve.ctrly2 =ctrlCubic2.getCenter().y;50

51 //Draw the curves

52 g2D.setPaint(Color.BLUE);53 g2D.draw(quadCurve);54 g2D.draw(cubicCurve);55

56 //Create and draw the markers showing the control points

57 g2D.setPaint(Color.red); //Set the color

58 ctrlQuad.draw(g2D);59 ctrlCubic1.draw(g2D);60 ctrlCubic2.draw(g2D);61 //Draw tangents from the curve end points to the control marker centers

62 Line2D.Double tangent = newLine2D.Double(startQ, ctrlQuad.getCenter());63 g2D.draw(tangent);64 tangent = newLine2D.Double(endQ, ctrlQuad.getCenter());65 g2D.draw(tangent);66

67 tangent = newLine2D.Double(startC, ctrlCubic1.getCenter());68 g2D.draw(tangent);69 tangent = newLine2D.Double(endC, ctrlCubic2.getCenter());70 g2D.draw(tangent);71 }72 }73

74 //Inner class defining a control point marker

75 private classMarker {76 publicMarker(Point2D.Double control) {77 center = control; //Save control point as circle center78

79 //Create circle around control point

80 circle = new Ellipse2D.Double(control.x-radius, control.y-radius,81 2.0*radius, 2.0*radius);82 }83

84 //Draw the marker

85 public voiddraw(Graphics2D g2D) {86 g2D.draw(circle);87 }88

89 //Get center of marker - the control point position

90 Point2D.Double getCenter() {91 returncenter;92 }93

94 //Test if a point x,y is inside the marker

95 public boolean contains(double x, doubley) {96 returncircle.contains(x,y);97 }98

99 //Sets a new control point location

100 public void setLocation(double x, doubley) {101 center.x = x; //Update control point

102 center.y = y; //coordinates

103 circle.x = x-radius; //Change circle position

104 circle.y = y-radius; //correspondingly

105 }106

107 Ellipse2D.Double circle; //Circle around control point

108 Point2D.Double center; //Circle center - the control point

109 static final double radius = 3; //Radius of circle

110 }111

112 private class MouseHandler extendsMouseInputAdapter {113 @Override114 public voidmousePressed(MouseEvent e) {115 //Check if the cursor is inside any marker

116 if(ctrlQuad.contains(e.getX(), e.getY()))117 selected =ctrlQuad;118 else if(ctrlCubic1.contains(e.getX(), e.getY()))119 selected =ctrlCubic1;120 else if(ctrlCubic2.contains(e.getX(), e.getY()))121 selected =ctrlCubic2;122 }123

124 @Override125 public voidmouseReleased(MouseEvent e) {126 selected = null; //Deselect any selected marker

127 }128

129 @Override130 public voidmouseDragged(MouseEvent e) {131 if(selected != null) { //If a marker is selected132 //Set the marker to current cursor position

133 selected.setLocation(e.getX(), e.getY());134 pane.repaint(); //Redraw pane contents

135 }136 }137

138 private Marker selected; //Stores reference to selected marker

139 }140

141 //Points for quadratic curve

142 private Point2D.Double startQ = new Point2D.Double(50, 75); //Start point

143 private Point2D.Double endQ = new Point2D.Double(150, 75); //End point

144 private Point2D.Double control = new Point2D.Double(80, 25); //Control point145

146 //Points for cubic curve

147 private Point2D.Double startC = new Point2D.Double(50, 150); //Start point

148 private Point2D.Double endC = new Point2D.Double(150, 150); //End point

149 private Point2D.Double controlStart = new Point2D.Double(80, 100); //1st cntrl point

150 private Point2D.Double controlEnd = new Point2D.Double(160, 100); //2nd cntrl point

151 private QuadCurve2D.Double quadCurve; //Quadratic curve

152 private CubicCurve2D.Double cubicCurve; //Cubic curve

153 private CurvePane pane = new CurvePane(); //Pane to contain curves154

155 //Markers for control points

156 private Marker ctrlQuad = newMarker(control);157 private Marker ctrlCubic1 = newMarker(controlStart);158 private Marker ctrlCubic2 = newMarker(controlEnd);159

160 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值