JAVA画板课程设计---总之岁月漫长,然而值得等待。

在校大学生的一份辛苦劳动成果写了一个小画板程序。

 

JAVA画板课程设计---总之岁月漫长,然而值得等待。_第1张图片

XI`AN TECHNOLOGICAL UNIVERSITY

 

实验报告

 

 

 

课程设计名称 画板软件                      

 

 

专    业:    软件工程                

班    级:    17060209                

姓    名:    杨路恒                  

学    号:    17060209117             

指导教师:    徐飞                    

成    绩:                            

                                 

    2018      6   26   

 

 

专业

软件工程

班级

17060209

姓名

杨路恒

学号

17060209117

 

实验课程

画板软件

指导教师

徐飞

实验日期

2018.6.26

同实验者

徐飞

实验项目

JAVA实现画板软件

实验设备及器材

计算机

西安工业大学实验报告

 

 

1  设计任务书... 1

1.1设计内容... 1

1.2设计任务及具体要求... 1

1.3软件开发运行环境.. 1

2  本组课题... 1

2.1课题... 1

2.2本人任务... 2

3     程序功能简介... 2

1.画板具体功能.. 2

2.功能分析:... 2

4     主体内容... 2

4.1设计分析... 2

4.2程序架构图... 3

4.2.1系统结构分析图... 3

4.2.2程序结构流程图... 4

4.2.3结构流程图... 5

4.3模块功能与说明... 6

4.4源程序... 10

4.4.1Gui类... 10

4.4.2DrawListener类... 13

4.4.3接口图形类... 18

4.5操作方法... 24

4.6实验结果... 25

4.7设计体会... 26

 

1  设计任务书

 

1.1设计内容

学习了《Java面向对象编程》的课程,为了更进一步的提高我们对Java的深入学习,进行了课程设计,我所选择课程设计是利用Java的GUI实现画图应用程度。其目的是通过本课程设计使我们能够较全面的掌握面向对象程序设计的有关概念和开发方法,以便能较全面地理解、掌握和综合运用所学的知识,提高自身的编程能力。

运用JAVA语言,实现界面画板,进行简单图形绘制。

1.2设计任务及具体要求

主要利用java语言设计开发一个小型的画图应用程序,至少具有如下功能:可以进行绘画,有颜色的选择,可以绘制直线、曲线、椭圆、矩形,操作界面要符合用户的一般习惯。

1.3软件开发运行环境

本软件开发平台:JAVA

本软件开发环境:IntelliJ IDEA Community Edition 2018.1.4 x64

本软件运行平台:Windows 10

 

2  本组课题

2.1课题

JAVA绘图板课程设计。

 

2.2本人任务

1.编写画板界面。

2.添加组件监听、鼠标监听功能。

3.编写图形类的接口。

4.编写各种图形类并实现图形的重绘。

3   程序功能简介

1.画板具体功能

1)利用Java设计一个画图应用程序;

2)可绘制直线、曲线、椭圆、矩形;

3)可改变线条颜色;

 

2.功能分析:

用户可以在界面上通过点击各个不同的按钮,选择实现绘制的是哪种图形和颜色,也可以清除画板,程序将自动把所绘制的图形类放在一个集合中,最大化和最小化后遍历该集合,图形实现重绘。

画板画笔分为两种,一种是Graphics,一种是Graphics 2D。可以用来画基本图形还有喷桶、橡皮擦效果。

 

 

 

 

 

 

 

 

 

4  主体内容

4.1设计分析

1.绘图工具已经广为使用,利用JAVA语言编写的画板可以满足基本的图形要求,为了实现绘制矩形、圆形、椭圆、多边形、直线、曲线铅笔等基本图形,在此程序中定义上述几种图形类,实现编写的一个重绘接口。

2.在用户界面GUI类中,继承了JFrame类,添加各种按钮、两个面板,分别用来画图和存放按钮组件,界面采用流式布局,为组件添加编写好的事件监听机制。

3.在事件监听DrawListener类中实现了MouseListener、MouseMotionListener、ActionListener监听接口,并重写其绘图方法。

4.在事件监听类中,定义一个字符串str用来储存按钮按下时上面的内容,如果无文字,则判断为颜色按钮,把画笔颜色color设置为按钮颜色;有文字的话根据按钮文字的不同,去调用不同的图形类方法,并在绘制图形后保存在集合中。

5.在界面GUI类中调用DrawListener类,新建draw引用,为每个按钮添加该事件引用并调用ArrayList集合类,遍历所绘制的图形类,进行重绘。

 

 

 

 

 

 

 

 

 

 

4.2程序架构图

4.2.1系统结构分析图

 

 

 

 

       
   
     
 

 

                            画图区                            画笔颜色

 

       
   
     
 

 

笔刷

 

           
     
   
       
 
 

 

                           工具栏               设置           橡皮檫

 

       
   
     
 

 

                                                             清除面板

                                                              

                          形状选择

 

                           
     
 
             
   
     
     
 
 
 

 

 

矩形      实矩形   椭圆       实椭圆       直线    曲线     多边形

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.2.2程序结构流程图

 

 
   

 

                        NetJavaShape接口

 

 
   

 

 

       图形类            事件监听类                 其它类

 

       
     
 
   

 

 

 

                         主界面类

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.2.3结构流程图

 

 
   

 

开始

 

       
     
 
   

 

 

                              Gui类

 

       
     
 
   

 

 

                           点击按钮

 

       
     
 
   

 

 

                        DrawListener类               NetJavaShape接口

 

               
     
           
 
   
 
 

 

 

 

                              判断

                             按钮               颜色类

 

       
     
   
 

 

 

 

                         图形类

 

               
     
 
     
 
   
     
 

 

 

 

       画图                        结束

 

 

 

 

4.3模块功能与说明

1.绘图主界面模块

1主类(GUI)用来生成主界面。

2完成画图的框架。

3主界面由画图区、工具栏组成,布局格式采用BorderLayout布局。

4界面截图

JAVA画板课程设计---总之岁月漫长,然而值得等待。_第2张图片

 

 

 

 

 

 

2.绘图类模块

1在程序中用到了JAVA的封装、多态、继承特性,定义了NetJavaShape接口,里面写了redraw()抽象方法。

2分别构造不同的图形类:直线类(reline)、实矩形类(refilljuxing)、曲线类(requxian)、射线类(reshexian)、矩形类(rejuxing)、折线类(rezhexian)、椭圆类(retuoyuan)、实椭圆类(refilltuoyuan)。

3每个图形类都实现NetJavaShape接口中的redraw()方法,每个方法中根据形状选择不同的绘制方法函数。

4每个类可以在事件监听类中新建不同的对象引用,根据按钮选择不同可以调用不同的图形类画法。

5部分代码

import java.awt.*;                //定义一个接口,里面用抽象方法让图形类实现该接口

public interface NetJavaShape{

    public abstract void redraw();

}

 

  1. 鼠标及组件的事件监听模块

1 DrawListener实现ActionListener、MouseListenerMouseMotionListener接口。

  2定义List getShapeArr()、setG(Graphics g)、setNowColor(JButton nowColor)方法用来获取图形类集合、画笔、当前按钮颜色。

  3在actionPerformed(ActionEvent e)方法中用来判断按钮按下时要执行哪种命令。mouseDragged(MouseEvent e)方法中用来执行射线、曲线、橡皮檫、笔刷操作。mousePressed(MouseEvent e)、mouseReleased(MouseEvent e)方法中记录当前鼠标点击和释放的位置坐标。mouseReleased(MouseEvent e)中执行矩形、椭圆、实椭圆、折线、直线、实矩形、喷桶、多边形、擦除面板操作。

  4 mouseClicked(MouseEvent e)方法中执行多边形连接操作。

  5模块代码

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.List;

 

public class DrawListener implements MouseListener,ActionListener,MouseMotionListener {               //实现鼠标、组件事件监听接口

    private Color color;                                                                              //颜色引用

    private Graphics g;                                                                               //创建画笔

    private Graphics2D g1;

    private String str;                                                                               //字符串用来存放按钮上的内容

    private int x1,x2,y1,y2,x3,y3,x4,y4,x,y,startx1,starty1,startx2,starty2;                          //坐标定义

    private JButton nowColor;                                                                         //当前选中颜色按钮

    private List shapeArr=new ArrayList();                                //存放图形类的集合

    //创建各种图形类对象引用

    private reline shapeline;

    private refilljuxing shapefilljuxing;

    private requxian shapequxian;

    private reshexian shapeshexian;

    private rejuxing shapejuxing;

    private rezhexian shapezhexian;

    private retuoyuan shapetuoyuan;

    private refilltuoyuan shapefilltuoyuan;

    private clear reclear;

    private bishua bs;

    private boolean flag=false;                                                                      //定义布尔变量用于判断多边形是否绘制

    private Random rand=new Random();                                                                //用随机数刻画喷桶的范围大小

 

    public List getShapeArr() {

        return shapeArr;

    }                                     //获取图形集合的方法

 

    public void setG(Graphics g){

        this.g=g;

    }                                                       //获取画笔方法

    public void setNowColor(JButton nowColor){

        this.nowColor=nowColor;

    }                            //获取当前颜色按钮

    @Override

    public void actionPerformed(ActionEvent e) {

        //获取按钮上的字符串

        if ("".equals(e.getActionCommand())){                                                       //点击的是颜色按钮

            JButton button=(JButton)e.getSource();

            color=button.getBackground();

            nowColor.setBackground(color);

        }

        else str=e.getActionCommand();                                                              //点击的是图形按钮

    }

 

 

4.4源程序

4.4.1Gui类

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.List;

import javax.swing.*;

class Gui extends JFrame{                                             //主界面类

    DrawListener draw=new DrawListener();                             //创建事件监听类对象

    private Graphics g;                                               //画笔是一个组件

 private List shapeArray=new ArrayList();

 

    JPanel panel;

    JPanel panel2;

    JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,clear,nowcolor,brush,eraser;

    JButton red,green,yellow,blue,purple,pink;

    public Gui(){                                                          //构造方法初始化对象

        panel=new JPanel();

        panel2=new JPanel(new GridLayout(15,6,10,10));         //网格布局

        button1=new JButton("矩形");

        button2=new JButton("椭圆");

        button3=new JButton("实椭圆");

        button4=new JButton("直线");

        button5=new JButton("射线");

        button6=new JButton("曲线");

        button7=new JButton("实矩形");

        button8=new JButton("折线");

        button9=new JButton("多边形");

        button10=new JButton("喷桶");

        brush=new JButton("笔刷");

        eraser=new JButton("橡皮擦");

        clear=new JButton("擦除面板");

        nowcolor=new JButton();

        nowcolor.setBackground(Color.black);

        red=new JButton();

        red.setBackground(Color.red);

        green=new JButton();

        green.setBackground(Color.green);

        yellow=new JButton();

        yellow.setBackground(Color.yellow);

        blue=new JButton();

        blue.setBackground(Color.blue);

        purple=new JButton();

        purple.setBackground(Color.magenta);

        pink=new JButton();

        pink.setBackground(Color.pink);

        //为按钮添加事件类对象

        button1.addActionListener(draw);

        button2.addActionListener(draw);

        button3.addActionListener(draw);

        button4.addActionListener(draw);

        button5.addActionListener(draw);

        button6.addActionListener(draw);

        button7.addActionListener(draw);

        button8.addActionListener(draw);

        button9.addActionListener(draw);

        button10.addActionListener(draw);

        brush.addActionListener(draw);

        eraser.addActionListener(draw);

        clear.addActionListener(draw);

        red.addActionListener(draw);

        green.addActionListener(draw);

        yellow.addActionListener(draw);

        blue.addActionListener(draw);

        purple.addActionListener(draw);

        pink.addActionListener(draw);

        nowcolor.addActionListener(draw);

        panel2.add(button1);

        panel2.add(button2);

        panel2.add(button3);

        panel2.add(button4);

        panel2.add(button5);

        panel2.add(button6);

        panel2.add(button7);

        panel2.add(button8);

        panel2.add(button9);

        panel2.add(button10);

        panel2.add(brush);

        panel2.add(eraser);

        panel2.add(red);

        panel2.add(green);

        panel2.add(yellow);

        panel2.add(blue);

        panel2.add(purple);

        panel2.add(pink);

        panel2.add(clear);

        panel2.add(nowcolor);

        panel.setBackground(Color.white);

        panel.setPreferredSize(new Dimension(880,800));

        panel2.setPreferredSize(new Dimension(190,100));

        this.add(panel,BorderLayout.WEST);

        this.add(panel2,BorderLayout.EAST);

        this.setTitle("简易画板***杨路恒");

        this.setSize(1100,800);

        this.setLocation(500,200);

        this.setResizable(false);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setVisible(true);

        //在绘图面板上画笔

        panel.addMouseListener(draw);

        panel.addMouseMotionListener(draw);

        g=panel.getGraphics();

        draw.setG(g);

        draw.setNowColor(nowcolor);

        shapeArray=draw.getShapeArr();

    }

 

    @Override

    public void paint(Graphics g) {                   //进行图形遍历重绘

        super.paint(g);

        for (NetJavaShape a:shapeArray){

            a.redraw();

        }

    }

 

    public static void main(String []args){

            new Gui();

        }     //程序运行

}

4.4.2DrawListener类

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.List;

 

public class DrawListener implements MouseListener,ActionListener,MouseMotionListener {               //实现鼠标、组件事件监听接口

    private Color color;                                                                              //颜色引用

    private Graphics g;                                                                               //创建画笔

    private Graphics2D g1;

    private String str;                                                                               //字符串用来存放按钮上的内容

    private int x1,x2,y1,y2,x3,y3,x4,y4,x,y,startx1,starty1,startx2,starty2;                          //坐标定义

    private JButton nowColor;                                                                         //当前选中颜色按钮

    private List shapeArr=new ArrayList();                                //存放图形类的集合

    //创建各种图形类对象引用

    private reline shapeline;

    private refilljuxing shapefilljuxing;

    private requxian shapequxian;

    private reshexian shapeshexian;

    private rejuxing shapejuxing;

    private rezhexian shapezhexian;

    private retuoyuan shapetuoyuan;

    private refilltuoyuan shapefilltuoyuan;

    private clear reclear;

    private bishua bs;

    private boolean flag=false;                                                                      //定义布尔变量用于判断多边形是否绘制

    private Random rand=new Random();                                                                //用随机数刻画喷桶的范围大小

 

    public List getShapeArr() {

        return shapeArr;

    }                                     //获取图形集合的方法

 

    public void setG(Graphics g){

        this.g=g;

    }                                                       //获取画笔方法

    public void setNowColor(JButton nowColor){

        this.nowColor=nowColor;

    }                            //获取当前颜色按钮

    @Override

    public void actionPerformed(ActionEvent e) {

        //获取按钮上的字符串

        if ("".equals(e.getActionCommand())){                                                       //点击的是颜色按钮

            JButton button=(JButton)e.getSource();

            color=button.getBackground();

            nowColor.setBackground(color);

        }

        else str=e.getActionCommand();                                                              //点击的是图形按钮

    }

 

    @Override

    public void mouseEntered(MouseEvent e) {

        System.out.println("成功");

    }

 

    @Override

    public void mouseExited(MouseEvent e) {

 

    }

    @Override

    public void mouseDragged(MouseEvent e) {

        if ("射线".equals(str)) {                               //调用射线类方法

            shapeshexian = new reshexian(g, x1, y1, x2, y2, color);

            shapeshexian.redraw();

            shapeArr.add(shapeshexian);                       //把射线类对象添加到集合中

            x2 = e.getX();

            y2 = e.getY();

        }

        else if ("曲线".equals(str)){

            x=e.getX();

            y=e.getY();

            shapequxian=new requxian(g,x,y,x1,y1,color);

            shapequxian.redraw();

            shapeArr.add(shapequxian);

            x1=x;

            y1=y;

 

        }

        else if ("橡皮擦".equals(str)){

            g.setColor(Color.white);

            g1=(Graphics2D)g;

            x=e.getX();

            y=e.getY();

            g1.setStroke(new BasicStroke(10));

            g1.drawLine(x,y,x1,y1);

            x1=e.getX();

            y1=e.getY();

            g1.drawLine(x,y,x1,y1);

            g1.setStroke(new BasicStroke(1));

 

        }

            else if ("笔刷".equals(str)){

                x1=e.getX();

                y1=e.getY();

                x2=e.getX();

                y2=e.getY();

                bs=new bishua(g,x1,y1,x2,y2,color);

                bs.redraw();

                shapeArr.add(bs);

 

        }

    }

    @Override

    public void mousePressed(MouseEvent e) {

        g.setColor(color);                                      //获取鼠标按下的画笔颜色和坐标

        x=e.getX();

        y=e.getY();

        x1=e.getX();

        y1=e.getY();

    }

 

    @Override

    public void mouseReleased(MouseEvent e) {

        x2=e.getX();                                         //获取鼠标释放时的坐标

        y2=e.getY();

        if ("矩形".equals(str)) {

            //shapejuxing=new rejuxing(g,Math.min(x,x1),Math.min(y,y1),Math.abs(x-x1),Math.abs(y-y1),color);

            shapejuxing=new rejuxing(g,x,y,x2,y2,color);

            shapejuxing.redraw();

            shapeArr.add(shapejuxing);

        }

        else if ("椭圆".equals(str)){

            x1=e.getX();

            y1=e.getY();

            shapetuoyuan=new retuoyuan(g,Math.min(x,x1),Math.max(y,y1),Math.abs(x-x1),Math.abs(y-y1),color);

            shapetuoyuan.redraw();

            shapeArr.add(shapetuoyuan);

 

        }

        else if ("实椭圆".equals(str)){

            x1=e.getX();

            y1=e.getY();

            shapefilltuoyuan=new refilltuoyuan(g,Math.min(x,x1),Math.max(y,y1),Math.abs(x-x1),Math.abs(y-y1),color);

            shapefilltuoyuan.redraw();

            shapeArr.add(shapefilltuoyuan);

 

        }

        else if ("折线".equals(str)){

            shapezhexian=new rezhexian(g,x,y,x3,y3,color);

            shapezhexian.redraw();

            x3=e.getX();

            y3=e.getY();

            shapezhexian=new rezhexian(g,x,y,x3,y3,color);

            shapezhexian.redraw();

            shapeArr.add(shapezhexian);

        }

        else if ("直线".equals(str)){

            x4=e.getX();

            y4=e.getY();

            shapeline=new reline(g,x,y,x4,y4,color);

            shapeline.redraw();

            shapeArr.add(shapeline);

        }

        else if ("实矩形".equals(str)){

            x3=e.getX();

            y3=e.getY();

            shapefilljuxing=new refilljuxing(g,x,y,x3,y3,color);

            shapefilljuxing.redraw();

            shapeArr.add(shapefilljuxing);

        }

        else if ("喷桶".equals(str)) {

            x2 = e.getX();

            y2 = e.getY();

            for (int i = 0; i < 20; i++) {

                int r1 = rand.nextInt(30) - 10;

                int r2 = rand.nextInt(30) - 10;

                g.drawLine(x2 + r1, y2 + r2, x2 + r1, y2 + r2);

            }

        }

        else if ("多边形".equals(str)){

            g.setColor(color);

            x2=e.getX();

            y2=e.getY();

            if (!flag) {                       //!flag说明为真

                g.drawLine(x1, y1, x2, y2);

                startx1=x1;

                starty1=y1;

                startx2=x2;

                starty2=y2;

                flag=true;

            }

 

        }

        else if ("擦除面板".equals(str)){

            reclear=new clear(g);

          reclear.redraw();

          shapeArr.add(reclear);

        }

    }

 

    @Override

    public void mouseClicked(MouseEvent e) {

        if(str.equals("多边形")&&flag){                            //鼠标点击时绘制连接多边形

            g.setColor(color);

                         x2=e.getX();                              //获取新的点的坐标

                         y2=e.getY();

                        if(e.getClickCount()==2){                  //记录点击次数,点击第二次后开始直线连接

                            g.drawLine(startx1,starty1,startx2,starty2);

                              flag=false;

                            }

                         g.drawLine(startx2,starty2,x2, y2);

 

                         startx2=x2;   //存下上个点的坐标

                         starty2=y2;

                        }

 

    }

 

    @Override

    public void mouseMoved(MouseEvent e) {

 

    }

 

}

4.4.3接口图形类

import java.awt.*;                //定义一个接口,里面用抽象方法让图形类实现该接口

public interface NetJavaShape{

    public abstract void redraw();

}

class reline implements NetJavaShape{                //实现重绘直线图形

    Graphics g;

    int x,y,x1,y1,x2,y2,x3,y3,x4,y4;

    Color color;

    public reline(Graphics g,int x1,int y1,int x2,int y2,Color color){    //构造方法初始化对象

        this.g=g;

        this.x1=x1;

        this.y1=y1;

        this.x2=x2;

        this.y2=y2;

        this.color=color;

    }

    @Override

    public void redraw() {                         //实现接口方法进行直线绘制

        g.setColor(color);

        g.drawLine(x1,y1,x2,y2);

    }

}

    class refilljuxing implements NetJavaShape {                //实现重绘实矩形图形

        Graphics g;

        int x, y, x1, y1, x2, y2;

        Color color;

 

        public refilljuxing(Graphics g, int x1, int y1, int x2, int y2, Color color) {

            this.g = g;

            this.x1 = x1;

            this.y1 = y1;

            this.x2 = x2;

            this.y2 = y2;

            this.color = color;

        }

 

        @Override

        public void redraw() {

            g.setColor(color);

            g.fillRect(x1,y1,x2,y2);                          //实现接口方法进行实矩形绘制

        }

    }

        class requxian implements NetJavaShape {                //实现重绘曲线图形

            Graphics g;

            int x, y, x1, y1, x2, y2;

            Color color;

 

            public requxian(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                this.g = g;

                this.x1 = x1;

                this.y1 = y1;

                this.x2 = x2;

                this.y2 = y2;

                this.color = color;

            }

 

            @Override

            public void redraw() {                                  //实现接口方法进行曲线绘制

                g.setColor(color);

                g.drawLine(x1,y1,x2,y2);

            }

        }

            class reshexian implements NetJavaShape {                //实现重绘射线方法

                Graphics g;

                int x, y, x1, y1, x2, y2;

                Color color;

 

                public reshexian(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                    this.g = g;

                    this.x1 = x1;

                    this.y1 = y1;

                    this.x2 = x2;

                    this.y2 = y2;

                    this.color = color;

                }

 

                @Override

                public void redraw() {                          //实现接口方法进行射线绘制

                    g.setColor(color);

                    g.drawLine(x1, y1, x2, y2);

                }

            }

                class rejuxing implements NetJavaShape {                //实现重绘矩形方法

                    Graphics g;

                    int x, y, x1, y1, x2, y2;

                    Color color;

 

                    public rejuxing(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                        this.g = g;

                        this.x1 = x1;

                        this.y1 = y1;

                        this.x2 = x2;

                        this.y2 = y2;

                        this.color = color;

                    }

 

                    @Override

                    public void redraw() {                                 //实现接口方法进行矩形绘制

                        g.setColor(color);

                         /*

                                窗体左上角的坐标为(0,0)。所以左上角的坐标是较小的.

                                所以只需调用一个取较小值的一个方法Math.min(x1,x2),Math.min(y1, y2)这样分别赋给x,y就可以了。

                                长宽的设置,我们也不能直接就x2-x1,y2-y1,需要对其取一个绝对值

                                Math.abs(x1-x2),Math.abs(y1-y2)。这样就可以正确的画出.

                                 */

                        //g.drawRect(Math.min(x1,x2),Math.max(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));

                        g.drawRect(x1,y1,x2,y2);

                    }

                }

                    class rezhexian implements NetJavaShape {                //实现重绘折线图形

                        Graphics g;

                        int x, y, x1, y1, x2, y2, x3, y3, x4, y4;

                        Color color;

 

                        public rezhexian(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                            this.g = g;

                            this.x1 = x1;

                            this.y1 = y1;

                            this.x2 = x2;

                            this.y2 = y2;

                            this.color = color;

                        }

 

                        @Override

                        public void redraw() {

                            g.setColor(color);

                            g.drawLine(x1, y1, x2, y2);

                        }

                    }

                        class retuoyuan implements NetJavaShape {                //实现重绘椭圆图形

                            Graphics g;

                            int  x1, y1, x2, y2;

                            Color color;

 

                            public retuoyuan(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                                this.g = g;

                                this.x1 = x1;

                                this.y1 = y1;

                                this.x2 = x2;

                                this.y2 = y2;

                                this.color = color;

                            }

 

                            @Override

                            public void redraw() {

                                g.setColor(color);

                                /*

                                窗体左上角的坐标为(0,0)。所以左上角的坐标是较小的.

                                所以只需调用一个取较小值的一个方法Math.min(x1,x2),Math.min(y1, y2)这样分别赋给x,y就可以了。

                                长宽的设置,我们也不能直接就x2-x1,y2-y1,需要对其取一个绝对值

                                Math.abs(x1-x2),Math.abs(y1-y2)。这样就可以正确的画出.

                                 */

                                g.drawOval(Math.min(x1,x2),Math.max(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));    //椭圆中x,y是左上角的位置,width,height是横轴和纵轴用Math函数找最大值最小值

                            }

                        }

                                class refilltuoyuan implements NetJavaShape {                //实现重绘实椭圆图形

                                        Graphics g;

                                        int x, y, x1, y1, x2, y2, x3, y3, x4, y4;

                                        Color color;

 

                                        public refilltuoyuan(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                                            this.g = g;

                                            this.x1 = x1;

                                            this.y1 = y1;

                                            this.x2 = x2;

                                            this.y2 = y2;

                                            this.color = color;

                                        }

 

                                        @Override

                                        public void redraw() {

                                            g.setColor(color);

                                            g.fillOval(Math.min(x1,x2),Math.max(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));

                                        }

                                }

                                       /* class redubianxing implements NetJavaShape{                //实现重绘多边形图形

                                            Graphics g;

                                            int x1, y1, x2, y2;

                                            Color color;

                                            public redubianxing(Graphics g, int x1, int y1, int x2, int y2, Color color){

                                                this.g = g;

                                                this.x1 = x1;

                                                this.y1 = y1;

                                                this.x2 = x2;

                                                this.y2 = y2;

                                                this.color = color;

                                            }

                                            @Override

                                            public void redraw() {

 

                                            }

                                        }*/

                                                class clear implements NetJavaShape{        //实现画板清除

                                                    Graphics g;

                                                    public clear(Graphics g){

                                                        this.g=g;

                                                    }

                                                    @Override

                                                    public void redraw() {

                                                        g.clearRect(0,0,8808,8800);

                                                    }

                                                }

                                                    class bishua implements NetJavaShape {     //实现笔刷功能

                                                        Graphics g;

                                                        Graphics2D g1;

                                                        int  x1, y1, x2, y2;

                                                        Color color;

 

                                                        public bishua(Graphics g, int x1, int y1, int x2, int y2, Color color) {

                                                            this.g1 = (Graphics2D) g;

                                                            this.x1 = x1;

                                                            this.y1 = y1;

                                                            this.x2 = x2;

                                                            this.y2 = y2;

                                                            this.color = color;

                                                        }

 

                                                        @Override

                                                        public void redraw() {

                                                            g1.setColor(color);

                                                            g1.setStroke(new BasicStroke(6));        //定义画笔大小

                                                            g1.drawLine(x1, y1, x2, y2);

                                                            g1.setStroke(new BasicStroke(2));

                                                        }

                                                    }

 

 

 

 

 

 

 

4.5操作方法

本软件由IntelliJ IDEA Community Edition 2018.1.4 x64开发,运行后直接进入界面。

1用户打开软件,点击工具栏界面上不同的按钮,可以画出不同的图形。    2系统默认画笔颜色是黑色,用户也可以点击颜色按钮进行不同颜色的图形绘制。                                                                             3当最大化和最小化时,绘制的图形不会改变,将自动进行图形重绘,也能够用笔刷和橡皮檫工具,实现图形美化。

 

 

 

 

 

4.6实验结果

基本实现图形绘制、颜色选择、橡皮檫、笔刷、喷桶功能,具体运行结果如下。

JAVA画板课程设计---总之岁月漫长,然而值得等待。_第3张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.7设计体会

当把程序写好并进行调试时,并不是一帆风顺的,在程序编译过程中遇到了许许多多的大大小小的错误,经过一次又一次的改正错误最终完成了程序设计

1画图应用程序的Java源代码全部正确能够通过编译并正常运行。

2画图程序要求功能:画直线、曲线、矩形、椭圆、选择颜色等功能完全实现。

 3通过Java编程完全实现了课程设计任务书的全部功能。学好一门语言,需要不断反复的练习,面向对象的设计模式,充分运用类与对象的关系,JAVA没有多继承,所以在画板软件中定义了一个接口,让不同的绘图类实现该接口,重写不同的方法。图形类中定义了面板、按钮组件,在事件监听类中,根据不同按钮,调用生成不同图形对象。在这里,为了实现重新绘制,需要图形对象放在集合中,然后在主类中遍历。
通过这次课程设计,提高了我对封装、继承、多态、接口、集合的认识,对事件处理也了解,知识体系进一步扩展,掌握的更加牢固紧密。

 

你可能感兴趣的:(JAVA)