製作一個畫圖介面
阿新 • • 發佈:2018-10-31
製作一個畫圖介面主要分為四個步驟
一、 建立一個圖畫介面
利用Java 的 swing,awt 提供的相關內容進行繪畫
二、 處理事件
事件監聽機制
三. 編寫事件處理的具體方法
四. 實現重繪
建立一個可以儲存相關資訊的類(Shape)來記錄資訊,在建立Shape[] array陣列來記錄資料
1. Drawing 主要用於實現畫圖介面的繪畫
import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComboBox; import javax.swing.JFrame; public class Drawing extends JFrame{ public Shape[] array = new Shape[1000]; //利用陣列將資料存起來 public static void main(String[] args) { Drawing draw = new Drawing(); draw.InitUI(); } public void InitUI() { setTitle("畫圖介面"); setLayout(new FlowLayout()); setSize(800,800); setLocationRelativeTo(null); setDefaultCloseOperation(3); //定義圖型下拉框 1. 用一個數組記錄下拉框中的內容 String[] typeArray = {"直線","矩形","圓","3D矩形","圖片","文字Hello","等腰三角形","五角星","任意多邊形","立方體","鉛筆","噴槍","橡皮擦"}; JComboBox<String> cbType = new JComboBox<String>(typeArray); cbType.setName("圖型"); //給下拉框取名字,以便區分點選的 圖型 、 顏色 、粗細中哪一個下拉框 add(cbType); //定義顏色下拉框 1. 用一個數組記錄下拉框中的內容 String[] colorArray = {"紅色","綠色","藍色"}; JComboBox<String> cbColor = new JComboBox<String>(colorArray); cbColor.setName("顏色"); //給下拉框取名字,以便區分點選的 圖型 、 顏色 、粗細中哪一個下拉框 add(cbColor); //定義粗細下拉框 1. 用一個數組記錄下拉框中的內容 String[] strokeArray = {"1","3","5"}; JComboBox<String> cbStroke = new JComboBox<String>(strokeArray); cbStroke.setName("粗細"); //給下拉框取名字,以便區分點選的 圖型 、 顏色 、粗細中哪一個下拉框 add(cbStroke); setVisible(true); Graphics g = getGraphics(); //畫筆一定要在setVisible(boolean)方法後定義,否則 g = null; DrawingListener dl = new DrawingListener((Graphics2D)g,array); cbType.addActionListener(dl); //處理下拉框 圖型中的 事件 cbColor.addActionListener(dl); //處理下拉框 顏色中的 事件 cbStroke.addActionListener(dl); //處理下拉框 粗細中的 事件 addMouseListener(dl); //處理在視窗上發生的動作 //主要是滑鼠的點選(mouseClicked),按下(mousePressed),釋放( mouseReleased),進入(mouseEntered),離開(mouseExited) addMouseMotionListener(dl); //處理在視窗上發生的動作 //滑鼠的拖動(mouseDragged),移動(mouseMoved) } public void paint(Graphics g) { //Drawing類繼承看JFrame,這裡重寫JFrame類中的paint()方法,以實現重繪 super.paint(g); //呼叫父類中的paint方法,以畫出視窗和下拉框 System.out.println("重繪窗體和元件"); //將視窗重繪前已經畫的內容繪畫一遍,以實現視窗重繪後內容的復原 for(int i = 0;i < array.length;i++) if(array[i] != null) array[i].draw((Graphics2D) g); } }
2.DrawingListener 主要用於處理事件
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.Scanner; import javax.swing.JComboBox; import javax.swing.JFrame; /** DrawingListener 繼承JFrame以得到畫筆,Toolkit * 繼承ActionListener,MouseListener,MouseMotionListener 事件接聽介面 * */ public class DrawingListener extends JFrame implements ActionListener,MouseListener,MouseMotionListener { private String type = "直線"; private Color color = Color.red; private float stroke = 1; private Graphics2D g; private int x1,y1,x2,y2; int fx,fy,lx,ly; //fx,fy記錄起點的座標,lx、ly記錄上一個點的座標 private boolean sign = true; //記錄是否為起點(多邊形) public Shape[] array ; int index; /** * 構造方法 * g是從窗體上傳遞過來的畫筆物件 * array 將陣列傳過來以儲存資料 */ public DrawingListener(Graphics2D g,Shape[] array) { this.g = g; this.array = array; } public void actionPerformed(ActionEvent e) { JComboBox<String> cb = (JComboBox<String>)e.getSource(); //獲取事件源物件 String str = cb.getName(); //獲取下拉框的名字,根據name屬性來做判斷 if(str.equals("圖型")) { type = cb.getSelectedItem().toString(); //獲取選擇的圖形資訊 }else if(str.equals("顏色")) { //獲取選擇的顏色資訊 if(cb.getSelectedItem().toString().equals("紅色")) color = Color.RED; else if(cb.getSelectedItem().toString().equals("綠色")) color = Color.GREEN; else if(cb.getSelectedItem().toString().equals("藍色")) color = Color.BLUE; }else if(str.equals("粗細")) { //Integer.parseInt(String) 將字串轉化為整型 stroke = (float)Integer.parseInt(cb.getSelectedItem().toString()); } } public void mouseClicked(MouseEvent e) { //滑鼠點選呼叫的方法 }; public void mousePressed(MouseEvent e) { //滑鼠按下呼叫的方法 x1 = e.getX(); //得到按下時的座標 y1 = e.getY(); g.setColor(color); //設定線條粗細 g.setStroke(new BasicStroke(stroke)); //設定粗細,注意setStroke方法只在Graphics2D中有,在Graphics中沒有 //所以你的畫筆g的型別為Graphics2D } public void mouseReleased(MouseEvent e) { //滑鼠(按下)釋放時呼叫的方法 x2 = e.getX(); //得到釋放時的座標 y2 = e.getY(); //我這裡是想將做需要的資訊存到array陣列中,在呼叫Shape中的draw方法 if(type.equals("直線")||type.equals("矩形")||type.equals("圓")||type.equals("3D矩形")||type.equals("文字Hello")||type.equals("等腰三角形")||type.equals("五角星")|type.equals("立方體")) { Shape s = new Shape(x1,y1,x2,y2,color,stroke,type); //儲存資料 if(index < 1000) //當儲存的資料超過1000後將不再儲存 array[index++] = s; s.draw(g); }else if(type.equals("圖片")) { Toolkit tool = this.getToolkit(); //這個this應該指向JFrame,JPanel,所以我在治理繼承了JFrame類 Image image = tool.getImage("C:\\Users\\ASUS\\Desktop\\p2.png"); //給image賦值 Shape s = new Shape(x1,y1,x2,y2,color,stroke,type,image); if(index < 1000) array[index++] = s; s.draw(g); }else if(type.equals("任意多邊形")){ if(sign) { //當此時畫的是任意多邊形的第一條線 fx = x1; fy = y1; //fx,fy記錄起點的座標,lx、ly記錄上一個點的座標 lx = x1; ly = y1; Shape s = new Shape(lx,ly,x2,y2,color,stroke,type); //直接儲存需要畫的直線 if(index < 1000) array[index++] = s; s.draw(g); sign = false; //sign = false 即接下來畫的不是任意多邊形的第一條線 }else { //畫的不是任意多邊形的第一條線 if(x2 > fx-9 && x2 < fx+9 && y2 > fy-9 && y2 < fy+9) { //判斷此時釋放的位置是不是任意多邊形結束的位置 Shape s = new Shape(lx,ly,fx,fy,color,stroke,type); if(index < 1000) array[index++] = s; s.draw(g); sign = true; } else { //釋放位置不是終止位置時 Shape s = new Shape(lx,ly,x2,y2,color,stroke,type); if(index < 1000) array[index++] = s; s.draw(g); } } lx = x2; ly = y2; } } public void mouseEntered(MouseEvent e) { //滑鼠進入是呼叫呃方法 } public void mouseExited(MouseEvent e) { //滑鼠離開時呼叫的方法 } public void mouseDragged(MouseEvent e) { //滑鼠拖動時呼叫的方法 if(type.equals("鉛筆")) { //實現鉛筆 x2 = e.getX(); y2 = e.getY(); Shape s = new Shape(x1,y1,x2,y2,color,stroke,type); if(index < 1000) array[index++] = s; s.draw(g); x1 = x2; y1 = y2; }else if(type.equals("噴槍")) { //實現噴槍 x2 = e.getX(); y2 = e.getY(); Shape s = new Shape(x2,y2,color,stroke,type); if(index < 1000) array[index++] = s; s.draw(g); }else if(type.equals("橡皮擦")) { //實現橡皮擦 x2 = e.getX(); y2 = e.getY(); Shape s = new Shape(x2,y2,color,stroke,type); if(index < 1000) array[index++] = s; s.draw(g); } } public void mouseMoved(MouseEvent e) { //滑鼠移動是呼叫的方法 } }
3. Shape 主要用於儲存資料,實現重繪
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.util.Random; import javax.swing.JFrame; public class Shape extends JFrame{ public int x1,y1,x2,y2; public Color color; public float stroke; public String type; public Image img; public Random rand = new Random(); /* * 建構函式的過載 */ public Shape(int x1,int y1,int x2,int y2,Color color,float strock,String type) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.color = color; this.stroke = strock; this.type = type; } public Shape(int x1,int y1,int x2,int y2,Color color,float stroke,String type,Image img) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.color = color; this.stroke = stroke; this.type = type; this.img = img; } public Shape(int x2,int y2,Color color,float stroke,String type) { this.x2 = x2; this.y2 = y2; this.color = color; this.stroke = stroke; this.type = type; } public void draw(Graphics2D g) { //畫圖 g.setColor(color); g.setStroke(new BasicStroke(stroke)); if(type.equals("直線")) { g.drawLine(x1, y1, x2, y2); } else if(type.equals("矩形")) { MyDrawRect myDrawRect = new MyDrawRect(g); myDrawRect.drawRect(x1, y1, x2, y2); //自定義畫矩形的方法,以使之不必侷限於只能往右下角畫矩形 } else if(type.equals("圓")) { MyDrawCircle myDrawCircle = new MyDrawCircle(g); myDrawCircle.drawCircle(x1, y1, x2, y2); //自定義畫圓的方法,以使之不必侷限於只能往右下角畫圓 } else if(type.equals("3D矩形")) { MyDraw3DRect myDraw3DRect = new MyDraw3DRect(g); myDraw3DRect.draw3DRect(x1, y1, x2, y2); //自定義畫3D矩形的方法,以使之不必侷限於只能往右下角畫3D矩形 }else if(type.equals("圖片")) { MyDrawImag myDrawImage = new MyDrawImag(g); myDrawImage.drawImage(x1,y1,x2,y2,img); //自定義畫圖片的方法,以使之不必侷限於只能往右下角畫圖片 }else if(type.equals("文字Hello")) { String str = "Hello"; g.drawString(str, x1, y1); }else if(type.equals("等腰三角形")) { g.drawLine((x1+x2)/2, y1, x1, y2); g.drawLine(x1, y2, x2, y2); g.drawLine((x1+x2)/2, y1, x2, y2); }else if(type.equals("五角星")) { MyDrawPentagram myDrawPentagram = new MyDrawPentagram(g); myDrawPentagram.drawPentagram(x1, y1, x2, y2); //自定義畫五角星的方法,以使之不必侷限於只能往右下角畫圖片 }else if(type.equals("任意多邊形")) { g.drawLine(x1, y1, x2, y2); //因為在DrawingListener類中已經進行了相關資料的處理,所以這裡只需畫直線即可 }else if(type.equals("立方體")) { MyDrawCube myDrawCube = new MyDrawCube(g); myDrawCube.drawCube(x1,y1,x2,y2); //自定義畫立方體的方法,以使之不必侷限於只能往右下角畫圖片 }else if(type.equals("鉛筆")) { g.drawLine(x1, y1, x2, y2); //鉛筆實際上就是在畫直線而已,只不過兩點之間的距離很短 }else if(type.equals("噴槍")) { for(int num = 0;num < 25;num++) { x1 = rand.nextInt(10)+x2-5; //在以(x2,y2)為中點,長為10的正方形中畫點 y1 = rand.nextInt(10)+y2-5; g.drawLine(x1,y1,x1,y2); } }else if(type.equals("橡皮擦")) { g.clearRect(x2-2, y2-2, 15, 15); //用clearRect方法畫與背景顏色一樣的矩形 } } }
4. 畫3D矩形
import java.awt.Graphics;
/*
* 畫3D矩形
*/
public class MyDraw3DRect {
private Graphics g;
int x1,y1,x2,y2;
MyDraw3DRect(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw3DRect(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
//區分x2,y2相對x1,y1的位置
if(x2 < x1 && y2 <y1) //左上
g.draw3DRect(x2, y2, x1-x2, y1-y2,true); //直接用draw3DRect()方法即可
else if(x2 > x1 && y2 < y1) //右上
g.draw3DRect(x1, y2, x2-x1, y1-y2,true);
else if(x2 < x1 && y2 > y1) //左下
g.draw3DRect(x2, y1, x1-x2, y2-y1,true);
else //右下
g.draw3DRect(x1, y1, x2-x1, y2-y1,true);
}
}
5. 畫圓
import java.awt.Graphics;
public class MyDrawCircle {
private Graphics g;
int x1,y1,x2,y2;
int d;
StrictMath math;
MyDrawCircle(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void drawCircle(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1) {
d = (int)math.sqrt(math.pow(x1-x2, 2)+math.pow(y1-y2, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else if(x2 > x1 && y2 < y1) {
d = (int)math.sqrt(math.pow(x2-x1, 2)+math.pow(y1-y2, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else if(x2 < x1 && y2 > y1) {
d = (int)math.sqrt(math.pow(x1-x2, 2)+math.pow(y2-y1, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else {
d = (int)math.sqrt(math.pow(x2-x1, 2)+math.pow(y2-y1, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
}
}
6. 畫立方體(其實此處畫的哪裡可以單獨分出來為一個方法)
import java.awt.Graphics;
/*
* 畫立方體
*/
public class MyDrawCube {
private Graphics g;
int x1,y1,x2,y2;
MyDrawCube(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
/*
* 畫立方體的虛線
*/
public void drawDashedShu(int x1,int y1,int x2,int y2) { // 畫豎虛線
g.drawLine(x1, y1, x1, y1+(y2-y1)/11);
g.drawLine(x1, y1+((y2-y1)*2)/11, x1, y1+((y2-y1)*3 )/11);
g.drawLine(x1, y1+((y2-y1)*4)/11, x1, y1+((y2-y1)*5)/11);
g.drawLine(x1, y1+((y2-y1)*6)/11, x1, y1+((y2-y1)*7)/11);
g.drawLine(x1, y1+((y2-y1)*8)/11, x1, y1+((y2-y1)*9)/11);
g.drawLine(x1, y1+((y2-y1)*10)/11, x1, y2);
}
public void drawDashedHeng(int x1,int y1,int x2,int y2) { //畫橫虛線
g.drawLine(x1, y1, x1+((x2-x1)*1)/11, y1);
g.drawLine(x1+((x2-x1)*2)/11, y1,x1+((x2-x1)*3)/11, y2);
g.drawLine(x1+((x2-x1)*4)/11, y1, x1+((x2-x1)*5)/11, y2);
g.drawLine(x1+((x2-x1)*6)/11, y1, x1+((x2-x1)*7)/11, y2);
g.drawLine(x1+((x2-x1)*8)/11, y1, x1+((x2-x1)*9)/11, y2);
g.drawLine(x1+((x2-x1)*10)/11, y1,x2, y2);
}
public void drawDashedXie(int x1,int y1,int x2,int y2) { //畫斜虛線
g.drawLine(x1, y1, x1+((x2-x1)*1)/7, y2+((y1-y2)*6)/7);
g.drawLine(x1+((x2-x1)*2)/7, y2+((y1-y2)*5)/7, x1+((x2-x1)*3)/7, y2+((y1-y2)*4)/7);
g.drawLine(x1+((x2-x1)*4)/7, y2+((y1-y2)*3)/7, x1+((x2-x1)*5)/7, y2+((y1-y2)*2)/7);
g.drawLine(x1+((x2-x1)*6)/7, y2+((y1-y2)*1)/7, x2, y2);
}
public void drawCube(int x1,int y1,int x2,int y2) { //畫立方體
setXY(x1,y1,x2,y2);
int w,h;
if(x2 < x1 && y2 <y1) { //左上角
w = (x1-x2)/4; h = (y1-y2)/4;
g.drawLine(x2+w, y2, x1, y2); //橫
g.drawLine(x2, y2+h, x1-w, y2+h);
g.drawLine(x2, y1, x1-w, y1);
drawDashedHeng(x2+w,y1-h,x1,y1-h);
g.drawLine(x2, y2+h, x2, y1); //豎
g.drawLine(x1-w, y2+h, x1-w, y1);
g.drawLine(x1, y2, x1, y1-h);
drawDashedShu(x2+w,y2,x2+w,y1-h);
g.drawLine(x2, y2+h, x2+w, y2); //斜
g.drawLine(x1-w, y2+h, x1, y2);
g.drawLine(x1-w, y1, x1, y1-h);
drawDashedXie(x2,y1,x2+w,y1-h);
}
else if(x2 > x1 && y2 < y1) { //右上角
w = (x2-x1)/4; h = (y1-y2)/4;
g.drawLine(x1+w, y2, x2, y2);
g.drawLine(x1, y2+h, x2-w, y2+h);
g.drawLine(x1, y1, x2-w, y1);
drawDashedHeng(x1+w,y1-h,x2,y1-h);
g.drawLine(x1, y2+h, x1, y1);
g.drawLine(x2-w, y2+h, x2-w, y1);
g.drawLine(x2, y2, x2, y1-h);
drawDashedShu(x1+w,y2,x1+w,y1-h);
g.drawLine(x1, y2+h, x1+w, y2);
g.drawLine(x2-w, y2+h, x2, y2);
g.drawLine(x2-w, y1, x2, y1-h);
drawDashedXie(x1,y1,x1+w,y1-h);
}
else if(x2 < x1 && y2 > y1) { //左下角
w = (x1-x2)/4; h = (y2-y1)/4;
g.drawLine(x2+w, y1, x1, y1);
g.drawLine(x2, y1+h, x1-w, y1+h);
g.drawLine(x2, y2, x1-w, y2);
drawDashedHeng(x2+w,y2-h,x1,y2-h);
g.drawLine(x2, y1+h, x2, y2);
g.drawLine(x1-w, y1+h, x1-w, y2);
g.drawLine(x1, y1, x1, y2-h);
drawDashedShu(x2+w,y1,x2+w,y2-h);
g.drawLine(x2, y1+h, x2+w, y1);
g.drawLine(x1-w, y1+h, x1, y1);
g.drawLine(x1-w, y2, x1, y2-h);
drawDashedXie(x2,y2,x2+w,y2-h);
}
else { //右下角
w = (x2-x1)/4; h = (y2-y1)/4;
g.drawLine(x1+w, y1, x2, y1);
g.drawLine(x1, y1+h, x2-w, y1+h);
g.drawLine(x1, y2, x2-w, y2);
drawDashedHeng(x1+w,y2-h,x2,y2-h);
g.drawLine(x1, y1+h, x1, y2);
g.drawLine(x2-w, y1+h, x2-w, y2);
g.drawLine(x2, y1, x2, y2-h);
drawDashedShu(x1+w,y1,x1+w,y2-h);
g.drawLine(x1, y1+h, x1+w, y1);
g.drawLine(x2-w, y1+h, x2, y1);
g.drawLine(x2-w, y2, x2, y2-h);
drawDashedXie(x1,y2,x1+w,y2-h);
}
}
}
7. 畫圖
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
/*
* 畫圖
*/
public class MyDrawImag extends JFrame{
private Graphics g;
private int x1,y1,x2,y2;
private Observer observer;
//Image img = new Image("C:\\Users\\ASUS\\Desktop\\p2.png"); 是錯誤的,Image不能夠由於例項化
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public MyDrawImag(Graphics g) {
this.g = g;
}
public void drawImage(int x1,int y1,int x2,int y2,Image image) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1)
g.drawImage(image, x2, y2, x1-x2, y1-y2, observer);
else if(x2 > x1 && y2 < y1)
g.drawImage(image,x1, y2, x2-x1, y1-y2,observer);
else if(x2 < x1 && y2 > y1)
g.drawImage(image,x2, y1, x1-x2, y2-y1,observer);
else
g.drawImage(image,x1, y1, x2-x1, y2-y1,observer);
}
}
8. 畫五角星
import java.awt.Graphics;
/*
* 畫五角星
*/
public class MyDrawPentagram {
private Graphics g;
int x1,y1,x2,y2;
int w,h; //記錄寬,高
int a1X,a1Y,a2X,a2Y,a3X,a3Y,a4X,a4Y,a5X,a5Y,a6X,a6Y,a7X,a7Y,a8X,a8Y,a9X,a9Y,a10X,a10Y; //記錄五角星的每個角
MyDrawPentagram(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void Line() {
g.drawLine(a1X, a1Y, a2X,a2Y);
g.drawLine(a2X, a2Y, a3X,a3Y);
g.drawLine(a3X, a3Y, a4X,a4Y);
g.drawLine(a4X, a4Y, a5X,a5Y);
g.drawLine(a5X, a5Y, a6X,a6Y);
g.drawLine(a6X, a6Y, a7X,a7Y);
g.drawLine(a7X, a7Y, a8X,a8Y);
g.drawLine(a8X, a8Y, a9X,a9Y);
g.drawLine(a9X, a9Y, a10X,a10Y);
g.drawLine(a10X, a10Y, a1X,a1Y);
}
public void drawPentagram(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1) {
w = x1-x2; h = y1-y2;
a1X = x2+w/2; a1Y = y2;
a2X = x2+(w*3)/5; a2Y = y2+(h*2)/5;
a3X = x1; a3Y = y2+(h*2)/5;
a4X = x2+(w*2)/3; a4Y = y2+(h*2)/3;
a5X = x2+(w*4)/5; a5Y = y1;
a6X = x2+w/2; a6Y = y2+(h*4)/5;
a7X = x2+w/5; a7Y = y1;
a8X = x2+w/3; a8Y = y2+(h*2)/3;
a9X = x2; a9Y = y2+(h*2)/5;
a10X = x2+(w*2)/5; a10Y = y2+(h*2)/5;
Line();
}
else if(x2 > x1 && y2 < y1) {
w = x2-x1; h = y1-y2;
a1X = x1+w/2; a1Y = y2;
a2X = x1+(w*3)/5; a2Y = y2+(h*2)/5;
a3X = x2; a3Y = y2+(h*2)/5;
a4X = x1+(w*2)/3; a4Y = y2+(h*2)/3;
a5X = x1+(w*4)/5; a5Y = y1;
a6X = x1+w/2; a6Y = y2+(h*4)/5;
a7X = x1+w/5; a7Y = y1;
a8X = x1+w/3; a8Y = y2+(h*2)/3;
a9X = x1; a9Y = y2+(h*2)/5;
a10X = x1+(w*2)/5; a10Y = y2+(h*2)/5;
Line();
}
else if(x2 < x1 && y2 > y1) {
w = x1-x2; h = y2-y1;
a1X = x2+w/2; a1Y = y1;
a2X = x2+(w*3)/5; a2Y = y1+(h*2)/5;
a3X = x1; a3Y = y1+(h*2)/5;
a4X = x2+(w*2)/3; a4Y = y1+(h*2)/3;
a5X = x2+(w*4)/5; a5Y = y2;
a6X = x2+w/2; a6Y = y1+(h*4)/5;
a7X = x2+w/5; a7Y = y2;
a8X = x2+w/3; a8Y = y1+(h*2)/3;
a9X = x2; a9Y = y1+(h*2)/5;
a10X = x2+(w*2)/5; a10Y = y1+(h*2)/5;
Line();
}
else {
w = x2-x1; h = y2-y1;
a1X = x1+w/2; a1Y = y1;
a2X = x1+(w*3)/5; a2Y = y1+(h*2)/5;
a3X = x2; a3Y = y1+(h*2)/5;
a4X = x1+(w*2)/3; a4Y = y1+(h*2)/3;
a5X = x1+(w*4)/5; a5Y = y2;
a6X = x1+w/2; a6Y = y1+(h*4)/5;
a7X = x1+w/5; a7Y = y2;
a8X = x1+w/3; a8Y = y1+(h*2)/3;
a9X = x1; a9Y = y1+(h*2)/5;
a10X = x1+(w*2)/5; a10Y = y1+(h*2)/5;
Line();
}
}
}
9.畫矩形
import java.awt.Graphics;
/*
* 畫矩形
*/
public class MyDrawRect {
private Graphics g;
int x1,y1,x2,y2;
MyDrawRect(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void drawRect(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1)
g.drawRect(x2, y2, x1-x2, y1-y2);
else if(x2 > x1 && y2 < y1)
g.drawRect(x1, y2, x2-x1, y1-y2);
else if(x2 < x1 && y2 > y1)
g.drawRect(x2, y1, x1-x2, y2-y1);
else
g.drawRect(x1, y1, x2-x1, y2-y1);
}
}