|
上面這個 Java applet 的製作(產生流程):
- 決定程式功能與內容 產生一個方格, 四邊加框, 依照參數可改變框內文字訊息及相對位置 可加參數: message (文字), xOffset (整數), yOffset (整數)
- 先產生程式檔(從簡單程式開始, 當然功能也較少)lesson1.java 程式內容與說明
- 執行編譯, 產生 lesson1.class
\java\bin\javac lesson1.java |
- 建立 HTML 檔案 test1.html 檔案內容與說明
- 閱覽測試
- 用瀏覽器 執行 Netscape/IE然後開啟檔案test1.html
- \java\bin\appletViewer test1.html
|
- 作業練習:將上述 描繪字串內容, 增加描繪一些簡單幾何圖形 (描繪幾何圖形入門, 常用方法(methods)簡介)
- 修課同學將有一帳號, 請於個人跟目錄下建立 www 目錄
- www 目錄內 index.html 便是你的個人首頁
- 基本內涵:姓名, 學號, 作業表格(鍊結到作業內容)
- 其餘內容, 自行發揮
- 請自行設計你喜歡的 幾何圖形, 參考作法
- 拷貝一份 lesson1.java 命名為 exercise1.java
- 使用文書處理程式, 在 paint(Graphics g)子程式內 加上繪圖指令, 例如
// 黑色為原有指令, 藍色為新增指令, 檔案 lesson1_1.javapublic void paint(Graphics g){ g.clearRect(0,0,area.width-1,area.height-1); g.setColor(Color.black); g.drawRect(0,0,area.width-1,area.height-1); // 在字串底部加上黃色背景 FontMetrics fm =g.getFontMetrics(); // 字型參數資料物件 int sw=fm.stringWidth(message); // 字串寬度 int sh=fm.getHeight(); // 字型高度 g.setColor(Color.yellow); // 背景顏色 g.fillRect(x,y-sh,sw,sh); // 背景塗色 g.setColor(Color.black); // 字串顏色 g.drawString(message, x, y); // 在文字前端 畫一橢圓, g.setColor(Color.red); // 紅色 int dx=20,dy=16; g.drawOval(x-dx, y-dy, dx, dy); g.setColor(Color.green); g.fillOval(x-dx*3/4, y-dy*3/4, dx/2,dy/2); } |
- 建議 擴大 java 程式所佔空間(寬度及高度)
- 利用字串寬度資料, 該如何修改程式, 使得字串左右對稱
- 作業 有問題 或 完成 時, 請給我 e-mail
|
lesson1.java 程式內容與說明
import java.awt.*; // 內建 使用者介面 java程式庫 (package), 通常是必須的 // awt ( Abstract Windowing Toolkit) public class lesson1 extends java.applet.Applet { // 物件導向程式(object oriented) //宣告整體變數 global variables // 內建 資料格式宣告與起始設定(初值) String message = "Hello World!"; int x = 0, y = 0; Dimension area; // awt 所 定義資料格式(member variable) // 起始執行程式, 只執行一次的起始化 initialization public void init(){ area = size(); // 傳回視窗的 寬度 與 高度 // 宣告 區域變數 String tmp = getParameter("message"); // 取得參數 message 的字串 // 控制流程 if ( tmp != null ) message = tmp; // 假如參數存在 // 運算符(operator) 與 運算式(expression) x = area.width/2 + getIntValue("xOffset"); // 設定字串起點位置 y = area.height/2 + getIntValue("yOffset"); } // 建立處理方式 (method) private int getIntValue (String param) { // 將 字串 轉為 整數的方法 // 宣告 區域變數 String tmp = getParameter(param); int iValue = 0; if ( tmp!= null) { } return iValue; } // 繪圖子程式, repaint() --> update(Graphics g) --> paint(Graphics g) public void paint(Graphics g) { // 畫在 物件 g 上 g.clearRect( 0, 0, area.width-1, area.height-1); // 清除區域 // 座標 :(x起點, y起點, 寬度, 高度) g.setColor(Color.black); // 設定顏色 g.drawRect(0, 0, area.width-1, area.height-1); // 化四方格子 g.drawString(message, x, y); // 畫字串於指定位置 } }
| 描繪幾何圖形入門, 常用方法(methods)簡介 : java.awt.Graphics Methods
- 基本說明:
- 繪圖時, 以螢幕的點(pixel)為單位, 範圍為四方形
- 以四方形左上角為原點 (x,y) = (0,0), x 往右, y 往下增加
- 如何知道 HTML 檔所給予 java 程式 空間大小
- 程式傳回物件 Dimension 包含兩元素(element) width 與 height
- 例如: (斜體字, 依個人喜好命名) Dimension area; // area = size(); // area.width 為寬度, area.height為高度
- 下列參數中常出現 (int x, int y, int width, int height)代表以 (x,y)為原點, 寬為 width, 高為 height 的方塊
|
- 繪圖參數設定與取得
- public abstract void clipRect(int x, int y, int width, int height);
| | - public abstract void setColor(Color c);
- public abstract Color getColor();
| - public abstract void setFont(Font font);
- public abstract Font getFont();
| - public FontMetrics getFontMetrics();
- public abstract FontMetrics getFontMetrics(Font f);
| - public abstract void setXORMode(Color c1);
- 每一繪圖指令, 以目前設定顏色 與 顏色 c1 交替使用來繪圖
- public abstract void setPaintMode();
|
- 線條描繪
- public abstract void drawLine(int x1, int y1, int x2, int y2);
| - public abstract void drawOval(int x, int y, int width, int height);
| - public void drawRect(int x, int y, int width, int height);
| - public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
- 類似 drawRect 但四角為橢圓弧, 寬高如末二參數
| - public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle);
- 描繪以 方塊區域中心 為原點, 四周範圍為邊的 橢圓弧
- 起始角度 startAngle, 描繪角度 arcAngle 以 角度為單位(非徑度)
- 三點鐘方向為零度, 逆時鐘方向為正
| - public abstract void drawPolygon(int xPoints[], int yPoints[], int nPoints);
- public void drawPolygon(Polygon p);
- 描繪以(Xi, Yi) 點所構成頭尾相連 nPoints 邊形
|
- 區域繪圖
- public abstract void clearRect(int x, int y, int width, int height);
| - public abstract void copyArea(int x, int y, int width, int height, int dx, int dy);
- 將區域內圖案複製到以 (x+dx,y+dy)為起點同樣大小區域內
| - public abstract void translate(int x, int y);
| - 以下各方法, 類似 線條描繪, 但僅以設定顏色塗滿 區域內部 (不含各邊)
- public void fill3DRect(int x, int y, int width, int height, boolean raised);
- public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);
- public abstract void fillOval(int x, int y, int width, int height);
- public abstract void fillPolygon(int xPoints[], int yPoints[], int nPoints);
- public void fillPolygon(Polygon p);
- public abstract void fillRect(int x, int y, int width, int height); public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);
|
- 畫出字串
- 方法
- public abstract void drawString(String str, int x, int y);
- public void drawBytes(byte data[], int offset, int length, int x, int y);
- public void drawChars(char data[], int offset, int length, int x, int y);
|
- 如何知道所畫字串範圍(借用 java.awt.FontMetrics 方法)
Graphics g = getGraphics(); // 繪圖物件 FontMetrics fm =g.getFontMetrics(); // 字型參數資料物件 int sw=fm.stringWidth(str); // 字串寬度 int sh=fm.getHeight(); // 字型高度 // getHeight() = getLeading() + getAscent() + getDescent() |
test1.html 檔案內容與說明
<HTML> <HEAD> <TITLE>java lesson-1</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#00FFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000"> <P> <APPLET CODE="lesson1.class" WIDTH=400 HEIGHT=50> <PARAM NAME="XOFFSET" VALUE="-150"> <PARAM NAME="message" VALUE="How are you! This is my first Java applet."> </APPLET> </P> </BODY> </HTML> | - 黑體字部份為 html 檔案基本格式
- 藍色部份為 與 java 程式關連部份
- 以 <APPLET CODE= lesson1.class WIDTH= 120 HEIGHT=50 > 為開始
- 黑體字部份必定要有, 斜體字 部份 依照 實際情況而變
- CODE= 指示所執行 java 程式路徑, 可加上完整路徑 或 URL 檔案格式, 例如 code=http://www.phy.ntnu.edu.tw/javapath/myjava.class
- WIDTH= 以及 HEIGHT= 指定 java 所佔有寬度與高度, 以 螢幕解析度 的點pixel 為單位
- 以 </APPLET> 為結尾
- 中間可加入所需的參數, 格式如下
- <PARAM NAME= name_for_parameter VALUE=value_of_parameter >
- name_for_parameter 為參數名稱, 需與程式內名稱配合, 不理會大小寫
- value_of_parameter 為參數數值, 可以是 整數, 實數(含小數點)或文字, 文字內容兩邊請加上" "雙引號
- java 程式實際讀取參數時, 皆以文字方式讀取, 之後再轉換資料格式
|
|
|