swing组件运用之个性画板

和计算器中不同的是,画板项目的实现中又更显的复杂一点.尽管有些复杂,但是只要把步骤写下来,复杂的也就是清晰的了.纵观画板的界面,主要由三个部分组成:1,左边添加功能的面板;2,下面添加颜色选择的面板;3,中间画布的面板.
有了这个大概的方向,思路就会清晰很多了.但是每个部分又可以分为几个步骤.第一部分,1)先创建窗体;2)创建面板,设置相关的属性;3)创建功能选择按钮,给按钮设置命令;4)将面板加到窗体上.第二部分和第三部分都有相应相同的情况.
下为原代码:
画板类

package lesson0129lianxi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.BevelBorder;


public class DrawBoard extends JFrame{
/**
* 主函数,程序的入口
* @param args
*/
public static void main(String[] args){
DrawBoard db = new DrawBoard();
db.showUI();
}
Color color=Color.black;
public void showUI(){
/**
* 设置窗体的属性
*/
this.setTitle("山寨画板Z.30");
this.setSize(850,630);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());

/**
* 创建画板左边的面板
*/
JPanel jp = new JPanel();
Dimension dime1 =new Dimension(70,800);
jp.setPreferredSize(dime1);
jp.setBackground(Color.LIGHT_GRAY);
jp.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
//设置边框效果
jp.setBorder(new BevelBorder(0,Color.BLUE,new Color(205,205,205)));
this.add(jp,BorderLayout.WEST);

/**
* 创建工具选择按钮
*/
ButtonGroup bg = new ButtonGroup();
for(int i=0;i<16;i++){
//将i=6的图标所对应的铅笔功能设为默认功能
if(i==6){
JRadioButton jbx = new JRadioButton("",true);
jbx.setActionCommand(6+"形状");
bg.add(jbx);
ImageIcon image1 = new ImageIcon("image/draw"+6+".jpg");
ImageIcon image2 = new ImageIcon("image/draw"+6+"-1.jpg");
ImageIcon image3 = new ImageIcon("image/draw"+6+"-2.jpg");
ImageIcon image4 = new ImageIcon("image/draw"+6+"-3.jpg");
jbx.setIcon(image1);
jbx.setRolloverIcon(image2);
jbx.setPressedIcon(image3);
jbx.setSelectedIcon(image4);
jp.add(jbx);
this.add(jp,BorderLayout.WEST);
continue;
}
JRadioButton jb = new JRadioButton();

jb.setActionCommand(i+"形状");
bg.add(jb);
ImageIcon image1 = new ImageIcon("image/draw"+i+".jpg");
ImageIcon image2 = new ImageIcon("image/draw"+i+"-1.jpg");
ImageIcon image3 = new ImageIcon("image/draw"+i+"-2.jpg");
ImageIcon image4 = new ImageIcon("image/draw"+i+"-3.jpg");
jb.setIcon(image1);
jb.setRolloverIcon(image2);
jb.setPressedIcon(image3);
jb.setSelectedIcon(image4);
jp.add(jb);
this.add(jp,BorderLayout.WEST);
}

/**
* 创建选择颜色的面板
*/
JPanel jp_down= new JPanel();
jp_down.setPreferredSize(new Dimension(800,50));
jp_down.setBackground(Color.green);
jp_down.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
this.add(jp_down,BorderLayout.SOUTH);

/**
* 创建颜色面板中的左右面板
*/
JPanel jp_left = new JPanel();
JPanel jp_right = new JPanel();
jp_left.setPreferredSize(new Dimension(40,40));
jp_right.setPreferredSize(new Dimension(540,40));
jp_left.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
jp_right.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));

jp_down.add(jp_left);
jp_down.add(jp_right);

/**
* 创建两个按钮,加到颜色面板中的做面板
*/
JButton jb_front = new JButton();
JButton jb_end = new JButton();
jb_front.setPreferredSize(new Dimension(20,20));
jb_end.setPreferredSize(new Dimension(20,20));
jb_front.setBackground(Color.WHITE);
jb_end.setBackground(Color.white);
/**
*将两个按钮放到左边的面板上,并形成重叠的效果,1.先将要添加的面板布局去掉,设为空布局 2.通过绝对布局来设置位置
*/
jp_left.setLayout(null);
jb_front.setBounds(7,7,20,20);
jb_end.setBounds(17,17,20,20);
jp_left.add(jb_front);
jp_left.add(jb_end);

/**
* 创建颜色按钮,并加到下方右面板
*/
//往右边面板添加一定数量的颜色选择按钮
//创建随机对象
ColorListener co = new ColorListener(this);
Random ra = new Random();
for(int x=0;x<60;x++){
JButton jbu = new JButton();
jbu.setPreferredSize(new Dimension(20,20));
jbu.setBackground(new Color(ra.nextInt(200102022)));
jbu.addActionListener(co);
jp_right.add(jbu);

}

/**
* 添加中间的画板,并创建画布到画板上
*/
JPanel jp_center = new JPanel();
jp_center.setPreferredSize(new Dimension(600,600));
jp.setBackground(Color.orange);
jp_center.setLayout(new FlowLayout(0));
this.add(jp_center);
/**
* 创建画布的画板,并将画布加上去
*/
JPanel jp_draw = new JPanel();
jp_draw.setBackground(Color.white);
jp_draw.setPreferredSize(new Dimension(450,450));
jp_center.add(jp_draw);
/**
* 添加两个标签
*/
JLabel jl_name = new JLabel(" Auther:Zhuzhenke");
JLabel jl_date = new JLabel("Date:2013.01.30");
jl_name.setBackground(Color.BLACK);
jl_date.setBackground(Color.BLACK);
jp_down.add(jl_name);
jp_down.add(jl_date);

this.setVisible(true);

Graphics g = jp_draw.getGraphics();


/**
* 创建画板监听器,
*/
DrawListener db =new DrawListener(bg,g,this);
jp_center.addMouseListener(db);//这是个添加监听器的方法,根据后面DrawListener调用的方法来添加,
jp_center.addMouseMotionListener(db);//也就是把DrawListener添加到中间的画板


}

}


画板监听器类

package lesson0129lianxi;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;

public class DrawListener extends MouseAdapter{
/**
* 声明变量
*/
public int x1,y1,x2,y2,x3,y3,x4,y4;
public ButtonGroup bg;
public Graphics g;
public DrawBoard db;
public String command;
/**
* 构造法
* @param bg
* @param g
*/
public DrawListener(ButtonGroup bg,Graphics g,DrawBoard db){
this.bg = bg;
this.g = g;
this.db = db;
}
/**
* 重写MouseAdapter中的方法
*/
public int count=1;
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
command = bg.getSelection().getActionCommand();
g.setColor(db.color);
if(command.equals("13形状")){
if(count==1){
x3=x1;
y3=y1;
count++;
}else{
x1=x2;
y1=y2;
}
}
}
public void mouseDragged(MouseEvent e){
if("6形状".equals(command)){
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
x1 =x2;
y1 = y2;
}

}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
/**
* 判断命令,执行相应的方法
*/
//定义command为从按钮组中得到的选择命令,从而得到按钮所得到的命令
String command = bg.getSelection().getActionCommand();
if("10形状".equals(command)){
g.drawLine(x1,y1,x2,y2);
}else if("12形状".equals(command)){
g.drawRect(Math.min(x1,x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
}else if("14形状".equals(command)){g.drawOval(Math.min(x2, x1), Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2));
}else if("15形状".equals(command)){
g.drawArc(Math.min(x2, x1), Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2),200,200);
}else if("13形状".equals(command)){
g.drawLine(x1, y1, x2, y2);
}
}
public void mouseClicked(MouseEvent e){
int w = e.getClickCount();//鼠标点击的次数
String command = bg.getSelection().getActionCommand();
if(w==2&&"13形状".equals(command)){
g.drawLine(x3, y3, x2, y2);
count = 1;
}

}

}

颜色按钮监听器类

package lesson0129lianxi;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JPanel;

public class ColorListener implements ActionListener{
public DrawBoard db;
public ColorListener(DrawBoard db){
this.db = db;
}
public void actionPerformed(ActionEvent e){
//e得到的事件源是按钮,用按钮代替事件源,将事件源强制转型为按钮
JButton jb = (JButton)e.getSource();
db.color= jb.getBackground();
}
}


[img]http://dl.iteye.com/upload/attachment/0080/0837/4e1be453-8716-3371-b694-f56ae46aa0cc.jpg[/img]

但是还有相当部分的功能还没能实现,这也算是个给我在家慢慢探讨的机会,毕竟要经过思考才能更实际的把自己的想法通过代码开表现出来.所以说软件开发是个创新的事物,是很有意义的事情.
但是遇到挫折是非常常见的事,如果说敲代码没有遇到什么困难,那么也不会有太多的意义,自己也不会有太多的提高,所以,无论是困难还是局部顺利的,都是很好的事情.
总之,来到蓝杰学习编程,自己收获很多.感谢左哥一遍又一遍耐心的为我解答和讲解.
相信大学的生活会变得更加有意义.为自己加油,为自己喝彩!
[img]http://dl.iteye.com/upload/attachment/0080/0837/4e1be453-8716-3371-b694-f56ae46aa0cc.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值