实现重绘的仿XP画板_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 实现重绘的仿XP画板

实现重绘的仿XP画板

 2014/4/19 3:36:47  齐在  程序员俱乐部  我要评论(0)
  • 摘要:做的是仿XP的画板,在功能上实现了铅笔、直线、喷枪、刷子、橡皮擦、矩形、圆角矩形、椭圆、多边形这些功能,左右键可以绘制不同颜色的图形。颜色栏有12种颜色可供选择,颜色框最左边的两个按钮分别以黑色和白色作为初始颜色。下面来介绍一下整个思路:首先,需要创建好整个界面;创建北边菜单栏,并利用一维数组和二维数组来存放文本内容;创建西边工具栏面板,利用数组来存放图片名;创建南边颜色栏面板,创建添加两个面板,一个用于放置展现左右键选定的颜色的按钮的面板,一个用于放置颜色选择的按钮(设置为网格布局)
  • 标签:实现 重绘
  • 做的是仿XP的画板,在功能上实现了铅笔、直线、喷枪、刷子、橡皮擦、矩形、圆角矩形、椭圆、多边形这些功能?,左右键可以绘制不同颜色的图形。
  • 颜色栏有12种颜色可供选择,颜色框最左边的两个按钮分别以黑色和白色作为初始颜色
  • 下面来介绍一下整个思路:?
  1. 首先,需要创建好整个界面;
  2. 创建北边菜单栏,并利用一维数组和二维数组来存放文本内容;
  3. 创建西边工具栏面板,利用数组来存放图片名;
  4. 创建南边颜色栏面板,创建添加两个面板,一个用于放置展现左右键选定的颜色的按钮的面板,一个用于放置颜色选择的按钮(设置为网格布局);
  5. 创建中间面板(设置为流式布局左对齐),并另外创建一个放置画布的面板;
  6. 创建监听器的方法,并添加到事件源上(工具栏的按钮共用一个监听器,颜色栏的按钮共用另一个监听器);
  7. 将各种创建的面板添加到界面上(一般是在创建之后就添加,这样可以不断调试来完善功能,美化界面)
  8. 接下来要做的事情就是需要重绘画图板,让画图板在移动、放大、缩小等操作后仍然保持原来的状态。思路就是要把所画的每一个图案都存起来,包括图案的形状,颜色等,然后再需要把这些存放好的图案取出来,这个时候就需要利用数组来实现了。
class="java" name="code">//主界面代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class FiFrame extends JFrame {
	public static void main(String args[]){
		FiFrame fiframe=new FiFrame();
		fiframe.FiPainter();
	}
	public void FiPainter(){
		//设置窗体关闭后退出
		this.setDefaultCloseOperation(3);
		//设置窗体的标题
		this.setTitle("终极画图板");
		this.setSize(1100, 600);
		this.setLocationRelativeTo(null);
		//调用创建北边菜单栏的方法
		this.NMenu();
		//调用创建西边面板的方法
		WPanel wpanel=new WPanel();
		this.add(wpanel,BorderLayout.WEST);		//设置位置为边框布局的西边
		//调用创建南边面板的方法
		SPanel spanel=new SPanel();
		this.add(spanel,BorderLayout.SOUTH);	//设置位置为边框布局的南边
		JPanel panel=new JPanel(){
			public void paint(Graphics g){
			super.paint(g);
			for(int i=0;i<FiListener.number;i++){
			Shapes shape =FiListener.array[i];
			shape.draw((Graphics2D)g);
		}
		}		
	};
		panel.setBackground(Color.WHITE);
		this.add(panel, BorderLayout.CENTER);
		//将窗体设置为可见
		this.setVisible(true);
		//只有在窗体可见之后才可以获取画布对象
		Graphics g=panel.getGraphics();
		String shapes;
		//调用创建监听器的方法,并传入参数
		FiListener fl=new FiListener(g,wpanel,spanel);
		//给事件源加上MouseListener和MouseMotionListener这两个监听器
		panel.addMouseListener(fl);
		panel.addMouseMotionListener(fl);
		
	}

	//定义一个创建菜单栏的方法
	public void NMenu(){
		//实例化一个JMenuBar类的对象nmenu
		JMenuBar nmenu = new JMenuBar();	
		//定义一个一维数组来存放JMenu中存放的文本内容
		String array[] = {"文字","查看","编辑","图像","颜色","帮助"};
		//定义一个二维数组来存放JMenuItem中存放的文本内容
		String [][] array2 ={{"新建","打开","保存"},{},{"工具箱","颜料盒"},{"清除"},{"编辑颜色"},{"关于画图"}};
		// 循环遍历array数组
		for(int i=0;i<array.length;i++){
			JMenu jmenu = new JMenu(array[i]);
			// 循环遍历array2数组
			for(int j=0;j<array2[i].length;j++){
				JMenuItem jmt=new JMenuItem(array2[i][j]);
				//将jmt添加到jmenu上
				jmenu.add(jmt);			
			}
			//将jmenu添加到nmenu上
			nmenu.add(jmenu);				
		}
		this.add(nmenu,BorderLayout.NORTH);
	}
	
}

?

//监听器代码
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

public class FiListener extends MouseAdapter {
	private boolean flag = true;
	private int x0,y0,x1,y1,x2,y2,x3,y3;
	private Graphics2D g;
	private SPanel spanel;
	private WPanel wpanel;
	public static Shapes[] array=new Shapes[10000];
	public static int number=0; 
	
	public FiListener(Graphics g,WPanel wpanel,SPanel spanel){
		this.wpanel=wpanel;
		this.spanel=spanel;
		this.g=(Graphics2D) g;
	}


	public void mouseClicked(MouseEvent e) {
    	//绘制多边形
    	if(flag==false && wpanel.getShapes().equals("png/多边形.png")){
    		int x=e.getX();
    		int y=e.getY();
    		Shapes shape=new ShapeLine(x3, y3, x, y,wpanel.getShapes(),g.getColor());
    		shape.draw(g);
    		array[number] = shape;
        	number++;
    		 if(x==x3 && y==y3){  
    			shape= new ShapeLine(x, y, x2, y2,wpanel.getShapes(),g.getColor());
    	    	shape.draw(g);
        		array[number] = shape;
            	number++;
    			flag=true;  
    		}  
    		x3=x;
    		y3=y;
    	}
    }
    public void mousePressed(MouseEvent e) {
    	//得到初始点的位置

    	x0=e.getX();
    	y0=e.getY();
    	
    	//获取颜色
    	if(e.getButton() ==1){
    				g.setColor(spanel.colorleft());
    			}
    	if(e.getButton()==3){
    				g.setColor(spanel.colorright());
    			}  		    
    }	
    public void mouseDragged(MouseEvent e){
    	//用铅笔绘制
		if(wpanel.getShapes().equals("png/铅笔.png")){
        	x1=e.getX();
        	y1=e.getY();
    		Shapes shape= new ShapeLine(x0, y0, x1, y1,"png/铅笔.png",g.getColor());
    		shape.draw(g);
        	//将末位置变成下一次的初始位置
        	x0=x1;
        	y0=y1;
    		array[number] = shape;
        	number++;
    	}
    	//使用橡皮擦
    	if(wpanel.getShapes().equals("png/橡皮擦.png")){
        	x1=e.getX();
        	y1=e.getY();
        	g.setColor(Color.WHITE);
        	((Graphics2D) g).setStroke(new BasicStroke(10));
    		Shapes shape= new ShapeLine(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
    		shape.draw(g);

    		//将末位置变成下一次的初始位置
        	x0=x1;
        	y0=y1;
        	array[number] = shape;
        	number++;
    	}
    	//使用喷枪绘制
    	if(wpanel.getShapes().equals("png/喷枪.png")){
        	x1=e.getX();
        	y1=e.getY();
        	Random r= new Random();
        	for(int i=0;i<20;i++){
        	int x= r.nextInt(10);
        	int y= r.nextInt(10);
        	((Graphics2D) g).setStroke(new BasicStroke(1));
    		Shapes shape= new ShapeLine(x1+x, y1+y, x1+x,y1+y,wpanel.getShapes(),g.getColor());
    		shape.draw(g);

        	//将末位置变成下一次的初始位置
        	x0=x1;
        	y0=y1;
        	array[number] = shape;
        	number++;
        
        	}
    	}
    	//使用刷子绘制
    	if(wpanel.getShapes().equals("png/刷子.png")){
        	x1=e.getX();
        	y1=e.getY();
        	((Graphics2D) g).setStroke(new BasicStroke(10));
    		Shapes shape= new ShapeLine(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
    		shape.draw(g);
        	//将末位置变成下一次的初始位置
        	x0=x1;
        	y0=y1;
        	array[number] = shape;
        	number++;
    	}
    }
    public void mouseMoved(MouseEvent e){

    }
    public void mouseReleased(MouseEvent e) {
    	if(wpanel.getShapes().equals("png/直线.png")){
        	x1=e.getX();
        	y1=e.getY();
        	((Graphics2D) g).setStroke(new BasicStroke(1));
    		Shapes shape= new ShapeLine(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
    		shape.draw(g);
    	   	array[number] = shape;
        	number++;
    	}
    	if(wpanel.getShapes().equals("png/矩形.png")){
        	x1=e.getX();
        	y1=e.getY();
        	((Graphics2D) g).setStroke(new BasicStroke(1));
        	Shapes shape=new ShapeRect(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
        	shape.draw(g);
           	array[number] = shape;
        	number++;
    	}
    	if(wpanel.getShapes().equals("png/圆角矩形.png")){
        	x1=e.getX();
        	y1=e.getY();
        	((Graphics2D) g).setStroke(new BasicStroke(1));
           	Shapes shape=new ShapeRect(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
        	shape.draw(g);
           	array[number] = shape;
        	number++;
    	}
    	if(wpanel.getShapes().equals("png/椭圆.png")){
        	x1=e.getX();
        	y1=e.getY();
        	((Graphics2D) g).setStroke(new BasicStroke(1));
        	Shapes shape=new ShapeRect(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
        	shape.draw(g);
           	array[number] = shape;
        	number++;	
    	}
    	if(wpanel.getShapes().equals("png/多边形.png")){
        	x1=e.getX();
        	y1=e.getY();
			if(flag){	
			Shapes shape1=new ShapeLine(x0, y0, x1, y1,wpanel.getShapes(),g.getColor());
        	shape1.draw(g);
    		array[number] = shape1;
        	number++;
        	x2=x0;
        	y2=y0;
        	x3=x1;
        	y3=y1;
			flag=false;

			}        		
    	}
    }
}

?

//颜色栏
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;

public class SPanel extends JPanel {
	private Color color;
	public SPanel(){
		spanel();
	}
	//创建调用left按钮颜色的方法
	public Color colorleft(){		
		return buttonleft.getBackground();		
	}
	//创建调用right按钮颜色的方法
	public Color colorright(){		
		return buttonright.getBackground();	
	}
	
	private JButton buttonright = new JButton();
	private JButton buttonleft = new JButton();
	// 事件处理类对象
	private MouseAdapter md=new  MouseAdapter(){
		public void mousePressed(MouseEvent e) {
			//获取事件源对象
			JButton button = (JButton) e.getSource();
			color = button.getBackground();
			//如果按下的是鼠标左键则改变的是left的背景颜色
			if(e.getButton() ==1){
				buttonleft.setBackground(color);
			}
			//如果按下的是鼠标左键则改变的是right的背景颜色
			if(e.getButton()==3){
				buttonright.setBackground(color);
			}
		}
	};
	public void spanel(){
		this.setPreferredSize(new Dimension(0,50));
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setBackground(Color.WHITE);
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setPreferredSize(new Dimension(40,40));
		//设置left按钮的大小和背景颜色
		buttonleft.setBounds(8, 8, 15, 15);
		buttonleft.setBackground(Color.BLACK);
		//设置right按钮的大小和背景颜色
		buttonright.setBounds(16, 16, 15, 15);
		buttonright.setBackground(Color.WHITE);
			
		panel1.add(buttonleft);
		panel1.add(buttonright);
		this.add(panel1);
		
		JPanel panel2 = new JPanel();
		//设置为网格布局,二行六列
		panel2.setLayout(new GridLayout(2,6));
		Color array[] ={Color.BLACK,Color.RED,Color.CYAN,Color.LIGHT_GRAY,
				Color.magenta,Color.WHITE,Color.BLUE,Color.GRAY,
				Color.green,Color.orange,Color.pink,Color.yellow};
		for(int i=0;i<array.length;i++){
			JButton jbutton = new JButton();
			jbutton.setBackground(array[i]);
			jbutton.setPreferredSize(new Dimension(15,15));
			jbutton.addMouseListener(md);

			panel2.add(jbutton);
		}
		this.add(panel2);
	}
	public Color getcolor() {
		return color;
	}
}

?

//工具栏代码
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;


public class WPanel extends JPanel {
	private String shapes="png/铅笔.png";
	public WPanel(){
			wpanel();
	}
	public String getShapes(){
		return shapes;
	}
	public void wpanel(){
		//定义一个一维数组来存放工具栏图片的名字
		String array[]={"png/裁剪.png","png/选定.jpg","png/橡皮擦.png","png/填充.png","png/取色.png",
				"png/放大镜.png","png/铅笔.png","png/刷子.png","png/喷枪.png","png/文字.png","png/直线.png",
				"png/曲线.png","png/矩形.png","png/多边形.png","png/椭圆.png","png/圆角矩形.png"};
		ActionListener al = new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				shapes=e.getActionCommand();
			}
		};
		for(int i=0;i<array.length;i++){
			//实例化ImageIcon类的对象icon
			ImageIcon icon = new ImageIcon(array[i]);
			JButton jbutton = new JButton(icon);
			//获取所选择的图形按钮所对应的图片名称
			jbutton.setActionCommand(array[i]);
			jbutton.addActionListener(al);
			jbutton.setPreferredSize(new Dimension(25,25));
			this.add(jbutton);
		}

		this.setPreferredSize(new Dimension(60,0));
		this.setBackground(Color.LIGHT_GRAY);
		
	}
}

?

import java.awt.Color;
import java.awt.Graphics2D;


public abstract class Shapes {
	private int x0,y0,x1,y1;
	private Color color;
	private String shapes;
	public void Shapes(){
		
	}
	public void Shapes(int x0, int y0,int x1,int y1, String shapes,Color color){
		this.x0=x0;
		this.y0=y0;
		this.x1=x1;
		this.y1=y1;
		this.shapes=shapes;
		this.color=color;
		}
	public int getX0() {
		return x0;
	}
	public void setX0(int x0) {
		this.x0 = x0;
	}
	public int getY0() {
		return y0;
	}
	public void setY0(int y0) {
		this.y0 = y0;
	}
	public int getX1() {
		return x1;
	}
	public void setX1(int x1) {
		this.x1 = x1;
	}
	public int getY1() {
		return y1;
	}
	public void setY1(int y1) {
		this.y1 = y1;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public String getShapes() {
		return shapes;
	}
	public void setShapes(String shapes) {
		this.shapes = shapes;
	}
	public void draw(Graphics2D g) {

	}
}

?

//画直线类图形
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;


public class ShapeLine extends Shapes {
	public ShapeLine(int x0, int y0,int x1,int y1, String shapes,Color color){
		super.Shapes(x0, y0, x1, y1, shapes,color);
	}

	public void draw(Graphics2D g) {
		
		if(getShapes().equals("png/铅笔.png")||getShapes().equals("png/直线.png")||getShapes().equals("png/喷枪.png")){
			g.setColor(getColor());
			g.setStroke(new BasicStroke(1));
		}
		else if(getShapes().equals("png/橡皮擦.png")){
			g.setColor(Color.WHITE);
			g.setStroke(new BasicStroke(10));
		}
		else if(getShapes().equals("png/刷子.png")){
			g.setColor(getColor());
			g.setStroke(new BasicStroke(10));
		}
		else{
			g.setColor(getColor());
			g.setStroke(new BasicStroke(1));
		}
		g.drawLine(getX0(), getY0(), getX1(), getY1());
		
	}
	
}

?

//画矩形类图形
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;


public class ShapeRect extends Shapes{
	public ShapeRect(int x0, int y0,int x1,int y1, String shapes,Color color){
		super.Shapes(x0, y0, x1, y1, shapes,color);
	}

		public void draw(Graphics2D g){
			
			g.setColor(getColor());
			g.setStroke(new BasicStroke(1));
			if(getShapes().equals("png/圆角矩形.png")){

				 g.drawRoundRect(getX0(), getY0(),Math.abs(getX1()-getX0()), Math.abs(getY1()-getY0()), 50, 50);
			}
			else if(getShapes().equals("png/矩形.png")){
				g.drawRect(getX0(),getY0(), 
						Math.abs(getX1()-getX0()), Math.abs(getY1()-getY0()));
			}
			else if(getShapes().equals("png/椭圆.png")){
				g.drawOval(getX0(),getY0(), 
						Math.abs(getX1()-getX0()), Math.abs(getY1()-getY0()));
			}
			
		}
	
	
}

?

?

?

?

  • 大小: 18.3 KB
  • 查看图片附件
发表评论
用户名: 匿名