201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
?????????????????????? 實驗十二??圖形程序設計
實驗時間?2018-11-14
1、實驗目的與要求
(1)?掌握Java?GUI中框架創建及屬性設置中常用類的API;
創建空框架
. 在Java中,常采用框架(Frame)創建初始界面,即GUI的頂層窗口
. AWT庫中有一個基于對等體的Frame類。
. 該類的Swing版本為JFrame,JFrame是Frame子類。
框架定位與框架屬性
定位:
  常用Component類的setLocation和setBounds方法
常用屬性
  Title:框架標題
  IconImage:框架圖標
(4)確定框架大小
通過調用Toolkit類的方法來得到屏幕尺寸信息。
Toolkit kit = Toolkit.getDefaultToolkit(); //生成Toolkit對象
Dimension screenSize = kit.getScreenSize();//返回Dimension對象的屏幕大小
int screenWidth = screenSize.width;//獲得Dimension對象屏幕的寬度
int screenHeight=screenSize.height; ;//獲得Dimension對象屏幕的高度創建框架時的常用類及API
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) 把組件縮放到指定的大小
(2)?掌握Java?GUI中2D圖形繪制常用類的API;
? java.awt.geom.Retangle2D.Double 1.2
– Retangle2D.Double(double x,double y,double w,double h)
? java.awt.geom.Retangle2D.Float 1.2
– Retangle2D.Float(float x,float y,float w, float h)
? java.awt.geom.Ellipse2D.Double 1.2
– Ellipse2D.Double(double x,double y,double w,double h)
? java.awt.geom.Point2D.Double 1.2
– Point2D.Double(double x,double y)
? java.awt.geom.Line2D.Double 1.2
– Line2D.double(Point2D start,Point2D end)
– Line2D.double(double startX,doublestartY,double endX,double endY)
– fill(Graphics2D rec) 填充顏色
– draw(Graphics2D rec) 繪制圖形rec
? java.awt.Graphic2D 1.2
– void drawString(String str, int x, int y);
– void draw(Shape
(3)?了解Java?GUI中2D圖形中字體與顏色的設置方法;
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
? 通過指定紅綠藍三色比例,用Color類對象來復合成一種新的顏色。
? Color構造器如下:
Color(int redness,int greenness,int blueness)
其中參數取值為0--255
(3)常用API教材428頁)
? java.awt.Color 1.0
– void Color(int r,int g,int b)
? java.awt.Graphics 1.0
– void setColor(Color c)
– Color getColor( ));
? java.awt.Graphic2D 1.2
– void setPaint(paint p)
– void fill(Shape s)
? java.awt.Cmponent 1.0
– void setBackground(Color c)
void setForeground(Color c)
(2)字體風格
? Font.PLAIN
? Font.BOLD
? Fond.ITALIC
? Fond.BOLD + Font.ITALIC
(3)設置字體
? Font serif=new Font(“Serif”,Font.BOLD,14);
? g2.setFont(serif);
(4)字體應用的常用API(教材434頁-435頁)
? java.awt.Font 1.0
– Font(String name,int style,int Size)
– String getFontName()
– String getFamily()
– String getName()
– Retangle2D getStringBounds(String s,FontRenderContext context)
(4)?了解Java?GUI中2D圖像的載入方法。
?(1)在Java應用程序中,一旦圖像保存在本地或因特網的某個位置上,就可以將它們直接讀入到java應用程序中。
String filename = “…”;
Image image = ImageIcon(filename).getImage();
(2)完成將一個圖像文件加載到程序中,再調用Graphics類提供的DrawImage()顯示它。
public void paintComponent(Graphics g)
{
…
g.drawImage(image, x, y, null);
}
? java.awt.Graphics 1.0
– boolean drawImage(Image img,int x,int y,ImageObserver observer)
– boolean drawImage(Image img,int x,int y,int width,int height,ImageObserverobserver)
– void copyArea(int x,int y,int width,int height,int dx,int dy)
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);???? ???} } | 
??
l?在elipse?IDE中調試運行教材407頁程序10-1,結合程序運行結果理解程序;與上面程序對比,思考異同;
l?掌握空框架創建方法;
l?了解主線程與事件分派線程概念;
l?掌握GUI頂層窗口創建技術。
1 package SimpleFrame; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.33 2015-05-12 8 * @author Cay Horstmann 9 */ 10 public class SimpleFrameTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 SimpleFrame frame = new SimpleFrame(); 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setVisible(true); 19 }); 20 } 21 } 22 23 class SimpleFrame extends JFrame 24 { 25 private static final int DEFAULT_WIDTH = 300; 26 private static final int DEFAULT_HEIGHT = 200; 27 28 public SimpleFrame() 29 { 30 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 31 } 32 }
測試程序2:
l?在elipse?IDE中調試運行教材412頁程序10-2,結合程序運行結果理解程序;
l?掌握確定框架常用屬性的設置方法。
1 package sizedFrame; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-06-16 8 * @author Cay Horstmann 9 */ 10 public class SizedFrameTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new SizedFrame(); 17 frame.setTitle("SizedFrame"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 class SizedFrame extends JFrame 25 { 26 public SizedFrame() 27 { 28 // get screen dimensions 29 30 Toolkit kit = Toolkit.getDefaultToolkit(); 31 Dimension screenSize = kit.getScreenSize(); 32 int screenHeight = screenSize.height; 33 int screenWidth = screenSize.width; 34 35 // set frame width, height and let platform pick screen location 36 37 setSize(screenWidth / 2, screenHeight / 2); 38 setLocationByPlatform(true); 39 40 // set frame icon 41 42 Image img = new ImageIcon("icon.gif").getImage(); 43 setIconImage(img); 44 } 45 }
測試程序3:
l?在elipse?IDE中調試運行教材418頁程序10-3,結合運行結果理解程序;
l?掌握在框架中添加組件;
l?掌握自定義組件的用法。
1 package notHelloWorld; 2 3 import javax.swing.*; 4 import java.awt.*; 5 6 /** 7 * @version 1.33 2015-05-12 8 * @author Cay Horstmann 9 */ 10 public class NotHelloWorld 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new NotHelloWorldFrame(); 17 frame.setTitle("NotHelloWorld"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 /** 25 * A frame that contains a message panel 26 */ 27 class NotHelloWorldFrame extends JFrame 28 { 29 public NotHelloWorldFrame() 30 { 31 add(new NotHelloWorldComponent()); 32 pack(); 33 } 34 } 35 36 /** 37 * A component that displays a message. 38 */ 39 class NotHelloWorldComponent extends JComponent 40 { 41 public static final int MESSAGE_X = 75; 42 public static final int MESSAGE_Y = 100; 43 44 private static final int DEFAULT_WIDTH = 300; 45 private static final int DEFAULT_HEIGHT = 200; 46 47 public void paintComponent(Graphics g) 48 { 49 g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y); 50 } 51 52 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 53 }
測試程序4:
l?在elipse?IDE中調試運行教材424?-425頁程序10-4,結合程序運行結果理解程序;
l?掌握2D圖形的繪制方法。
1 package draw; 2 3 import java.awt.*; 4 import java.awt.geom.*; 5 import javax.swing.*; 6 7 /** 8 * @version 1.33 2007-05-12 9 * @author Cay Horstmann 10 */ 11 public class DrawTest 12 { 13 public static void main(String[] args) 14 { 15 EventQueue.invokeLater(() -> 16 { 17 JFrame frame = new DrawFrame(); 18 frame.setTitle("DrawTest"); 19 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 frame.setVisible(true); 21 }); 22 } 23 } 24 25 /** 26 * A frame that contains a panel with drawings 27 */ 28 class DrawFrame extends JFrame 29 { 30 public DrawFrame() 31 { 32 add(new DrawComponent()); 33 pack(); 34 } 35 } 36 37 /** 38 * A component that displays rectangles and ellipses. 39 */ 40 class DrawComponent extends JComponent 41 { 42 private static final int DEFAULT_WIDTH = 400; 43 private static final int DEFAULT_HEIGHT = 400; 44 45 public void paintComponent(Graphics g) 46 { 47 Graphics2D g2 = (Graphics2D) g; 48 49 // draw a rectangle 50 51 double leftX = 100; 52 double topY = 100; 53 double width = 200; 54 double height = 150; 55 56 Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); 57 g2.draw(rect); 58 59 // draw the enclosed ellipse 60 61 Ellipse2D ellipse = new Ellipse2D.Double(); 62 ellipse.setFrame(rect); 63 g2.draw(ellipse); 64 65 // draw a diagonal line 66 67 g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); 68 69 // draw a circle with the same center 70 71 double centerX = rect.getCenterX(); 72 double centerY = rect.getCenterY(); 73 double radius = 150; 74 75 Ellipse2D circle = new Ellipse2D.Double(); 76 circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); 77 g2.draw(circle); 78 } 79 80 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 81 }
?測試程序5:
l?在elipse?IDE中調試運行教材432頁-433程序10-5,結合程序運行結果理解程序;
l?了解2D圖形中字體的設置的方法;
1 package font; 2 3 import java.awt.*; 4 import java.awt.font.*; 5 import java.awt.geom.*; 6 import javax.swing.*; 7 8 /** 9 * @version 1.34 2015-05-12 10 * @author Cay Horstmann 11 */ 12 public class FontTest 13 { 14 public static void main(String[] args) 15 { 16 EventQueue.invokeLater(() -> 17 { 18 JFrame frame = new FontFrame(); 19 frame.setTitle("FontTest"); 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.setVisible(true); 22 }); 23 } 24 } 25 26 /** 27 * A frame with a text message component 28 */ 29 class FontFrame extends JFrame 30 { 31 public FontFrame() 32 { 33 add(new FontComponent()); 34 pack(); 35 } 36 } 37 38 /** 39 * A component that shows a centered message in a box. 40 */ 41 class FontComponent extends JComponent 42 { 43 private static final int DEFAULT_WIDTH = 300; 44 private static final int DEFAULT_HEIGHT = 200; 45 46 public void paintComponent(Graphics g) 47 { 48 Graphics2D g2 = (Graphics2D) g; 49 50 String message = "Happy every day!"; 51 52 Font f = new Font("Serif", Font.BOLD, 36); 53 g2.setFont(f); 54 55 // measure the size of the message 56 57 FontRenderContext context = g2.getFontRenderContext(); 58 Rectangle2D bounds = f.getStringBounds(message, context); 59 60 // set (x,y) = top left corner of text 61 62 double x = (getWidth() - bounds.getWidth()) / 2; 63 double y = (getHeight() - bounds.getHeight()) / 2; 64 65 // add ascent to y to reach the baseline 66 67 double ascent = -bounds.getY(); 68 double baseY = y + ascent; 69 70 // draw the message 71 72 g2.drawString(message, (int) x, (int) baseY); 73 74 g2.setPaint(Color.LIGHT_GRAY); 75 76 // draw the baseline 77 78 g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); 79 80 // draw the enclosing rectangle 81 82 Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); 83 g2.draw(rect); 84 } 85 86 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 87 }
?測試程序6:
l?在elipse?IDE中調試運行教材436頁-437程序10-6,結合程序運行結果理解程序;
l?了解2D圖形圖像的顯示方法。
1 package image; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-05-12 8 * @author Cay Horstmann 9 */ 10 public class ImageTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new ImageFrame(); 17 frame.setTitle("ImageTest"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 /** 25 * A frame with an image component 26 */ 27 class ImageFrame extends JFrame 28 { 29 public ImageFrame() 30 { 31 add(new ImageComponent()); 32 pack(); 33 } 34 } 35 36 /** 37 * A component that displays a tiled image 38 */ 39 class ImageComponent extends JComponent 40 { 41 private static final int DEFAULT_WIDTH = 300; 42 private static final int DEFAULT_HEIGHT = 200; 43 44 private Image image; 45 46 public ImageComponent() 47 { 48 image = new ImageIcon("blue-ball.gif").getImage(); 49 } 50 51 public void paintComponent(Graphics g) 52 { 53 if (image == null) return; 54 55 int imageWidth = image.getWidth(null); 56 int imageHeight = image.getHeight(null); 57 58 // draw the image in the upper-left corner 59 60 g.drawImage(image, 0, 0, null); 61 // tile the image across the component 62 63 for (int i = 0; i * imageWidth <= getWidth(); i++) 64 for (int j = 0; j * imageHeight <= getHeight(); j++) 65 if (i + j > 0) 66 g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); 67 } 68 69 public Dimension getPreferredSize() 70 { 71 return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); 72 } 73 }實驗2:課后完成PTA平臺題目集:2018秋季西北師范大學面向對象程序設計(Java)練習題集(ch6-ch9)
實驗總結:
?通過這次的課程設計的學習,我學到了Java中開發圖形應用程序的常用控件、?熟悉了JAVA?SWING中的容器、組件、布局以及事件監視器和事件接口的使用。通過該實驗掌握了JAVA圖形界面程序設計的方法。 對如何創建框架,以及如何生成一個繪制圖形的面板 ,以及如何在面板上繪制圖形有了一定的了解,以及如何設置窗口中的字體,以及窗口的背景,還有字體大小字體顏色有了一定了解,通過對源碼的查看,對如何設計窗口,以及窗口中的一些屬性有了進一步的理解。 ?轉載于:https://www.cnblogs.com/wy201771010126/p/9965683.html
總結
以上是生活随笔為你收集整理的201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 使用PHP获取图像文件的EXIF信息
- 下一篇: 计算机打字速成,计算机汉字输入五笔字型打
