Attach Files..

Download Report

Transcript Attach Files..

ให้ ทำกำรเขียนโปรแกรมเพือ่ แสดงรูปภำพ ดังนี้





การวาดรู ปหลายเหลี่ยม (polygon)


Polygon()
Polygon(int[ ] xpoints, int[ ] ypoints, int[ ]
npoints)
int[ ] xpoints คือ Array ที่กาหนดค่า X
int[ ] ypoints คือ Array ที่กาหนดค่า Y
int npoints คือ จานวนจุด
import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
public void paint(Graphics g) {
Polygon poly = new Polygon();
poly.addPoint(100, 100);
poly.addPoint(175, 50);
poly.addPoint(250, 100);
poly.addPoint(250, 200);
poly.addPoint(175, 250);
poly.addPoint(100, 200);
g.drawPolygon(poly); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 400);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
public void paint(Graphics g) {
Polygon poly = new Polygon();
int[] x = {75, 150, 225, 225, 150, 75};
int[] y = {100, 50, 100, 200, 250, 200};
g.drawPolygon(x, y, x.length); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setDefaultCloseOperation(EXIT_ON_CLOSE);
d.setSize(400, 400);
d.setBackground(Color.white);
d.setVisible(true); }
}
Java 2D Geom
Package
การสร้างกราฟิ กด้วยชุดของ
java.awt.geom
Character of Geom


เป็ นชุดการสร้าง 2D ที่มีความหลากหลายของวัตถุที่สร้างมากกว่า
java.awt.*
เช่ น การสร้ างเส้นโค้ง(arc) หรื อรู ปหลายเหลี่ ยม
(polygon) เป็ นต้น โดยที่ ก ารเขี ย นค าสั่ ง นั้น ยัง คงรู ปแบบการเขี ย นที่
คล้ายกันซึ่ งมีลาดับการทางานแบบ sequential ที่เรี ยงลาดับคาสั่งจาก
บนลงล่างเสมอ
ก่อนการเขียนคาสัง่ ต้องมีการ import คลาส ด้วยคาสัง่
import java.awt.geom.*
Program Structure
import java.awt.geom.*;
import java.awt.*;
class ชื่อคลาส extends Frame{
Constructor( ){ configuration of Frame }
paint( ){ Drawing Shape }
}
2D Shape

Area() A
เป็ นการกาหนดพื้นที่การวาดรู ป
CubicCurve2D()
Dimension2D()
Ellipse2D() A เป็ นการวาดรู ปวงรี

Line2D() A


Point2D() A
QuadCurve2D()
Rectangle2D()A เป็ นการวาดรู ปสี่ เหลี่ยม

RoundRectangle2D() A เป็ นการวาดรู ปสี่ เหลี่ยมมุมโค้ง




เป็ นการวาดเส้นตรง
เป็ นการกาหนดจุด
Line2D()

เป็ นคาสัง่ วาดเส้นตรงโดยมีรูปแบบคาสัง่ ดังนี้
Line2D.Float(X1,Y1,X2,Y2);
Line2D.Double(X1,Y1,X2,Y2);
ตาแหน่งเริ่ ม
X2,Y2 ตาแหน่งสิ้ นสุ ด
X1,Y1
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(100, 100, 250, 260);
g2.draw(lin);
}
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 400);
d.setBackground(Color.white);
d.setVisible(true);
}
}
QuadCurve2D()

เป็ นการวาดรู ปเส้นโค้งที่มีกาหนดจุดต้น จุดที่ดึงเส้น และจุดปลาย
ของเส้น สามารถสร้างโดยคาสัง่

QuadCurve2D.Float()
QuadCurve2D.Double()
QuadCurve2D.Float(x1,y1,ctrlx,ctrly,x2,y2)
QuadCurve2D.Double(x1,y1,ctrlx,ctrly,x2,y2)



import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
QuadCurve2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new QuadCurve2D.Float();
lin.setCurve(100, 100, 200, 400,300,100);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setDefaultCloseOperation(EXIT_ON_CLOSE);
d.setSize(400, 400);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
QuadCurve2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new QuadCurve2D.Float(100, 100, 200, 400,300,100);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setDefaultCloseOperation(EXIT_ON_CLOSE);
d.setSize(400, 400);
d.setBackground(Color.white);
d.setVisible(true);
}
}
CubicCurve2D()

เป็ นการวาดรู ปเส้นโค้งที่มีกาหนดจุดต้น จุดที่ดึงเส้น 2 จุด และจุด
ปลายของเส้น




CubicCurve2D.Float()
CubicCurve2D.Double()
CubicCurve2D.Float(x1,y1,ctrlx1,ctrly1, ctrlx2,ctrly2,x2,y2)
CubicCurve2D.Double(x1,y1,ctrlx,ctrly, ctrlx2,ctrly2,x2,y2)
ctrlx1,ctrly1
x1,y1
ctrlx2,ctrly2
x2,y2
import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
CubicCurve2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new CubicCurve2D.Float();
lin.setCurve(100, 100, 200, 400,300,50,600,100);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setDefaultCloseOperation(EXIT_ON_CLOSE);
d.setSize(700, 400);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends javax.swing.JFrame {
CubicCurve2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new CubicCurve2D.Float(100, 100, 200, 400,300,50,600,100);
g2.draw(lin);
}
public static void main(String args[]) {
Drawing d = new Drawing();
d.setDefaultCloseOperation(EXIT_ON_CLOSE);
d.setSize(700, 400);
d.setBackground(Color.white);
d.setVisible(true);
}
}
Ellipse2D()

เป็ นการวาดรู ปวงรี โดยจะระบุจุดเริ่ ม x,y ความกว้าง และความสู ง
ของวงรี
คาสัง่ ในการสร้าง





Ellipse2D.Double ()
Ellipse2D.Float ()
Ellipse2D.Double (double X, double Y, double Width, double Height)
Ellipse2D.Float (float X, float Y, float Width, float Height)
คาสัง่ Ellipse2D.Double () และ Ellipse2D.Float () จะมีการกาหนดการ
วาดรู ปด้วยคาสัง่ setFrame(X, Y, Width, Height)
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
Ellipse2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new Ellipse2D.Float(100, 100, 200, 400);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
Ellipse2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new Ellipse2D.Float(100, 100, 200, 400);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
100, 100
400
200
Rectangle2D()

เป็ นการวาดรู ปสี่ เหลี่ยม โดยจะระบุจุดเริ่ ม x,y ความกว้าง และ
ความสู งของสี่ เหลี่ยม
คาสัง่ ในการสร้าง





Rectangle2D.Double ()
Rectangle2D.Float ()
Rectangle2D.Double (double X, double Y, double Width, double
Height)
Rectangle2D.Float (float X, float Y, float Width, float Height)
คาสัง่ Rectangle2D.Double () และ Rectangle2D.Float () จะมีการ
กาหนดการวาดรู ปด้วยคาสัง่ setFrame(X, Y, Width, Height)
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
Rectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new Rectangle2D.Float(100, 100, 200, 400);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
Rectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new Rectangle2D.Float(100, 100, 200, 400);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
Rectangle2D()

เป็ นการวาดรู ปสี่ เหลี่ยมขอบมน โดยจะระบุจุดเริ่ ม x,y ความกว้าง
และความสู งของสี่ เหลี่ยม ความกว้างของมุม และความสู งของมุม
คาสัง่ ในการสร้าง





Rectangle2D.Double ()
Rectangle2D.Float ()
Rectangle2D.Double (double X, double Y, double Width, double
Height)
Rectangle2D.Float (float X, float Y, float Width, float Height)
คาสัง่ Rectangle2D.Double () และ Rectangle2D.Float () จะมีการ
กาหนดการวาดรู ปด้วยคาสัง่ setFrame(X, Y, Width, Height)
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
RoundRectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new RoundRectangle2D.Float(100, 100, 200, 400,20 ,20);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
RoundRectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new RoundRectangle2D.Float(100, 100, 200, 400, 20, 20);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
Arc2D()

เป็ นการวาดรู ปสี่ เหลี่ยมเส้นโค้งโดยระบุจุดเริ่ ม x,y ความกว้าง
และความสู ง มุมเริ่ มต้นในแกนนอน มุมในแกนแกนตั้ง และชนิด
ของเส้นโค้ง
ชนิดของเส้ นโค้ง


0 คือ เส้นโค้งจากจุดเริ่ ม-สิ้ นสุ ด หรื อ Arc2D.OPEN
1 คือ เส้นโค้งจากจุดเริ่ ม-สิ้ นสุ ดและมีเส้นระหว่างจุดทั้งสอง หรื อ
Arc2D.CHORD

2 คือ เส้นโค้งจากจุดเริ่ ม-สิ้ นสุ ดและมีเส้นระหว่างจุดทั้งสองกับ
จุดตัดของเส้นมุมทั้งสอง หรื อ Arc2D.PIE
0
1
2
คาสัง่ ในการสร้าง





Arc2D.Double ()
Arc2D.Float ()
Arc2D.Double (X, Y, Width, Height, Theta, Delta, Type)
Arc2D.Float (X, Y, Width, Height, Theta, Delta, Type)
คาสัง่ Arc2D.Double () และ Arc2D.Float () จะมีการกาหนดการวาดรู ป
ด้วยคาสัง่ setFrame(X, Y, Width, Height, Theta, Delta, Type)
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
Arc2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new Arc2D.Float(10,50,150,150,0,60,0);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(200, 300);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
RoundRectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new RoundRectangle2D.Float(100, 100, 200, 400,20 ,20);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
RoundRectangle2D lin;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
lin = new RoundRectangle2D.Float(100, 100, 200, 400, 20, 20);
g2.draw(lin); }
public static void main(String args[]) {
Drawing d = new Drawing();
d.setSize(400, 600);
d.setBackground(Color.white);
d.setVisible(true); }
}
Area()



รู ปแบบคาสัง่
Area() เป็ นการสร้างพื้นที่เปล่าๆ โดยไม่มีการกาหนดรู ปร่ างของ
พื้นที่วา่ มีรูปร่ างแบบใด
Area(Shape s) เป็ นการสร้างพื้นที่ข้ ึนเราโดยกาหนดให้มี
รู ปร่ างเช่น triangle, rectangular เป็ นต้น
Operation with Area


Add() Adds the shape of the specified Area to
the shape of this Area. The resulting shape of
this Area will include the union of both
shapes, or all areas that were contained in
either this or the specified Area.
Subtract() Subtracts the shape of the
specified Area from the shape of this Area.
The resulting shape of this Area will include
areas that were contained only in this Area
and not in the specified Area.
Operation with Area

Intersect() Sets the shape of this Area to the
intersection of its current shape and the
shape of the specified Area. The resulting
shape of this Area will include only areas that
were contained in both this Area and also in
the specified Area.
Operation with Area

exclusiveOr() Sets the shape of this Area to
be the combined area of its current shape
and the shape of the specified Area, minus
their intersection. The resulting shape of this
Area will include only areas that were
contained in either this Area or in the
specified Area, but not in both.
add(Area rhs)
// Example:
Area a1 = new Area([triangle 0,0 => 8,0 => 0,8]);
Area a2 = new Area([triangle 0,0 => 8,0 => 8,8]);
a1.add(a2);
a1(before) + a2 =
a1(after)
subtract(Area rhs)
// Example:
Area a1 = new Area([triangle 0,0 => 8,0 => 0,8]);
Area a2 = new Area([triangle 0,0 => 8,0 => 8,8]);
a1.subtract(a2);
a1(before) - a2 = a1(after)
intersect(Area rhs)
// Example:
Area a1 = new Area([triangle 0,0 => 8,0 => 0,8]);
Area a2 = new Area([triangle 0,0 => 8,0 => 8,8]);
a1.intersect(a2);
a1(before) intersect a2 = a1(after)
exclusiveOr(Area rhs)
// Example:
Area a1 = new Area([triangle 0,0 => 8,0 => 0,8]);
Area a2 = new Area([triangle 0,0 => 8,0 => 8,8]);
a1.exclusiveOr(a2);
a1(before) exclusiveOr a2 = a1(after)
กำรเทียบเคียงกับ Set Operation





Add() คือ การ Union ()
Subtract() คือ การ – เช่น A-B คือ อยูใ่ น A แต่ไม่อยูใ่ น B
Intersect() คือ การ Intersect ()
exclusiveOr() คือ A’  B’ หรื อ (AB)-(AB)
http://java.sun.com/products/javamedia/2D/samples/suite/Clipping/Areas.java
A
B
A-B
U
A∩B
A’UB’
import java.awt.*;
import java.awt.geom.*;
class Drawing extends Frame {
public static void main(String[] args) {
Drawing f = new Drawing();
f.setTitle(" Drawing v1.0"); f.setSize(300, 150);
f.setVisible(true); }
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
Ellipse2D e = new Ellipse2D.Double(10,50,70,70);
Area leaf1 = new Area(e);
Ellipse2D f = new Ellipse2D.Double(50,50,70,70);
Area leaf2 = new Area(f);
leaf2 = new Area(f);
leaf1.exclusiveOr(leaf2);
g2.fill(leaf1); }
}





