画图板总结

画图板的基本结构为北部的菜单栏,中部的画布,南部的颜色选择栏,和西部的工具栏所组成,要实现画图板的功能首先需要有一块画布.
可以这样实现,首先实例化一个面板对象,然后用getGraphics()方法获取面板的画布,之后再把面板对象添加到窗体的中部就可以了
当然弄好画布之后还需要对画布的四周进行一定的修饰。如在北部需要有菜单项,所以需要用到JMenuBar,JMenu,JMenuItem这三个类
,而西部与南部,则需要用到JToolBar类,目的是可以将西部或南部面板拉到窗体之外。这样画图板的基本结构就做好了。接下来进入最重要也是最复杂的
部分,给工具栏与颜色栏以及画布添加监听器来实现画图的功能,具体怎么实现呢?


第一,给工具栏添加监听器,我们的目的是当你点击你所需要的图标时,在画布上画的时候就能实现特定的功能,因为我们在工具栏里放入的是图片,而图片都是有名字的,我们可以采用
图片特定的名字来作为我们所区分的依据,例如:代表直线的图片为line.jpg,就可以通过判断是不是这个名字来获取你点击的是直线。
代码如下:
//定义一个匿名内部类
private ActionListener al=new ActionListener(){

public void actionPerformed(ActionEvent e) {

type=(String)e.getActionCommand();//返回一个事件类型的对象
System.out.println("type="+type);
}
};
通过调用匿名内部类来得到图片的type,之后再写type的get方法就可以了
西部搞定之后,就只剩下颜色栏了,首先因为每一个颜色选择都是一个按钮,所以当我们点击颜色框的时候只要获取按钮的颜色就行了
color=btn.getBackground();//获取按钮的颜色,之后因为会用到颜色,所以也需要写一下颜色的get方法。但是程序远远没有这样简单,我们发现
画板是区分前景色和背景色的,而且当你点击鼠标左键时为前景色,鼠标右键时为背景色。我们需要用到鼠标监听器里面的e.getbutton()方法,这样得到的
值为int型,只有3个值,1代表当前点击了左键,2代表鼠标滚轮操作,3代表点击了右键。这样我们就可以实现了
代码:
//使用匿名内部类的方式来实现事件处理类,设置左键为前景色,右键为背景色
private MouseAdapter al=new MouseAdapter(){
//得到按的是左键还是右键
public void mouseClicked(MouseEvent e) {
JButton btn=(JButton)e.getSource();//返回一个事件类型的对象
color=btn.getBackground();//获取按钮的颜色
System.out.println("输出颜色"+color);
int count=e.getButton();//得到计数器,1为左键,2滚轮,3右键
//判断
if(count==1){
//如果为左键,设置前景色
buttonfront.setBackground(color);
colorfront=color;//前景色为color的颜色
}
else if(count==3){
//如果是右键,设置背景色
buttonback.setBackground(color);
colorback=color;//背景色为color的颜色
}
}

};
这样当我们写好colorfront,colorback的get方法后就可以了

接下来就是画图的方法:因为要画各种各样的图形,我们发现每个图形都会有坐标,以及画的方法,我们可以先写一个shape类,定义坐标属性以及抽象画图形方法
package Tool;

//图形抽象类
import java.awt.Color;
import java.awt.Graphics;


/**
* 图形抽象类
*
* @author Administrator
*
*/
public abstract class Shape {

private int x1, y1, x2, y2;
public byte type;//用于判断
private Color color;
/**
* 构造方法
*
* @param x1起始X
* @param y1起始Y
* @param x2结束X
* @param y2结束Y
* @param color图形颜色
*/
public Shape(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
public int getX1() {
return x1;
}

public int getY1() {
return y1;
}

public int getX2() {
return x2;
}

public int getY2() {
return y2;
}

public Color getColor() {
return color;
}

public abstract void draw(Graphics g);
//得到队列对象
}

之后你需要画直线就可以写一个shapeline的类通过继承
shape的属性以及重写其draw方法就可以了
具体如何就不作赘述
现在还有最关键的一步就是写监听器类了
首先我们要用到工具栏里图片中的type
以及颜色栏中的color
所以我们需要在监听器里创建工具栏以及颜色栏的属性
通过构造方法传参来创建新对象,再调用对象的gettype方法和getcolor方法
就可以了
核心代码:
public DrawingListener(Graphics g, ColorPanel cp, ToolPanel tp) {
this.g = g;
this.cp = cp;
this.tp = tp;
}
z最后通过重写鼠标监听器里的方法就能实现画图板的基本功能了。
/**
* 鼠标在事件源上按下的时候调用的方法
*/
public void mousePressed(MouseEvent e) {
if (!tp.getType().equals("Polygon") || i == 1) {
x1 = e.getX();
y1 = e.getY();
}
}

/**
* 鼠标在事件源上释放的时候调用的方法
*/
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (tp.getType().equals("Line")) {
// 实例化shape的对象
shape = new ShapeLine(x1, y1, x2, y2, cp.getcolor());
shape.type=1;//规定type为1时画直线
System.out.println(shape + " " + g);
shape.draw(g);
//将shape储存到队列中
list.add(shape);
System.out.println("++++++"+list);
System.out.println("在画直线");
}
if (tp.getType().equals("Rect")) {
// 实例化shape的对象
shape = new ShapeRect(x1, y1, x2, y2, cp.getcolor());
shape.type=2;//规定type为2时画矩形
System.out.println(shape + " " + g);
shape.draw(g);
//将shape储存在队列中
list.add(shape);
System.out.println("在画矩形");
}
if (tp.getType().equals("Round")) {
// 实例化shape的对象
shape = new ShapeRound(x1, y1, x2, y2, cp.getcolor());
shape.type=3;//规定type为3时为画圆
System.out.println(shape + " " + g);
shape.draw(g);
//将shape储存在队列中
list.add(shape);
System.out.println("在画圆");
}
if (tp.getType().equals("RoundRect")) {
// 实例化shape的对象
shape = new ShapeRoundRect(x1, y1, x2, y2, cp.getcolor());
shape.type=4;//规定type为4时为画圆脚矩形
shape.draw(g);
//将shape储存在队列中
list.add(shape);
}
//多边形
if (tp.getType().equals("Polygon")) {
if (i == 1) {
x0 = x1;
y0 = y1;
i++;
}
// 实例化shape的对象
shape = new ShapePolygon(x1, y1, x2, y2, cp.getcolor());
if (Math.abs(x0 - x2) < 5 && Math.abs(y0 - y2) < 5) {
// 实例化shape的对象
shape = new ShapePolygon(x0, y0, x1, y1, cp.getcolor());
i = 1;
}
shape.draw(g);
x1 = x2;
y1 = y2;
}
}

/**
* 鼠标在事件源上按下并拖动时执行的方法
*/
public void mouseDragged(MouseEvent e) {
if (tp.getType().equals("Atomizer") || tp.getType().equals("Eraser")
|| tp.getType().equals("Pencil")
|| tp.getType().equals("Brush")) {
x2 = e.getX();
y2 = e.getY();
// 判断点击的是否是喷枪
if (tp.getType().equals("Atomizer")) {
// 实例化Shape的对象
shape = new ShapeAtomizer(x1, y1, x2, y2, cp.getcolor());
shape.type=6;//规定为6时为喷枪
shape.draw(g);
list.add(shape);

}
if (tp.getType().equals("Eraser")) {
// 实例化shape的对象
shape = new ShapeEraser(x1, y1, x2, y2, cp.getcolor());
shape.type=7;//规定为7时为橡皮
x1 = x2;
y1 = y2;
shape.draw(g);
list.add(shape);//将shape对象添加到队列中
}
if (tp.getType().equals("Pencil")) {
// 实例化shape的对象
shape = new ShapePencil(x1, y1, x2, y2, cp.getcolor());
shape.type=8;//规定为8时为铅笔
x1 = x2;
y1 = y2;
shape.draw(g);
list.add(shape);
}
if (tp.getType().equals("Brush")) {
// 实例化shape的对象
shape = new ShapeBrush(x1, y1, x2, y2, cp.getcolor());
shape.type=9;
x1 = x2;
y1 = y2;
shape.draw(g);
list.add(shape);
}
//System.out.println("shape"+shape);
//System.out.println("g"+g);
//shape.draw(g);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值