简单的画图板总结


一、简单的画图板设计结构



 

二、简单画图板的实现结果



 三、实现过程中的主要问题

1、UI
(1)BorderLayout的使用

JFrame jf = new JFrame();
jf.setLayout(new BorderLayout());
jf.add(top,BorderLayout.NORTH);

 (2)必须在设置可见之后才可以得到画布对象

 jf.setVisible(true);
g = panel.getGraphics();

 (3)菜单栏的添加

jf.setMenuBar(db);   (正确)
jf.add(db);          (错误)

 (4)鼠标监听器的添加

panel.addMouseListener(dl);
 panel.addMouseMotionListener(dl);

 2、ShapePanel
(1)在构造方法中调用内部方法以实现

//构造方法
public ShapePanel(){
         shapepanel();
}

 (2)另外实例化一个专门放置形状选择图片的面板,GridLayout的使用

//实例化一个JPanel对象,作为形状选择的面板
 
JPanel panelshape=new JPanel();
 
//设置形状选择面板的布局为网格布局

 panelshape.setLayout(new GridLayout(3,7,3,3));

 (3)通过定义图片数组,并使用for循环结构,将图片加到形状按钮上

 //定义一个数组,包含所有形状选择的图片
      String array={"shapeimages/001.jpg","shapeimages/002.jpg","shapeimages/003.jpg","shapeimages/004.jpg",
                        "shapeimages/005.jpg","shapeimages/006.jpg","shapeimages/007.jpg","shapeimages/008.jpg",
			"shapeimages/009.jpg","shapeimages/010.jpg","shapeimages/011.jpg","shapeimages/012.jpg",
			"shapeimages/013.jpg","shapeimages/014.jpg","shapeimages/015.jpg","shapeimages/016.jpg",
			"shapeimages/017.jpg","shapeimages/018.jpg","shapeimages/019.jpg","shapeimages/020.jpg","shapeimages/021.jpg"};
      //使用循环结构方便将图片添加到按钮上
		for(int i=0;i<array.length;i++){
			//实例化ImageIcon对象,地址为数组
			ImageIcon image=new ImageIcon(array[i]);
			//实例化按钮对象,并添加图片
			JButton btn=new JButton(image);
		}

 

 (4)得到图片的名字(substring的使用),并设置按钮的命令为图片的名字

String filename=array[i].substring(array[i].indexOf("/")+1, array[i].lastIndexOf(".jpg"));
	btn.setActionCommand(filename);

 (5)设置优先大小

//设置按钮的优先选取的大小(Dimension:尺寸)
	btn.setPreferredSize(new Dimension(20,20));

 

(6)在按钮上添加得到其命令的鼠标监听器
btn.addActionListener(al);
	//定义一个监听器
	private ActionListener al=new ActionListener (){
		 public void actionPerformed(ActionEvent e){
			 //获取事件源对象
			 Object obj=e.getSource();
			 //强制转型为按钮对象
			 JButton btn=(JButton)obj;
			 //把按钮的动作命令赋值给type
			 DrawingListener.type=btn.getActionCommand();
		 }
	};
 3、ToolPanel
(1)定义了粗细的选择之后,设置弹出菜单的实现JpopupMenu的使用,show方法的调用,设置所有者
//弹出菜单的实现
		final javax.swing.JPopupMenu menu = new javax.swing.JPopupMenu();
		String[] array1 = { "images/4.jpg",  "images/5.jpg", "images/6.jpg", "images/7.jpg" };

		for (int i = 0; i < array1.length; i++) {
			ImageIcon icon = new ImageIcon(array1[i]);
			javax.swing.JMenuItem item = new javax.swing.JMenuItem(icon);
			menu.add(item);
			//substring 返回一个新字符串,它是此字符串的一个子字符串。
			String s = array1[i].substring(array1[i].indexOf("/") + 1,array1[i].lastIndexOf(".jpg"));			
			item.setActionCommand(s);
			item.addActionListener(al1);
			
		}
       		 //设置弹出菜单的所有者
		menu.setInvoker(btn2);
        	//给按钮添加监听器
		btn2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                //设置点击按钮时,出现弹出菜单,并且设置弹出菜单的位置
				menu.show(btn2, btn2.getLocation().x-100 ,
						btn2.getLocation().y + btn2.getHeight()-10);

			}

		});
 (2)返回粗细值方法的使用
private float stroke=1;
	public float getstroke(){
		return stroke;
	}
	private ActionListener al1 = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			JMenuItem item = (JMenuItem) e.getSource();
			DrawingListener.type = item.getActionCommand();
			if(DrawingListener.type.equals("4")){
				stroke=1;
			}
			else if(DrawingListener.type.equals("5")){
				stroke=4;
			}
			else if(DrawingListener.type.equals("6")){
				stroke=7;
			}
			else if(DrawingListener.type.equals("7")){
				stroke=10;
			}

		}

	};
 4、ColorPanel
(1)鼠标进入出现红色边框的实现,LineBorder的使用
MouseListener mlis = new MouseAdapter() {
		
		public void mouseReleased(MouseEvent e) {
			JLabel label = (JLabel) e.getSource();
			color = label.getBackground();
			DrawingListener.command = "颜色面板";
		}

		public void mouseEntered(MouseEvent e) {
			JLabel label = (JLabel) e.getSource();
			label.setBorder(new LineBorder(Color.RED));

		}

		public void mouseExited(MouseEvent e) {
			JLabel label = (JLabel) e.getSource();
			label.setBorder(new LineBorder( Color.GRAY));
		}

	};
 (2)绘制边界内像素点
//如果为 true,则该组件绘制其边界内的所有像素。
	btn.setOpaque(true);
 (3)得到颜色的方法
public Color color;
	
	//定义选取得到颜色的方法
	public Color getColor(){
		return color;
	}
 5、ColorChooserPanel
(1)弹出颜色选择器
//设置按钮的监听器
	btn.addMouseListener(new MouseAdapter(){
		public void mouseClicked(MouseEvent e) {
			showcolorchooser();
			//btn.setActionCommand("选择颜色");
			DrawingListener.command="选择颜色";
		}
			
	});
	//弹出颜色选择器
	private void showcolorchooser(){
		color=JColorChooser.showDialog(null, "颜色选择器", Color.black);
	}

 (2)得到颜色的方法

private Color color;
	//得到颜色
	public Color getcolor(){
		return color;
	}

 

6、DrawingPanel
(1)重绘
//实例化一个JPanel对象,作为画布绘制的对象,并设置重绘
         public  JPanel panel=new JPanel(){
    	 //重写方法,进行重绘
    	 	public void paint(Graphics g){
			super.paint(g);
			for(int i=0;i<UI.count;i++){
				//将储存的图形一一重新绘制
			    UI.shapearray[i].shape(g);
			}
		}
         }	
 (2)得到画画面板的方法
public  JPanel getJPanel(){
		return panel;
	}
 7、DrawingListener
(1)改变面板大小,并设置鼠标光标,Cursor的使用
public void mouseEntered(MouseEvent e) {
		 
		 if ((x1 >= panel.getSize().width-50 && x1 <= panel.getSize().width) 
				||( y1 >= panel.getSize().height-50
				&& y1 <= panel.getSize().height )) {
 		//调整窗口右下角大小的光标类型。
			dp.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
 	
		}
		 else{
			//调整窗口光标类型还原。
				dp.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		 }
	}

	public void mouseDragged(MouseEvent e) {
		if (x1 >= panel.getSize().width-50 
				&& x1 <= panel.getSize().width 
				&& y1 >= panel.getSize().height-50 
				&& y1 <= panel.getSize().height) {
			panel.setSize(new Dimension(x2, y2));
			panel.repaint();
			g = panel.getGraphics();
		}
	}
 (2)设置粗细
Graphics2D g2d = (Graphics2D) g;
	g2d.setStroke(new BasicStroke(tp.getstroke()));
 (3)多边形的完成
public void mouseReleased(MouseEvent e) {
	//多边形
    	if(type.equals("006")){
        	if (state) {
    			x2 = e.getX();
    			y2 = e.getY();
    			a = x1;
    			b = y1;
    			c = x2;
    			d = y2;
    			 shape = new ShapePolygon(x1, y1, x2, y2, color);
    			shape.shape(g);
    			state = false;
    		} else {
    			x1 = c;
    			y1 = d;
    			x2 = e.getX();
    			y2 = e.getY();
    			c = x2;
    			d = y2;
    			 shape =  new ShapePolygon(x1, y1, x2, y2, color);
    			shape.shape(g);
    		}
    	} 
}
public void mouseClicked(MouseEvent e) {
	Graphics2D g2d = (Graphics2D) g;
	g2d.setStroke(new BasicStroke(tp.getstroke()));
	if (type.equals("006")) {
		NetJavaShape shape = new ShapePolygon(c, d, a, b, color);
		shape.shape(g);
		state = true;
	}

}
 (4)实现拖拽图形
Color color_temp = Color.white;
	if(type.equals("001")){
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setStroke(new BasicStroke(tp.getstroke()));
    		shape=new ShapeLine(x1,y1,x2,y2,color);
    		NetJavaShape shape_temp=new ShapeLine(x1,y1,x3,y3,color_temp);
    		shape_temp.shape(g);//清除上一次的图形
        	shape.shape(g);//画新的图形
        	//调用重绘方法
        	x3 = x2;
        	y3 = y2;
    	}
 (5)判断颜色来自颜色面板,还是颜色编辑器
//颜色的选择
        String command=DrawingListener.command;
    	if(command.equals("选择颜色")){
    		color = ccp.getcolor();
    		}
    	else if(command.equals("颜色面板")){
    		color = cp.getColor();  		
    	}
 (6)重绘中储存图形
//储存图形,计数器加1,已进行重绘
    	if(UI.count<UI.shapearray.length){
    		System.out.println("shape = "+shape);
			UI.shapearray[UI.count] = shape;
			UI.count++;
		}
 8、ToolEraser(粗了一点的白色铅笔)
9、ToolText(JOptionPane的使用)
public void shape(Graphics g) {
		g.setColor(color);
		String str=JOptionPane.showInputDialog("请输入文字:");
	    g.drawString(str, x1, y1);

	}
 10、DrawingBar
(1)二维数组的使用已实现菜单,菜单栏,菜单项
//调用的内部方法
	private void drawingbar() {
		//实例化菜单数组
		String [] arraymenu={"文件(F)","编辑(E)","图像(I)","颜色(C)","查看(V)","帮助(H)"};
		//实例化菜单项数组
		String[][] arraymenuitem = {
				{ "新建(N)", "打开(O)", "保存(S)", "另存为(A)", "打印预览(V)", "页面设置(U)",
						"打印(P)", "发送(E)", "设置为墙纸(平铺(B))", "设置为墙纸(居中)(K)",
						"退出(X)" },
				{ "撤销(U)", "重复(R)", "剪切(T)", "复制(C)", "粘贴(P)", "清除选定内容(L)",
						"全选(A)", "复制到(O)", "粘贴来源(F)" },
				{ "工具箱(T)", "颜料盒(C)", "状态栏(S)", "文字工具栏(E)", "缩放(Z)", "查看位图(V)" },
				{ "翻转/旋转(F)", "拉伸/扭曲(S)", "反色(I)", "属性(A)", "清除图像(C)",
				"不透明处理(D)" }, 
				{ "编辑颜色(E)" },
				{ "帮助主题(H)", "关于画图(A)" } 
				};
		//利用循环实现菜单栏(注意嵌套循环结构的使用)
		for(int i=0;i<arraymenu.length;i++){
			Menu menu=new Menu(arraymenu[i]);
			for(int j=0;j<arraymenuitem[i].length;j++){
				MenuItem menuitem=new MenuItem(arraymenuitem[i][j]);
				menu.add(menuitem);
			}
			this.add(menu);
		}
		
	}
	
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值