201771010128王玉蘭《面向物件程式設計(Java)》第十二週學習總結
第一部分:理論知識
1.AWT與Swing簡介
(1)Swing使用者介面庫是非基於對等體的GUI工具箱。
Swing具有更豐富並且更方便的使用者介面元素集合。
Swing對底層平臺的依賴很少,因此與平臺相關的bug很少。
Swing會帶來交叉平臺上的統一視覺體驗。
Swing類庫被放在javax.swing包裡。
(2)兩者之間的關係:
大部分AWT元件都有其Swing的等價元件。
Swing元件的名字一般是在AWT元件名前面新增一個字母“J”,如:JButton,JFrame,JPanel等。
2.建立框架
(1)元件:通常把由Component類的子類或間接子類建立的物件稱為一個元件。
(2)容器是Java中能容納和排列元件的元件。.常用的容器是框架(Frame,JFrame)
(3)Container類提供了一個方法add(),用來在容器類元件物件中新增其他元件。
(4)容器本身也是一個元件,可以把一個容器新增到另一個容器裡,實現容器巢狀。
3.框架
(1)建立空框架
. 在Java中,常採用框架(Frame)建立初始介面,即GUI的頂層視窗. AWT庫中有一個基於對等體的Frame類。
. 該類的Swing版本為JFrame,JFrame是Frame子類。
定位:
—常用Component類的setLocation和setBounds方法常用屬性
—Title:框架標題
—IconImage:框架圖示
java.awt.Component 1.0
– boolean isVisible() 檢查元件是否可見
– void setVisible(boolean b) 設定元件可見
– void setSize(int width,int height) 把元件縮放到指
定寬度和高度
– void setBounds(int x,int y,int
width,int height) 移動並縮放元件
– Dimension getSize() 得到元件的大小
– void setSize(Dimension d) 把元件縮放到指定的大小
4.2D圖形
(1)Java SE了包含一個Java 2D庫,該庫提供了一個非常強大的圖形操作集。
(2) Graphics 2D 類是Graphics 類的子類, Graphics2D類物件通常可用Graphics物件轉換而來。
5.顏色的使用
Graphics2D類的setPaint方法(Graphics類為setColor方法)用來設定顏色。
例:g2.setPaint(Color.RED);g2.drawString(“Set Color”,100,100);
(1)Color類中定義的13種標準顏色
BLACK、BLUE、CYAN、DARK_GRAY、GRAY、GREEN、
LIGHT_GRAY、MAGENTA、ORANGE、PINK、RED、
WHITE、YELLOW
6,字型的使用:
(1)AWT中定義的五種邏輯字型名
SanaSerif
Serif
Monospaced
Dialog
DialogInput
這些邏輯字型在不同語言和作業系統上對映為不同
的物理字型。
(2)字型風格
Font.PLAIN
Font.BOLD
Fond.ITALIC
Fond.BOLD + Font.ITALIC
(3)設定字型
Font serif=new Font(“Serif”,Font.BOLD,14);
g2.setFont(serif);
第二部分:實驗部分:
1、實驗目的與要求
(1) 掌握Java GUI中框架建立及屬性設定中常用類的API;
(2) 掌握Java GUI中2D圖形繪製常用類的API;
(3) 瞭解Java GUI中2D圖形中字型與顏色的設定方法;
(4) 瞭解Java GUI中2D影象的載入方法。
2、實驗內容和步驟
實驗1: 匯入第10章示例程式,測試程式並進行程式碼註釋。
測試程式1:
l 執行下列程式,觀察程式執行結果。
import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(0, 0,300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } |
package demo; import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(300, 300,300, 300);//前兩個引數是位置,後兩個引數是視窗的長和寬 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//設定預設的關閉操作 frame.setVisible(false); //設定視窗是否可見 } }
l 在elipse IDE中除錯執行教材407頁程式10-1,結合程式執行結果理解程式;與上面程式對比,思考異同;
l 掌握空框架建立方法;
l 瞭解主執行緒與事件分派執行緒概念;
l 掌握GUI頂層視窗建立技術。
package simpleFrame; import java.awt.*; import javax.swing.*; /** * @version 1.34 2018-04-10 * @author Cay Horstmann */ public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { SimpleFrame frame = new SimpleFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// frame.setVisible(true);//判斷頁面是否可見 }); } } class SimpleFrame extends JFrame { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public SimpleFrame()//構造器 { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//設定彈出頁面的大小 } }
測試程式2:
l 在elipse IDE中除錯執行教材412頁程式10-2,結合程式執行結果理解程式;
l 掌握確定框架常用屬性的設定方法。
package sizedFrame; import java.awt.*; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class SizedFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { SizedFrame frame = new SizedFrame(); frame.setTitle("SizedFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } class SizedFrame extends JFrame { public SizedFrame() { //獲取螢幕尺寸 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; // 設定框架寬度、高度並讓平臺選擇螢幕位置 setSize(screenWidth / 2, screenHeight / 2); setLocationByPlatform(true); //設定幀圖示 Image img = new ImageIcon("icon.gif").getImage(); //生成ImgeIcon類物件,呼叫此物件的getImage方法 setIconImage(img); //用來定義使用者介面的圖示 } }
測試程式3:
l 在elipse IDE中除錯執行教材418頁程式10-3,結合執行結果理解程式;
l 掌握在框架中新增元件;
l 掌握自定義元件的用法。
package notHelloWorld; import javax.swing.*; import java.awt.*; /** * @version 1.34 2018-04-10 * @author Cay Horstmann */ public class NotHelloWorld { public static void main(String[] args) { EventQueue.invokeLater(() -> { NotHelloWorldFrame frame = new NotHelloWorldFrame(); frame.setTitle("NotHelloWorld"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame that contains a message panel */ class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame() { add(new NotHelloWorldComponent());//將指定元件追加到此容器的尾部 pack(); } } /** * A component that displays a message. */ class NotHelloWorldComponent extends JComponent { public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g) { g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
測試程式4:
l 在elipse IDE中除錯執行教材424 -425頁程式10-4,結合程式執行結果理解程式;
l 掌握2D圖形的繪製方法。
package draw; import java.awt.*; import java.awt.geom.*; import java.awt.geom.Rectangle2D.Double; import javax.swing.*; /** * @version 1.34 2018-04-10 * @author Cay Horstmann */ public class DrawTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { DrawFrame frame = new DrawFrame(); frame.setTitle("DrawTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame that contains a panel with drawings */ class DrawFrame extends JFrame { public DrawFrame() { add(new DrawComponent()); pack(); } } /** * A component that displays rectangles and ellipses. */ class DrawComponent extends JComponent { private static final int DEFAULT_WIDTH = 400; private static final int DEFAULT_HEIGHT = 400; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // 畫一個長方形; double leftX = 100; double topY = 100; double width = 200; double height = 150; Double rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); // 畫封閉橢圓 java.awt.geom.Ellipse2D.Double ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); // 畫一條對角線 g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); //用同一個中心畫一個圓 double centerX = rect.getCenterX(); double centerY = rect.getCenterY(); double radius = 150; java.awt.geom.Ellipse2D.Double circle = new Ellipse2D.Double(); circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); g2.draw(circle); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}
測試程式5:
l 在elipse IDE中除錯執行教材432頁-433程式10-5,結合程式執行結果理解程式;
l 瞭解2D圖形中字型的設定的方法;
package font; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.geom.Rectangle2D.Double; import javax.swing.*; /** * @version 1.35 2018-04-10 * @author Cay Horstmann */ public class FontTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { FontFrame frame = new FontFrame(); frame.setTitle("FontTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame with a text message component */ class FontFrame extends JFrame { public FontFrame() { add(new FontComponent()); pack(); } } /** * A component that shows a centered message in a box. */ class FontComponent extends JComponent { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); // 測量訊息的大小 FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // 設定(x,y)=文字左上角 double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // 增加到Y到達基線 double ascent = -bounds.getY(); double baseY = y + ascent; // 汲取資訊 g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // 繪製基線 g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // 繪製封閉矩形 Double rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
測試程式6:
l 在elipse IDE中除錯執行教材436頁-437程式10-6,結合程式執行結果理解程式;
l 瞭解2D圖形影象的顯示方法。
package image; import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-05-12 * @author Cay Horstmann */ public class ImageTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new ImageFrame(); frame.setTitle("ImageTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame with an image component */ class ImageFrame extends JFrame { public ImageFrame() { add(new ImageComponent()); pack(); } } /** * A component that displays a tiled image */ class ImageComponent extends JComponent { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; private Image image; public ImageComponent() { image = new ImageIcon("blue-ball.gif").getImage(); } public void paintComponent(Graphics g) { if (image == null) return; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); // 在左上角繪製圖像 g.drawImage(image, 0, 0, null); // 在元件上平鋪影象 for (int i = 0; i * imageWidth <= getWidth(); i++) for (int j = 0; j * imageHeight <= getHeight(); j++) if (i + j > 0) g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}
package image; import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-05-12 * @author Cay Horstmann */ public class ImageTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new ImageFrame(); frame.setTitle("ImageTest"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame with an image component */ class ImageFrame extends JFrame { public ImageFrame() { add(new ImageComponent()); pack(); } } /** * A component that displays a tiled image */ class ImageComponent extends JComponent { private static final int DEFAULT_WIDTH =500; private static final int DEFAULT_HEIGHT = 300; private Image image; public ImageComponent() { image = new ImageIcon("water.gif").getImage(); } public void paintComponent(Graphics g) { if (image == null) return; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); // 在左上角繪製圖像 g.drawImage(image, 0, 0, null); // 在元件上平鋪影象 for (int i = 0; i * imageWidth <= getWidth(); i++) for (int j = 0; j * imageHeight <= getHeight(); j++) if (i + j > 0) g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
實驗2:課後完成PTA平臺題目集:2018秋季西北師範大學面向物件程式設計(Java)練習題集(ch6-ch9)
第三部分:實驗總結:
本週學習了圖形程式設計的內容,基本瞭解了使用者介面的框架設定、屬性,以及如何關閉視窗,用JFrame。(EXIT_ON_CLOSE、JFrame.DO_NOTHING_ON_CLOSE的用法);本章知識對我而言就是2D圖形的處理,在web前段開發技術課程中基本瞭解了字型、顏色的設定,最基本的用法需要牢記掌握,因此這一章比較容易理解這部分的內容;最後一個實驗,需要修改一下視窗的可關閉操作以及圖片的路徑設定,在實驗課上,有了學長的演示,下來自己做的時候就很簡單。但是畢竟這是在引領下完成的,我還需要思考自己能不能做出來。