1. 程式人生 > >第十二周java實驗作業

第十二周java實驗作業

raw imp null 分享圖片 拾取 svi 轉換 結構 private

實驗十二 圖形程序設計

實驗時間 2018-11-14

1、實驗目的與要求

(1) 掌握Java GUI中框架創建及屬性設置中常用類的API;

Java的集合框架實現了對各種數據結構的封裝。

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)移動並縮放組件

Dimension getSize() 得到組件的大小

Void setSize(Dimsion d) 把組件縮小到指定的大小

java.awt.Window 1.0

void toFront() 放在其他窗口面前

void toBack() 放在其他窗口後面

void setLocationByPlatfrom(blooean b)有平臺選擇一個合適的位置

java.awt.Frame 1.0

void setResizable(boolaen b) 縮放框架

void setTitle(String s) 設置框架標題

void setIconImage(Image image) 將Image用作框架圖標

java.awt.Toolkit 1.0

Static Toolkit getdefaultToolkit() 返回默認工具箱

Demension getScreenSize() 得到用戶屏幕大小

(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(Point2D startX,Point2D startY,Point2D endX ,Point2D endX)

使用給定的起點和終點,構造一條直線。

(3) 了解Java GUI中2D圖形中字體與顏色的設置方法;

java.awt.Color 1.0

Color(int r,int g,int b)

創建一個顏色對象

參數:r 紅色值(0-255)

g 綠色值(0-255)

b 藍色值(0-255)

java.awt.Graphics 1.0

Color getColor()

Color setColor(Color c)

獲得的改變當前的顏色。所有後續的繪圖操作都使用這個新顏色

參數:c 新顏色

java.awt.Graphics 1.2

Color getPaint()

Color setPaint(Paint p)

獲取或設置這個圖形環境的繪制屬性。Color類實現了Paint接口。因此,可以使用這個方法繪制屬性設置為純色。

void fill(Shaps s) 用當前顏料填充該圖形

java.awt.Component 1.0

Color getBackground()

Color setBackground(Color c)

獲取或設置背景顏色。

參數:c 新背景顏色

Color getForeground()

Color setForeground(Color c)

獲取或設置前景顏色。

參數:c 新前景顏色

2、實驗內容和步驟

實驗1: 導入第10章示例程序,測試程序並進行代碼註釋。

測試程序1:

l 運行下列程序,觀察程序運行結果。

package 小陳;

import javax.swing.*;
public class SimpleFrameTest
{
   public static void main(String[] args)//框架
   {
     JFrame  frame = new JFrame(); 
     frame.setBounds(0, 0,300, 300);//四個參數:位置坐標(左上角),寬,高
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置默認的關閉操作
     frame.setVisible(true); //獲得或設置visible屬性,值為true,可見,false,不可見
   }
}

運行結果:

技術分享圖片

l 在elipse IDE中調試運行教材407頁程序10-1,結合程序運行結果理解程序;與上面程序對比,思考異同;

l 掌握空框架創建方法;

l 了解主線程與事件分派線程概念;

l 掌握GUI頂層窗口創建技術。

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表達式,通過線程去開啟一個對象
         {
            SimpleFrame frame = new SimpleFrame();//設置frame類對象
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //設置默認的關閉操作,值為3,退出並關閉
            frame.setVisible(true);//獲得或設置visible屬性,值為true,可見,false,不可見
         });
   }
}
//自定義組建類
class SimpleFrame extends JFrame//JFrame:框架類
{   
    private static final int DEFAULT_WIDTH = 300;//設置窗口寬度
    private static final int DEFAULT_HEIGHT = 200;//設置窗口高度,(靜態、私有)
   public SimpleFrame()
  {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//獲得當前組件的size屬性
  }
}

運行結果:

技術分享圖片

測試程序2

l 在elipse IDE中調試運行教材412頁程序10-2,結合程序運行結果理解程序;

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

package simpleFrame;

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;

      // set frame width, height and let platform pick screen location
      // 設置框架寬度、高度,並讓平臺拾取屏幕位置

      setSize(screenWidth / 2, screenHeight / 2);
      //將框架大小設置為屏幕寬高的50%
      setLocationByPlatform(true);//告知系統定位框架

      // set frame icon  設置框架圖標

      Image img = new ImageIcon("icon.gif").getImage();

      setIconImage(img);      
   }
}

運行結果:

技術分享圖片

測試程序3:

l 在elipse IDE中調試運行教材418頁程序10-3,結合運行結果理解程序;

l 掌握在框架中添加組件;

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

package simpleFrame;

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

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;
   
   //由paintComponent方法自動獲得一個Graphics2D類對象
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;//類型轉換

      // draw a rectangle 畫一個矩形

      double leftX = 100;
      double topY = 100;
      double width = 200;
      double height = 150;

      Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
      g2.draw(rect);//調用Graphics2D類中的draw方法

      // 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 simpleFrame;

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(() ->////lambda表達式,通過線程去開啟一個對象
         {
            JFrame frame = new FontFrame();//創建frame類對象
            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;
//執行將Hello world 字段畫上去,畫出來的文字是以圖形的形式顯示的
   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); }
}

運行結果:

技術分享圖片

測試程序6:

l 在elipse IDE中調試運行教材436頁-437程序10-6,結合程序運行結果理解程序;

l 了解2D圖形圖像的顯示方法。

package simpleFrame;

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 = 1920;
   private static final int DEFAULT_HEIGHT = 1080;

   private Image image;

   public ImageComponent()
   {
      image = new ImageIcon("楚商郁雪.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); }
}

運行結果:

技術分享圖片

總結:本周主要學習了java的圖形程序設計。通過本周的學習,我對圖形創建過程中一些常用的API有了一定的了解。學會了框架的創建等知識。對swing類和awt類有了一定的區分。還需要繼續深入學習。

第十二周java實驗作業