新普金娱乐网址


《时间之问20》亚岁大暑与黄钟临月?

燕舞:烦人的课业!

Java 2D高级绘图

  • 四月 01, 2019
  • 数学
  • 没有评论

文化要点:

缘何要用转义字符串?

HTML中<,>,&等有卓殊意义(<,>,用于链接签,&用于转义),不可能直接使用。那么些标记是不显得在大家最后见到的网页里的,那即便大家期望在网页中体现那几个标记,该如何做吧?

那即将提起HTML转义字符串(Escape Sequence)了。

转义字符串(Escape
Sequence)也称字符实体(Character
Entity)。在HTML中,定义转义字符串的原委有七个:第三个原因是像“<”和“>”这类符号已经用来代表HTML标签,因而就不可能直接当做文本中的符号来选择。为了在HTML文书档案中选择这么些标记,就需求定义它的转义字符串。当解释程序遇到这类字符串时就把它表明为实在的字符。在输入转义字符串时,要严加服从字母大小写的条条框框。第二个原因是,有些字符在ASCII字符集中没有概念,因而要求利用转义字符串来代表。

率先节 Java 二D的增加功用

转义字符串的构成

转义字符串(Escape Sequence),即字符实体(Character
Entity)分成三有个别:第二有的是三个&符号,英文叫ampersand;第二有的是实业(Entity)名字或许是#丰硕实体(Entity)编号;第3有个别是2个支行。

比如,要出示小于号(<),就能够写 < 或然 < 。

用实体(Entity)名字的好处是相比好精晓,一看lt,大致就猜出是less
than的趣味,不过其劣势在于并不是有所的浏览器都帮忙最新的Entity名字。而实体(Entity)编号,各样浏览器都能处理。

提醒:实体名称(Entity)是分别轻重缓急写的。

备考:同叁个标记,能够用“实体名称”和“实体编号”三种艺术引用,“实体名称”的优势在于便宜记念,但无法确认保障拥有的浏览器都能顺遂辨识它,而“实体编号”则从未这种焦虑,但它其实不方便人民群众纪念。

概述、AWT图形能力的不足、Java 贰D API

什么样呈现空格?

熟视无睹状态下,HTML会自动截去多余的空格。不管您加多少空格,都被视作二个空格。比如你在多个字之间加了13个空格,HTML会截去8个空格,只保留三个。为了在网页中追加空格,你能够选择 表示空格。


第三节 图形绘制的主干措施

HTML特殊转义字符列表

转换Graphics二D对象、Graphics 类性格、绘图的性子和宗旨编制程序方法

最常用的字符实体 Character Entities

显示 说明 实体名称 实体编号
  半方大的空白 &ensp; &#8194;
  全方大的空白 &emsp; &#8195;
  不断行的空白格 &nbsp; &#160;
< 小于 &lt; &#60;
> 大于 &gt; &#62;
& &符号 &amp; &#38;
" 双引号 &quot; &#34;
© 版权 &copy; &#169;
® 已注册商标 &reg; &#174;
商标(美国) &#8482;
× 乘号 &times; &#215;
÷ 除号 &divide; &#247;

其三节 曲线难点的高级应用开发

ISO 8859-1 (Latin-1)字符集

HTML 4.01 支持 ISO 8859-1 (Latin-1) 字符集。

备注:为了有利于起见,以下表格中,“实体名称”简称为“名称”,“实体编号”简称为“编号”

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
  &nbsp; &#160; ¡ &iexcl; &#161; ¢ &cent; &#162; £ &pound; &#163; ¤ &curren; &#164;
¥ &yen; &#165; ¦ &brvbar; &#166; § &sect; &#167; ¨ &uml; &#168; © &copy; &#169;
ª &ordf; &#170; « &laquo; &#171; ¬ &not; &#172; ­ &shy; &#173; ® &reg; &#174;
¯ &macr; &#175; ° &deg; &#176; ± &plusmn; &#177; ² &sup2; &#178; ³ &sup3; &#179;
´ &acute; &#180; µ &micro; &#181; &para; &#182; · &middot; &#183; ¸ &cedil; &#184;
¹ &sup1; &#185; º &ordm; &#186; » &raquo; &#187; ¼ &frac14; &#188; ½ &frac12; &#189;
¾ &frac34; &#190; ¿ &iquest; &#191; À &Agrave; &#192; Á &Aacute; &#193; Â &Acirc; &#194;
à &Atilde; &#195; Ä &Auml; &#196; Å &Aring; &#197; Æ &AElig; &#198; Ç &Ccedil; &#199;
È &Egrave; &#200; É &Eacute; &#201; Ê &Ecirc; &#202; Ë &Euml; &#203; Ì &Igrave; &#204;
Í &Iacute; &#205; Î &Icirc; &#206; Ï &Iuml; &#207; Ð &ETH; &#208; Ñ &Ntilde; &#209;
Ò &Ograve; &#210; Ó &Oacute; &#211; Ô &Ocirc; &#212; Õ &Otilde; &#213; Ö &Ouml; &#214;
× &times; &#215; Ø &Oslash; &#216; Ù &Ugrave; &#217; Ú &Uacute; &#218; Û &Ucirc; &#219;
Ü &Uuml; &#220; Ý &Yacute; &#221; Þ &THORN; &#222; ß &szlig; &#223; à &agrave; &#224;
á &aacute; &#225; â &acirc; &#226; ã &atilde; &#227; ä &auml; &#228; å &aring; &#229;
æ &aelig; &#230; ç &ccedil; &#231; è &egrave; &#232; é &eacute; &#233; ê &ecirc; &#234;
ë &euml; &#235; ì &igrave; &#236; í &iacute; &#237; î &icirc; &#238; ï &iuml; &#239;
ð &eth; &#240; ñ &ntilde; &#241; ò &ograve; &#242; ó &oacute; &#243; ô &ocirc; &#244;
õ &otilde; &#245; ö &ouml; &#246; ÷ &divide; &#247; ø &oslash; &#248; ù &ugrave; &#249;
ú &uacute; &#250; û &ucirc; &#251; ü &uuml; &#252; ý &yacute; &#253; þ &thorn; &#254;
ÿ &yuml; &#255;

直线难点尖锐钻研、贝塞尔(Bezier)曲线、自定义样条曲线编制程序、

数学和希腊共和国字母标志 symbols, mathematical symbols, and Greek letters

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
ƒ &fnof; &#402; Α &Alpha; &#913; Β &Beta; &#914; Γ &Gamma; &#915; Δ &Delta; &#916;
Ε &Epsilon; &#917; Ζ &Zeta; &#918; Η &Eta; &#919; Θ &Theta; &#920; Ι &Iota; &#921;
Κ &Kappa; &#922; Λ &Lambda; &#923; Μ &Mu; &#924; Ν &Nu; &#925; Ξ &Xi; &#926;
Ο &Omicron; &#927; Π &Pi; &#928; Ρ &Rho; &#929; Σ &Sigma; &#931; Τ &Tau; &#932;
Υ &Upsilon; &#933; Φ &Phi; &#934; Χ &Chi; &#935; Ψ &Psi; &#936; Ω &Omega; &#937;
α &alpha; &#945; β &beta; &#946; γ &gamma; &#947; δ &delta; &#948; ε &epsilon; &#949;
ζ &zeta; &#950; η &eta; &#951; θ &theta; &#952; ι &iota; &#953; κ &kappa; &#954;
λ &lambda; &#955; μ &mu; &#956; ν &nu; &#957; ξ &xi; &#958; ο &omicron; &#959;
π &pi; &#960; ρ &rho; &#961; ς &sigmaf; &#962; σ &sigma; &#963; τ &tau; &#964;
υ &upsilon; &#965; φ &phi; &#966; χ &chi; &#967; ψ &psi; &#968; ω &omega; &#969;
? &thetasym; &#977; ? &upsih; &#978; ? &piv; &#982; &bull; &#8226; &hellip; &#8230;
&prime; &#8242; &Prime; &#8243; &oline; &#8254; &frasl; &#8260; &weierp; &#8472;
&image; &#8465; &real; &#8476; &trade; &#8482; &alefsym; &#8501; &larr; &#8592;
&uarr; &#8593; &rarr; &#8594; &darr; &#8595; &harr; &#8596; &crarr; &#8629;
&lArr; &#8656; &uArr; &#8657; &rArr; &#8658; &dArr; &#8659; &hArr; &#8660;
&forall; &#8704; &part; &#8706; &exist; &#8707; &empty; &#8709; &nabla; &#8711;
&isin; &#8712; &notin; &#8713; &ni; &#8715; &prod; &#8719; &sum; &#8721;
&minus; &#8722; &lowast; &#8727; &radic; &#8730; &prop; &#8733; &infin; &#8734;
&ang; &#8736; &and; &#8743; &or; &#8744; &cap; &#8745; &cup; &#8746;
&int; &#8747; &there4; &#8756; &sim; &#8764; &cong; &#8773; &asymp; &#8776;
&ne; &#8800; &equiv; &#8801; &le; &#8804; &ge; &#8805; &sub; &#8834;
&sup; &#8835; &nsub; &#8836; &sube; &#8838; &supe; &#8839; &oplus; &#8853;
&otimes; &#8855; &perp; &#8869; &sdot; &#8901; ? &lceil; &#8968; ? &rceil; &#8969;
? &lfloor; &#8970; ? &rfloor; &#8971; ? &lang; &#9001; ? &rang; &#9002; &loz; &#9674;
&spades; &#9824; &clubs; &#9827; &hearts; &#9829; &diams; &#9830;

用户数量的曲线显得、曲线用Applet展现的数目出自难题

第壹的国际标记 markup-significant and internationalization characters

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
" &quot; &#34; & &amp; &#38; < &lt; &#60; > &gt; &#62; Π&OElig; &#338;
œ &oelig; &#339; Š &Scaron; &#352; š &scaron; &#353; Ÿ &Yuml; &#376; ˆ &circ; &#710;
˜ &tilde; &#732; &ensp; &#8194; &emsp; &#8195; &thinsp; &#8201; &zwnj; &#8204;
&zwj; &#8205; &lrm; &#8206; &rlm; &#8207; &ndash; &#8211; &mdash; &#8212;
&lsquo; &#8216; &rsquo; &#8217; &sbquo; &#8218; &ldquo; &#8220; &rdquo; &#8221;
&bdquo; &#8222; &dagger; &#8224; &Dagger; &#8225; &permil; &#8240; &lsaquo; &#8249;
&rsaquo; &#8250; &euro; &#8364;

第六节 字符串的高级处理

JavaScript转义符

转义序列 字符
\b 退格
\f 走纸换页
\n 换行
\r 回车
\t 横向跳格 (Ctrl-I)
\’ 单引号
\" 双引号
\\ 反斜杠

编制程序的时候要专注特殊字符的难题,很多周转时出现的难题都以因为特殊字符的出现而引起的。

小心,由于反斜杠本人作为转义符,因而不可能直接在本子中键入2个反斜杠。假若要发出3个反斜杠,必须联合键入多少个反斜杠
(\\)。


TextLayout类、LineMetrics类

编码转换(to Unicode)

(程序代码来源于网络)

 

Js版

<script>
     test = "你好abc"
     str = ""
     for( i=0;    i<test.length; i++ )
     {
      temp = test.charCodeAt(i).toString(16);
      str    += "\\u"+ new Array(5-String(temp).length).join("0") +temp;
     }
     document.write (str)
</script>

vbs版

Function Unicode(str1)
     Dim str,temp
     str = ""
     For i=1    to len(str1)
      temp = Hex(AscW(Mid(str1,i,1)))
      If len(temp) < 5 Then    temp = right("0000" & temp, 4)
      str = str & "\u" & temp
     Next
     Unicode = str
End Function

Function htmlentities(str)
     For i = 1 to Len(str)
         char = mid(str, i, 1)
         If Ascw(char) > 128 then
             htmlentities = htmlentities & "&#" & Ascw(char) & ";"
         Else
             htmlentities = htmlentities & char
         End if
     Next
End Function

 

 

coldfusion版

 

function nochaoscode(str)
{
     var new_str = “”;
     for(i=1; i lte len(str);i=i+1){
         if(asc(mid(str,i,1)) lt 128){
             new_str = new_str & mid(str,i,1);
         }else{
             new_str = new_str & “&##” & asc(mid(str,i,1));
         }
     }
     return new_str;
}

 


 

附:

在php中大家能够用mbstring的mb_convert_encoding函数实现那么些正向及反向的转向。
如:

 

mb_convert_encoding (“你好”, “HTML-ENTITIES”, “gb2312”);
//输出:你好

mb_convert_encoding (“你好”, “gb2312”, “HTML-ENTITIES”);
//输出:你好

 

只要急需对任何页面转化,则只须要在php文件的头顶加上那叁行代码:

 

mb_internal_encoding(“gb231二”); // 那里的gb231贰是你网址原来的编码

mb_http_output(“HTML-ENTITIES”);

ob_start(‘mb_output_handler’);

出自:HTML转义字符: http://114.xixik.com/character/

连带:正则表明式: http://114.xixik.com/regex/

第4节 构造几何样子

二D几何样子的宏图、构造型区域几何样子、变换、缓冲的图像

第肆节 三个维度图形处理的筹划技术

透视投影、透视图形的彰显、隐蔽面解决难题

第十节 同条件相互

GraphicsEnvironment类、GraphicsDevice类、GraphicsConfiguration类

首先节 Java 贰D的进步成效

一、概述:

由Sun公司与Adobe系统公司同盟推出的Java 二D
API,提供了三个功用强大而且相当灵活的二维图形框架。Java 2D
API恢弘了java.awt包中定义的Graphics类和Image类,提供了高质量的二维图形、图像和文字,同时又保证了对现有AWT应用的合作。

2、AWT图形能力的供不应求:

在 AWT 的开头达成中,图形能力并不丰裕健全。因为支付 JDK
是打算将其看成平毕尔巴鄂立的落实平台,所以其本来的效益被限制于“最少公共职能”上,全体被协助的操作系统上确认保证提供那些公共职能;

在 Java 二D
出现在此之前,对绘制能力、字体操作和图像控制的扶助很是少。而对诸如用画图实行着色、形状操作以及图片变换之类的要紧操作的支撑则一心没有。

Java 2D 满意了跨平台完成中对那么些成效以及任何作用的急需。

三、Java 2D API:

它是JFC (Java Fundation Classes)的1员,加强了价值观AWT( Abstract
Windowing Toolkit )的刻画成效。在 JDK一.2中壹度匡助 Java 2D
的施用。透过Java 二D API
,程序员能够轻松地描绘出任意的几何图形、运用差别的填色效果、对图片做旋转(
rotate)、缩放( scale)、扭曲( shear)等。如图所示,程序员透过2D
API所提供的效劳,简单地使用不一致类别的线或然填色效果绘出计算图,以界别出差别的资料。

它们是根据Graphics贰D类的绘图功能,是对AWT中的Graphics类的愈益的扩张和抓牢。首要浮今后:

一。对渲染质量的支配:消除锯齿以平滑绘制对象的边缘

二.裁剪、合成和发光度:它们允许使用任意形状来限制绘制操作的疆界。它们还提供对图纸举行分层以及控制光滑度和不光滑度的能力。

叁.操纵和填充简单及复杂的形态:那种作用提供了1个 Stroke 代理和一个Paint
代理,前者定义用来绘制形状轮廓的笔(定义绘制的笔的大幅度和体裁),后者允许用纯色、渐变色和绘画来填充形状。

4。图像处理和转移:Java 2D 同 Java 高级图像 API(Java Advanced Imaging
API (JAI))同盟,援助用大方图片格式处理千丝万缕的图像。Java 贰D
还为您提供了修改图像、形状和字体字符的转移能力。

5。特殊的填写格局,如梯度或许图案

陆.高档字体处理和字符串格式化:允许象操作任何其余图形形状1样操作字体字符。除此以外,能够象文字处理程序①样,通过为
String 中的字符应用质量和样式消息来创建格式化文本。

java.awt.geom 包中的Areas类支援联合公司( union)、交集(
intersection)、差集(subtraction )、Exclusive OXC90(XO陆风X捌)等布尔运算。最後, AffineTransform
体系则提供图片物件做Scale(比例)、Shear(剪裁)
、Rotate(旋转)等座标上的转换。

其次节 图形绘制的主干措施

一、转换Graphics2D对象

制图图形时,能够在Graphics对象可能Graphics二D对象上开始展览,它们都意味着了亟需绘图的区域,选用那三个取决于是不是要运用所充实的Java二D的图样作用。但要注意的是,全体的Java二D图形操作都必须在Graphics2D对象上调用。Graphics二D是Graphics的子类,同样富含在java.awt包中。

public void paintComponent(Graphics comp)

{ Graphics2D comp2D=(Graphics2D)comp;

}

或者

public void paint (Graphics comp)

{ Graphics2D comp2D=(Graphics2D)comp;

}

二、Graphics 类特性

Graphics 类协理三种分明图形环情的个性。以下列出了有些风味:

1)Color:当前绘制颜色,它属于 java.awt.Color
类型。全数的绘图、着色和纯文本输出都将以内定的颜料显示。

2)Font:当前字体,它属于 java.awt.Font
类型。它是将用来全体纯文本输出的书体。

三)Clip:java.awt.Shape
类型的对象,它充当用来定义几何样子的接口。该性子包涵的形制定义了图片环境的区域,绘制将作用于该区域。常常状态下,那壹形象与总体图形环境一致,但也并不一定如此。

四)ClipBounds:java.awt.Rectangle 对象,它代表将包围由 Clip 天性定义的
Shape 的极小矩形。它是只读性格。

伍)FontMetrics:java.awt.FontMetrics
类型的只读天性。该对象涵盖关于图形环境中当前起效果的 Font
的音讯。就如大家将见到的那样,获取此音讯的那种机制已被 LineMetrics
类所取代

陆)Paint Mode:该特性决定环境使用当前颜色的不二等秘书籍。如若调用了
setPaintMode() 方法,那么具有绘制操作都将使用当前颜色。如若调用了
setXO大切诺基Mode() 方法(该办法获得二个 Color
类型的参数),那么就用钦赐的颜色对像素做“XO福睿斯”操作。XO奥迪Q三具有在重复绘制时回涨早先位情势的风味,因而它被看作橡皮擦除和卡通操作。

三、绘图的性质和大旨编制程序方法

一)颜色Color类:没有变动。

二)填充格局:

Paint(油漆桶)
接口有几个实际的实现,它们允许用纯色、渐变色或图案来填充形状。

一,纯色填充(Color类):对 java.awt.Color 类做了有的调整以贯彻
Paint,并且能够用于纯色填充。

二,渐变色来填充(梯度填充GradientPaint类):java.awt.GradientPaint
类允许用线性颜色渐变色来填充形状,线性颜色渐变色允许在五个内定的 Color
对象之间创立过渡。能够将渐变色设置成“周期性的”,那将促成渐变色图案重复出现。

三,图案填充(纹理TexturePaint类):提供了 java.awt.TexturePaint
类,它能够用由 BufferedImage 描述的美术填充形状

编制程序方法:

应用Graphics2D类中的setPaint()方法并利用Paint对象作为其参数,但由于别的能够看成填充的类如GradientPaint、TexturePaint和Color都完结了Paint接口(该接口注意定义了在Graphics二D下的水彩填充情势),因而能够将它们当做参数。如:

public void paintComponent(Graphics comp)

{ Graphics2D comp2D=(Graphics2D)comp;

GradientPaint pat=new
GradientPaint(0f,0f,Color.white,100f,45f,Color.blue);

comp2D.setPaint(pat);

}

三)设置笔的形状:

Stroke 接口由 java.awt.BasicStroke
类达成。该类允许实行多量的选取以修改线的绘图细节。可以编制程序内定BasicStroke
宽度,也得以钦命对名叫支柱和交点的途径上端点和交点的“装饰”。今后也足以绘制点划线了,只须安装
BasicStroke 的破折号属性即可。

在Graphics类中线条是一个点宽,而在Graphics二D中得以因此BasicStoke类中的setStroke()方法来安装。其构造函数是BasicStroke(float
width, int cap, int join)

里面width提示线宽(缺省时为1.0)

cap提醒线的前边(大梁,在BasicStroke类中定义出多个static
类型的常量如CAP_BUTT没有常德( )、CAP_ROUND圆包头(
)、CAP_SQUARE方包头( )的样式

join提示线段之间的拐角(在BasicStroke类中定义出多少个static
类型的常量如JOIN_BEVEL( )、JOIN_MITER( )、 JOIN_ROUND(
)样式。

四)编制程序方法:

public void paintComponent(Graphics comp)

{ Graphics2D comp2D=(Graphics2D)comp;

BasicStroke pen

=new BasicStroke(2.0f, BasicStroke .CAP_BUTT, BasicStroke
.JOIN_ROUND);

comp2D.setStroke (pen);

}

代码示例:

float thick = 0.5f; //设置画刷的粗细为 0.5

BufferedImage bi = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);

Graphics2D g = (Graphics2D)bi.getGraphics();

Stroke stroke = g.getStroke(); //获得当前的画刷

g.setStroke(new BasicStroke(thick, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND));

g.draw(new Line2D.Float(x1, y1, x2, y2)); 画线

g.setStroke( stroke ); //将画刷复原

5)创立要绘制的造型对象

在Java二D中展开绘图时,不是采用对应的章程来兑现,而是为要兑现某中造型成立出相应的造型对象。那能够通过应用java.awt.geom包中的类来定义所要创制的模样。如线条Line2D.Float类、距形Rectangle二D.Float或许Rectangle二D.Double类、椭圆Ellipes二D.Float、圆弧Arc二D.Float类等。

陆)绘制对象:

一,能够应用Graphics二D类中的方法draw()用于绘制轮廓,而fill()方法用于填充。它们都是前面所创制的图纸对象作为参数。

贰,Java二D中的字符串的绘图如故选取drawString()方法,但有drawString(String
s, float x, float y)和drawString(String str, int x, int y)。

3,绘制概况:draw(Shape s)个中的Shape接口在Graphics二D中被定义

新的 Java 二D Shape
类都有“二D”后缀。这一个新的样子使用浮点值(而不是整数)来叙述其几何样子。

Polygon类(int[] xpoints, int[] ypoints, int npoints)

RectangularShape(抽象类,其子类有Arc二D, Ellipse2D, Rectangle2D,
RoundRectangle2D), Rectangle(距形)

QuadCurve二D(三次贝塞尔样条曲线,贝塞尔曲线由八个端点以及叁个或多少个控制点钦赐。贝塞尔曲线创设了符合于多数表示的曲线。)

CubicCurve2D(3回贝塞尔样条曲线)

Area(区域)

GeneralPath(由直线、一遍样条曲线、1次样条曲线所结合)

Line2D

8)基本步骤

绘制的首先个步骤是产生 Graphics2D 对象。
然后设定所要的情事属性。例如你想要对1物件做渐层式的填色,能够设定属性
Paint为
GradientPaint。最後再调用Graphics二D所提供的办法fill或是draw,实现全部绘图的先后。

玖)程序实例

例一:

这是二个最简易的例子,也得以认为是绘图的三个最简便的框架。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class Map extends JFrame

{ public Map()

{ super(“Map”);

setSize(350,350);

MapPane map=new MapPane();

getContentPane().add(map);

}

public static void main(String [] arg)

{ Map frame=new Map();

frame.show();

}

}

class MapPane extends JPanel

{ public void paintComponent(Graphics comp)

{ Graphics2D comp2D=(Graphics2D)comp;

comp2D.drawString(“sbcd”,200,200);

Line2D.Float line=new Line2D.Float(1.0f,2.0f,200.0f,200.0f);

comp2D.draw(line);

}

}

例二:

上面是在Graphics二D情势下的骨干绘图框架。大家得以看到,利用:

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

因此该形式的安装,使图形去除锯齿状,能够取得多么细腻的图样。

样例:

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

public class DrawDemo extends JFrame {

public DrawDemo(){

//设置窗口的轻重、标题

this.setSize(new Dimension(600, 400));

//创立绘制各样形态的器皿

ShapesPanel shapesPanel = new ShapesPanel();

//将该容器插足窗口

getContentPane().add(shapesPanel, BorderLayout.CENTER);

}

public static void main(String[] args) {

DrawDemo frame = new DrawDemo();

frame.setVisible(true);

//当窗口关闭时清空内存

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

}

//创设种种容器的类

class ShapesPanel extends JPanel {

final int maxCharHeight = 15;

final Color bg = Color.white; //证明背景颜色为黑色

final Color fg = Color.blue; //注脚前景颜色为墨玉绿

public ShapesPanel() {

setBackground(bg); //设置背景颜色

setForeground(fg); //设置前景颜色

//创立组合边框

setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createRaisedBevelBorder(),

BorderFactory.createLoweredBevelBorder()));

}

public void paintComponent(Graphics g1) {

super.paintComponent(g一); //清空背景颜色

float thick = 0.伍f; //设置画刷的粗细为 0.伍

Graphics2D g = (Graphics2D)g1;

Stroke stroke = g.getStroke(); //得到当前的画刷

g.setStroke(new BasicStroke(thick, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND));

//通过该方法使图形去除锯齿状

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

String txt= “小编的篇章”;

int style=2;//0普通1粗体2斜体3粗斜

g.setFont(new Font(“楷体”, 贰, 1伍)); //一5为字大小

//设置笔刷为驼色

g.setPaint(Color.black);

g.drawString(txt,200,150);

g.setPaint(Color.red);

g.draw(new Line2D.Float(0,0,200,150)); //画线

g.setPaint(Color.blue);

g.draw(new Rectangle2D.Float(200,150,100,100));

g.setStroke(stroke); //将画刷复原

}

}

例3:图形灵活的彰显

在地点的事例中,是行使3个继续于JPanel的类,覆盖它的void
paintComponent(Graphics
g一)事件措施,实现绘图的,那样固然便宜,但出示灵活性不高。

骨子里,通过JPanel对象直接赋值给Graphics二D对象,往往能够使程序有所十分大的灵活性。程序能够如此来写。

JPanel contentPane= (JPanel) this.getContentPane();

public Graphics2D comp2D=(Graphics2D)contentPane .getGraphics();

样例:

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawDemo1 extends JFrame

{

public JPanel contentPane; //绘图窗口

public Graphics二D comp二D; //绘图对象

JPanel jPanel一 = new JPanel();//控件容器

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

//构造函数

public DrawDemo1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//控件的初步化

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setSize(new Dimension(400, 300));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

jPanel1.setLayout(null);

jButton1.setBounds(new Rectangle(30, 235, 100, 31));

jButton1.setText(“画线保留”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(150, 235, 100, 30));

jButton二.setText(“画线删除”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

jPanel1.add(jButton1, null);

jPanel1.add(jButton2, null);

}

public static void main(String[] args) {

DrawDemo1 frame=new DrawDemo1();

frame.show();

frame.comp2D=(Graphics2D)frame.contentPane .getGraphics();

frame.comp2D.setBackground(Color.white);

frame.comp2D.clearRect(0,0,401,221);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

void jButton1_actionPerformed(ActionEvent e){

comp2D.setPaint(Color.red);

Line2D.Float line=new Line2D.Float(1.0f,2.0f,200.0f,220.0f);

comp2D.draw(line);

}

void jButton2_actionPerformed(ActionEvent e) {

comp2D.clearRect(0,0,401,221);

comp2D.setPaint(Color.blue);

Line2D.Float line=new Line2D.Float(1.0f,100.0f,300.0f,220.0f);

comp2D.draw(line);

}

}

例四:剪裁

图形处理难点中,剪裁由的时候是相当难处理的,所谓剪裁是指超越绘图取得内容不展现,看起来那是个简易难题,但鉴于全部的线条必须总计与境界的交点,而且边界有多个趋势,那就使难点变得很复杂。java
2D很好的解决了这一个难题,请看下边包车型地铁例证。

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawDemo2 extends JFrame

{

public JPanel contentPane; //绘图窗口

public Graphics2D comp二D; //绘图对象

JPanel jPanel壹 = new JPanel();//控件容器

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

//构造函数

public DrawDemo2() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//控件开首化

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setSize(new Dimension(400, 300));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

jPanel1.setLayout(null);

jButton1.setBounds(new Rectangle(30, 235, 100, 31));

jButton1.setText(“画线保留”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(150, 235, 100, 30));

jButton2.setText(“画线删除”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

jPanel1.add(jButton1, null);

jPanel1.add(jButton2, null);

}

public static void main(String[] args) {

DrawDemo2 frame=new DrawDemo2();

frame.show();

frame.comp2D=(Graphics2D)frame.contentPane .getGraphics();

frame.comp2D.setBackground(Color.white);

frame.comp2D.clearRect(0,0,401,221);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

void jButton1_actionPerformed(ActionEvent e){

comp2D.setPaint(Color.red);

Line2D.Float line=new Line2D.Float(1.0f,2.0f,200.0f,220.0f);

comp2D.draw(line);

}

void jButton2_actionPerformed(ActionEvent e) {

comp2D.clearRect(0,0,401,221);

//剪裁

comp2D.setClip(50,50,300,150);

comp2D.setPaint(Color.blue);

Line2D.Float line=new Line2D.Float(1.0f,100.0f,300.0f,220.0f);

comp2D.draw(line);

}

}

其叁节 曲线难点的高级应用开发

在jdk尚未支援 二D图形从前,只好够画出直的、相同粗细的线条。以往得以由此二D
API绘出分裂粗细的线条及圆滑的曲线。在java.awt.geom包中提供了Line二D、
QuadCurve贰D(3回贝塞尔曲线)及
CubicCurve贰D(三回贝塞尔曲线)等连锁的类,让程序员能够轻松地绘出想要的线条。

实际绘图的着力是画线,下边通过有个别实例对有的难点展开深刻的议论。

壹、直线难题尖锐钻探

样例:

作者们透过一个例证,深切的商讨一下绘制直线和折线必要领悟怎么着内容。

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawDemo1 extends JFrame

{

public JPanel contentPane; //绘图窗口

public Graphics二D comp2D; //绘图对象

JPanel jPanel一 = new JPanel();//控件容器

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

JButton jButton3 = new JButton();

//构造函数

public DrawDemo1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//控件开头化

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setSize(new Dimension(400, 300));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

jPanel1.setLayout(null);

jButton1.setBounds(new Rectangle(30, 235, 100, 31));

jButton1.setText(“园头”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(150, 235, 100, 30));

jButton2.setText(“方头”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

jButton3.setBounds(new Rectangle(270, 235, 100, 30));

jButton3.setText(“封闭”);

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton3_actionPerformed(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

jPanel1.add(jButton1, null);

jPanel1.add(jButton2, null);

jPanel1.add(jButton3, null);

}

public static void main(String[] args) {

DrawDemo1 frame=new DrawDemo1();

frame.show();

frame.comp2D=(Graphics2D)frame.contentPane .getGraphics();

frame.comp2D.setBackground(Color.white);

frame.comp2D.clearRect(0,0,401,221);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

int Xs1[]={10,60,120,200,260,340};

int Ys1[]={10,200,120,180,60,130};

void jButton1_actionPerformed(ActionEvent e){

comp2D.clearRect(0,0,401,221);

//笔宽度

float thick = 10f;

//设置笔刷

//园头园连接

comp2D.setStroke(new BasicStroke(thick,

BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

comp2D.setPaint(Color.red);

//通过该措施使图形去除锯齿状

comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

comp2D.drawPolyline(Xs1,Ys1,Xs1.length);

}

void jButton2_actionPerformed(ActionEvent e) {

comp2D.clearRect(0,0,401,221);

//笔宽度

float thick = 10f;

//设置笔刷

//方头方连接

comp2D.setStroke(new BasicStroke(thick,

BasicStroke.CAP_SQUARE, BasicStroke.CAP_SQUARE));

comp2D.setPaint(Color.blue);

//通过该方式使图形去除锯齿状

//comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

// RenderingHints.VALUE_ANTIALIAS_ON);

comp2D.drawPolyline(Xs1,Ys1,Xs1.length);

}

void jButton3_actionPerformed(ActionEvent e) {

comp2D.clearRect(0,0,401,221);

//笔宽度

float thick = 1f;

//设置笔刷

//方头方连接

comp2D.setStroke(new BasicStroke(thick,

BasicStroke.CAP_SQUARE, BasicStroke.CAP_SQUARE));

comp2D.setPaint(Color.blue);

//通过该办法使图形去除锯齿状

//comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

// RenderingHints.VALUE_ANTIALIAS_ON);

//画封闭线

comp2D.drawPolygon(Xs1,Ys1,Xs1.length);

}

}

二、贝塞尔(Bezier)曲线

java 2D提供的QuadCurve2D(二遍贝塞尔曲线)及
CubicCurve二D(一回贝塞尔曲线)等皮之不存毛将焉附的类,能够很简单的画出贝赛尔曲线。

QuadCurve二D为四个数据,中间二个为控制点。

CubicCurve贰D为多少个数据,中间多少个为控制点。

样例:

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawDemo1 extends JFrame

{

public JPanel contentPane; //绘图窗口

public Graphics二D comp2D; //绘图对象

JPanel jPanel壹 = new JPanel();//控件容器

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

//构造函数

public DrawDemo1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//控件开头化

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setSize(new Dimension(400, 300));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

jPanel1.setLayout(null);

jButton1.setBounds(new Rectangle(30, 235, 150, 31));

jButton一.setText(“二阶贝塞尔”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(200, 235, 150, 30));

jButton2.setText(“3阶贝塞尔”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

jPanel1.add(jButton1, null);

jPanel1.add(jButton2, null);

}

public static void main(String[] args) {

DrawDemo1 frame=new DrawDemo1();

frame.show();

frame.comp2D=(Graphics2D)frame.contentPane .getGraphics();

frame.comp2D.setBackground(Color.white);

frame.comp2D.clearRect(0,0,401,221);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

void jButton1_actionPerformed(ActionEvent e){

double[] x1={50,180,300};

double[] y1={100,190,100};

comp2D.clearRect(0,0,401,221);

//笔宽度

float thick = 1f;

comp2D.setPaint(Color.red);

QuadCurve2D.Double qc=new QuadCurve2D.Double();

qc.setCurve(x1[0],y1[0],x1[1],y1[1],x1[2],y1[2]);

comp2D.draw(qc);

comp2D.drawLine((int)x1[1]-5,(int)y1[1],(int)x1[1]+5,(int)y1[1]);

comp2D.drawLine((int)x1[1],(int)y1[1]-5,(int)x1[1],(int)y1[1]+5);

comp2D.setPaint(Color.blue);

x1[1]=180;

y1[1]=30;

qc.setCurve(x1[0],y1[0],x1[1],y1[1],x1[2],y1[2]);

comp2D.draw(qc);

comp2D.drawLine((int)x1[1]-5,(int)y1[1],(int)x1[1]+5,(int)y1[1]);

comp2D.drawLine((int)x1[1],(int)y1[1]-5,(int)x1[1],(int)y1[1]+5);

}

void jButton2_actionPerformed(ActionEvent e) {

double[] x1={50,80,200,300};

double[] y1={100,70,190,100};

comp2D.clearRect(0,0,401,221);

//笔宽度

float thick = 1f;

comp2D.setPaint(Color.red);

CubicCurve2D.Double qc=new CubicCurve2D.Double();

qc.setCurve(x1[0],y1[0],x1[1],y1[1],x1[2],y1[2],x1[3],y1[3]);

comp2D.draw(qc);

comp2D.drawLine((int)x1[1]-5,(int)y1[1],(int)x1[1]+5,(int)y1[1]);

comp2D.drawLine((int)x1[1],(int)y1[1]-5,(int)x1[1],(int)y1[1]+5);

comp2D.drawLine((int)x1[2]-5,(int)y1[2],(int)x1[2]+5,(int)y1[2]);

comp2D.drawLine((int)x1[2],(int)y1[2]-5,(int)x1[2],(int)y1[2]+5);

float dash1[] = {10.0f};

//画虚线

BasicStroke dashed = new BasicStroke(1.0f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_MITER,

10.0f, dash1, 0.0f);

comp2D.setStroke(dashed);

comp2D.setPaint(Color.darkGray);

comp2D.drawLine((int)x1[1],(int)y1[1],(int)x1[2],(int)y1[2]);

//画实线

BasicStroke stroke = new BasicStroke(1.0f);

comp2D.setStroke(stroke);

comp2D.setPaint(Color.blue);

x1[1]=180;

y1[1]=70;

x1[2]=80;

y1[2]=190;

qc.setCurve(x1[0],y1[0],x1[1],y1[1],x1[2],y1[2],x1[3],y1[3]);

comp2D.draw(qc);

comp2D.drawLine((int)x1[1]-5,(int)y1[1],(int)x1[1]+5,(int)y1[1]);

comp2D.drawLine((int)x1[1],(int)y1[1]-5,(int)x1[1],(int)y1[1]+5);

comp2D.drawLine((int)x1[2]-5,(int)y1[2],(int)x1[2]+5,(int)y1[2]);

comp2D.drawLine((int)x1[2],(int)y1[2]-5,(int)x1[2],(int)y1[2]+5);

comp2D.setStroke(dashed);

comp2D.setPaint(Color.darkGray);

comp2D.drawLine((int)x1[1],(int)y1[1],(int)x1[2],(int)y1[2]);

comp2D.setStroke(stroke);

}

}

三、自定义样条曲线编制程序

当我们须要平滑多个数据样本点的时候,贝塞尔曲线就无法知足须要了,为此,能够选拔最早由United States“波音”飞机创造集团提议来的样条曲线来成功,那种曲线所以称之为样条,是因为它模拟了造船业中的放样原理。

样条曲线的数学原理请参见总括机图形学,那里给出的是由java编写的一回样条曲线的事例。曲线平滑的准绳是,必须经过全部的样本点,其它,不论有些许样本点,曲线的阶次最大为贰遍,所以是安静的。

这边大家能够看出来,程序设计的生命是何等呢?数学!那是程序设计着的性命所在。语言只是壹种标准依然是贰个工具,要实在写出好的次序,没有牢固的数学功底,是纯属不容许的。

样例:

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

public class DrawCurve extends JFrame

{

public JPanel contentPane; //绘图窗口

JPanel jPanel1 = new JPanel();//控件容器

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

JButton jButton3 = new JButton();

GraphicsCurve gracu;

//构造函数

public DrawCurve() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//控件伊始化

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setSize(new Dimension(500,400));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

jPanel1.setLayout(null);

jButton1.setBounds(new Rectangle(30, 310, 100, 31));

jButton1.setText(“直线”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(150, 310, 100, 30));

jButton贰.setText(“样条曲线”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

jButton3.setBounds(new Rectangle(270, 310, 100, 30));

jButton3.setText(“粗线条”);

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton3_actionPerformed(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

jPanel1.add(jButton1, null);

jPanel1.add(jButton2, null);

jPanel1.add(jButton3, null);

gracu=new GraphicsCurve();

}

public static void main(String[] args) {

DrawCurve frame=new DrawCurve();

frame.show();

frame.gracu.myGraphics=(Graphics2D)frame.contentPane .getGraphics();

frame.gracu.myGraphics.setBackground(Color.white);

frame.gracu.myGraphics.clearRect(0,0,500,300);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

int Xs1[]={10,60,120,200,260,340};

int Ys1[]={10,200,120,180,60,130};

//画折线

void jButton1_actionPerformed(ActionEvent e){

gracu.myGraphics.setPaint(Color.blue);

gracu.myGraphics.drawPolyline(Xs1,Ys1,Xs1.length);

}

//画样条

void jButton2_actionPerformed(ActionEvent e) {

gracu.myGraphics.setPaint(Color.red);

gracu.DrawCurves(Xs1,Ys1);

}

//画粗线

void jButton3_actionPerformed(ActionEvent e) {

//笔宽度

float thick = 10f;

//设置笔刷

//方头园连接

//gracu.myGraphics.setStroke(new BasicStroke(thick,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

//园头园连接

gracu.myGraphics.setStroke(new BasicStroke(thick,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

gracu.myGraphics.setPaint(Color.blue);

//通过该措施使图形去除锯齿状

gracu.myGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

gracu.myGraphics.drawPolyline(Xs1,Ys1,Xs1.length);

}

}

class GraphicsCurve

{

//绘图对象

public Graphics2D myGraphics;

public GraphicsCurve()

{

}

public GraphicsCurve(Graphics2D graphics)

{

this.myGraphics=graphics;

}

//参数表

//x数组,y数组,笔刷

public void DrawCurves(int[] xa,int[] ya)

{

int[] x, y;

double[] a, b, c;

double[] px, py, qx, qy, tt;

double[] dx, dy;

int px1,py1,px2,py2;

x=xa;

y=ya;

px1=x[0];

py1=y[0];

int n=x.length;

a=new double[n];

b=new double[n];

c=new double[n];

px=new double[n];

py=new double[n];

qx=new double[n];

qy=new double[n];

tt=new double[n];

dx=new double[n];

dy=new double[n];

int i, t, es;

double bx3, bx4, by3, by4, cx, cy;

bx4 = 0;

by3 = 0;

es = 3;

px[0] = 1;

py[0] = 1;

px[n-1] = 1;

py[n-1] = 1;

if (n>1)

{

for (i = 1;i<n;i++)

tt[i] = Math.sqrt((x[i] – x[i – 1]) * (x[i] – x[i – 1]) +
(y[i] – y[i – 1]) * (y[i] – y[i – 1]));

switch(n)

{

case 2:

break;

case 3:

for (i = 1;i<n – 1;i++)

{

a[i] = 2 * (tt[i] + tt[i + 1]);

b[i] = tt[i + 1];

c[i] = tt[i];

dx[i] = 3 * (tt[i] * (x[i + 1] – x[i]) / tt[i + 1] + tt[i +
1] * (x[i] – x[i – 1]) / tt[i]);

dy[i] = 3 * (tt[i] * (y[i + 1] – y[i]) / tt[i + 1] + tt[i +
1] * (y[i] – y[i – 1]) / tt[i]);

}

dx[1] = dx[1] – tt[2] * px[0];

dx[n – 2] = dx[n – 2] – tt[n – 2] * px[n-1];

dy[1] = dy[1] – tt[2] * py[0];

dy[n – 2] = dy[n – 2] – tt[n – 2] * py[n-1];

//注意,那是n=三的情景专有总结

px[1] = dx[1] / a[1];

py[1] = dy[1] / a[1];

break;

default:

for (i = 1;i<n – 1;i++)

{

a[i] = 2 * (tt[i] + tt[i + 1]);

b[i] = tt[i + 1];

c[i] = tt[i];

dx[i] = 3 * (tt[i] * (x[i + 1] – x[i]) / tt[i + 1] + tt[i +
1] * (x[i] – x[i – 1]) / tt[i]);

dy[i] = 3 * (tt[i] * (y[i + 1] – y[i]) / tt[i + 1] + tt[i +
1] * (y[i] – y[i – 1]) / tt[i]);

}

dx[1] = dx[1] – tt[2] * px[0];

dx[n – 2] = dx[n – 2] – tt[n – 2] * px[n-1];

dy[1] = dy[1] – tt[2] * py[0];

dy[n – 2] = dy[n – 2] – tt[n – 2] * py[n-1];

c[1] = c[1]/ a[1];

for (i = 2 ;i< n – 1;i++)

{

a[i] = a[i] – b[i] * c[i – 1];

c[i] = c[i] / a[i];

}

qx[1] = dx[1] / a[1];

qy[1] = dy[1] / a[1];

for (i = 2 ;i< n – 1;i++)

{

qx[i] = (dx[i] – b[i] * qx[i – 1]) / a[i];

qy[i] = (dy[i] – b[i] * qy[i – 1]) / a[i];

}

px[n – 2] = qx[n – 2];

py[n – 2] = qy[n – 2];

for (i = n – 3;i>=1;i–)

{

px[i] = qx[i] – c[i] * px[i + 1];

py[i] = qy[i] – c[i] * py[i + 1];

}

break;

}

for (i = 0 ;i< n – 1;i++)

{

bx3 = (3 * (x[i + 1] – x[i]) / tt[i + 1] – 2 * px[i] – px[i +
1]) / tt[i + 1];

bx4 = ((2 * (x[i] – x[i + 1]) / tt[i + 1] + px[i] + px[i +
1]) / tt[i + 1]) / tt[i + 1];

by3 = (3 * (y[i + 1] – y[i]) / tt[i + 1] – 2 * py[i] – py[i +
1]) / tt[i + 1];

by4 = ((2 * (y[i] – y[i + 1]) / tt[i + 1] + py[i] + py[i +
1]) / tt[i + 1]) / tt[i + 1];

t = 0;

while (t < tt[i + 1])

{

t = t + es;

cx = x[i] + (px[i] + (bx3 + bx4 * t) * t) * t;

cy = y[i] + (py[i] + (by3 + by4 * t) * t) * t;

px2 = (int)cx;

py2 = (int)cy;

myGraphics.drawLine(px1,py1,px2,py2);

px1 = px2;

py1 = py2;

}

}

}

}

}

4、用户数据的曲线显得

当供给用曲线表明数据的时候,大家纵然能够利用由厂家提供的“图表”组件,但更加多的要么须求自个儿编排的,请仔细研讨下边包车型客车顺序,当对图纸设计有更透彻的了然。

上面包车型地铁例证,大家组织3个特地处理用户数量的类,在那么些类里,完成了用户坐标和显示屏坐标的转移,同时提交了多少个电动绘制坐标的法子。我们也间接运用了地点大家斟酌过的样条曲线的类实现多少平滑。例子中的数据可以来自于数据库或任何任哪个地点方,仔细的商讨这些事例,能够见见只要大家开动脑筋,把java强大的效率和大家对难题的掌握结合在共同,就可以写出多么灵活多变的顺序来啊!

样例:

package myDrawDemo;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.geom.*;

public class DataDrawDemo extends JFrame

{

public JPanel contentPane; //控件容器

JPanel jPanel一 = new JPanel();//绘图控件

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

JButton jButton3 = new JButton();

JButton jButton4 = new JButton();

JButton jButton5 = new JButton();

JButton jButton6 = new JButton();

JButton jButton7 = new JButton();

JButton jButton8 = new JButton();

JButton jButton9 = new JButton();

JButton jButton10 = new JButton();

JTextField jText1=new JTextField();

JTextField jText2=new JTextField();

boolean kcu=true;

//用户坐标转换对象

myGraphicsData mp=new myGraphicsData();

//曲线转换对象

GraphicsCurve gracu=new GraphicsCurve();

//窗口范围

double wx1,wx2,wy1,wy2;

//构造函数

public DataDrawDemo() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//伊始化代码

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(null);

this.setSize(new Dimension(650, 500));

this.setTitle(“Frame Title”);

//contentPane.setSize(400,240);

//jPanel1.setLayout(null);

jPanel1.setBounds(0,90,650,420);

jButton1.setBounds(new Rectangle(30, 20, 80, 25));

jButton1.setText(“开始”);

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

jButton2.setBounds(new Rectangle(120, 20, 80, 25));

jButton2.setText(“左移”);

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton2_actionPerformed(e);

}

});

jButton3.setBounds(new Rectangle(210, 20, 80, 25));

jButton3.setText(“右移”);

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton3_actionPerformed(e);

}

});

jButton4.setBounds(new Rectangle(300, 20, 80, 25));

jButton4.setText(“上移”);

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton4_actionPerformed(e);

}

});

jButton5.setBounds(new Rectangle(390, 20, 80, 25));

jButton5.setText(“下移”);

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton5_actionPerformed(e);

}

});

jButton6.setBounds(new Rectangle(120, 50, 80, 25));

jButton6.setText(“X扩”);

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton6_actionPerformed(e);

}

});

jButton7.setBounds(new Rectangle(210, 50, 80, 25));

jButton7.setText(“X缩”);

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton7_actionPerformed(e);

}

});

jButton8.setBounds(new Rectangle(300, 50, 80, 25));

jButton8.setText(“Y扩”);

jButton8.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton8_actionPerformed(e);

}

});

jButton9.setBounds(new Rectangle(390, 50, 80, 25));

jButton9.setText(“Y缩”);

jButton9.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton9_actionPerformed(e);

}

});

//样条控制

jButton10.setBounds(new Rectangle(30, 50, 80, 25));

jButton10.setText(“样条”);

jButton10.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton10_actionPerformed(e);

}

});

jText1.setBounds(new Rectangle(490, 20, 120, 20));

jText1.setText(“”);

jText2.setBounds(new Rectangle(490, 50, 120, 20));

jText2.setText(“”);

//鼠标按下侦听器

jPanel1.addMouseListener(new java.awt.event.MouseAdapter(){

public void mousePressed(MouseEvent e) {

JPanel_mousePressed(e);

}

});

//鼠标拖动侦听器

jPanel1.addMouseMotionListener(new
java.awt.event.MouseMotionAdapter(){

public void mouseDragged(MouseEvent e){

JPanel_mouseDragged(e);

}

});

//鼠标释放侦听器

jPanel1.addMouseListener(new java.awt.event.MouseAdapter(){

public void mouseReleased(MouseEvent e) {

JPanel_mouseReleased(e);

}

});

contentPane.add(jPanel1, BorderLayout.CENTER);

contentPane.add(jButton1, null);

contentPane.add(jButton2, null);

contentPane.add(jButton3, null);

contentPane.add(jButton4, null);

contentPane.add(jButton5, null);

contentPane.add(jButton6, null);

contentPane.add(jButton7, null);

contentPane.add(jButton8, null);

contentPane.add(jButton9, null);

contentPane.add(jButton10, null);

contentPane.add(jText1, null);

contentPane.add(jText2, null);

}

public static void main(String[] args) {

DataDrawDemo frame=new DataDrawDemo();

frame.show();

frame.gracu.myGraphics=(Graphics2D)frame.jPanel1.getGraphics();

frame.mp.myGraphics=(Graphics2D)frame.jPanel1.getGraphics();

frame.mp.myGraphics.setBackground(Color.white);

frame.mp.myGraphics.clearRect(0,0,650,375);

}

//第一组数据

double[] Xs1=new double[]{-2,2,4,6,8,10,12,14};

double[] Ys1=new double[]{-4,10,3,14,4,10,6,7};

//第三组数据

double[] Xs2=new double[]{-1,2,4,6,8,10,12,14,16};

double[] Ys2=new double[]{1,5,7,1,13,11,4,10,8};

//这是三个画曲线的次序

void DwData()

{

//剪裁,能够尝试没有剪裁是如何表现?

mp.myGraphics.clipRect(10,10,621,351);

//由于是用七个对象绘图,所以应该分别剪切

gracu.myGraphics.clipRect(10,10,621,351);

//清除绘图空间

mp.myGraphics.clearRect(0,0,650,375);

//用户坐标和显示器坐标转换

mp.truemode(10,630,10,360,wx1,wx2,wy1,wy2);

//设置颜色

mp.myGraphics.setPaint(Color.darkGray);

//画边框

mp.myGraphics.drawRect(10,10,620,350);

mp.myGraphics.setFont(new Font(“小篆”, 0, 10)); //玖为字大小

//画坐标

mp.axis(2,2,2,2);

//画红线

int[] x=new int[Xs1.length];

int[] y=new int[Ys1.length];

int[] myxy;

//统一完毕坐标转换

for (int i=0;i<x.length;i++)

{

myxy=mp.moxy(Xs1[i],Ys1[i]);

x[i]=myxy[0];

y[i]=myxy[1];

}

if (kcu)

{

mp.myGraphics.setPaint(Color.red);

mp.myGraphics.drawPolyline(x,y,x.length);

}

else

{

gracu.myGraphics.setPaint(Color.red);

gracu.DrawCurves(x,y);

}

//画蓝线

x=new int[Xs2.length];

y=new int[Ys2.length];

for (int i=0;i<x.length;i++)

{

myxy=mp.moxy(Xs2[i],Ys2[i]);

x[i]=myxy[0];

y[i]=myxy[1];

}

if (kcu)

{

mp.myGraphics.setPaint(Color.blue);

mp.myGraphics.drawPolyline(x,y,x.length);

}

else

{

gracu.myGraphics.setPaint(Color.blue);

gracu.DrawCurves(x,y);

}

}

//退出窗口事件

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

//鼠标按下事件

void JPanel_mousePressed(MouseEvent e)

{

double[] zs=mp.ScrtoCon(e.getX(),e.getY());

jText1.setText(String.valueOf((float)zs[0]));

jText2.setText(String.valueOf((float)zs[1]));

}

//鼠标释放事件

void JPanel_mouseReleased(MouseEvent e)

{

double[] zs=mp.ScrtoCon(e.getX(),e.getY());

jText1.setText(“”);

jText2.setText(“”);

//画红线

mp.myGraphics.setPaint(Color.darkGray);

mp.myGraphics.drawLine(e.getX()-3,e.getY(),e.getX()+3,e.getY());

mp.myGraphics.drawLine(e.getX(),e.getY()-3,e.getX(),e.getY()+3);

mp.myGraphics.drawString(String.valueOf((float)zs[0]),e.getX()+10,e.getY());

mp.myGraphics.drawString(String.valueOf((float)zs[1]),e.getX()+10,e.getY()+12);

}

//鼠标拖动事件

void JPanel_mouseDragged(MouseEvent e)

{

double[] zs=mp.ScrtoCon(e.getX(),e.getY());

jText1.setText(String.valueOf((float)zs[0]));

jText2.setText(String.valueOf((float)zs[1]));

}

//开始

void jButton1_actionPerformed(ActionEvent e){

wx1=1000000;

wy1=1000000;

wx2=-1000000;

wy2=-1000000;

//试验中数量来源于贰个数组,实际中可来自别的地点

//设置开始范围

for (int i=0;i<Xs1.length;i++)

{

if (Xs1[i]< wx1)

wx1=Xs1[i];

if (Xs1[i]> wx2)

wx2=Xs1[i];

if (Ys1[i]< wy1)

wy1=Ys1[i];

if (Ys1[i]> wy2)

wy2=Ys1[i];

}

for (int i=0;i<Xs2.length;i++)

{

if (Xs2[i]< wx1)

wx1=Xs2[i];

if (Xs2[i]> wx2)

wx2=Xs2[i];

if (Ys2[i]< wy1)

wy1=Ys2[i];

if (Ys2[i]> wy2)

wy2=Ys2[i];

}

DwData();

}

//左移

void jButton2_actionPerformed(ActionEvent e) {

wx1-=1;

wx2-=1;

DwData();

}

//右移

void jButton3_actionPerformed(ActionEvent e) {

wx1+=1;

wx2+=1;

DwData();

}

//上移

void jButton4_actionPerformed(ActionEvent e) {

wy1-=1;

wy2-=1;

DwData();

}

//下移

void jButton5_actionPerformed(ActionEvent e) {

wy1+=1;

wy2+=1;

DwData();

}

//X扩

void jButton6_actionPerformed(ActionEvent e) {

wx1-=1;

wx2+=1;

DwData();

}

//X缩

void jButton7_actionPerformed(ActionEvent e) {

wx1+=1;

wx2-=1;

DwData();

}

//Y扩

void jButton8_actionPerformed(ActionEvent e) {

wy1-=1;

wy2+=1;

DwData();

}

//Y缩

void jButton9_actionPerformed(ActionEvent e) {

wy1+=1;

wy2-=1;

DwData();

}

//样条控制

void jButton10_actionPerformed(ActionEvent e) {

if (kcu)

{

jButton10.setText(“直线”);

kcu=false;

}

else

{

jButton10.setText(“样条”);

kcu=true;

}

DwData();

}

}

//图形处理类

class myGraphicsData

{

//显示器坐标

private int X11, Y11, X12, Y12; //x1,y1,x2,y2

//用户坐标

private double W1, W2, W3, W4; //x1,x2,y1,y2

//绘图对象

public Graphics2D myGraphics;

double Ax8, Ay8;

//用户窗口与显示屏窗口的转移

//x1,x二,,y一,y二为荧屏坐标

//wx壹,wx二,wy一,wy贰为用户坐标

public void truemode(int x1, int x2, int y1,int y2, double wx1, double
wx2, double wy1, double wy2)

{

X11 = x1 ; X12 = x2;

Y11 = y1 ; Y12 = y2;

W1 = wx1 ; W2 = wx2;

W3 = wy1 ; W4 = wy2;

Ax8 = (X12 – X11) / (wx2 – wx1);

Ay8 = (Y12 – Y11) / (wy2 – wy1);

}

//把用户坐标转为显示器坐标

public int[] moxy(double Xa, double Ya)

{

int[] myout=new int[2];

myout[0] = (int)(Ax8 * (Xa – W1) + X11);

myout[1] = (int)(Y12 – Ay8 * (Ya – W3));

return myout;

}

//把荧屏坐标转为用户坐标

public double[] ScrtoCon(int X6, int Y6)

{

double[] myout=new double[2];

myout[0] = (X6 – X11) / Ax8 + W1;

myout[1] = (Y12 – Y6) / Ay8 + W3;

return myout;

}

//画线

public void Dline(double xa, double ya, double xb, double yb)

{

try

{

int x6, y6, x7, y7;

x6 = (int)(Ax8 * (xa – W1) + X11);

y6 = (int)(Y12 – Ay8 * (ya – W3));

x7 = (int)(Ax8 * (xb – W1) + X11);

y7 = (int)(Y12 – Ay8 * (yb – W3));

myGraphics.drawLine(x6, y6, x7, y7);

}

catch(Exception e){}

}

//画坐标U,V为 X,Y轴单位,ns,nt为 x,y轴写字间隔

public void axis(double u, double v, int ns, int nt)

{

double p9, q9, s;

int n2, swx,swy;

int xk=0;

int yk=0;

double ge;

int[] showxy=new int[2];

swx = 0;

swy=4;

ge = (double)0.008 * (W2 – W1);

p9 = W1;

q9 = (double)(W3 + (W4 – W3) * 0.05);

if ((W1 < 0) && (W2 > 0)) p9 = 0;

if ((W3 < 0) && (W4 > 0)) q9 = 0;

Dline(p9, W3, p9, W4);

n2 = 0;

s = 0;

while (s < W4)

{

Dline(p9, s, p9 + ge, s);

if (n2 >= nt)

{

Dline(p9, s, p9 + ge + ge, s);

n2 = 1;

showxy=moxy(p9 + ge + ge,s);

myGraphics.drawString(String.valueOf(s),showxy[0] – swx+4, showxy[1]

  • swy+4);

}

else

{

n2++;

}

s += v;

}

//End While

s = 0;

n2 = 0;

while (s > W3)

{

Dline(p9, s, p9 + ge, s);

if (n2 >= nt)

{

Dline(p9, s, p9 + ge + ge, s);

n2 = 1;

showxy=moxy(p9 + ge + ge, s);

myGraphics.drawString(String.valueOf(s),showxy[0] – swx+4, showxy[1]

  • swy+4);

}

else

{

n2 ++;

}

s -= v; <b

相关文章

No Comments, Be The First!
近期评论
    分类目录
    功能
    网站地图xml地图