java画板源代码Word文件下载.docx
《java画板源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《java画板源代码Word文件下载.docx(15页珍藏版)》请在冰点文库上搜索。
//颜色组区域
JPanelbottom=newJPanel();
//画板组区域
JPaneltop=newJPanel();
//顶部区域
ImageIconicon1=newImageIcon("
src\\huaban\\picture\\c1.jpg"
);
ImageIconicon2=newImageIcon("
src\\huaban\\picture\\c2.jpg"
ImageIconicon3=newImageIcon("
src\\huaban\\picture\\c3.jpg"
ImageIconicon4=newImageIcon("
src\\huaban\\picture\\c4.jpg"
ImageIconicon5=newImageIcon("
src\\huaban\\picture\\c5.jpg"
ImageIconicon6=newImageIcon("
src\\huaban\\picture\\c6.jpg"
ButtonGroupg=newButtonGroup();
//按钮组
JRadioButtonop1=newJRadioButton(icon1,true);
//单选框
JRadioButtonop2=newJRadioButton(icon2,false);
JRadioButtonop3=newJRadioButton(icon3,false);
JRadioButtonop4=newJRadioButton(icon4,false);
JRadioButtonop5=newJRadioButton(icon5,false);
JCheckBoxop6=newJCheckBox(icon6);
//复选框
Colorc=Color.black;
//设置画笔颜色,也是刚开始的默认颜色
Colorc1=Color.white;
///设置按钮背景颜色
intf=1;
intsx,sy,x,y;
//坐标记录
booleanfill=false;
booleanflag=false;
JScrollBarr1=newJScrollBar(JScrollBar.HORIZONTAL,251,4,0,255);
//横向滚动条,初始值,扩展大小,最小值,最大值
JScrollBarg1=newJScrollBar(JScrollBar.HORIZONTAL,251,4,0,255);
JScrollBarb1=newJScrollBar(JScrollBar.HORIZONTAL,251,4,0,255);
publichuaban(){
Imageicon=Toolkit.getDefaultToolkit().getImage("
frame.setIconImage(icon);
getContentPane().setLayout(newBorderLayout());
//边界布局
bottom.setBackground(Color.white);
//设置画板背景颜色
p1.setLayout(newFlowLayout());
//按钮为流水布局
p1.setBackground(Color.white);
//设置背景颜色
p2.setLayout(newGridLayout(1,3));
//设置颜色滚动轴布局
p2.add(r1);
p2.add(g1);
p2.add(b1);
//添加滚动轴
r1.setBackground(Color.red);
//设置滚动轴颜色
g1.setBackground(Color.green);
b1.setBackground(Color.blue);
op1.setBackground(c1);
//设置按钮背景颜色
op2.setBackground(c1);
op3.setBackground(c1);
op4.setBackground(c1);
op5.setBackground(c1);
op6.setBackground(c1);
g.add(op1);
//单选按钮组
g.add(op2);
g.add(op3);
g.add(op4);
g.add(op5);
//添加按钮进入面板p1
p1.add(op1);
p1.add(op2);
p1.add(op3);
p1.add(op4);
p1.add(op5);
p1.add(op6);
top.setLayout(newGridLayout(2,1,10,5));
top.add(p1);
top.add(p2);
//顶部区域增加组建p1,p2
op6.addItemListener(this);
//填充按钮增加监听器
r1.addAdjustmentListener(this);
//红色监听器
g1.addAdjustmentListener(this);
//绿色监听器
b1.addAdjustmentListener(this);
//蓝色监听器
op1.addItemListener(this);
//图形选择监听器
op2.addItemListener(this);
op3.addItemListener(this);
op4.addItemListener(this);
op5.addItemListener(this);
bottom.addMouseListener(this);
//画板设置监听器
bottom.addMouseMotionListener(this);
getContentPane().add(top,"
North"
getContentPane().add(bottom,"
Center"
//这样设置可以实现画板随窗口大小变化而延伸
this.setTitle("
画板"
//标题
this.setSize(800,500);
//大小
this.setVisible(true);
//可见
this.setLocationRelativeTo(null);
//居中
this.setDefaultCloseOperation(3);
//随窗口关闭而关闭进程
}
/**
*mousePressed()设置图形绘画起点mouseReleased()设置鼠标释放时候进行覆盖重画mouseDragged()也是画
*画出的图跟用mouseReleased()画出来的一样,只是更加体现过程。
//注册鼠标事件监听器
publicvoidmousePressed(MouseEvente){
flag=false;
sx=e.getX();
sy=e.getY();
//获取鼠标按下时的坐标
publicvoidmouseReleased(MouseEvente){
intx0,y0;
Graphicsg=bottom.getGraphics();
//获取按钮事件
switch(f){//覆盖重画
case2:
//直线
g.drawLine(sx,sy,x,y);
x=e.getX();
y=e.getY();
g.setColor(c);
g.drawLine(sx,sy,x,y);
break;
case3:
case4:
x=e.getX();
y=e.getY();
x0=x>
sx?
sx:
x;
y0=y>
sy?
sy:
y;
if(!
fill){
if(f==3)
g.drawOval(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
else
g.drawRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
}
else{
g.fillOval(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
g.fillRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
case5:
g.setColor(Color.white);
g.fillRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
g.drawRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
}
publicvoidmouseEntered(MouseEvente){
publicvoidmouseExited(MouseEvente){
publicvoidmouseClicked(MouseEvente){
publicvoidmouseMoved(MouseEvente){
*Dragged()都是最终不实现的,使用XOR只展现过程曲线
*最终都会小时,真正实现“画出来”的是Release()
publicvoidmouseDragged(MouseEvente){
switch(f){
case1:
//画任意线
sx=x;
sy=y;
flag){
x=e.getX();
y=e.getY();
g.drawLine(sx,sy,x,y);
flag=true;
//实现重画用白色的画笔重画所有的轨迹也就是抹去拖拽过程中的痕迹
//设置异或进入绘图模式
g.setXORMode(Color.white);
//开始覆盖
x0=x>
y0=y>
if(!
if(f==3)
g.drawOval(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
else
g.drawRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
}
else{
g.fillOval(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
g.fillRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
//声明异或进入绘图模式
//开始画
}else{
g.drawRect(x0,y0,Math.abs(x-sx),Math.abs(y-sy));
else{
//开始绘画
publicvoidadjustmentValueChanged(AdjustmentEvente){
//滚动条可以产生AdjustmentEvent类代表的调整事件
//当用户改变滑块位置时会产生调整事件.
//Color(R,G,B)这个是一个调试颜色的函数
//getValue()是获取当前滚动轴的值
c=newColor(r1.getValue(),g1.getValue(),b1.getValue());
c1=newColor(r1.getValue(),g1.getValue(),b1.getValue());
r1.setBackground(newColor(r1.getValue(),0,0));
g1.setBackground(newColor(0,g1.getValue(),0));
b1.setBackground(newColor(0,0,b1.getValue()));
p1.setBackground(c);
publicvoiditemStateChanged(ItemEvente){
/**
*选择事件事件源当在单选按钮组中选择时会产生选择事件当用户单击复选框改变它的选中状态时,会产生选择事件
*/
//instanceof指出对象是否是特定类的一个实例返回一个布尔值
if(e.getItemSelectable()/*获取事件源*/instanceofJCheckBox){
JCheckBoxtemp=(JCheckBox)e.getItemSelectable();
//强制转换
fill=temp.isSelected();
//判断复选框是否被选中
}else{//单选框
JRadioButtontemp=(JRadioButton)e.getItemSelectable();
if(temp==op1)
f=1;
//自由画
elseif(temp==op2)
f=2;
elseif(temp==op3)
f=3;
//椭圆
elseif(temp==op4)
f=4;
//矩形
elseif(temp==op5)
f=5;
//橡皮擦
publicstaticvoidmain(Stringargs[]){
huabanmainFrame=newhuaban();
}