Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)
Applet程序。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*; @SuppressWarnings("serial")
public class CurveApplet extends JApplet {
// Initialize the applet
@Override
public void init() {
pane = new CurvePane(); // Create pane containing curves
Container content = getContentPane(); // Get the content pane // Add the pane displaying the curves to the content pane for the applet
content.add(pane); // BorderLayout.CENTER is default position
} // Class defining a pane on which to draw
class CurvePane extends JComponent {
// Constructor
public CurvePane() {
quadCurve = new QuadCurve2D.Double( // Create quadratic curve
startQ.x, startQ.y, // Segment start point
control.x, control.y, // Control point
endQ.x, endQ.y); // Segment end point cubicCurve = new CubicCurve2D.Double( // Create cubic curve
startC.x, startC.y, // Segment start point
controlStart.x, controlStart.y, // Control pt for start
controlEnd.x, controlEnd.y, // Control point for end
endC.x, endC.y); // Segment end point
} @Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g; // Get a 2D device context // 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); // Set the color
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);
}
} // Inner class defining a control point marker
private class Marker {
public Marker(Point2D.Double control) {
center = control; // Save control point as circle center // Create circle around control point
circle = new Ellipse2D.Double(control.x-radius, control.y-radius,
2.0*radius, 2.0*radius);
} // Draw the marker
public void draw(Graphics2D g2D) {
g2D.draw(circle);
} // Get center of marker - the control point position
Point2D.Double getCenter() {
return center;
} Ellipse2D.Double circle; // Circle around control point
Point2D.Double center; // Circle center - the control point
static final double radius = 3; // Radius of circle
} // Points for quadratic curve
private Point2D.Double startQ = new Point2D.Double(50, 75); // Start point
private Point2D.Double endQ = new Point2D.Double(150, 75); // End point
private Point2D.Double control = new Point2D.Double(80, 25); // Control point // Points for cubic curve
private Point2D.Double startC = new Point2D.Double(50, 150); // Start point
private Point2D.Double endC = new Point2D.Double(150, 150); // End point
private Point2D.Double controlStart = new Point2D.Double(80, 100); // 1st cntrl point
private Point2D.Double controlEnd = new Point2D.Double(160, 100); // 2nd cntrl point
private QuadCurve2D.Double quadCurve; // Quadratic curve
private CubicCurve2D.Double cubicCurve; // Cubic curve
private CurvePane pane = new CurvePane(); // Pane to contain curves // Markers for control points
private Marker ctrlQuad = new Marker(control);
private Marker ctrlCubic1 = new Marker(controlStart);
private Marker ctrlCubic2 = new Marker(controlEnd); }
HTML文件与上一例同。
Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)的更多相关文章
- Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)
		Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.*; import javax.swing.event. ... 
- Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)
		Applet程序. 定义自由曲线的类有两个,其中一个定义二次曲线,另一个定义三次曲线.这些自由曲线是用一系列线段定义的参数化曲线.二次曲线段用方程定义,方程包含独立变量x的平方.三次曲线也用方程定义, ... 
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
		控制台程序. 在模型中表示数据视图的类用来显示草图并处理用户的交互操作,所以这种类把显示方法和草图控制器合并在一起.不专用于某个视图的通用GUI创建和操作在SketcherFrame类中处理. 模型对 ... 
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)
		控制台程序. 在Sketcher中创建形状时,并不知道应该以什么顺序创建不同类型的形状,这完全取决于使用Sketcher程序生成草图的人.因此需要绘制形状,对它们执行其他操作而不必知道图形是什么.当然 ... 
- Java基础之在窗口中绘图——渐变填充(GradientApplet 1)
		Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.*; @SuppressWarnings("s ... 
- Java基础之在窗口中绘图——填充星型(StarApplet 2 filled stars)
		Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.GeneralPath; @SuppressWarnin ... 
- Java基础之在窗口中绘图——绘制星星(StarApplet 1)
		Applet程序. 可以把更复杂的几何形状定义为GeneralPath类型的对象.GeneralPath可以是直线.Quad2D曲线和Cubic2D曲线的结合体,甚至可以包含其他GeneralPath ... 
- Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
		控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ... 
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
		控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ... 
随机推荐
- A VNC server is already running as :1
			root@host:~# rm -f /tmp/.X1-lock root@host:~# rm -f /tmp/.X11-unix/X1 root@host:~# vncserver -geomet ... 
- php获得网站根目录的几个方法
			php获得网站根目录的几个方法 电脑软硬件应用网 45IT.COM 时间:2015-01-08 12:54 作者:佚名 在php中我们要得到网站根目录可以用很多全局变量实现了,如可以利用__file_ ... 
- mysql 将null转代为0
			mysql 将null转代为0 分类: Mysql2012-12-15 11:56 6447人阅读 评论(1) 收藏 举报 1.如果为空返回0 select ifnull(null,0) 2.如果为空 ... 
- PHP实例练习--投票和租房子
			一,调查问卷 效果图: 
- vi 整行 多行 复制与粘贴
			vi编辑器中的整行(多行)复制与粘贴就非常必要了. 1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制: 2)多行复制 在命令模式下,将光标移动到将要复制的首行处,按 ... 
- Sign-Magnitude Representation
			COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION There are several alt ... 
- PureBasic 打开一个一行有多个数据的文件并读取其中某个数据
			如果有一个文件如下: TITLE = "Water Wurface Elevation"VARIABLES = "X", "Y", &quo ... 
- Ubuntu 14.04 在桌面上双击运行shell 脚本文件
			http://askubuntu.com/questions/465531/how-to-make-a-shell-file-execute-by-double-click up vote7down ... 
- Bootstrap 一. 排版样式(内联文本元素,对齐,大小写,缩略语,地址文本,引用文本,列表排版 ,代码 )
			第 2 章 排版样式 在 h1 ~ h6 元素之间,还可以嵌入一个 small 元素作为副标题 <h1>Bootstrap 框架 <small>Bootstrap 小标题< ... 
- 让Qt的无边框窗口支持拖拽、Aero Snap、窗口阴影等特性
			环境:Desktop Qt 5.4.1 MSVC2013 32bit 需要的库:dwmapi.lib .user32.lib 需要头文件:<dwmapi.h> .<windowsx. ... 
