`
hmeng
  • 浏览: 15204 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

画板感想与小结

阅读更多
   至画板重绘,我的画板已基本实现主要功能了,虽然还有很多需要完善,但看到自己做出的成果能够感到真正发自内心的愉悦,原来自己也能做出以前觉得很神奇的东西,每一次运行成功,得到想要结果时都会忍不住发出一声“哇塞!”,顿时倍感满足和开心。
    尽管以后肯定会觉得这个是特别简单的小工程,不过当付出自己的时间和精力去完成,并得到想要的结果时那种成就感和满足感真的令人痴迷,其实这也是我每一次努力做每一件事所追求的简单目标,我确实没有雄心壮志去完成一件事,多么,多么的惊天动地的一件事,有时这是一件坏事,可有时因为简单的目标容易实现,不也更容易感到满足和高兴么?
    总会遇到各种各样的Bug,粗心也好,逻辑问题也好,格式错误也罢,只有经过Bug的磨练才能更加为自己解决问题实现运行而感到满足。开始时真的是对界面设计一点都不懂,因为自己写代码老是有Bug,看到红色的Bug就感到心凉,烦躁,觉得好浪费时间,三五次下来就没有动力去写代码了,于是看熊哥的代码,有时就完全对着抄一下,只为完成任务,这样的效果肯定是不好的,也并没有自己的成就感,现在完成了画板,虽然不是自己完全重新靠自己写的,但是我会继续努力哒,因为实现代码的结果的成就感也能成为一种动力,认真去学习,好好消化每次所学的知识变为自己的理解,提高自己的能力。

  再总结下自己经常出现比较弱智的Bug的问题吧
1、JAVA内部类的类名避免写错,比如重写监听器方法mousepressed开头字母写成大写(后来发现其实很多不用自己写的,避免自己写错)。
2、if条件语句的使用,if(1)这是错误的,括号内只能用bool型或其他条件判断语句 尽量用if(){} else if(){}(以防出错)。
3、防止空指针异常有些变量赋初值(计算器的功能实现中)。
4、类型转换,字符型整型浮点型互换。。
5、要在窗体显示后取画布。
6、在鼠标监听器方法中才可以对事件源getButton。。
7、出现异常时可通过不同位置输出标记查找问题源。

  另外,我希望有机会可以把这个画板做成手机APP的软件,这样在手机中打开自己做的APP感觉特别炫酷。

贴上自己的代码:
(1)画板界面设计部分
public class XPDraw {
	private JFrame jf;//设置窗体为属性
	private String shape="Line";
	private Color fcolor;
	private Color bcolor;
	private int size=1;
	private JButton left;
	private JButton right;

	public String getShape(){
		return shape;
	}
	public Color getfColor(){
		return fcolor;
	}
	public Color getbColor(){
		return bcolor;
		
	}
	public int getSize(){
	    return	size;
	}

	public static void main(String[] args) {
		XPDraw XP=new XPDraw();
		XP.draw();
	}

	private void draw() {
		jf=new JFrame();
		//设置窗体属性
		jf.setTitle("HM画板");
		jf.setSize(800, 600);
		jf.setDefaultCloseOperation(3);
		jf.setLocationRelativeTo(null);
		//调用创建菜单栏的方法
		this.Manu();
		//调用创建工具栏的方法
		this.Tools();
		//调用创建颜色栏的方法
		this.Colors();
		//调用创建绘图板的方法
		JPanel draw=this.Drawpanel();
		//设置窗口可见
		jf.setVisible(true);
		//取画布
		Graphics g=draw.getGraphics();
		//给画布面板添加监听器方法
		DrawListener al=new DrawListener (g,this);
		draw.addMouseListener(al);
		draw.addMouseMotionListener(al);
	}
//定义创建绘画板的方法
	private JPanel Drawpanel() {
		//实例化一个中间面板
		JPanel center =new JPanel(){
			//重写绘画方法
			public void paint(Graphics g){
				//调用父类的
				super.paint(g);
				for(int i=0;i<DrawListener.cnt;i++){
					//得到数组中元素
					Shape shape=DrawListener.array[i];
					shape.draw((Graphics2D)g);
				}
			}
		};
			
		
		
		center.setLayout(new FlowLayout(FlowLayout.LEFT));
		center.setBackground(Color.gray);
		//把其面板添加到窗体中央
		jf.add(center,BorderLayout.CENTER);	
		
		//实例化一个绘画板
		JPanel draw=new JPanel();
		draw.setPreferredSize(new Dimension(650,400));
		//设置其背景色为白色
		draw.setBackground(Color.white);
		center.add(draw);
		//返回绘画面板
		return draw;
	}
//定义创建颜色栏的方法
	private void Colors() {
		JPanel colors=new JPanel();
		//设置为流式布局靠左,大小
		colors.setLayout(new FlowLayout(FlowLayout.LEFT));
		colors.setPreferredSize(new Dimension(0,50));
		jf.add(colors,BorderLayout.SOUTH);
		
		//实例化一个面板
		JPanel pane=new JPanel();
		pane.setPreferredSize(new Dimension(40,40));
		pane.setLayout(null);
		//实例化两个按钮,并添加到面板上
		left=new JButton();
		right=new JButton();
		left.setBounds(10, 10, 15, 15);
		left.setBackground(Color.black);
		right.setBounds(15, 15, 16, 16);
		right.setBackground(Color.white);
		
		pane.add(left);
		pane.add(right);
		//把双色面板添加到颜色面板上
		colors.add(pane);
		
		//实例化一个面板对象,设置网格布局
		JPanel panel=new JPanel();
		panel.setLayout(new GridLayout(2,2));
		 
		Color arr[]={Color.BLACK,Color.RED,Color.blue,Color.GRAY,Color.GREEN,Color.YELLOW,Color.PINK,Color.LIGHT_GRAY,
					Color.ORANGE,Color.WHITE,Color.DARK_GRAY,new Color(100,200,255),new Color(10,188,220),new Color(200,60,80),
					new Color(200,0,230),new Color(240,180,103)};		
		for(int i=0;i<arr.length;i++){
			JButton col=new JButton();
			col.setBackground(arr[i]);
			col.setPreferredSize(new Dimension(15,15));
			//添加到面板上
			panel.add(col);
			//使用匿名内部类
			col.addMouseListener(new MouseListener() {
				public void mouseReleased(MouseEvent e) {}
				public void mousePressed(MouseEvent e) {}
				public void mouseExited(MouseEvent e) {}
				public void mouseEntered(MouseEvent e) {}
				public void mouseClicked(MouseEvent e) {
					if(e.getButton()==1){
						//获取事件源按钮
						JButton bu=(JButton) e.getSource();
						//获取事件源对象得背景色
						fcolor=bu.getBackground();	
						left.setBackground(fcolor);
						}
			    	if(e.getButton()==3){
						//获取事件源按钮
						JButton bu=(JButton) e.getSource();
						//获取事件源对象得背景色
						bcolor=bu.getBackground();	
						right.setBackground(bcolor);
						}
					
					
				}
			});
		}
		colors.add(panel);
		
	}
//定义创建工具栏的方法
	private void Tools() {
		JPanel jp=new JPanel();
		//设置面板属性位置
		jp.setPreferredSize(new Dimension(60,0));
		jf.add(jp,BorderLayout.WEST);

		//添加图形按钮
		String []array={"images/line.png","images/eraser.png","images/pencil.png","images/rectangular.png",
				"images/brush.png","images/椭圆.png","images/圆角矩形.png","images/喷枪.png","images/001.jpg","images/rec.jpg","images/多边形.png","images/填充.png"};
		for(int i=0;i<array.length ;i++){
			ImageIcon icon=new ImageIcon(array[i]);
			//将图片放到按钮上
			JButton jb=new JButton(icon);
			jb.setActionCommand(""+i);
			//设置按钮大小
			jb.setPreferredSize(new Dimension(25,25));
			//给每个按钮添加监听器方法,使用匿名内部类
			
			jb.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent e) {
					if(e.getActionCommand().equals("0")){
						shape="Line";
					}if(e.getActionCommand().equals("1")){
						shape="Eraser";
					}if(e.getActionCommand().equals("2")){
						shape="Pencil";
					}if(e.getActionCommand().equals("3")){
						shape="fillRect";
					}if(e.getActionCommand().equals("4")){
						shape="Brush";
					}if(e.getActionCommand().equals("5")){
						shape="Circle";
					}if(e.getActionCommand().equals("6")){
						shape="RoundRect";
					}if(e.getActionCommand().equals("7")){
				        shape="Spray";
					}if(e.getActionCommand().equals("8")){
			            shape="Dshape";
			        }if(e.getActionCommand().equals("9")){
				        shape="Rect";
			        }if(e.getActionCommand().equals("10")){
			        	shape="3Drect";
			        }if(e.getActionCommand().equals("11")){
			        	shape="TC";
			        }
				}
			});
			//添加到工具栏面板
			jp.add(jb);	
		}
			//实例化一个面板设置线条粗细
			JPanel jpl=new JPanel();
			//设置面板属性
			jpl.setBackground(Color.LIGHT_GRAY);
			jpl.setPreferredSize(new Dimension(45,80));
			//添加到面板上
			jp.add(jpl);
	
			//添加图形按钮
			String []arr={"images/2.jpg","images/3.jpg","images/4.jpg","images/5.jpg","images/6.jpg"};
			for(int i=0;i<arr.length;i++){
				ImageIcon icn=new ImageIcon(arr[i]);
				//实例化按钮
				JButton bt=new JButton(icn);
				//设置命令属性
				bt.setActionCommand(""+(i+1));//!!!
				//设置大小
				bt.setPreferredSize(new Dimension(40,10));
				//给每个按钮添加监听器方法,使用匿名内部类
				bt.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						//System.out.print("++++++++++"+size);
						if(e.getActionCommand().equals("1")){
							size=2;
						//System.out.print("--------"+size);
						}
						else if(e.getActionCommand().equals("2")){
							size=5;						
						}
						else if(e.getActionCommand().equals("3"))
							size=10;
						else if(e.getActionCommand().equals("4"))
							size=18;
						if(e.getActionCommand().equals("5"))
							size=25;		
					}
					
				});
			
				//添加到该面板上
				jpl.add(bt);
	
			}
			
			
			
		
		
	}
//定义创建菜单栏的方法
	private void Manu() {
		//实例化一个JMenuBar作为菜单栏
		JMenuBar jm=new JMenuBar();
		//利用数组储存
		String Array[]={"文件","编辑","查看","图像","颜色","帮助"};
		String ArrayItem[][]={{"新建","打开","保存","另存为"},{"撤销","剪切","复制","粘贴"},{"工具箱","浏览","历史记录"},{"浏览文件"},{"颜色选择器"},{"帮助"}};
		for(int i=0;i<Array.length;i++){
			//实例化菜单选项
			JMenu mu=new JMenu(Array[i]);
			for(int j=0;j<ArrayItem[i].length;j++){
				//实例化菜单选项的子项
				JMenuItem mui=new JMenuItem(ArrayItem[i][j]);
				//添加到选项中
				mu.add(mui);
			}
			//把菜单选项添加到菜单栏
			jm.add(mu);
		}
		//设置其为窗体菜单栏对象
		jf.setJMenuBar(jm);
	}
}

(2)监听器处理方法部分
public class DrawListener extends MouseAdapter {

	
	private int x1, y1, x2, y2;
	private Graphics g;
	private XPDraw xp;//图形属性
	private int n = 1;
	private int x3, y3, x4, y4;
	private Color col;

	
	public static Shape [] array = new Shape[10000];//用来存储图形,静态变量用来方便外部调用
	public static int cnt = 0;//记录元素总数

	public DrawListener(Graphics g, XPDraw xp) {
		this.g = g;
		this.xp = xp;
		
	}

	public void mousePressed(MouseEvent e) {
		x1 = e.getX();
		y1 = e.getY();
		// 设置图形颜色
		if (e.getButton() == 1) {
			col=xp.getfColor();
		} else if (e.getButton() == 3) {
			col=xp.getbColor();
		}
		g.setColor(col);

	}

	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		//获取图形的形状
		String s = xp.getShape();
		System.out.println("will draw shape is:" + s);
		//绘制直线
		if (s.equals("Line")) {
			
			//实例化一个对象
			Shape sp=new ShapeLine(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
		}// 在画布上画矩形
		if (s.equals("Rect")) {
			
			//实例化一个对象
			Shape sp=new ShapeRect(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制矩形
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
		}
         if (s.equals("fillRect")) {
			
			//实例化一个对象
			Shape sp=new ShapeRect(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制矩形
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
		}
		//画圆角矩形
		if(s.equals("RoundRect")){
			
			//实例化一个对象
			Shape sp=new ShapeRect(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制矩形
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
		}
		// 画圆
		if (s.equals("Circle")) {
			
			//实例化一个对象
			Shape sp=new ShapeOval(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制圆
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
		}//3D图形
		if(s.equals("3Drect")){
		
			for(int i=0;i<30;i++ ){
				Color c=new Color(255-8*i,255-8*i,255-8*i);
				//实例化一个对象
				Shape sp=new ShapeRect(x1+i,y1+i,x2+i,y2+i,c,s,xp.getSize());
				//绘制矩形
				sp.draw((Graphics2D)g);
				//把图形记录到数组
				array[cnt]=sp;
				cnt++;
			}
			
		}
		// 喷枪
		if (s.equals("Spray")) {
		
			for (int i = 0; i < 40; i++) {
				Random ran = new Random();
				// 取随机数0-29
				int s1 = ran.nextInt(30) - 15;
				int s2 = ran.nextInt(20) - 10;
				// 画点
				//实例化一个对象
				Shape sp=new ShapeLine(x1-s1,y1-s2,x2-s1,y2-s2,col,s,xp.getSize());
				//绘制直线
				sp.draw((Graphics2D)g);
				//把图形记录到数组
				array[cnt]=sp;
				cnt++;
				//实例化一个对象
				sp=new ShapeLine(x1+s1,y1+s2,x2+s1,y2+s2,col,s,xp.getSize());
				//绘制直线
				sp.draw((Graphics2D)g);
				//把图形记录到数组
				array[cnt]=sp;
				cnt++;
			}
		}	
		// 多边形
		if (n == 1 && s.equals("Dshape")) {
			// 画出第一条直线'
			System.out.println("====>");
			//实例化一个对象
			Shape sp=new ShapeLine(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			x3 = x1;
			y3 = y1;
			x4 = x2;
			y4 = y2;
			n = 0;
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		if (n == 0 && xp.getShape().equals("Dshape")) {
			int x = e.getX();
			int y = e.getY();
			//实例化一个对象
			Shape sp=new ShapeLine(x4,y4,x,y,col,xp.getShape(),xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			// 判断是否双击
			if (x == x4 && y == y4) {
				//实例化一个对象
				sp=new ShapeLine(x,y,x3,y3,col,xp.getShape(),xp.getSize());
				//绘制直线
				sp.draw((Graphics2D)g);
				//把图形记录到数组
				array[cnt]=sp;
				cnt++;
				n = 1;
			}
			// 把坐标转存
			x4 = x;
			y4 = y;
		}
	}

	public void mouseDragged(MouseEvent e) {
		String s = xp.getShape();
		if (s.equals("Pencil")) {// 用画笔在画布上画曲线
			x2 = e.getX();
			y2 = e.getY();
	
			//实例化一个对象
			Shape sp=new ShapeLine(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
			x1 = x2;// 交换首尾值
			y1 = y2;
			
		}
		if (s.equals("Eraser")) {// 在画布上擦除
			x2 = e.getX();
			y2 = e.getY();
			g.setColor(Color.white);// 设置图形颜色为白色
			//实例化一个对象
			Shape sp=new ShapeLine(x1,y1,x2,y2,Color.white,s,xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
		
			x1 = x2;// 交换首尾值
			y1 = y2;
		}
		// 刷子
		if (s.equals("Brush")) {
			x2 = e.getX();
			y2 = e.getY();
			
			//实例化一个对象
			Shape sp=new ShapeLine(x1,y1,x2,y2,col,s,xp.getSize());
			//绘制直线
			sp.draw((Graphics2D)g);
			//把图形记录到数组
			array[cnt]=sp;
			cnt++;
			
			x1 = x2;// 交换首尾值
			y1 = y2;
		}
	}

}


(3)图形抽象类以及实现该抽象类的类
public abstract class Shape {
	private int x1, y1, x2, y2;
	private Color color;
	private String shapes;
	private int size;
	
	public Shape(int x1,int y1,int x2,int y2,Color color,String shapes,int size){
		this.x1=x1;
		this.x2=x2;
		this.y1=y1;
		this.y2=y2;
		this.color=color;
		this.shapes=shapes;
		this.size=size;
	}
	
	public Color getColor(){
		return color;
	}
	
	public String getShape(){
		return shapes;
	}
	
	public int getX1(){
		return x1;
	}
	
	public int getX2(){
		return x2;
	}
	
	public int getY1(){
		return y1;
	}
	
	public int getY2(){
		return y2;
	}
	public int getSize(){
		return size;
	}
	
	public abstract void draw(Graphics2D g);
	

}


public class ShapeLine extends Shape{

	public ShapeLine(int x1, int y1, int x2, int y2, Color color, String shapes, int size) {
		super(x1, y1, x2, y2, color, shapes, size);//调用父类的构造方法
		
	}

	@Override
	public void draw(Graphics2D g) {
		g.setColor(getColor());//设置颜色
		//设置笔画大小
		((Graphics2D) g).setStroke(new BasicStroke(getSize()));
		//绘制直线
		g.drawLine(getX1(), getY1(), getX2(), getY2());
		
		
		
	}
	

}


public class ShapeOval extends Shape{
    //定义构造方法
	public ShapeOval(int x1, int y1, int x2, int y2, Color color, String shapes, int size) {
		super(x1, y1, x2, y2, color, shapes,size);
		
	}

	@Override
	public void draw(Graphics2D g) {
		g.setColor(getColor());
		//设置笔画大小
		((Graphics2D) g).setStroke(new BasicStroke(getSize()));
		//画椭圆
		g.drawOval(Math.min(getX1(), getX2()),Math.min( getY1(),getY2()), Math.abs(getX2() - getX1()), Math.abs(getY2() - getY1()));
		
		
	}

}


public class ShapeRect extends Shape{

	public ShapeRect(int x1, int y1, int x2, int y2, Color color, String shapes,int size) {
		super(x1, y1, x2, y2, color, shapes,size);
		
	}

	@Override
	public void draw(Graphics2D g) {
		g.setColor(getColor());//设置颜色
		//设置笔画大小
		((Graphics2D) g).setStroke(new BasicStroke(getSize()));
		//绘制长方形
		if(this.getShape().equals("Rect"))
		    g.drawRect(Math.min(getX1(), getX2()),Math.min( getY1(),getY2()), Math.abs(getX2() - getX1()), Math.abs(getY2() - getY1()));
		//绘制圆角矩形
		else if (this.getShape().equals("RoundRect"))
			g.drawRoundRect(Math.min(getX1(),getX2()), Math.min(getY1(), getY2()), Math.abs(getX2()-getX1()), Math.abs(getY2()-getY1()), 20, 20);
		else if(this.getShape().equals("3Drect"))
			g.drawRect(Math.min(getX1(), getX2()),Math.min( getY1(),getY2()), Math.abs(getX2() - getX1()), Math.abs(getY2() - getY1()));
		else if(this.getShape().equals("fillRect"))
			g.fillRect(Math.min(getX1(), getX2()),Math.min( getY1(),getY2()), Math.abs(getX2() - getX1()), Math.abs(getY2() - getY1()));
	}

}


这是界面的效果图  

  • 大小: 77 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics