狄慧201771010104《面向对象程序设计(java)》第十二周学习总结

实验十二  图形程序设计

实验时间 2018-11-14

一、知识点总结:

1、swing概述:swing基于AWT架构之上,Swing是指被绘制的用户界、面类,AWT是指像事件处理这样的窗口工具箱的底层机制,Swing可以让用户随意的选择喜欢的感官。

2、框架:没有包含在其他窗口中的窗口被称为框架(frame),在AWT中有一个Frame类,是用来描述框架的,这个类在Swing版本中是JFrame(绝大数的Swing组件都有J开头),它是极少数几个不绘制在画布上的Swing组件之一,它的修饰部件(如按钮,标题栏,图标等)由用户的窗口系统绘制,而不是由Swing绘制,Swing用来绘制框架里的内容。 

     JFrame.setVisible(true)方法可以让框架可见,

     JFrame.show()可以让框架可见并且置于其他窗口的前面。

     JFrame.setDefaultCloseOperation(int i)可以定义关闭这个框架时候的响应动作,让程序退出的常量是JFrame.EXIT_ON_CLOSE。(以上方法都不是静态方法!)在初始化完后,main方法退出,这个时候程序并没有终止,终止的只是主线程,事件调度线程保持程序处于激活状态,直到关闭框架或者调用System.exit。 

3、改变框架: 

  3.1、Component类:setVisible(boolean bool)设置窗口是否显示;

                                  set||getSize(int w,int h||Dimension d)设置/获取大小;

                                 set||getLocation(int x,int y)设置获取位置,setBounds(int x,int y,int w,int h)设置位置和大小。 

  3.2、windows类:toBack()将窗口移动到桌面窗口栈的后面 ;

                             toFront()将窗口移动到桌面的前面;

                             setLocationByPlatfrom(boolean bool)由平台选择一个合适的位置显示窗口。 

  3.3、Frame类: isResizable(boolean bool)是否可以让用户改变大小;

                           setTitle(String str)设置标题;

                           setIconImage(Image img)设置框架的图标。 

                           setUndecorate(boolean bool)框架显示中的标题栏以及关闭按钮这些是否显示;

                           setExtendedState(int state)设置窗口的状态,如Frame.ICONIFIED可以让窗口最小化。

  3.4、Toolkit类:getDefaultToolkit()返回一个默认的工具箱;

                         Dimension getScreen()返回用户屏幕的尺寸;

                         Image getImage(String filename)加载得到一个指定的图像对象。

4、绘制组件:绘制一个组件,需要定义一个扩展JComponent的类,并覆盖其中的paintComponent(Graphics g)方法,在JAVA中,所有的绘制都必须使用Craphics对象。只要窗口需要重新绘图时,事件处理器就会通告组件,从而执行所有组件的paintComponent方法。不用自己调用paintComponent方法,在应用程序需要重新绘图的时候,这个方法会自动的被调用,如果需要强制重新绘制组件,那么要调用的是repaint方法,他将引发采用相应配置的Graphics对象调用所有组件的paintComponent方法。

  4.1、JFrame类:getContentPane()返回框架里的内容窗口;

                           add(Component comp)将一个给定的组件添加到该框架的内容窗口中。 

  4.2、Component类:repaint()重新绘制组件。

  4.3、JPanel类是一个可以包含其他组件的容器,但同样也可以在其上面进行绘制。

 5、绘制2D图形:主要是用java.awt.Graphics2D类去绘制图形的。图形的类主要在java.awt.geom里。要掌握这些之间的关系。

      paintComponent方法可以自动获得一个Graphics对象,可以把他强制转型成Graphics2D对象。

二、实验内容:

1、实验目的与要求

(1) 掌握Java GUI中框架创建及属性设置中常用类的API

(2) 掌握Java GUI中2D图形绘制常用类的API;

(3) 了解Java GUI中2D图形中字体与颜色的设置方法;

(4) 了解Java GUI中2D图像的载入方法。

2、实验内容和步骤

实验1: 导入第10示例程序,测试程序并进行代码注释。

测试程序1:

运行下列程序,观察程序运行结果。

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

   }

}

package simpleFrame;
import javax.swing.*;
public class SimpleFrameTest1
{
   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);    
   }
}

elipse IDE中调试运行教材407页程序10-1,结合程序运行结果理解程序;与上面程序对比,思考异同;

掌握空框架创建方法;

了解主线程与事件分派线程概念;

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

测试程序2

elipse IDE中调试运行教材412页程序10-2,结合程序运行结果理解程序;

掌握确定框架常用属性的设置方法。

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

测试程序3

elipse IDE中调试运行教材418页程序10-3,结合运行结果理解程序;

掌握在框架中添加组件;

掌握自定义组件的用法。

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

elipse IDE中调试运行教材424 -425页程序10-4,结合程序运行结果理解程序;

掌握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;

      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

elipse IDE中调试运行教材432-433程序10-5,结合程序运行结果理解程序;

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

测试程序6

elipse IDE中调试运行教材436-437程序10-6,结合程序运行结果理解程序;

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

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

三、实验总结:

       本周我们主要学习了与图形用户界面设计有关的知识,通过本章的学习,我对图形用户界面设计的相关知识有了一定的了解,了解了创建框架时的一些常用API,以及如何用一些组件去画字,图形等。

猜你喜欢

转载自www.cnblogs.com/dhlll/p/9977110.html