关闭

JAVA小白的奋斗之旅

标签: JAVA小白奋斗之旅java
42人阅读 评论(0) 收藏 举报
分类:

  练习画板:

  一、需要一个窗体并且设置它的相关属性

  public void UI(){
		setTitle("画板");
		setSize(600,600);
		setDefaultCloseOperation(3);
		setLocationRelativeTo(null);
		setVisible(true);
	}

   二、我把画板分为了三个部分,左边的工具栏、中间的绘画区、下边的颜色选择区

所以需要三个面板,并且要设置一下窗体的布局,个人认为边框布局挺适合这种情况的。

工具栏:

JPanel tool = new JPanel();
	tool.setPreferredSize(new Dimension(70,0));     
add(tool,BorderLayout.WEST);

工具栏有很多工具进行选择需要用到按钮,这里我使用的是单选按钮然后使用循环进行创建、添加。因为是单选按钮,所以还需要一个按钮组来限制这些按钮:

 ButtonGroup bgp = new ButtonGroup();
		//按钮
		for(int i = 0;i<16;i++){
			JRadioButton toolbutton = new JRadioButton();
			bgp.add(toolbutton);
			tool.add(toolbutton);
		}

工具的图片添加,选择工具有四种状态,分别用四张图片表示所以我创建了四个ImageIcon对象:

for(int i = 0;i<16;i++){
	JRadioButton toolbutton = new JRadioButton();
   toolbutton.setBorder(null);			
	ImageIcon img1 = new ImageIcon("images/draw"+i+".jpg");
	ImageIcon img2 = new ImageIcon("images/draw"+i+"-1.jpg");
	ImageIcon img3 = new ImageIcon("images/draw"+i+"-2.jpg");
	ImageIcon img4 = new ImageIcon("images/draw"+i+"-3.jpg");		
        toolbutton.setIcon(img1);
	toolbutton.setRolloverIcon(img2);
	toolbutton.setPressedIcon(img3);
        toolbutton.setSelectedIcon(img4);	
        bgp.add(toolbutton);
	tool.add(toolbutton)
绘画区,绘画区我分为两个部分一个是下面的底板部分,一个是上面真正的绘画区,所以还需要在draw面板上添加一个新的面板:
JPanel draw = new JPanel();
draw.setBackground(Color.gray);
draw.setLayout(new FlowLayout(1,0,0));
add(draw,BorderLayout.CENTER);
//绘画面板
JPanel draw_draw = new JPanel();
draw_draw.setPreferredSize(new Dimension(400,400));
draw_draw.setBackground(Color.WHITE);
draw.add(draw_draw);

绘画面板添加鼠标监听、画笔使其可以画东西:

画笔通过构造方法来传递:

public static Graphics g;
   public test1(Graphics gr){
      g = gr;
   }      

鼠标监听先定义四个int型全局变量x1,y1,x2,y2然后通过mousePressedmouseReleased两个方法来获取鼠标的坐标使其画东西:

public void mousePressed(MouseEvent e) {
	x1 = e.getX();
	y1 = e.getY();	
}
public void mouseReleased(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
	g.drawLine(x1, y1, x2, y2);
}

颜色选择区:分为前景背景区以及颜色选择区所以还需要在上面加两个面板:

前景、背景区:用绝对定位与按钮实现:

JPanel color = new JPanel();
color.setPreferredSize(new Dimension(0,70));
color.setLayout(newFlowLayout(0,0,0));  
color.setBackground(Color.cyan);  
add(color,BorderLayout.SOUTH);
JPanel color_bg = new JPanel();
color_bg.setLayout(null);
color_bg.setBackground(new Color(238,238,238));
color_bg.setPreferredSize(new Dimension(80,70)); 
color_bg.setBorder(newBevelBorder(0, Color.WHITE, Color.GRAY)); 
color.add(color_bg);
JButton bgb = new JButton();
bgb.setBackground(Color.BLACK);
bgb.setBounds(20,20, 25, 25);
color_bg.add(bgb);
JButton bgw = new JButton();
bgw.setBackground(Color.WHITE);
bgw.setBounds(30,30, 25, 25);
color_bg.add(bgw);
颜色选择区:使用按钮和颜色数组实现

JPanel color_choose = new JPanel();
color_choose.setBackground(new Color(238,238,238));
color_choose.setLayout(newFlowLayout(0,0,0));
color_choose.setPreferredSize(new Dimension(205,40));
color_choose.setBorder(newBevelBorder(1,Color.WHITE,Color.GRAY));
color.add(color_choose);    
Color[]color_array={Color.red,Color.black,Color.blue,Color.cyan,Color.gray,Color.green,Color.lightGray,Color.magenta,Color.orange,Color.pink,Color.white,Color.yellow,new Color(238,238,238),new Color(200,255,200),new Color(159,255,250),new Color(159,215,120),new Color(214,15,20),new Color(30,201,202),new Color(120,200,200),new Color(104,109,18)};
for(inti = 0;i<20;i++){
JButton color_btn = new JButton();
color_btn.setPreferredSize(new Dimension(20,20));
color_btn.setBorder(new BevelBorder(1,Color.white,Color.GRAY));
color_btn.setBackground(color_array[i]);
color_choose.add(color_btn);
}

颜色选择区的按钮监听实现:使用ActionListener来实现,通过ActionListener中的getSource方法拿到事件源然后获取事件源的背景色给画笔

ActionListener action = new ActionListener(){
       public voidactionPerformed(ActionEvent e) {
          // TODO Auto-generated method stub
            JButtonjbtn= (JButton)e.getSource();
            Colorc = jbtn.getBackground();
       g.setColor(c);
    }  };

工具选择的监听实现:定义一个Stringcommand,使用ActionCommand给单选按钮命名,再在mousePressed方法里面获取到按钮组中是点击了哪个按钮并传给command,在mouseReleased方法里判断是获取哪个并作出相应的动作:

public String command;
	public void mousePressed(MouseEvent e) {
	x1 = e.getX();
	y1 = e.getY();
	ButtonModel bm = bg.getSelection();
	command = bm.getActionCommand();
}
public void mouseReleased(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
	if(command.equals("10")){
	g.drawLine(x1, y1, x2, y2);
	}
}

铅笔、刷子、橡皮擦的实现:使用MouseMotionListener中的mouseDragged来实现:

MouseMotionListener mml = new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
	// TODO Auto-generated method stub
	x2 = e.getX();
	y2 = e.getY();
	if(command.equals("6")){
	g.drawLine(x1,y1,x2,y2);
	x1 = x2;
	y1 = y2;
	}else if(command.equals("7")){
	g.setStroke(new BasicStroke(10));
	g.drawLine(x1,y1,x2,y2);
        x1 = x2;
        y1 = y2;
	g.setStroke(new BasicStroke(1));
	}else if(command.equals("2")){
	Color c1 =g.getColor();
	g.setStroke(new BasicStroke(7));
	g.setColor(Color.WHITE);
	g.drawLine(x1,y1,x2,y2);
	x1 = x2;
	y1 = y2;
	g.setStroke(new BasicStroke(1));
g.setColor(c1);
	}		
}
成果:











0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:337次
    • 积分:71
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论