201771010112罗松《面向对象程序设计(java)》第十二周学习总结
一、實驗?zāi)康呐c要求
(1)?掌握J(rèn)ava?GUI中框架創(chuàng)建及屬性設(shè)置中常用類的API;
(2)?掌握J(rèn)ava?GUI中2D圖形繪制常用類的API;
(3)?了解Java?GUI中2D圖形中字體與顏色的設(shè)置方法;
(4)?了解Java?GUI中2D圖像的載入方法。
2、實驗內(nèi)容和步驟
實驗1:?導(dǎo)入第10章示例程序,測試程序并進(jìn)行代碼注釋。
測試程序1:
l?運行下列程序,觀察程序運行結(jié)果。
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中調(diào)試運行教材407頁程序10-1,結(jié)合程序運行結(jié)果理解程序;與上面程序?qū)Ρ?#xff0c;思考異同;
l?掌握空框架創(chuàng)建方法;
l?了解主線程與事件分派線程概念;
l?掌握GUI頂層窗口創(chuàng)建技術(shù)。
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(() ->//lambda表達(dá)式 {SimpleFrame frame = new SimpleFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出并關(guān)閉,值是3frame.setVisible(true);});} }class SimpleFrame extends JFrame {private static final int DEFAULT_WIDTH = 300;private static final int DEFAULT_HEIGHT = 200;public SimpleFrame()//構(gòu)造器 {setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//調(diào)節(jié)組件的大小 } }?實驗結(jié)果如下圖所示:
區(qū)別:所給示例程序是Java自帶的,而測試程序是用戶自定義的。
測試程序2:
l?在elipse?IDE中調(diào)試運行教材412頁程序10-2,結(jié)合程序運行結(jié)果理解程序;
l?掌握確定框架常用屬性的設(shè)置方法。
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();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;// 設(shè)置框架寬度、高度,并讓平臺拾取屏幕位置setSize(screenWidth / 2, screenHeight / 2);setLocationByPlatform(true);// 設(shè)置框架圖標(biāo) Image img = new ImageIcon("icon.gif").getImage();setIconImage(img); } }實驗結(jié)果如下圖所示:
測試程序3:
l?在elipse?IDE中調(diào)試運行教材418頁程序10-3,結(jié)合運行結(jié)果理解程序;
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();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;//行坐標(biāo)public static final int MESSAGE_Y = 100;//列坐標(biāo)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); } }實驗結(jié)果如下圖所示:
測試程序4:
l?在elipse?IDE中調(diào)試運行教材424?-425頁程序10-4,結(jié)合程序運行結(jié)果理解程序;
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;// 畫一個矩形double leftX = 100;double topY = 100;double width = 200;double height = 150;Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);g2.draw(rect);// 畫出封閉的橢圓 Ellipse2D 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;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); } }實驗結(jié)果如下圖所示:
?
測試程序5:
l?在elipse?IDE中調(diào)試運行教材432頁-433程序10-5,結(jié)合程序運行結(jié)果理解程序;
l?了解2D圖形中字體的設(shè)置的方法;
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());//往容器內(nèi)添加了一個窗口 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);// 測量message的大小 FontRenderContext context = g2.getFontRenderContext();Rectangle2D bounds = f.getStringBounds(message, context);// 設(shè)置(x,y) =文本左上角double x = (getWidth() - bounds.getWidth()) / 2;double y = (getHeight() - bounds.getHeight()) / 2;// 增加上升到y(tǒng)以達(dá)到基線double ascent = -bounds.getY();double baseY = y + ascent;//畫出message g2.drawString(message, (int) x, (int) baseY);g2.setPaint(Color.LIGHT_GRAY);// 畫出基線 g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));// 繪制包圍的矩形 Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());g2.draw(rect);}public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }實驗結(jié)果如圖所示:
測試程序6:
l?在elipse?IDE中調(diào)試運行教材436頁-437程序10-6,結(jié)合程序運行結(jié)果理解程序;
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); } }實驗2:課后完成PTA平臺題目集:2018秋季西北師范大學(xué)面向?qū)ο蟪绦蛟O(shè)計(Java)練習(xí)題集(ch6-ch9)
三、實驗總結(jié)
通過本周的學(xué)習(xí),我知道了AWT與Swing的概念,了解了Java GUI中框架創(chuàng)建及屬性設(shè)置中常用的API。具體了解Swing庫中容器,組件和框架的的定義,大致了解了圖形程序設(shè)計,知道了以后如果開發(fā)游戲,這方面的知識非常有用,我會利用閑暇時間努力學(xué)習(xí)的。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xuezhiqian/p/9977557.html
總結(jié)
以上是生活随笔為你收集整理的201771010112罗松《面向对象程序设计(java)》第十二周学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。