张云飞 201771010143 《面对对象程序设计(java)》第十二周学习总结

实验十二  图形程序设计

理论:

10.1 AWT与Swing简介

10.2 框架的创建
10.3 图形程序设计
10.4 显示图像

(具体学习总结在最后)

实验:

实验时间 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,结合程序运行结果理解程序;与上面程序对比,思考异同;

代码:

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

SimpleFrameTest

运行结果:

两个程序的异同点:

相同点:

1.生成的框架(frame)大小和初始位置相同

2.生成的框架均可以由用户改变大小(setResizable方法)

3.均设置了setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)方法以关闭框架

不同点:

示例1:

使用setBounds方法设置框架大小

p407 10-1:

使用继承的方法(用lambda表达式简化)设置框架大小

l 掌握空框架创建方法;

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

l 掌握GUI顶层窗口创建技术。

测试程序2:

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

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

框架的图标发生变化

代码:

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()
{
// 获得Dimension的大小

Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

// 确定框架的大小,以及在屏幕上的位置

setSize(screenWidth / 2, screenHeight / 2);
setLocationByPlatform(true);

// 将图像设置为框架的图标

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

SizedFrameTest

运行结果:

 

测试程序3:

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

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

NotHelloWorld

运行结果:

测试程序4:

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

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;

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

DrawTest

运行结果:

测试程序5:

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

本程序中的“Hello,World!”是图形,不是文本,无法拷贝

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

FontTest

运行结果:

测试程序6:

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

该程序中的gif图像会通过for循坏语句,在框架的长和宽上整齐排列,全部铺满

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

/**
 * 带有图像组建的框架
 */
class ImageFrame extends JFrame
{
   public ImageFrame()
   {
      add(new ImageComponent());
      pack();
   }
}

/**
 * 显示平铺图像的组件
 */
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)

本周学习总结:

对象窗口工具箱(Abstact Window Toolkit,AWT),是JAVA1.0刚出现的时候,包含的一个GUI设计类库。它将处理用户界面元素的任务委派给每个目标平台(Windows,Macintosh等)的本地GUI工具箱。本地GUI工具箱负责用户界面元素的创建和动作。比方说,如果你在JAVA窗体中放一个文本框,就会有一个低层的“对等体”文本框(应该可以理解为本地图形元素吧)用来真正处理文本输入。理论上讲程序可以运行在任何平台。 SUN公司对AWTi的口号是:一次编写,随处使用。

  对于简单的应用程序来说,这种方法还是不错的。但是想要编写依赖本地一些高质量、可移植的图形库的程序就会暴露出一些缺陷。首先用户界面元素在不同的平台上,在观感上和操作行为上存在一些差别。其次由于同平台上的用户界面元素的丰富程度不同,可移植库的规模很受限制。最糟糕的是在不同平台上AWT用户界面库存在着不同的bug.研发人员需要在每个平台上测试应用程序,人们戏称AWT为:一次编写,随处调试。

  后来SUN公司和Netscape创建了Swing用户界面库,它的模式是用户把用户界面元素绘制在空白窗口上,而对等体只需要完成对窗口的创建和绘制。Swing是Java基础类库(Java Foundation Class,JFC)的一部分,而这个类库除了Swing组件,还包括一个可访问性API,一个2DAPI和一个可拖放API。

  Swing显示元素要比显示AWT基于对等体组件的速度慢一些。但是这不是啥大问题,Swing有无可抗拒的优势使得人们选择Swing:

  1、Swing有一个丰富、便捷的用户界面元素集合

  2、Swing对底层平台依赖很少,因此与平台相关的bug少。

  3、Swing给予不同平台的用户一致的感觉

  即使这样,Swing并不能够完全取代AWT,它是基于AWT之上,Swing仅仅提供了强大的用户界面组件,还是要使用基本的AWT处理事件的。

  other tips:

  1、绝大多是Swing组件都是以"J"开头,eg:JButton,JFrame等。而AWT组件类不带“J”,eg:Button,JFrame等。二者若混合使用会导致组件视觉和行为的不一致。

  2、Swing类位于javav.swing包中,包名javax表示这是一个Java扩展包,而不是核心包。

 

 

猜你喜欢

转载自www.cnblogs.com/fairber/p/9977424.html