1. 程式人生 > >馬凱軍201771010116《面向物件與程式設計Java》第十二週學習總結

馬凱軍201771010116《面向物件與程式設計Java》第十二週學習總結

一、理論與知識學習部分

Java的抽象視窗工具箱(Abstract Window Toolkit, AWT)包含在java.awt包中,它提供了許多用來設計GUI的元件類和容器類。

大部分AWT元件都有其Swing的等價元件,Swing元件的名字一般是在AWT元件名前面新增一個字母“J”。

通常把由Component類的子類或間接子類建立的物件稱為一個元件。

元件類的常用API: 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) 把元件縮放到指定的大小

 void setLocation(int x,int y)

容器是Java中能容納和排列元件的元件。常用的容器是框架(Frame,JFrame)。

Container類提供了一個方法add(),用來在容器類 元件物件中新增其他元件。

容器本身也是一個元件,可以把一個容器新增到 另一個容器裡,實現容器巢狀。

框架定位:常用Component類的setLocation和setBounds方法。常用屬性:Title:框架標題;IconImage:框架圖示。

確定框架大小:通過呼叫Toolkit類的方法來得到螢幕尺寸資訊。

Jframe的結構:它由根面板、層級面板、玻璃面板和內容面板(content pane)四層面板構成。

實驗十二  圖形程式設計

實驗時間 2018-11-14

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);    

   }

}

l 在elipse IDE中除錯執行教材407頁程式10-1,結合程式執行結果理解程式;與上面程式對比,思考異同;

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(() ->
         {
            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);
   }
}

l 掌握空框架建立方法;

l 瞭解主執行緒與事件分派執行緒概念;

l 掌握GUI頂層視窗建立技術。

測試程式2

l 在elipse IDE中除錯執行教材412頁程式10-2,結合程式執行結果理解程式;

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()
   {
      // get screen dimensions

      Toolkit kit = Toolkit.getDefaultToolkit();
      Dimension screenSize = kit.getScreenSize();
      int screenHeight = screenSize.height;
      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);      
   }
}

l 掌握確定框架常用屬性的設定方法。

測試程式3:

l 在elipse IDE中除錯執行教材418頁程式10-3,結合執行結果理解程式;

l 掌握在框架中新增元件;

l 掌握自定義元件的用法。

測試程式4:

l 在elipse IDE中除錯執行教材424 -425頁程式10-4,結合程式執行結果理解程式;

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()
   {
      // get screen dimensions

      Toolkit kit = Toolkit.getDefaultToolkit();
      Dimension screenSize = kit.getScreenSize();
      int screenHeight = screenSize.height;
      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);      
   }
}

l 掌握2D圖形的繪製方法。

測試程式5:

l 在elipse IDE中除錯執行教材432頁-433程式10-5,結合程式執行結果理解程式;

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());
      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);

      // measure the size of the message

      FontRenderContext context = g2.getFontRenderContext();
      Rectangle2D bounds = f.getStringBounds(message, context);

      // 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); }
}

l 瞭解2D圖形中字型的設定的方法;

測試程式6:

l 在elipse IDE中除錯執行教材436頁-437程式10-6,結合程式執行結果理解程式;

l 瞭解2D圖形影象的顯示方法。

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("book.jpg.jpg").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); }
}

 

實驗2課後完成PTA平臺題目集:2018秋季西北師範大學面向物件程式設計(Java)練習題集(ch6-ch9)

實驗總結:

       通過這次實驗的程式,以及課本知識的學習,我感覺Java的學習從剛開始在程式的的下方顯示出程式的執行結果到建立新的視窗,是一個很大的轉變,在這次的實驗主要學會了GUI中框架建立及屬性設定中常用類的API。