1、 JAVA 期末画板实验报告课程题目:运用 JAVA 语言编写一个画板程序,这个画板要有画图,特殊图形绘制,颜色改变,画笔大小改变等功能。设计思想:1. 进行界面设计2. 进行算法实现3. 运行调试4. 总结问题设计过程:A. 界面设计新建一个 Jframe 容器,然后定义一个 panel( Panel convas2) ,convas2 是工具栏。用 borderlayout 布置容器的边框布局,将工具栏放在 NORTH 部分。完成界面设计。add(canvas2,BorderLayout.NORTH);Panel canvas2 = new Panel();添加工具按键Panel canv
2、as2 = new Panel();Button btn1 = new Button(“直线“ );Button btn2 = new Button(“椭圆“ );Button btn3 = new Button(“矩形“ );Button btn4 = new Button(“清除“ );Button btn5 = new Button(“画笔“ );Button btn6 = new Button(“橡皮“ );Choice choice = new Choice(); Choice choice1 = new Choice();canvas2.add(choice);canvas2.ad
3、d(choice1);canvas2.add(btn1);canvas2.add(btn2);canvas2.add(btn3);canvas2.add(btn4);canvas2.add(btn5);canvas2.add(btn6);在工具表中,有两个下拉菜单,分别收录画笔的大小和颜色。1. 画笔的大小:choice1.add(“1“);choice1.add(“3“);choice1.add(“5“);choice1.add(“7“);choice1.add(“9“);2. 画笔颜色choice.add(“黑色“);choice.add(“蓝色“);choice.add(“红色“);ch
4、oice.add(“黄色“);choice.add(“绿色“);3 展示画板B. 算法实现运用画板,首先要对工具栏按键和鼠标进行监听,然后对监听进行响应。运行相应程序进行画图1. 监听程序展示:choice.addItemListener(this);choice1.addItemListener(this);btn1.addActionListener(this);btn2.addActionListener(this);btn3.addActionListener(this);btn4.addActionListener(this);btn5.addActionListener(this)
5、;btn6.addActionListener(this);addMouseListener(this);addMouseMotionListener(this);2. 按键监听响应监听到按键后,判断当前会话工具public void actionPerformed(ActionEvent e) if(e.getSource().equals(btn1)a=1;if(e.getSource().equals(btn2)a=2;if(e.getSource().equals(btn3)a=3;if(e.getSource().equals(btn4) Graphics g = this.getG
6、raphics();g.clearRect(0, 0, this.getWidth(), this.getHeight();if(e.getSource().equals(btn5)a=4;if(e.getSource().equals(btn6)a=5;定义一个变量 a,对按键监听进行响应,例如当监听到 btn1时赋值 a=1(a 为 char 型) ,即当前工具为直线。但是当监听到 btn4 时不进行对 a 赋值而是直接进行清除操作,由于清除操作是对画布进行清除,不参与鼠标事件,因此可以直接执行。3. 鼠标监听响应a. 鼠标按钮按下事件,获取画图起始坐标 x0,y0.public void
7、 mousePressed(MouseEvent e)x0=e.getX();y0=e.getY();b. 鼠标移动事件public void mouseMoved(MouseEvent e) c.鼠标按钮松开事件,此次响应完成对直线,椭圆,矩形进行绘制。public void mouseReleased(MouseEvent e)Graphics2D g = (Graphics2D)this.getGraphics();size=new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);g.setColor(b);g.s
8、etStroke(size);switch(a)case 1:g.drawLine(x0, y0, e.getX(), e.getY();break;case 2:g.drawOval(x0, y0, e.getX() - x0, e.getY() - y0);break;case 3:g.drawRect(x0, y0, e.getX() - x0, e.getY() - y0);break;d.鼠标进入事件:public void mouseEntered(MouseEvent e)e.鼠标离开事件:public void mouseExited(MouseEvent e) f.鼠标单击事
9、件:public void mouseClicked(MouseEvent e)g.鼠标拖动事件:此次响应实现画笔(随笔画),橡皮的绘制。public void mouseDragged(MouseEvent e) Graphics2D g = (Graphics2D)this.getGraphics();size=new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);g.setColor(b);g.setStroke(size);switch(a)case 4:g.drawLine(x0, y0, e.getX(),
10、 e.getY();x0 = e.getX();y0 = e.getY();break;case 5:g.clearRect(x0 - 10, y0 - 10, 20, 20);x0 = e.getX();y0 = e.getY();break;4.下拉列表响应a.颜色下拉列表响应:if(e.getSource()=choice)if(choice.getSelectedIndex()=0)b=Color.black;if(choice.getSelectedIndex()=1)b=Color.BLUE;if(choice.getSelectedIndex()=2)b=Color.red;if
11、(choice.getSelectedIndex()=3)b=Color.yellow;if(choice.getSelectedIndex()=4)b=Color.green;调用choice.getSelectedIndex()返回被选中的选项的序号(下拉列表中第一个选项序号为0,依次递加。)即进行选择颜色的操作b.画笔大小下拉列表响应:if(e.getSource()=choice1)if(choice1.getSelectedItem()=“1“)con=1;else if(choice1.getSelectedItem()=“3“)con=3;else if(choice1.getS
12、electedItem()=“5“)con=5;else if(choice1.getSelectedItem()=“7“)con=7;else if(choice1.getSelectedItem()=“9“)con=9;调用choice1.getSelectedItem()返回被选中选项的标签文本字符串,即确定画笔的大小(con)。5.工具栏工具实现程序详解(1)Graphics2D g = (Graphics2D)this.getGraphics();size=newBasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
13、g.setColor(b);g.setStroke(size);绘制图形用到类Graphics2D,其中包括很多绘制图形的方法,定义一个Graphics2D类g ,用getGraphics()来得到一个Graphics2D对象,它相当于组建的绘图环境,可以进行各种绘图操作。size=new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);定义一个 BasicStroke类 size来确定画笔的大小,其中BasicStroke类定义针对图形图元轮廓呈现属性的一个基本集合,这些图元使用 Graphics2D 对象呈现,而该对
14、象的 Stroke 属性设置为此 BasicStroke。其中con为画笔宽度,CAP_BUTT 指无装饰地结束未封闭的子路径和虚线线段,JOIN_BEVEL通过直线连接宽体轮廓的外角,将路径线段连接在一起。(以上内容为网上查找资料所得。)(2 ) 实现直线,椭圆,矩形绘制:switch(a)case 1:g.drawLine(x0, y0, e.getX(), e.getY();break;case 2:g.drawOval(x0, y0, e.getX() - x0, e.getY() - y0);break;case 3:g.drawRect(x0, y0, e.getX() - x0,
15、 e.getY() - y0);break;(3)随笔画和橡皮switch(a)case 4:g.drawLine(x0, y0, e.getX(), e.getY();x0 = e.getX();y0 = e.getY();break;case 5:g.clearRect(x0 - 10, y0 - 10, 20, 20);x0 = e.getX();y0 = e.getY();break;6.主类程序得以运行的入口,进入主类后即运行litao()类。C.调试运行调试运行后得到了画板和预期的所有功能,实现结果展示:D.遇到问题1.绘制完成后,将窗口最大化或者最小化后再打开,原本绘制的图形消失
16、。尚未解决。2.确定画笔大小时不得要领,后来通过查找资料解决。附表1:源程序:import java.awt.*;import java.awt.event.*;import javax.swing.JFrame;public class litao extends JFrame implements MouseMotionListener,MouseListener,ActionListener, ItemListener Panel canvas2 = new Panel();Button btn1 = new Button(“直线 “);Button btn2 = new Button(
17、“椭圆 “);Button btn3 = new Button(“矩形 “);Button btn4 = new Button(“清除 “);Button btn5 = new Button(“画笔 “);Button btn6 = new Button(“橡皮 “);Choice choice = new Choice(); Choice choice1 = new Choice();Color b;int x0,y0;char a=4;int con=1;BasicStroke size;private static Color colors = Color.black, Color.bl
18、ue, Color.red,Color.yellow, Color.green ;public litao()choice1.add(“1“);choice1.add(“3“);choice1.add(“5“);choice1.add(“7“);choice1.add(“9“);choice.add(“黑色“);choice.add(“蓝色“);choice.add(“红色“);choice.add(“黄色“);choice.add(“绿色“);canvas2.add(choice);canvas2.add(choice1);canvas2.add(btn1);canvas2.add(btn2
19、);canvas2.add(btn3);canvas2.add(btn4);canvas2.add(btn5);canvas2.add(btn6);choice.addItemListener(this);choice1.addItemListener(this);btn1.addActionListener(this);btn2.addActionListener(this);btn3.addActionListener(this);btn4.addActionListener(this);btn5.addActionListener(this);btn6.addActionListener
20、(this);addMouseListener(this);addMouseMotionListener(this);add(canvas2,BorderLayout.NORTH);setBackground(Color.white);canvas2.setBackground(Color.blue);this.setBackground(Color.white);setSize(800, 600);setVisible(true);public void paint(Graphics g)public void actionPerformed(ActionEvent e) if(e.getS
21、ource().equals(btn1)a=1;if(e.getSource().equals(btn2)a=2;if(e.getSource().equals(btn3)a=3;if(e.getSource().equals(btn4) Graphics g = this.getGraphics();g.clearRect(0, 0, this.getWidth(), this.getHeight();if(e.getSource().equals(btn5)a=4;if(e.getSource().equals(btn6)a=5;public void mousePressed(Mouse
22、Event e)x0=e.getX();y0=e.getY();public void mouseMoved(MouseEvent e) public void mouseReleased(MouseEvent e)Graphics2D g = (Graphics2D)this.getGraphics();size=new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);g.setColor(b);g.setStroke(size);switch(a)case 1:g.drawLine(x0, y0, e.getX(),
23、 e.getY();break;case 2:g.drawOval(x0, y0, e.getX() - x0, e.getY() - y0);break;case 3:g.drawRect(x0, y0, e.getX() - x0, e.getY() - y0);break;public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public void mouseDragged(MouseEvent e) Graphics
24、2D g = (Graphics2D)this.getGraphics();size=new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);g.setColor(b);g.setStroke(size);switch(a)case 4:g.drawLine(x0, y0, e.getX(), e.getY();x0 = e.getX();y0 = e.getY();break;case 5:g.clearRect(x0 - 10, y0 - 10, 20, 20);x0 = e.getX();y0 = e.getY()
25、;break;public void itemStateChanged(ItemEvent e) if(e.getSource()=choice)if(choice.getSelectedIndex()=0)b=Color.black;if(choice.getSelectedIndex()=1)b=Color.BLUE;if(choice.getSelectedIndex()=2)b=Color.red;if(choice.getSelectedIndex()=3)b=Color.yellow;if(choice.getSelectedIndex()=4)b=Color.green;if(e.getSource()=choice1)if(choice1.getSelectedItem()=“1“)con=1;else if(choice1.getSelectedItem()=“3“)con=3;else if(choice1.getSelectedItem()=“5“)con=5;else if(choice1.getSelectedItem()=“7“)con=7;else if(choice1.getSelectedItem()=“9“)con=9;public static void main(String args) new litao();