JAVA简单画板程序设计实验报告.docx

上传人:b****3 文档编号:10971118 上传时间:2023-05-28 格式:DOCX 页数:18 大小:164.83KB
下载 相关 举报
JAVA简单画板程序设计实验报告.docx_第1页
第1页 / 共18页
JAVA简单画板程序设计实验报告.docx_第2页
第2页 / 共18页
JAVA简单画板程序设计实验报告.docx_第3页
第3页 / 共18页
JAVA简单画板程序设计实验报告.docx_第4页
第4页 / 共18页
JAVA简单画板程序设计实验报告.docx_第5页
第5页 / 共18页
JAVA简单画板程序设计实验报告.docx_第6页
第6页 / 共18页
JAVA简单画板程序设计实验报告.docx_第7页
第7页 / 共18页
JAVA简单画板程序设计实验报告.docx_第8页
第8页 / 共18页
JAVA简单画板程序设计实验报告.docx_第9页
第9页 / 共18页
JAVA简单画板程序设计实验报告.docx_第10页
第10页 / 共18页
JAVA简单画板程序设计实验报告.docx_第11页
第11页 / 共18页
JAVA简单画板程序设计实验报告.docx_第12页
第12页 / 共18页
JAVA简单画板程序设计实验报告.docx_第13页
第13页 / 共18页
JAVA简单画板程序设计实验报告.docx_第14页
第14页 / 共18页
JAVA简单画板程序设计实验报告.docx_第15页
第15页 / 共18页
JAVA简单画板程序设计实验报告.docx_第16页
第16页 / 共18页
JAVA简单画板程序设计实验报告.docx_第17页
第17页 / 共18页
JAVA简单画板程序设计实验报告.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JAVA简单画板程序设计实验报告.docx

《JAVA简单画板程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《JAVA简单画板程序设计实验报告.docx(18页珍藏版)》请在冰点文库上搜索。

JAVA简单画板程序设计实验报告.docx

JAVA简单画板程序设计实验报告

JAVA期末画板实验报告

课程题目:

运用JAVA语言编写一个画板程序,这个画板要有画图,特殊图形绘制,颜色改变,画笔大小改变等功能。

设计思想:

1.进行界面设计

2.进行算法实现

3.运行调试

4.总结问题

设计过程:

A.界面设计

新建一个Jframe容器,然后定义一个panel(Panelconvas2),convas2是工具栏。

用borderlayout布置容器的边框布局,将工具栏放在NORTH部分。

完成界面设计。

add(canvas2,BorderLayout.NORTH);

Panelcanvas2=newPanel();

添加工具按键

Panelcanvas2=newPanel();

Buttonbtn1=newButton("直线");

Buttonbtn2=newButton("椭圆");

Buttonbtn3=newButton("矩形");

Buttonbtn4=newButton("清除");

Buttonbtn5=newButton("画笔");

Buttonbtn6=newButton("橡皮");

Choicechoice=newChoice();

Choicechoice1=newChoice();

canvas2.add(choice);

canvas2.add(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("红色");

choice.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);

btn6.addActionListener(this);

addMouseListener(this);

addMouseMotionListener(this);

2.按键监听响应

监听到按键后,判断当前会话工具

publicvoidactionPerformed(ActionEvente){

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

{

Graphicsg=this.getGraphics();

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.

publicvoidmousePressed(MouseEvente)

{

x0=e.getX();

y0=e.getY();

}

b.鼠标移动事件

publicvoidmouseMoved(MouseEvente){

}

c.鼠标按钮松开事件,此次响应完成对直线,椭圆,矩形进行绘制。

publicvoidmouseReleased(MouseEvente)

{

Graphics2Dg=(Graphics2D)this.getGraphics();

size=newBasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g.setColor(b);

g.setStroke(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.鼠标进入事件:

publicvoidmouseEntered(MouseEvente){}

e.鼠标离开事件:

publicvoidmouseExited(MouseEvente){}

•f.鼠标单击事件:

publicvoidmouseClicked(MouseEvente){}

g.鼠标拖动事件:

此次响应实现画笔(随笔画),橡皮的绘制。

publicvoidmouseDragged(MouseEvente){

Graphics2Dg=(Graphics2D)this.getGraphics();

size=newBasicStroke(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();

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(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;

}

elseif(choice1.getSelectedItem()=="3")

{

con=3;

}

elseif(choice1.getSelectedItem()=="5")

{

con=5;

}

elseif(choice1.getSelectedItem()=="7")

{

con=7;

}

elseif(choice1.getSelectedItem()=="9")

{

con=9;

}

}

调用choice1.getSelectedItem()返回被选中选项的标签文本字符串,即确定画笔的大小(con)。

5.工具栏工具实现程序详解

(1)Graphics2Dg=(Graphics2D)this.getGraphics();

size=newBasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g.setColor(b);

g.setStroke(size);

绘制图形用到类Graphics2D,其中包括很多绘制图形的方法,定义一个Graphics2D类g,用getGraphics()来得到一个Graphics2D对象,它相当于组建的绘图环境,可以进行各种绘图操作。

size=newBasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

定义一个BasicStroke类size来确定画笔的大小,其中BasicStroke类定义针对图形图元轮廓呈现属性的一个基本集合,这些图元使用Graphics2D对象呈现,而该对象的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,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.绘制完成后,将窗口最大化或者最小化后再打开,原本绘制的图形消失。

尚未解决。

2.确定画笔大小时不得要领,后来通过查找资料解决。

附表1:

源程序:

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.JFrame;

publicclasslitaoextendsJFrameimplementsMouseMotionListener,MouseListener,ActionListener,ItemListener{

Panelcanvas2=newPanel();

Buttonbtn1=newButton("直线");

Buttonbtn2=newButton("椭圆");

Buttonbtn3=newButton("矩形");

Buttonbtn4=newButton("清除");

Buttonbtn5=newButton("画笔");

Buttonbtn6=newButton("橡皮");

Choicechoice=newChoice();

Choicechoice1=newChoice();

Colorb;

intx0,y0;

chara='4';

intcon=1;

BasicStrokesize;

privatestaticColor[]colors={Color.black,Color.blue,Color.red,Color.yellow,Color.green};

publiclitao()

{

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

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

}

publicvoidpaint(Graphicsg)

{

}

publicvoidactionPerformed(ActionEvente){

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

{

Graphicsg=this.getGraphics();

g.clearRect(0,0,this.getWidth(),this.getHeight());

}

if(e.getSource().equals(btn5))

{

a='4';

}

if(e.getSource().equals(btn6))

{

a='5';

}

}

publicvoidmousePressed(MouseEvente)

{

x0=e.getX();

y0=e.getY();

}

publicvoidmouseMoved(MouseEvente){

}

publicvoidmouseReleased(MouseEvente)

{

Graphics2Dg=(Graphics2D)this.getGraphics();

size=newBasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g.setColor(b);

g.setStroke(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;

}

}

publicvoidmouseEntered(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseClicked(MouseEvente){}

publicvoidmouseDragged(MouseEvente){

Graphics2Dg=(Graphics2D)this.getGraphics();

size=newBasicStroke(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();

break;

}

}

publicvoiditemStateChanged(ItemEvente){

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;

}

elseif(choice1.getSelectedItem()=="3")

{

con=3;

}

elseif(choice1.getSelectedItem()=="5")

{

con=5;

}

elseif(choice1.getSelectedItem()=="7")

{

con=7;

}

elseif(choice1.getSelectedItem()=="9")

{

con=9;

}

}

}

publicstaticvoidmain(String[]args){

newlitao();

}

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2