Applet程序。

 import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent; @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
MouseHandler handler = new MouseHandler(); // Create the listener
pane.addMouseListener(handler); // Monitor mouse button presses
pane.addMouseMotionListener(handler); // as well as movement } // 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 // 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); // 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;
} // Test if a point x,y is inside the marker
public boolean contains(double x, double y) {
return circle.contains(x,y);
} // Sets a new control point location
public void setLocation(double x, double y) {
center.x = x; // Update control point
center.y = y; // coordinates
circle.x = x-radius; // Change circle position
circle.y = y-radius; // correspondingly
} Ellipse2D.Double circle; // Circle around control point
Point2D.Double center; // Circle center - the control point
static final double radius = 3; // Radius of circle
} private class MouseHandler extends MouseInputAdapter {
@Override
public void mousePressed(MouseEvent e) {
// Check if the cursor is 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;
} @Override
public void mouseReleased(MouseEvent e) {
selected = null; // Deselect any selected marker
} @Override
public void mouseDragged(MouseEvent e) {
if(selected != null) { // If a marker is selected
// Set the marker to current cursor position
selected.setLocation(e.getX(), e.getY());
pane.repaint(); // Redraw pane contents
}
} private Marker selected; // Stores reference to selected marker
} // 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 3 moving the control points)的更多相关文章

  1. Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)

    Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.*; @SuppressWarnings("s ...

  2. Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)

    Applet程序. 定义自由曲线的类有两个,其中一个定义二次曲线,另一个定义三次曲线.这些自由曲线是用一系列线段定义的参数化曲线.二次曲线段用方程定义,方程包含独立变量x的平方.三次曲线也用方程定义, ...

  3. Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)

    控制台程序. 在Sketcher中创建形状时,并不知道应该以什么顺序创建不同类型的形状,这完全取决于使用Sketcher程序生成草图的人.因此需要绘制形状,对它们执行其他操作而不必知道图形是什么.当然 ...

  4. Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)

    控制台程序. 在模型中表示数据视图的类用来显示草图并处理用户的交互操作,所以这种类把显示方法和草图控制器合并在一起.不专用于某个视图的通用GUI创建和操作在SketcherFrame类中处理. 模型对 ...

  5. Java基础之在窗口中绘图——渐变填充(GradientApplet 1)

    Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.*; @SuppressWarnings("s ...

  6. Java基础之在窗口中绘图——填充星型(StarApplet 2 filled stars)

    Applet程序. import javax.swing.*; import java.awt.*; import java.awt.geom.GeneralPath; @SuppressWarnin ...

  7. Java基础之在窗口中绘图——绘制星星(StarApplet 1)

    Applet程序. 可以把更复杂的几何形状定义为GeneralPath类型的对象.GeneralPath可以是直线.Quad2D曲线和Cubic2D曲线的结合体,甚至可以包含其他GeneralPath ...

  8. Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)

    控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...

  9. Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)

    控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...

随机推荐

  1. 51Nod 1001 数组中和等于K的数对 Label:Water

    给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...

  2. jquery 动画效果插件

    从jQuery API 文档中可以知道,jQuery自定义动画的函数.animate( properties [, duration] [, easing] [, complete] )有四个参数: ...

  3. Vijos1425子串清除 题解

    Vijos1425子串清除 题解   描述: 我们定义字符串A是字符串B的子串当且仅当我们能在B串中找到A串.现在给你一个字符串A,和另外一个字符串B,要你每次从B串中从左至右找第一个A串,并从B串中 ...

  4. 【noiOJ】p8211 (PS:二分浮点数的精度问题)

    05:派 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 我的生日要到了!根据习俗,我需要将一些派分给大家.我有N个不同口味.不同大小的派.有F个朋友会来参加 ...

  5. 一个简单的零配置命令行HTTP服务器 - http-server (nodeJs)

    http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...

  6. 纯css3绘制扇形

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. iOS五种本地缓存数据方式

    iOS五种本地缓存数据方式   iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  8. 从全局中通过class类名获取标签

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Android Studio 环境部署 (转载)

    Android Studio的安装和使用过程经常需要下载以来文件和Gradle版本,而Google网站在天朝的访问可谓步履维艰,没有稳定的FQ工具是非常痛苦的.何况,作为一个优秀的程序员,不能访问国外 ...

  10. tomcat启动闪退

    TOMCAT启动时报错:the CATALINA_HOME environment variable is not defined correctly 运行tomcat/bin目录下的startup. ...