实现画板的基本思路: 1.设计画板的布局,分为三块,左边放工具按钮,下边放颜色按钮,中间放画图区域
2.实现三块区域的功能,用一个面板放置工具按钮,用一个面板放置颜色按钮,用一个面板作为画图区域
3.设置监听器方法,在工具按钮面板类里设置获取工具按钮文本内容的动作监听器方法,在颜色按钮面板类里设置获取每个颜色按钮背景色的动作监听器方法
对画图面板设置鼠标监听器方法使鼠标可以在该区域画图
画板实现具体步骤:
1.写出界面,把窗体分为三个区域,工具区,颜色区,画图区;每个区用一个面板实现
1).写出一个窗体界面
代码:
public DrawingBorder extends JFrame{//定义一个DrawingBorder类继承自JFrame类
public static void main(String args[]){//程序入口
DrawingBorder db=new DrawingBorder();//实例化DrawingBorder类的一个对象db
db.init();//调用对象db中的方法init()}
public void init(){//定义一个初始化主窗体的方法init()
this.setTitle("画板");//设置窗体的标题为“画板”
this.setSize(600,500);//设置窗体的大小为600*500像素
this.setDefaultCloseOperation(3);//设置点击关闭按钮时关闭窗体
this.setLocationRelativeTo(null);//设置窗体居中显示
this.setVisible(true);//设置窗体可见
}
}
2).把画图工具放到一个面板上,再把添加了工具的面板添加到一个面板中
代码:
public class ToolsPanel extends JPanel{//定义一个放置工具面板的面板类ToolsPanel继承自JPanel
public ToolsPanel(){//定义类ToolsPanel的一个无参构造方法,调用方法init()
init();
}
public void init(){//定义一个初始化工具面板的方法
JPanel jp=new JPanel();//实例化一个新面板用来放置工具按钮
jp.setLayout(new GridLayout(3,2,2,2));//设置新面板对象jp的布局为网格布局,布局为3行2列,按钮间距为2*2像素
public String [] arry={"直线","矩形","圆","填充圆","多边形","五角星"};//定义一个数组arry[]来存放按钮的文本内容
for(int i=0;i<arry.Length;i++){//用一个循环来实例化按钮并设置按钮的文本内容
JButton jb=new JButton(arry[i]);
jp.add(jb);}
}this.add(jp);//把存放工具按钮的面板添加到面板ToolsPanel中
}
3).把颜色放到一个面板上,再把颜色的面板加到另一个面板去
public class ColorPanel extends JPanel{//定义一个存放颜色面板的面板类ColerPanel继承自JPanel类
public ColorPanel(){//定义类ColorPanel的一个无参构造方法,调用方法init()
init();}
public void init(){//定义一个初始化ColorPanel 面板的方法
this.setBackground(Color.GRAY);//设置面板的背景色
JPanel cjp=new JPanel();//实例化一个新面板cjp
cjp.setLayout(new GridLayout(2,6,2,2));//设置面板cjp的布局为网格布局,为2行6列,按钮间距为2*2像素
public Color arry[]= {Color.black,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN,
Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED,Color.YELLOW};};//定义一个数组arry[]存放按钮的背景色
for(int i=0;i<){用一个循环来实例化按钮并为按钮设置背景色
JButton cjb=new JButton();
cjb.setBackground(arry[i]);
cjp.add(cjb);}
}
this.add(cjp);把面板cjp添加到ColorPanel面板上
}
4).新建一个画图的面板
代码:
public class DrawingPanel extends JPanel{//定义一个画图面板类DrawingPanel继承自JPanel
public DrawingPanel(){//定义类DrawingPanel的一个无参构造器方法,调用方法init()
init();}
public void init(){//定义初始化面板的方法
this.setBackground(Color.WHITE);}//设置面板的背景色
}
5).把工具面板、颜色面板和画图面板添加到窗体上,布局使用边框布局
代码:public DrawingBorder extends JFrame{//定义一个DrawingBorder类继承自JFrame类
public static void main(String args[]){
//程序入口
DrawingBorder db=new DrawingBorder();
//实例化DrawingBorder类的一个对象db
db.init();//调用对象db中的方法init()}
public void init(){
//定义一个初始化主窗体的方法init()
this.setTitle("画板");
//设置窗体的标题为“画板”
this.setSize(600,500);
//设置窗体的大小为600*500像素
this.setDefaultCloseOperation(3);
//设置点击关闭按钮时关闭窗体
this.setLocationRelativeTo(null);
//设置窗体居中显示
this.setVisible(true);//设置窗体可见 ToolsPanel tp=new ToolsPanel();//实例化工具面板类ToolsPanel的一个对象tp this.add(tp,BorderLayout.WEST);//把tp添加到窗体的西边 ColorPanel cp=new ColorPanel();//实例化颜色面板类ColorPanel的一个对象cp this.add(cp,BorderLayout.SOUTH);//把cp添加到窗体的南边 DrawingPanel dp=new DrawingPanel();//实例化画图面板类DrawingPanel的对dp this.add(dp,BorderLayout.CENTER);//把dp添加到窗体的中间 } }
2.给工具面板中的按钮添加监听器方法,绑定处理类的对象,获取到按钮的文本
代码:public class ToolsPanel extends JPanel{
public ToolsPanel(){
init();
}
public String type="直线";
public void init(){
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(3,2,2,2));
public String [] arry={"直线","矩形","圆","填充圆","多边形","五角星"};
ActionListener a1=new ActionListener(){//实例化一个监听器a1
public void actionPerformed(ActionEvent e){
type=e.getActioncommand();//获取动作按钮的文本内容}
};
for(int i=0;i<arry.Length;i++){//用循环把每个按钮都添加一个动作监听器a1
JButton jb=new JButton(arry[i]);
jb.addActionListener(a1);
jp.add(jb);
}
this.add(jp);
}
}
3.给颜色面板中的颜色按钮添加监听器方法,绑定处理类的对象,获取到按钮的颜色
代码:public class ColorPanel extends JPanel{
public ColorPanel(){
init();}
public Color color=Color.BLACK;
public void init(){
this.setBackground(Color.GRAY);
JPanel cjp=new JPanel();
cjp.setLayout(new GridLayout(2,6,2,2));
public Color arry[]= {Color.black,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN,
Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED,Color.YELLOW};};
ActionListener a2=new ActionListener(){//实例化一个动作监听器a2
public void actionPerformed(ActionEvent e){
JButton bt = (JButton) e.getSource();//获取事件源
color=bt.getBackground();//把按钮的背景色存入变量color中}};
for(int i=0;i<){
JButton cjb=new JButton();
cjb.setBackground(arry[i]);
cjp.add(cjb);}
}
this.add(cjp);
}
4.设置一个画图的鼠标监听器
代码:public class DrawingListener implements MouseListener, MouseMotionListener {//实例化一个监听器DrawingListener继承自MouseListener和MouseMotionListener
public int x1, x2, y1, y2;//定义存放鼠标动作的坐标
public ToolsPanel tp;
public ColorPanel cp;
public Graphics g;
private boolean state = true;定义一个布尔变量state并赋值为true
private int startX,startY,endX,endY;
public DrawingListener(ToolsPanel tp, ColorPanel cp, Graphics g) {//定义类DrawingListener的一个有参构造器,传递对象tp,cp,g
this.tp =tp;
this.cp = cp;
this.g = g;
}
public void mouseClicked(MouseEvent e) {
if (tp.type.equals("多边形")) {//如果用户选择的是多边形,则执行画多边形的任务
x2 = e.getX();
y2 = e.getY();
//获得鼠标点击的坐标
if (endX == x2 && endY == y2) {
//若鼠标点击的点与画的前一条直线的结束点坐标相同,则连接第一点与鼠标点击的点
g.drawLine(startX, startY, x2, y2); state = true; } else {//若鼠标点击的点与画的前一条之下的结束点坐标不同,则连接前一条线的结束点 g.drawLine(endX, endY, x2, y2); } endX = x2; endY = y2; } } public void mousePressed(MouseEvent e) {//获取鼠标按下时的坐标 x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY();//获取鼠标释放时的坐标 g.setColor(cp.color);//设置画图的颜色 if (tp.type.equals("直线")) {//若用户点击的按钮是直线,则执行画直线的任务 g.drawLine(x1, y1, x2, y2); } else if (tp.type.equals("圆")) {//若用户点击的按钮是圆,则执行画圆的任务 g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (tp.type.equals("矩形")) {//若用户点击的按钮是矩形,则执行画矩形的任务 g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if tp.type.equals("填充圆")) {//若用户点击的按钮是填充圆,则执行画填充圆的任务 g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (tp.type.equals("圆角矩形")) {//若用户点击的按钮是圆角矩形,则执行画圆角矩形的任务 g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 20, 20); } else if (tp.type.equals("多边形")) {//若用户点击的按钮是多边形,则执行画多边形的任务 if(state){//若state=true则话一条直线 g.drawLine(x1, y1, x2, y2); state = false;//把state的值赋为false startX = x1;//把x1赋值给startX startY = y1;//把y1赋值给startY endX = x2;//把x2赋值给endX endY = y2;//把y2赋值给endY } } } public void mouseDragged(MouseEvent e) { g.setColor(cp.color);//设置画图的颜色 if (tp.type.equals("刷子")) {// 如果用户选择是刷子,则执行绘制刷子的任务 // 获取鼠标在拖动过程中的坐标 x2 = e.getX(); y2 = e.getY(); // 将Graphics的对象强制转型为Graphics2D Graphics2D g2d = (Graphics2D) g; // 设置线条的粗细 g2d.setStroke(new BasicStroke(10)); // 用g2d去绘制刷子 g2d.drawLine(x1, y1, x2, y2); // 交换坐标,这样才能绘制出曲线 x1 = x2; y1 = y2; // 设置线条的粗细 g2d.setStroke(new BasicStroke(1)); } else if (tp.type.equals("喷枪")) {// 如果用户选择是喷枪,则执行绘制喷枪的任务 // 获取鼠标在拖动过程中的坐标 x2 = e.getX(); y2 = e.getY(); // 表示在拖动的过程中,只要获取到了坐标,就会随机在该坐标位置画十个点 for (int i = 0; i < 10; i++) { // 随机获取一个x值 int x = rand.nextInt(8); // 随机获取一个y值 int y = rand.nextInt(8); // 开始画点 g.drawLine(x2 + x, y2 + y, x2 + x, y2 + y); } } else if (tp.type.equals("铅笔")) {//若用户选择的按钮是铅笔,则执行铅笔的任务 x2 = e.getX(); y2 = e.getY();//获取鼠标拖动过程中的坐标 g.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2;//交换坐标 } else if (tp.type.equals("橡皮擦")) {//若用户选择的按钮是橡皮擦,则执行橡皮擦的任务 g.setColor(Color.WHITE);//设置画图的颜色为白色 x2 = e.getX(); y2 = e.getY();//获取鼠标拖动过程中的坐标 Graphics2D g2d = (Graphics2D) g;//将Graphics的对象强制转型为Graphics2D的对象 g2d.setStroke(new BasicStroke(10));//设置线条的粗细 g2d.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2;//交换坐标 g2d.setStroke(new BasicStroke(1));//设置线条的粗细 } else if (tp.type.equals("曲线")) {//若用户选择的按钮是曲线,则执行绘制曲线的任务 x2 = e.getX(); y2 = e.getY();//获取鼠标拖动过程中的坐标 g.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2;//交换坐标 } } // 实例化一个随机数类的对象 private Random rand = new Random(); public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } }
5.把鼠标监听器绑定处理类的对象,实现画图功能
代码:public DrawingBorder extends JFrame{
public static void main(String args[]){
DrawingBorder db=new DrawingBorder();
db.init();}
public void init(){
this.setTitle("画板");
this.setSize(600,500);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
ToolsPanel tp=new ToolsPanel();
this.add(tp,BorderLayout.WEST);
ColorPanel cp=new ColorPanel();
this.add(cp,BorderLayout.SOUTH);
DrawingPanel dp=new DrawingPanel();
this.add(dp,BorderLayout.CENTER);
Graphics g = dp.getGraphics();//实例化一个画图的方法对象g
DrawingListener a3 = new DrawingListener(tp, cp, g);//实例监听器DrawingListener的对象a3
dp.addMouseListener(a3);//把鼠标监听器添加到画图区域
dp.addMouseMotionListener(a3);//把鼠标移动监听器添加到画图区域
}
}