zoukankan      html  css  js  c++  java
  • 第十二周java实验作业

    实验十二  图形程序设计

    实验时间 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:

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

    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类有了一定的区分。还需要继续深入学习。

  • 相关阅读:
    基于python实现自动化办公学习笔记一
    [django]上下文管理器
    [django]中间件
    分布式锁实现
    为什么Redis可以方便地实现分布式锁
    索引字段说明
    COUNT 和 IFNULL函数
    占用空间区别
    java排序算法(七):折半插入排序
    java排序算法(六):直接插入排序
  • 原文地址:https://www.cnblogs.com/980303CYR/p/9977552.html
Copyright © 2011-2022 走看看