java2d-4.PDF

Similar documents
Swing-02.pdf


新・解きながら学ぶJava

Microsoft PowerPoint - ch02

JavaIO.PDF

PowerPoint 簡報

Chapter 9: Objects and Classes

javaexample-02.pdf

Microsoft Word - 01.DOC

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

untitled

chp6.ppt

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

《大话设计模式》第一章

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

EJB-Programming-4-cn.doc

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Java

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft Word - 第3章.doc

3.1 num = 3 ch = 'C' 2

Strings

[Short Essay:] 1. (5 points) Use state-of-memory diagram to describe the differences between x and y declared as follows: double[] x = new double[4];

软件工程文档编制

Strings

Microsoft Word - Broker.doc

Microsoft PowerPoint - ch03

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

CHAPTER VC#

2009年9月全国计算机等级考试二级Java真题及答案

Microsoft PowerPoint - course2.ppt

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

chp11.ppt

untitled

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

Adobe® Flash® 的 Adobe® ActionScript® 3.0 程式設計

HTML5 & CSS3 色彩 背景與漸層屬性 12-1 (color property) CSS 3 color HTML opacity HTML CSS Color Module Level 3 W3C CSS

区 域 活 动 进 入 中 班 我 们 区 域 的 设 置 和 活 动 材 料 都 有 所 变 化, 同 时 也 吸 引 孩 子 们 积 极 的 参 与 学 习 操 作 区 的 新 材 料 他 们 最 喜 欢, 孩 子 们 用 立 方 块 进 行 推 理 操 作 用 扑 克 牌 进 行 接 龙 游

FIT1改1.FIT)

使用MapReduce读取XML文件

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

Microsoft Word - ch04三校.doc

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

untitled

untitled

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

新版 明解C++入門編

基于JAVA的连连看游戏设计

三种方法实现Hadoop(MapReduce)全局排序(1)

Learning Java

Microsoft PowerPoint - Kaochia-PE PROTECTIVE FILM(CN) ppt [相容模式]

壹:教育文化公益慈善機關或團體免納所得稅適用標準

(给多有拉姆)佛子行三十七颂1——7

《佛子行三十七颂》讲记1

至 尊 法 王 蒋 阳 龙 朵 加 参 尊 者 上 师 瑜 伽 皈 依 境

<4D F736F F D20D6D0CEC4B7A88C57B454CABF8C57CEBBD593CEC4D28EB9A0>

JBuilder Weblogic

Guava学习之Resources

untitled

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

IoC容器和Dependency Injection模式.doc

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

yy.xls

穨finaldiss.doc

13. 下 列 植 物 的 向 性 或 運 動, 哪 些 是 受 到 生 長 素 作 用 的 影 響?(5-4) 甲. 睡 蓮 的 花 到 了 晚 上 會 合 起 來 ; 乙. 黃 瓜 的 捲 鬚 攀 附 竹 竿 向 上 生 長 ; 丙. 含 羞 草 的 葉 經 碰 觸 後 閉 合 ; 丁. 紅 豆

Transcription:

75

7 6 G r a d i e n t P a i n t B a s i c S t r o k e s e t P a i n t ( ) s e t S t o r k e ( ) import java.awt.*; import java.awt.geom.*; public class PaintingAndStroking extends ApplicationFrame { public static void main(string[] args) { PaintingAndStroking f = new PaintingAndStroking(); f.settitle("paintingandstroking v1.0"); f.setsize(300, 150); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; double x = 15, y = 50, w = 70, h = 70; Ellipse2D e = new Ellipse2D.Double(x, y, w, h); GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true); // g2.setpaint(gp); g2.fill(e); // e.setframe(x + 100, y, w, h); g2.setpaint(color.black);

7 7 g2.setstroke(new BasicStroke(8)); g2.draw(e); // e.setframe(x + 200, y, w, h); g2.setpaint(gp); g2.draw(e); s e t P a i n t () G r a p h i c s 2 D s e t P a i n t () j a v a. a w t. P a i n t G r a p h i c s 2 D P a i n t G r a p h i c s 2 D P a i n t P a i n t S h a p e G r a p h i c s 2 D f i l l () P a i n t G r a p h i c s 2 D s e t P a i n t () S h a p e f i l l () G r a p h i c s 2 D P a i n t

7 8 j a v a. a w t. C o l o r P a i n t C o l o r C o l o r C o l o r C o l o r public static final Color white; public static final Color lightgray; public static final Color gray; public static final Color darkgray; public static final Color black; public static final Color red; public static final Color pink; public static final Color orange; public static final Color yellow; public static final Color green; public static final Color magenta; public static final Color cyan; public static final Color blue;

7 9 public Color(int r, int g, int b) public Color(float r, float g, float b) import java.awt.*; import java.awt.geom.*; public class SolidPaint extends ApplicationFrame { public static void main(string[] args) { SolidPaint f = new SolidPaint(); f.settitle("solidpaint v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; Arc2D pie = new Arc2D.Float(0, 50, 150, 150, -30, 90, Arc2D.PIE); g2.setpaint(color.blue); g2.fill(pie); G r a p h i c s s e t C o l o r ( ) s e t C o l o r ( ) s e t C o l o r ( c ) G r a p h i c s 2 D s e t P a i n t ( c )

8 0 Swing j a v a x. s w i n g. J C o l o r C h o o s e r public static Color showdialog(component component, String title, Color initialcolor) c o m p o n e n t t i t l e i n i t i a l C o l o r import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RedTown extends JFrame { public static void main(string[] args) { new RedTown(); public RedTown() { super("redtown v1.0"); createui(); setvisible(true); protected void createui() { setsize(400, 400); setlocation(100, 100); getcontentpane().setlayout(new GridBagLayout()); JButton colorbutton = new JButton("Choose a color...");

8 1 getcontentpane().add(colorbutton); colorbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent ae) { Color c = JColorChooser.showDialog( RedTown.this, "Choose a color...", getbackground()); if (c!= null) getcontentpane().setbackground(c); ); addwindowlistener(new WindowAdapter() { public void windowclosing(windowevent we) { System.exit(0); ); G ra d i e n t Pa i n t j a v a. a w t. G r a d i e n t P a i n t P o i n t 2 D C o l o r G r a d i e n t P a i n t

8 2 G r a d i e n t P a i n t G r a d i e n t P a i n t G r a d i e n t P a i n t P o i n t 2 D C o l o r G r a d i e n t P a i n t G r a d i e n t P a i n t

8 3 public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2) public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2) G r a d i e n t P a i n t p t 1 c o l o r 1 p t 2 c o l o r 2 public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic) public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic) G r a d i e n t P a i n t p t 1 c o l o r 1 p t 2 c o l o r 2 c y c l i c t r u e G r a d i e n t P a i n t G r a d i e n t P a i n t public Point2D getpoint1() public Color getcolor1() public Point2D getpoint2() public Color getcolor2() public boolean iscyclic() import java.awt.*; import java.awt.geom.*; public class GradientPaintFill extends ApplicationFrame { public static void main(string[] args) { GradientPaintFill f = new GradientPaintFill(); f.settitle("gradientpaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true);

8 4 public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D e = new Ellipse2D.Float(40, 40, 120, 120); GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true); g2.setpaint(gp); g2.fill(e); Tex t u r e Pa i n t j a v a. a w t. T e x t u r e P a i n t T e x t u r e P a i n t B u f f e r e d I m a g e B u f f e r e d I m a g e R e c t a n g l e 2 D T e x t u r e P a i n t

8 5 T e x t u r e P a i n t public TexturePaint(BufferImage txtr, Rectangle2D anchor) T e x t u r e d I m a g e anchor T e x t u r e P a i n t public BufferedImage getimage() public Rectangle2D getanchorrect() T e x t u r e P a i n t p a i n t () T e x t u r e P a i n t T e x t u r e P a i n t import java.awt.*; import java.awt.geom.*; import java.awt.image.bufferedimage; import java.io.*; import com.sun.image.codec.jpeg.*; public class TexturePaintFill

8 6 extends ApplicationFrame { public static void main(string[] args) throws Exception { TexturePaintFill f = new TexturePaintFill("roa2.jpg"); f.settitle("texturepaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); private BufferedImage mimage; public TexturePaintFill(String filename) throws IOException, ImageFormatException { // JPEG InputStream in = getclass().getresourceasstream(filename); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); mimage = decoder.decodeasbufferedimage(); in.close(); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; // RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25, 25); // Rectangle2D tr = new Rectangle2D.Double(0, 0, mimage.getwidth(), mimage.getheight()); // TexturePaint TexturePaint tp = new TexturePaint(mImage, tr); // g2.setpaint(tp); g2.fill(r);

8 7 P a i n t P a i n t j a v a. a w t. P a i n t C o n t e x t P a i n t C o n t e x t G r a p h i c s 2 D P a i n t P a i n t C o n t e x t P a i n t C o n t e x t Trans parency P a i n t j a v a. a w t. T r a n s p a r e n c y T r a n s p a r e n c y public static final int OPAQUE public static final int BITMASK public static final int TRANSLUCENT T r a n s p a r e n c y public int gettransparency() O P A Q U E B I T M A S K T R A N S L U C E N T C o l o r M o d e l T r a n s p a r e n c y C o l o r M o d e l

8 8 Paint T r a n s p a r e n c y P a i n t P a i n t P a i n t C o n t e x t public PaintContext createcontext(colormodel cm, Rectangle devicebounds, Rectangle2D userbounds, AffineTransform xform, RenderingHints hints) P a i n t C o n t e x t P a i n t C o n t e x t d e v i c e B o u n d s u s e r B o u n d s A f f i n e T r a n s f o r m h i n t s P a i n t C o n t e x t T e x t u r e P a i n t P a i n t C o n t e x t K E Y _ I N T E R P O L A T I O N P a i n t c r e a t e C o n t e x t () PaintContext c r e a t e C o n t e x t ( ) P a i n t C o n t e x t P a i n t P a i n t C o n t e x t public void dispose() P a i n t C o n t e x t public ColorModel getcolormodel() P a i n t C o n t e x t P a i n t c r e a t e C o n t e x t ( )

8 9 public Raster getraster(int x, int y, int w, int h) P a i n t C o n t e x t R a s t e r R a s t e r P a i n t P a i n t R o u n d G r a d i e n t P a i n t R o u n d G r a d i e n t C o n t e x t R o u n d G r a d i e n t P a i n t c r e a t e C o n t e x t ( ) R o u n d G r a d i e n t C o n t e x t import java.awt.*; import java.awt.geom.*; import java.awt.image.colormodel; public class RoundGradientPaint implements Paint { protected Point2D mpoint; protected Point2D mradius; protected Color mpointcolor, mbackgroundcolor; public RoundGradientPaint(double x, double y, Color pointcolor, Point2D radius, Color backgroundcolor) { if (radius.distance(0, 0) <= 0) throw new IllegalArgumentException("Radius must be greater than 0."); mpoint = new Point2D.Double(x, y); mpointcolor = pointcolor; mradius = radius; mbackgroundcolor = backgroundcolor;

9 0 public PaintContext createcontext(colormodel cm, Rectangle devicebounds, Rectangle2D userbounds, AffineTransform xform, RenderingHints hints) { Point2D transformedpoint = xform.transform(mpoint, null); Point2D transformedradius = xform.deltatransform(mradius, null); return new RoundGradientContext(transformedPoint, mpointcolor, transformedradius, mbackgroundcolor); public int gettransparency() { int a1 = mpointcolor.getalpha(); int a2 = mbackgroundcolor.getalpha(); return (((a1 & a2) == 0xff)? OPAQUE : TRANSLUCENT); R o u n d G r a d i e n t P a i n t T r a n s p a r e n c y g e t T r a n s p a r e n c y () O P A Q U E T R A N S L U C E N T R o u n d G r a d i e n t P a i n t R o u n d G r a d i e n t P a i n t T R A N S L U C E N T R o u n d G r a d i e n t P a i n t new Color(255, 0, 0, 255) new Color(255, 0, 0, 0) R o u n d G r a d i e n t P a i n t g e t R a s t e r ( ) for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { double distance = mpoint.distance(x + i, y + j); double radius = mradius.distance(0, 0); double ratio = distance / radius; if (ratio > 1.0) ratio = 1.0;

9 1 data[base + 0] = (int)(mc1.getred() + ratio * (mc2.getred() - mc1.getred())); data[base + 1] = (int)(mc1.getgreen() + ratio * (mc2.getgreen() - mc1.getgreen())); data[base + 2] = (int)(mc1.getblue() + ratio * (mc2.getblue() - mc1.getblue())); data[base + 3] = (int)(mc1.getalpha() + ratio * (mc2.getalpha() - mc1.getalpha())); import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class RoundGradientContext implements PaintContext { protected Point2D mpoint; protected Point2D mradius; protected Color mc1, mc2; public RoundGradientContext(Point2D p, Color c1, Point2D r, Color c2) { mpoint = p; mc1 = c1; mradius = r; mc2 = c2; public void dispose() { public ColorModel getcolormodel() { return ColorModel.getRGBdefault(); public Raster getraster(int x, int y, int w, int h) { WritableRaster raster = getcolormodel().createcompatiblewritableraster(w, h); int[] data = new int[w * h * 4]; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { double distance = mpoint.distance(x + i, y + j); double radius = mradius.distance(0, 0); double ratio = distance / radius;

9 2 if (ratio > 1.0) ratio = 1.0; int base = (j * w + i) * 4; data[base + 0] = (int)(mc1.getred() + ratio * (mc2.getred() - mc1.getred())); data[base + 1] = (int)(mc1.getgreen() + ratio * (mc2.getgreen() - mc1.getgreen())); data[base + 2] = (int)(mc1.getblue() + ratio * (mc2.getblue() - mc1.getblue())); data[base + 3] = (int)(mc1.getalpha() + ratio * (mc2.getalpha() - mc1.getalpha())); raster.setpixels(0, 0, w, h, data); return raster; R o u n d G r a d i e n t P a i n t import java.awt.*; import java.awt.geom.*; public class RoundGradientPaintFill extends ApplicationFrame { public static void main(string[] args) { RoundGradientPaintFill f = new RoundGradientPaintFill(); f.settitle("roundgradientpaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25, 25); RoundGradientPaint rgp = new RoundGradientPaint(75, 75, Color.magenta, new Point2D.Double(0, 85), Color.blue);

9 3 g2.setpaint(rgp); g2.fill(r); s e t S t r o k e () G r a p h i c s 2 D j a v a. a w t. S t r o k e j a v a. a w t. B a s i c S t r o k e s e t P a i n t () G r a p h i c s 2 D P a i n t G r a p h i c s 2 D d r a w () G r a p h i c s 2 D S t r o k e P a i n t G r a p h i c s 2 D S t r o k e G r a p h i c s 2 D d r a w () G r a p h i c s 2 D S t r o k e S t r o k e S h a p e public abstract Shape createstrokedshape(shape p) S h a p e

9 4 S t r o k e d r a w ( ) G r a p h i c s 2 D S h a p e S h a p e S h a p e S h a p e S h a p e S h a p e G r a p h i c s 2 D d r a w () public void longwindeddraw(graphics2d g2, Shape s) { Stroke stroke = g2.getstroke(); Shape strokedoutline = stroke.createstrokedshape(s); g2.fill(strokedoutline); B a s i c S t r o ke S t r o k e j a v a. a w t. B a s i c S t r o k e B a s i c S t r o k e B a s i c S t r o k e

9 5 public static final int CAP_BUTT public static final int CAP_ROUND public static final int CAP_SQUARE B a s i c S t r o k e B a s i c S t r o k e

9 6 public static final int JOIN_BEVEL public static final int JOIN_MITER J O I N _ B E V E L public static final int JOIN_ROUND C A P _ R O U N D J O I N _ M I T E R halflength miterlength miterlimit m i t e r L e n g t h m i t e r L i m i t h a l f W i d t h J O I N _ B E V E L J O I N _ M I T T E R m i t e r L i m i t m i t e r l i m i t m i t e r l i m i t B a s i c S t r o k e h a l f L e n g t h m i t e r L e n g t h J O I N _ M I T E R

9 7 B a s i c S t r o k e f l o a t G e n e r a l P a t h C A P _ B U T T C A P _ R O U N D C A P _ S Q U A R E

9 8 B a s i c S t r o k e P a i n t S t r o k e public BasicStroke() S t r o k e C A P _ S Q U A R E J O I N _ M I T E R

9 9 public BasicStroke(float width) public BasicStroke(float width, int cap, int join) S t r o k e J O I N _ M I T E R public BasicStroke(float width, int cap, int join, float miterlimit) J O I N _ M I T E R public BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) S t r o k e Rectangle2D r = new Rectangle2D.Double(50, 50, 100, 100); Stroke stroke = new BasicStroke(8, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 12, 12, 0); g2.setstroke(stroke); g2.draw(r);

1 0 0 o v e r l a p Color smokey = new Color(128, 128, 128, 128); g2.setpaint(smokey); g2.fill(e); g2.draw(e); g2.setpaint(color.black); g2.draw(e); g2.setpaint(color.gray); g2.fill(e);

1 0 1 g2.setpaint(color.gray); g2.fill(e); g2.setpaint(color.black); g2.draw(e); import java.awt.*; import java.awt.geom.*; public class Overlap extends ApplicationFrame { public static void main(string[] args) { Overlap f = new Overlap(); f.settitle("overlap v1.0"); f.setsize(300, 150); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; double x = 15, y = 50, w = 70, h = 70; Ellipse2D e = new Ellipse2D.Double(x, y, w, h);

1 0 2 g2.setstroke(new BasicStroke(8)); // Color smokey = new Color(128, 128, 128, 128); g2.setpaint(smokey); g2.fill(e); g2.draw(e); // e.setframe(x + 100, y, w, h); g2.setpaint(color.black); g2.draw(e); g2.setpaint(color.gray); g2.fill(e); // e.setframe(x + 200, y, w, h); g2.setpaint(color.gray); g2.fill(e); g2.setpaint(color.black); g2.draw(e);