画板总结

实现仿xp画板
1.定义一个画板主界面类,给类要继承自JFrame。
public class SampleDrawing extends JFrame{
}
2.在类中定义一个主函数和初始化界面的方法。
    public static void main (String [] args ){
       //实例化对象
       SampleDrawing sd = new SampleDrawing();
       //调用初始化界面的方法
       sd.init();
    }
3.初始化界面,设置窗体的属性,添加不同的工作面板。
1.由xp画板可知一个简单的画板基本上需要有一个工具面板,颜色面板,画图形面板。
2.定义一个工具类面板

public class ToolsPanel extends JPanel{

//定义一个属性用来保存用户选择的图形
private String type = "Line";
//定义获取图形属性值的方法
public String getType(){
return type;
}
//定义构造方法
public  ToolsPanel(){
//// 给ToolsPanel设置布局为流式布局,但是JPanel默认的布局就是流布局,所以此处不需要设
this.setLayout(new FlowLayout());
//实例化一个面板用来添加按钮
JPanel jp = new JPanel();
// 设置panel对象的布局为网格布局
panel.setLayout(new GridLayout(8, 2, 2, 2));
//创建一个数组,=来储存图片
String [] array={
//图片的地址名
"image/BrushTool.jpg", "image/EraserTool.jpg",
"image/LineTool.jpg", "image/PencilTool.jpg",
"image/AnyTool.jpg", "image/RectTool.jpg",
"image/RoundRectTool.jpg", "image/BigTool.jpg",
"image/SprayGunTool.jpg", "image/QulineTool.jpg",
"image/FilledTool.jpg", "image/Rect1Tool.jpg",
"image/tubianTool.jpg", "image/WordTool.jpg",
"image/OvalTool.jpg", "image/diguanTool.jpg"
};

/**
* 使用匿名内部类来实现动作监听器事件处理类对象
*/
ActionListener al = new ActionListener(){
//动作事件的处理方法
public void actionPerformed(ActionEvent e) {
//获取用户选择的图形
type = e.getActionCommand();
//输出
System.out.println(type);
}

};
// 使用循环来实例化按钮,并且添加到面板上
for (int i = 0; i < array.length; i++) {
//实例化一个图标对象
ImageIcon image = new ImageIcon(array[i]);
// 实例化按钮对象,让按钮显示图标
JButton btn = new JButton(image);
//设置按钮的大小
btn.setPreferredSize(new Dimension(30,30));
//获取图片的名字
String imageName = array[i].substring(array[i].indexOf("/")+1, array[i].lastIndexOf("Tool.jpg"));
//设置按钮的动作命令
btn.setActionCommand(imageName);
//给按钮加上动作监听器方法,绑定事件处理类对象
btn.addActionListener(al);
// 将按钮添加到panel面板上
jp.add(btn);
}
//设置ToolsPanel的背景颜色
this.setBackground(Color.DARK_GRAY);
// 将jp面板对象添加到ToolsPanel上
this.add(jp);
}
}
3.定义一个颜色面板类
public class ColorPanel extends JPanel{

//定义颜色属性
private Color color=Color.BLACK;
                      //定义获取颜色属性值的方法
public Color getColor(){
return color;
}

/**
* 定义构造方法
*/
public ColorPanel (){

//给ColorPanel设置布局为流式布局,但是JPanel默认的布局就是流布局(注意:流布局默认是剧中的),
//所以此处需要设置布局
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//实例化一个面板,放置颜色按钮
JPanel jp =new JPanel(); //设置面板布局格式
jp.setLayout(new GridLayout(2,14,2,2));
// 定义一个颜色数组
Color[] array = { 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.WHITE, Color.YELLOW,
new Color(100, 140, 100), new Color(17  0, 100, 200),
new Color(100, 150, 100), new Color(100, 200, 100),
new Color(100, 140, 100), new Color(105, 170, 150),
new Color(170, 100, 160), new Color(130, 170, 160),
new Color(150, 100, 100), new Color(200, 190, 130),
new Color(100, 120, 100), new Color(200, 104, 100),
new Color(100, 160, 180), new Color(200, 130, 140),
new Color(160, 100, 180) };
//使用匿名内部类
ActionListener al = new ActionListener(){
//动作事件的处理方法
public void actionPerformed(ActionEvent e) {
//得到事件源对象
JButton btn = (JButton)e.getSource();
//获取用户选择的颜色
color = btn.getBackground();
//输出
System.out.println(color);
}
};
// 使用循环来实例化按钮,并且添加到面板上
for (int i = 0; i < array.length; i++) {
// 实例化按钮对象
JButton btn = new JButton();
//设置按钮的背景颜色
btn.setBackground(array[i]);
//设置按钮的大小
btn.setPreferredSize(new Dimension(15,15));
//给按钮添加动作监听器方法,绑定事件处理类对象al
btn.addActionListener(al);

jp.add(btn);
}
//设置ColorPanel的背景颜色
this.setBackground(Color.DARK_GRAY);
// 将jp添加到ColorPanel上
this.add(jp);
}


}
4.创建一个画图面板
public class DrawingPanel extends JPanel {

/**
* 定义构造方法
*/
public DrawingPanel(){
//设置背景颜色为白色
this.setBackground(Color.WHITE);
}

}
5.将上面三者添加到DrawingBorder 窗体上去,
6.给给画图的面板类添加鼠标监听器方法,绑定事件处理类对象。定义鼠标事件处理类,
实现MouseListener接口和MouseMotionLOistener借口,实现接口中的方法,在方法里边去画图形。
public class DrawingListener implements MouseListener,MouseMotionListener{
//用来获取图像
private ToolsJpanel tp;
//用来获取颜色
private ColorJpanel cp;
//用来画图
private Graphics g;
//用来存储两次点击的坐标值
private int x1,y1,x2,y2,x,y,temx,temy,count=0;
public DrawingListener(ToolsJpanel tp,ColorJpanel cp, Graphics g){
this.tp=tp;
this.cp=cp;
this.g=g;
}
/**
* 鼠标在事件源上按下时调用的方法
*/
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.getColor());
//判断用户选择的图形是否是直线
if(tp.getType().equals("Line")){
g.drawLine(x1, y1, x2, y2);
}else if(tp.getType().equals("Rect")){
if (x1 < x2 && y1 < y2) {
g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (x1 > x2 && y1 > y2) {
g.drawRect(x2, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 > x2 && y1 < y2) {
g.drawRect(x2, y1, Math.abs(x1 - x2), Math.abs(y1 - y2));
} else if (x1 < x2 && y1 > y2) {
g.drawRect(x1, y2, Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}else if(tp.getType().equals("Oval")){
//......
}else if(tp.getType().equals("tubian")){//实现多边形
if(count==0){
g.drawLine(x1, y1, x2, y2);
x=x1;
y=y1;
temx=x2;
temy=y2;
count++;
}else{
g.drawLine( temx,temy, x2, y2);
if(temx==x2&&temy==y2){
g.drawLine( x, y, x2, y2);
}
temx=x2;
temy=y2;
}
}
}

/**
* 鼠标在事件源上按下并拖动时执行的方法
*/
public void mouseDragged(MouseEvent e) {
//设置图形的颜色
g.setColor(cp.getColor());
if(tp.getType().equals("Brush")){//判断用户选择的图形是否是刷子,如果是则开始画粗线条的线
//鼠标在事件源按下拖动时获取第二个点的坐标值
x2 = e.getX();
y2 = e.getY();
//设置线条的粗细
//首先将Graphics类型的g对象强制转换为Graphics2D的对象
Graphics2D g2d = (Graphics2D)g;
//调用Graphics2D设置线条粗细的方法来设置线条的粗细
g2d.setStroke(new BasicStroke(8));
//开始绘制粗线条
g2d.drawLine(x1, y1, x2, y2);
//将线结束点的坐标作为下一条线的开始点
x1 = x2;
y1 = y2;
//重新设置线条的粗线
g2d.setStroke(new BasicStroke(1));
}else if(tp.getType().equals("SprayGun")){//如果是喷枪
//获取坐标值
x2=e.getX();
y2=e.getY();
//实例化一个随机数对象
Random rand = new Random();
//循环画多个点
for(int i=0;i<20;i++){
//随机生成新的x和y点,用来绘制不同的点
int x = rand.nextInt(8);
int y = rand.nextInt(8);
//开始绘制点
g.drawLine(x2+x, y2+y, x2+x, y2+y);
}
}
//其他
}

/**
* 鼠标进入事件源时调用的方法
*/
public void mouseEntered(MouseEvent e) {

}

/**
* 鼠标退出事件源时调用的方法
*/
public void mouseExited(MouseEvent e) {

}

/**
* 鼠标在事件源同一个点上按下和释放时调用的方法
*/
public void mouseClicked(MouseEvent e) {

}
/**
* 鼠标在事件源上移动时执行的方法
*/
public void mouseMoved(MouseEvent e) {

}


}

猜你喜欢

转载自978499985.iteye.com/blog/1506429