1、實驗目的與要求java
(1) 掌握Java GUI中框架建立及屬性設置中經常使用類的API;程序員
(2) 掌握Java GUI中2D圖形繪製經常使用類的API;數組
(3) 瞭解Java GUI中2D圖形中字體與顏色的設置方法;框架
(4) 瞭解Java GUI中2D圖像的載入方法。ide
1、理論知識函數
10.1 AWT與Swing簡介工具
(1)用戶界面(User Interface) :用戶與計算機系統(各類程序)交互的接口
(2)圖形用戶界面(Graphical User Interface) :以圖形方式呈現的用戶界面學習
(3)Java的抽象窗口工具箱(AbstractWindow Toolkit,AWT)包含在java.awt包中,它提供了許 多用來設計GUI的組件類和容器類測試
(4)AWT庫處理用戶界面元素的方法:把圖形元素的創 建和行爲委託給本地GUI工具箱進行處理字體
(5)Swing用戶界面庫是非基於對等體的GUI工具箱。Swing具備更豐富而且更方便的用戶界面元素集合。
Swing對底層平臺的依賴不多,所以與平臺相關的bug不多。
(6) AWT與Swing的關係:大部分AWT組件都有其Swing的等價組件。
(7)Swing組件的名字通常是在AWT組件名前面添加一個字母「J」,如:JButton,JFrame,JPanel等。
10.2 框架的建立
(1)組件:由Component類的子類或間接子類建立的對象;
(2)容器是Java中能容納和排列組件的組件。 經常使用的容器是框架(Frame,JFrame)
(3)Container類提供了一個方法add(),用來在容器類 組件對象中添加其餘組件。 例如fra.add(button); fra.add(textField);
(4)容器自己也是一個組件,能夠把一個容器添加到 另外一個容器裏,實現容器嵌套;
(5)建立空框架:在Java中,常採用框架(Frame)建立初始界面, 即GUI的頂層窗口 ,AWT庫中有一個基於對等體的Frame類。
(6)Swing程序員最關心的是內容面板,也稱爲內容 窗格;
(7)在AWT中可調用add()方法把組件直接添加到AWT Frame中,在Swing中組件則添加到內容窗格里;
10.3 圖形程序設計
(1)2D對象 :Java SE了包含一個Java 2D庫,該庫提供了一個 很是強大的圖形操做集,Java 2D圖形類使用浮點數座標系,這樣可爲座標指定單位。
(2)2D庫爲每一個圖形類提供兩個版本的靜態內部類: –Retangle2D.Float –Retangle2D.Double
(3)顏色的使用:Graphics2D類的setPaint方法(Graphics類爲 setColor方法)用來設置顏色。
(4)複合色:經過指定紅綠藍三色比例,用Color類對象來複合成 一種新的顏色。 Color構造器以下: Color(intredness,intgreenness,intblueness)
(5)字體的使用:(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);
10.4顯示圖像
(1)在Java應用程序中,一旦圖像保存在本地或因 特網的某個位置上,就能夠將它們直接讀入到java 應用程序中;
(2)完成將一個圖像文件加載到程序中,再調用 Graphics類提供的DrawImage()顯示它。
2、實驗內容和步驟
實驗1: 導入第10章示例程序,測試程序並進行代碼註釋。
測試程序1:
運行下列程序,觀察程序運行結果。
package simpleFrame; import javax.swing.*; public class Sf { public static void main(String[] args) { JFrame frame = new JFrame(); //生成一個JFrame類 frame.setBounds(0, 0,300, 200);//給窗口設置位置、大小 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);//設置窗口可見 } }
結果以下:
package simpleFrame; import java.awt.*; import javax.swing.*; /** * @version 1.33 2015-05-12 * @author Cay Horstmann */ public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() ->//lamdba函數 { SimpleFrame frame = new SimpleFrame();//構建一個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.34 2015-06-16 * @author Cay Horstmann */ public class SizedFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new SizedFrame();//生成GUI界面對象 frame.setTitle("SizedFrame");//定義屬性 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉窗口 frame.setVisible(true); }); } } class SizedFrame extends JFrame//SizedFrame類繼承JFrame { public SizedFrame() { // get screen dimensions Toolkit kit = Toolkit.getDefaultToolkit();//生成Toolkit對象 Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height;//經過對象訪問屬性,得到Dimension對象屏幕的長度 int screenWidth = screenSize.width; // set frame width, height and let platform pick screen location setSize(screenWidth / 2, screenHeight / 2); setLocationByPlatform(true); // set frame icon Image img = new ImageIcon("icon.gif").getImage();//定義圖形用戶界面圖標 setIconImage(img); } }
結果以下:
測試程序3:
l 在elipse IDE中調試運行教材418頁程序10-3,結合運行結果理解程序;
l 掌握在框架中添加組件;
l 掌握自定義組件的用法。
package notHelloWorld; import javax.swing.*; import java.awt.*; /** * @version 1.33 2015-05-12 * @author Cay Horstmann */ public class NotHelloWorld { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new NotHelloWorldFrame();//生成GUI界面對象 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());//add方法的使用 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 javax.swing.*; /** * @version 1.33 2007-05-12 * @author Cay Horstmann */ public class DrawTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame 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; // draw a rectangle double leftX = 100; double topY = 100; double width = 200; double height = 150; //返回閉合矩形的長、寬、x、y座標值 Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); // draw the enclosed ellipse Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); // draw a diagonal line g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); // draw a circle with the same center double centerX = rect.getCenterX(); double centerY = rect.getCenterY(); double radius = 150; Ellipse2D 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 javax.swing.*; /** * @version 1.34 2015-05-12 * @author Cay Horstmann */ public class FontTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame 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());//add了組件 pack();//調整窗口大小 } } /** * A component that shows a centered message in a box. */ class FontComponent extends JComponent//繼承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); // measure the size of the message //測量信息大小 FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context);//獲取矩形的2D邊界 // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY(); double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle Rectangle2D 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("E:\\t01e7d4d9d2529f5500.gif").getImage(); } public void paintComponent(Graphics g) { if (image == null) return; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); // draw the image in the upper-left corner //獲取圖片的 寬和高 g.drawImage(image, 0, 0, null); // tile the image across the component 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); } }
結果以下:
3、總結
本章咱們學習了圖形程序設計,咱們學習了java GUI中框架建立及屬性設置中經常使用類的API、Java GUI中2D圖形繪製經常使用類的API,雖然不用所有記下,但我以爲在閒暇時刻仍是要多看這些API,才能更好的運用到程序中去,其次我瞭解Java GUI中2D圖形中字體與顏色的設置方法,能夠本身設置想要的字體和顏色,經過老師學長的講解和代碼註釋,我也能更好的理解新接觸到的那些知識,感受收穫頗豐。