博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
201771010130 王志成《面向对象程序设计(java)》第十二周学习总结
阅读量:5097 次
发布时间:2019-06-13

本文共 8926 字,大约阅读时间需要 29 分钟。

 

 

 

 

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

l 掌握空框架创建方法;

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

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

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

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

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

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:

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

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

l 掌握自定义组件的用法。

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图形的绘制方法。

 

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:

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

l 了解2D图形中字体的设置的方法;

 

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:

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("C:\\Users\\Administrator\\Desktop\\12.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); }}

 

 

 

 

总结:这周JAVA课程学习了图形程序设计,明白了组件是构成图形用户界面的元素,用图形表示(能在屏幕上显示,能和用户进行交互),容器是Java中能容纳和排列组件的组件。 常用的容器是框架(Frame,JFrame),了解了2D图形的绘画方式,又通过上一章的测试,使得对上一章知识有了更好的回顾。

 

转载于:https://www.cnblogs.com/847118824wang/p/9965211.html

你可能感兴趣的文章
android 通过AlarmManager实现守护进程
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>
win7下把电脑设置成wlan热
查看>>
Java 多态 虚方法
查看>>
jquery.validate插件在booststarp中的运用
查看>>
java常用的包
查看>>
PHP批量覆盖文件并执行cmd命令脚本
查看>>
Unity之fragment shader中如何获得视口空间中的坐标
查看>>
支持向量机——内核
查看>>
MFC注册热键
查看>>
万能的SQLHelper帮助类
查看>>
uboot分析:uboot的启动过程分析
查看>>
tmux的简单快捷键
查看>>
springboot笔记04——读取配置文件+使用slf4j日志
查看>>
[Swift]LeetCode653. 两数之和 IV - 输入 BST | Two Sum IV - Input is a BST
查看>>
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
查看>>
微信小程序的wxml文件和wxss文件在webstrom的支持
查看>>
Html5 离线页面缓存
查看>>
[php]在PHP中读取和写入WORD文档的代码
查看>>
WCF傻瓜模式写程序
查看>>