java学习笔记->画图板

简单画图板的制作

  • 工具
  • 步骤一:基本面板的创建
    • 1.创建一个窗体
    • 2.创建一个监听器类实现鼠标监听器
    • 3.取到按下释放的x,y值,取到Graphics 对象
    • 4.再将对象传递到监听器类中
    • 运行效果
  • 步骤二:基本画图功能的实现
    • 代码,有注释哦
    • 运行结果
    • 中间遇到的问题
      • Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
      • 超出边界画图问题
  • 步骤三:代码优化及功能丰富
    • 效果展示
  • 总结
  • 更新代码
    • 效果

环境:eclipse

工具

swing包(用于制作界面)
监听器:鼠标监听器MouseListener(用于监听鼠标动作)
绘制类:graphics类(用于画图)

步骤一:基本面板的创建

1.创建一个窗体

新建package命名为drawing_board,在其下新建class命名为board

package drawing_board;

import java.awt.FlowLayout;
import javax.swing.JFrame;

public class board {

	public static void main(String[] args){
	  board bo = new board();
	  bo.initUI();
	}

	public void initUI() {

		// top container -form JFrame instantiate object→create a form object
		JFrame jf = new JFrame();
		jf.setTitle("drawing board");
		jf.setSize(1920, 1080);
		//frame size自定义窗体大小
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// process close关闭进程
		jf.setLocationRelativeTo(null);
		// centered居中显示
		FlowLayout fl = new FlowLayout();
		// Fluid layout流式布局
		jf.setLayout(fl);
		

		jf.setVisible(true);// 可视化

	}
}

2.创建一个监听器类实现鼠标监听器

新建class命名为DrawingListener,输入

package drawing_board;

import java.awt.event.MouseListener;

public class DrawingListener implements MouseListener{

}

将鼠标移到MouseListener,按F3,复制粘贴相关代码

package drawing_board;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class DrawListener implements MouseListener {

	public void mouseClicked(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {
	
	}

}

3.取到按下释放的x,y值,取到Graphics 对象

继续补充代码

package drawing_board;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Graphics;

public class DrawListener implements MouseListener {
	int x1, x2, y1, y2;
	Graphics g;

	public void mouseClicked(MouseEvent e) {

		System.out.println("点击");
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("按下");
		x1 = e.getX();
		y1 = e.getY();
	}

	public void mouseReleased(MouseEvent e) {
		System.out.println("释放");
		x2 = e.getX();
		y2 = e.getY();
		g.drawLine(x1, y1, x2, y2);
		//to draw a line实现绘图功能
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("进入");
	}

	public void mouseExited(MouseEvent e) {
		System.out.println("离开");
	}

}

4.再将对象传递到监听器类中

接下来,在board中实现它的方法,在jf.setVisible(true)前后分别增添如下代码

		DrawListener dl = new DrawListener();
		jf.addMouseListener(dl);  
		//调用接口方法      
		jf.setVisible(true);// 可视化

		Graphics g = jf.getGraphics();
		dl.g=g;
		//传递画图对象

整个board代码如下

package drawing_board;

import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JFrame;

public class board {

	public static void main(String[] args){
	  board bo = new board();
	  bo.initUI();
	}

	public void initUI() {

		// top container -form JFrame instantiate object→create a form object
		JFrame jf = new JFrame();
		jf.setTitle("drawing board");
		jf.setSize(1920, 1080);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// process close关闭进程
		jf.setLocationRelativeTo(null);
		// centered居中显示
		FlowLayout fl = new FlowLayout();
		// Fluid layout流式布局
		jf.setLayout(fl);
		


		DrawListener dl = new DrawListener();
		jf.addMouseListener(dl);        
		jf.setVisible(true);// 可视化

		Graphics g = jf.getGraphics();
		dl.g=g;
	}
}

运行效果

java学习笔记->画图板_第1张图片
由于录屏软件的问题,鼠标被自动隐藏了。

步骤二:基本画图功能的实现

代码,有注释哦

上面的程序只能画直线,这肯定是不够的,拿着只能画直线的东东有啥用嘛,还不如自己一支铅笔一张纸靠谱呢,是吧。ᐕ)⁾⁾
来,我们先看一看窗体的构建,打开eclipse,左上角,new file,然后new package,然后新建一个DrawPad2类(class)和一个Listener类

package DrawPad2;

import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JButton;

public class DrawPad {
	Listener ls = new Listener();
	public static void main(String[] args) {
		DrawPad dp = new DrawPad();
		dp.initUI();
	}
	public void initUI() {
		JFrame jf = new JFrame();
		jf.setSize(1280, 720);
		jf.setLocationRelativeTo(null);
		//居中center the frame
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JButton jb1 = new JButton("直线");
		JButton jb2 = new JButton("矩形");
		JButton jb3 = new JButton("圆");
		JButton jb4 = new JButton("三角形");
		JButton jb5 = new JButton("红色");
		JButton jb6 = new JButton("蓝色");
		JButton jb7 = new JButton("黄色");
		JButton jb8 = new JButton("绿色");
		JButton jb9 = new JButton("黑色");
		FlowLayout fl = new FlowLayout();
        //啦啦啦(∩△∩),老样子,流式布局
        //Fiuid layout as usual
        
		Listener ls = new Listener();
		jb1.addActionListener(ls);
		jb2.addActionListener(ls);
		jb3.addActionListener(ls);
		jb4.addActionListener(ls);
		jb5.addActionListener(ls);
		jb6.addActionListener(ls);
		jb7.addActionListener(ls);
		jb8.addActionListener(ls);
		jb9.addActionListener(ls);
		jf.addMouseListener(ls);
        //调用Listener类,Listener继承了动作监听器和鼠标监听器的方法,等会会解释╰(*´︶`*)╯
        //call the Listener class which implements ActionListener and MouseListener
        
		jf.setLayout(fl);
		jf.add(jb1);
		jf.add(jb2);
		jf.add(jb3);
		jf.add(jb4);
		jf.add(jb5);
		jf.add(jb6);
		jf.add(jb7);
		jf.add(jb8);
		jf.add(jb9);
		jf.setVisible(true);
		//没这个窗体都打不开哦,我经常忘记这个,十分尴尬<(。_。)>
		Graphics g = jf.getGraphics();
		ls.g=g;
		//调用graphics类的方法,不然没法作图哟(*σ´∀`)σ
	}
}

很简单,只是建立了几个按钮而已啦,仍然采用流式布局~
咳咳,接下来才是重点,我们一起来see see那个Listener类,先看代码

package DrawPad2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Listener implements ActionListener, MouseListener {
	int x1, x2, y1, y2, x0, y0, w0, h0, y3, y4, y5, i = 0;
	int tx[] = { 0, 0, 0 };
	int ty[] = { 0, 0, 0 };
	//这个如果不赋初值,有可能会报错的哟,大家可以自己试一试
	Graphics g;

	String intest = "";


	public void actionPerformed(ActionEvent e) {
		intest = e.getActionCommand();
		if (intest.equals("红色"))
			g.setColor(Color.RED);
		if (intest.equals("蓝色"))
			g.setColor(Color.BLUE);
		if (intest.equals("黄色"))
			g.setColor(Color.YELLOW);
		if (intest.equals("绿色"))
			g.setColor(Color.GREEN);
		if (intest.equals("黑色"))
			g.setColor(Color.BLACK);
	    //呐,这个很简单的啦,线条颜色设定嘛,问题不大

	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("点击");
		
//三角形方法一
		if (intest.contentEquals("三角形")) {
			tx[i] = e.getX();
			ty[i] = e.getY();
			i++;
			if (i == 1) {

				g.drawLine(tx[0], ty[0], tx[0], ty[0]);

			}
			else if (i == 2) {

				g.drawLine(tx[0], ty[0], tx[1], ty[1]);

			}
			else if (i == 3) {

				g.drawLine(tx[1], ty[1], tx[2], ty[2]);
				g.drawLine(tx[2], ty[2], tx[0], ty[0]);
				
				i = 0;
                //重置计数
			}
//这是一个,取两个点就画一条线的模式,待会儿会给大家演示的啦

//三角形方法二
//		if (i == 3) {
//			g.drawLine(tx[0], ty[0], tx[1], ty[1]);
//			g.drawLine(tx[1], ty[1], tx[2], ty[2]);
//			g.drawLine(tx[2], ty[2], tx[0], ty[0]);
//			System.out.println(ty[0] + "    " + tx[0]);
//		System.out.println(ty[1] + "    " + tx[1]);
//		System.out.println(ty[2] + "    " + tx[2]+"  3");
//		System.out.println(ty[3] + "    " + tx[3]);
////这几行是测试代码啦,我调值的时候遇到了老多波折了,哭唧唧,上面第一种方法,我把这四行代码复制了3遍,才搞清楚关系
//			i = 0;
//
//		}
//这是一个,取完三个点再一次性划线的模式

		}

	}

	public void mousePressed(MouseEvent e) {
		System.out.println("按下");
		x1 = e.getX();
		y1 = e.getY();

	}

	public void mouseExited(MouseEvent e) {
		System.out.println("离开");

        //其实这里没有必要的啦,但是,不加这串代码的话,会出问题的,就是鼠标出边界的时候,就画不了图了,等会给大家演示下哈
		x2 = e.getX();
		y2 = e.getY();
		if (x1 > x2) {
			x0 = x2;
			w0 = x1 - x2;
		} else {
			x0 = x1;
			w0 = x2 - x1;
		}

		if (y1 > y2) {
			y0 = y2;
			h0 = y1 - y2;
		} else {
			y0 = y1;
			h0 = y2 - y1;
		}
		
        //大家看懂这波操作了嘛,看懂了?大佬大佬,我还是太嫩了,求大佬给些更好的意见。没看懂?来来来,首先啊,Graphics类,默认的坐标轴,是y轴向下的,同时左上角是原点,然后,Graphics作图的习惯是,取一个基准点(总是两个坐标中的最小值),然后规定长宽,同时Graphics类里的函数都不可以带负值,所以,我们要把长宽取绝对值,前后两个坐标的x和y分别取小的值,还不清楚可以留言问我哦
        
		if (intest.equals("矩形")) {

			g.drawRect(x0, y0, w0, h0);
		} else if (intest.equals("圆")) {
			g.drawOval(x0, y0, w0, h0);
		}

	}



	public void mouseReleased(MouseEvent e) {
		System.out.println("释放");
		x2 = e.getX();
		y2 = e.getY();

		if (x1 > x2) {
			x0 = x2;
			w0 = x1 - x2;
		} else {
			x0 = x1;
			w0 = x2 - x1;
		}

		if (y1 > y2) {
			y0 = y2;
			h0 = y1 - y2;
		} else {
			y0 = y1;
			h0 = y2 - y1;
		}
        //跟上面一样的操作啦(>y<)
		if (intest.equals("直线")) {

			g.drawLine(x1, y1, x2, y2);

		} else if (intest.equals("矩形")) {

			g.drawRect(x0, y0, w0, h0);

		} else if (intest.equals("圆")) {

			g.drawOval(x0, y0, w0, h0);

		}
//		else 
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("进入");
	}

}

//下面是之前的旧代码啦,有点冗余,先得出变量结果再进入画的东西就很方便啦
//旧矩形
//if (x2 > x1) {
//
//	if (y2 > y1)
//		g.drawRect(x1, y1, x2 - x1, y2 - y1);
//	else
//		g.drawRect(x1, y2, x2 - x1, y1 - y2);
//} else {
//	if (y2 > y1)
//		g.drawRect(x2, y1, x1 - x2, y2 - y1);
//	else
//		g.drawRect(x2, y2, x1 - x2, y1 - y2);
//}

//旧圆
//if (x2 > x1) {
//
//	if (y2 > y1)
//		g.drawOval(x1, y1, x2 - x1, y2 - y1);
//	else
//		g.drawOval(x1, y2, x2 - x1, y1 - y2);
//} else {
//	if (y2 > y1) 
//		g.drawOval(x2, y1, x1 - x2, y2 - y1);
//	else
//		g.drawOval(x2, y2, x1 - x2, y1 - y2);
//}

运行结果

java学习笔记->画图板_第2张图片

中间遇到的问题

Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 3

这个问题出现的可能有很多,原因都是计算机中出现了未定义的变量之类的,我给大家示范一下

大家看,为什么点击三下之后报错呢,因为去掉i=1后,i会无限增加,但我一这个监听器方法里必须i=1,2,3,三角形方法失效了,就会出错,如果有同学遇到了一样的报错提示,去找找自己的变量问题吧~good luck了您嘞(^ω^)

超出边界画图问题

我先给大家示范一下这个问题哈
java学习笔记->画图板_第3张图片
大家注意到了嘛,那么方法就是,把我注释掉的东西加回来咯,啦啦啦~,不过这个方法也有弊端,有同学可能也在上面运行结果图中看到了,就是,碰到按钮它也会触发离开方法,这个问题,等我们明天学个新框框,就可以解决啦

步骤三:代码优化及功能丰富

步骤二中的代码过于冗杂,大家可以看到 按钮界面 我傻傻的从前往后疯狂加。。。非常浪费时间和空间,我们先来看看升级后的代码,新的代码优化了布局,还增加了两个新物件。
首先新建一个package取名DrawPad,其下有两个class,DrawPad3和Listener3(为什么要加数字呢。。。因为本人之前做了个非常傻的事情。。。我有两个DrawPad包,然后我调试bug的时候疯狂修改第一个的Listener,妄图解决第二个DrawPad的问题。。。那能解决掉么。。。被身边的同学嘲笑了半天,真的是无地自容(T▽T)),好了,看代码吧,首先是DrawPad3

package com_0905_DrawPad;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class DrawPad3 {

	public static void main(String[] args) {
		DrawPad3 dp = new DrawPad3();
		dp.initUI();

	}

	public void initUI() {
		JFrame jf = new JFrame();
		BorderLayout bl = new BorderLayout();
		FlowLayout fl = new FlowLayout();
		Listener3 ls = new Listener3();
		// 创建窗体

		jf.setSize(1280, 720);
		jf.setLayout(bl);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 设置大小属性,使用边框布局,

		JPanel jp1 = new JPanel();
		jp1.setPreferredSize(new Dimension(1280, 50));
		jp1.setLayout(fl);
		jp1.setBackground(Color.gray);
		// 北方向jp1采用流式布局
		
		JPanel jp2 = new JPanel();
		jp2.setBackground(Color.white);
		JPanel jp3 = new JPanel();
		jp3.setBackground(Color.gray);
		JPanel jp4 = new JPanel();
		jp4.setBackground(Color.gray);
		JPanel jp5 = new JPanel();
		jp5.setBackground(Color.gray);
		// 设置东西南北中五个方向的属性颜色
		
		JPanel jp1_2 = new JPanel();
		jp1_2.setLayout(fl);
//		jp1_2.setPreferredSize(new Dimension(640,50));
		jp1_2.setBackground(Color.gray);
		//设置颜色区属性
		
		JPanel jp1_1 = new JPanel();
		jp1_1.setLayout(fl);
//		jp1_1.setPreferredSize(new Dimension(640,50));
		jp1_1.setBackground(Color.gray);
		//设置功能区属性

		jp1.add(jp1_1);
		jp1_1.setPreferredSize(new Dimension(600, 50));
		// 在北方向jp1的窗口中创建按钮西方向的窗口,采用流式布局
		jp1.add(jp1_2);
		jp1_2.setPreferredSize(new Dimension(600, 50));
		// 在北方向jp1的窗口中创建东方向的窗口,采用流式布局

		JButton jb1 = new JButton("功能区");
		jp1_1.add(jb1);
		String[] btnstr = { "直线", "矩形", "圆", "三角形", "哆啦A梦", "小章鱼" };
		for (int i = 0; i < btnstr.length; i++) {
			JButton btn = new JButton(btnstr[i]);
			jp1_1.add(btn);
			btn.addActionListener(ls);
			//调用动作监听器
		}
		// 在jp1的西方向窗口创建功能区按钮
		
		JButton jb2 = new JButton("颜色区");
		jp1_2.add(jb2);
		Color[] btnco = { Color.red, Color.blue, Color.yellow, Color.green, Color.black };
		for (int i = 0; i < btnco.length; i++) {
			JButton btn = new JButton("");
			btn.setBackground(btnco[i]);
			btn.setPreferredSize(new Dimension(50, 30));
			//设置单个按钮大小
			jp1_2.add(btn);
			btn.addActionListener(ls);
			//调用动作监听器
		}
		// 在jp1的东方向按钮中创建颜色区按钮

		jf.add(jp1, BorderLayout.NORTH);
		jf.add(jp2, BorderLayout.CENTER);
		jf.add(jp3, BorderLayout.SOUTH);
		jf.add(jp4, BorderLayout.EAST);
		jf.add(jp5, BorderLayout.WEST);
		// 将东西南北中四个组件添加到jf中

		jf.setVisible(true);
		// 可视化

		jp2.addMouseListener(ls);
        //在绘画面板中加入鼠标监听器
        
		Graphics g = jp2.getGraphics();
		ls.g = g;
		//调用graphics类的画图功能
	}

}

然后是Listener

package DrawPad;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;

public class Listener3 implements ActionListener, MouseListener {
	int x1, x2, y1, y2, x0, y0, w0, h0, y3, y4, y5, i = 0;
	int tx[] = { 0, 0, 0 };
	int ty[] = { 0, 0, 0 };
	Graphics g;

	String intest = "三角形";

	public void actionPerformed(ActionEvent e) {
		intest = e.getActionCommand();
		System.out.println(" 点击按钮 ");
		if ("".equals(intest)) {
			JButton btn = (JButton) e.getSource();
			Color c = btn.getBackground();
			g.setColor(c);
			System.out.println(c);
			//如果按钮是空文本,就取背景颜色
		}

	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("点击");
		tx[i] = e.getX();
		ty[i] = e.getY();
		i++;
		if (intest.equals("三角形")) {
			if (i == 1) {

				g.drawLine(tx[0], ty[0], tx[0], ty[0]);

			} else if (i == 2) {

				g.drawLine(tx[0], ty[0], tx[1], ty[1]);

			} else if (i == 3) {

				g.drawLine(tx[1], ty[1], tx[2], ty[2]);
				g.drawLine(tx[2], ty[2], tx[0], ty[0]);
				i = 0;

			}
		}
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("按下");
		x1 = e.getX();
		y1 = e.getY();

	}

	public void mouseExited(MouseEvent e) {
		System.out.println("离开");

	}

	public void mouseReleased(MouseEvent e) {
		System.out.println("释放");
		x2 = e.getX();
		y2 = e.getY();

		if (x1 > x2) {
			x0 = x2;
			w0 = x1 - x2;
		} else {
			x0 = x1;
			w0 = x2 - x1;
		}

		if (y1 > y2) {
			y0 = y2;
			h0 = y1 - y2;
		} else {
			y0 = y1;
			h0 = y2 - y1;
		}

		if (intest.equals("直线")) {

			g.drawLine(x1, y1, x2, y2);

		} else if (intest.equals("矩形")) {

			g.drawRect(x0, y0, w0, h0);

		} else if (intest.equals("圆")) {

			g.drawOval(x0, y0, w0, h0);

		} else if (intest.equals("哆啦A梦")) {
			g.setColor(Color.blue);
			g.fillOval(x0, y0, w0, h0);
			// 脸部大圆
			g.setColor(Color.white);
			g.fillOval((int) (x0 + w0 * 0.075), (int) (y0 + h0 * 0.075), (int) (w0 * 0.85), (int) (h0 * 0.85));
			// 脸部小圈
			g.setColor(Color.white);
			g.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			g.fillOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			g.setColor(Color.black);
			g.drawOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			g.drawOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			// 眼睛外轮廓
			g.setColor(Color.black);
			g.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05), (int) (h0 * 0.05));
			g.fillOval((int) (x0 + w0 / 2 - w0 * 0.05), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05),
					(int) (h0 * 0.05));
			// 黑眼球
			g.setColor(Color.black);
			g.drawLine((int) ((x0 + w0 / 2 - w0 * 0.025) + w0 * 0.05 / 2),
					(int) ((y0 * 1.2 + h0 * 0.375) + h0 * 0.05 / 2), (int) (x0 + w0 * 0.25 + w0 * 0.5 / 2),
					(int) (y0 + h0 / 3 + h0 / 2));
			g.drawArc((int) (x0 + w0 * 0.25), (int) (y0 + h0 / 3), (int) (w0 * 0.5), (int) (h0 / 2), 0, -180);
			// 嘴巴微笑
			g.setColor(Color.red);
			g.fillOval((int) (x0 + w0 / 2 - w0 * 0.025), (int) (y0 * 1.2 + h0 * 0.375), (int) (w0 * 0.05),
					(int) (h0 * 0.05));
			// 鼻子
			g.setColor(Color.black);
		} else if (intest.equals("小章鱼")) {

			g.drawOval(x0, y0, w0, h0);
			// 脸部大圆
			g.drawOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			g.drawOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
			g.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05), (int) (h0 * 0.05));
			g.fillOval((int) (x0 + w0 / 2 - w0 * 0.05), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05),
					(int) (h0 * 0.05));
			// 两个眼睛
			g.drawOval((int) (x0 + w0 * 0.5 - w0 * 0.25 / 2), (int) (y0 + h0 / 2 + h0 / 4 - h0 * 0.25),
					(int) (w0 * 0.25), (int) (h0 * 0.25));
			g.drawOval((int) (x0 + w0 * 0.5 - w0 * 0.15 / 2), (int) (y0 + h0 / 2 + h0 / 4 - h0 * 0.1875),
					(int) (w0 * 0.15), (int) (h0 * 0.15));
			// 嘴巴
			g.drawArc((int) (x0), (int) (y0 + h0 / 2), (int) (w0), (int) (h0 / 2), 0, -180);
			g.drawArc((int) (x0), (int) (y0 + 3 * h0 / 4), (int) (w0), (int) (h0 / 4), 0, -180);
			g.drawArc((int) (x0), (int) (y0 + 5 * h0 / 6), (int) (w0), (int) (h0 / 6), 0, -180);
			// 小腿
		}
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("进入");
	}

}

效果展示

java学习笔记->画图板_第4张图片
咳咳,这哆啦A梦和那个章鱼,丑是丑了点,这只是测试版本嘛,有能力的同学设计了更好看的图像可以在下面留个言,让我学习一下丫,和上面第二步代码的区别,在注释里我都标注了,我总结一下,就是按钮用循环来写,以及用数学方法设计组合图形。

总结

鼠标监听器可以记录鼠标的点击,按压,释放,进入和离开(界面),graphics类可以帮助我们画图,配合动作监听器获取坐标,结合一个窗体便是完整的画图板咯,更多进阶功能,可以自行开发呀兄弟萌。✧٩(ˊωˋ*)و✧

更新代码

新增了一个可以控制画笔大小的滑块儿,及一些分型功能,左上角新建了菜单(但没有赋予实际功能)。
DrawPad3.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JButton;

public class DrawPad3 {

	public static void main(String[] args) {
		DrawPad3 dp = new DrawPad3();
		dp.initUI();

	}

	public void initUI() {
		JFrame jf = new JFrame();
		BorderLayout bl = new BorderLayout();
		FlowLayout fl = new FlowLayout();
		Listener3 ls = new Listener3();
		//create a new frame

		
		jf.setSize(1920, 1280);
		// set the size;
		jf.setLayout(bl);
		// set the main frame as borderlayout
		jf.setLocationRelativeTo(null);
		//set the location to center
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//set the close button

		JMenuBar menuBar = new JMenuBar();
		jf.setJMenuBar(menuBar);
		JMenu menu1 = new JMenu("file");
		menu1.addActionListener(ls);
		JMenu menu2 = new JMenu("edit");
		JMenu menu3 = new JMenu("window");
		menuBar.add(menu1);
		menuBar.add(menu2);
		menuBar.add(menu3);
		JMenuItem item1 = new JMenuItem("open");
		JMenuItem item2 = new JMenuItem("save");
		JMenuItem item3 = new JMenuItem("save as");
		JMenuItem item4 = new JMenuItem("exit");
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu1.addSeparator();
		menu1.add(item4);
        //create a simple menu
		
		JPanel jp1 = new JPanel();
		jp1.setPreferredSize(new Dimension(1920, 100));
		jp1.setLayout(fl);
		jp1.setBackground(Color.gray);
		// set jp1 in north as flowlayout

		JPanel jp2 = new JPanel();
		jp2.setBackground(Color.white);
		JPanel jp3 = new JPanel();
		jp3.setBackground(Color.gray);
		JPanel jp4 = new JPanel();
		jp4.setBackground(Color.gray);
		JPanel jp5 = new JPanel();
		jp5.setBackground(Color.gray);
		// set other four panels

		JPanel jp1_2 = new JPanel();
		jp1_2.setLayout(fl);
		jp1_2.setPreferredSize(new Dimension(400, 50));
		jp1_2.setBackground(Color.gray);
		// set the color section as flowlayout

		JPanel jp1_1 = new JPanel();
		jp1_1.setLayout(fl);
		jp1_1.setPreferredSize(new Dimension(1200, 50));
		jp1_1.setBackground(Color.gray);
		// set the funtion section as flowlayout
		
//		int con=1;
//		BasicStroke size = new BasicStroke (con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
		//set the size of pen
		JSlider jp1_3 = new JSlider(0,20,1);
		jp1_3.setPreferredSize(new Dimension(200, 50));
		jp1_3.addChangeListener(ls);
		//add changelistener

        // 设置主刻度间隔
		jp1_3.setMajorTickSpacing(5);

        // 设置次刻度间隔
		jp1_3.setMinorTickSpacing(1);

        // 绘制 刻度 和 标签
		jp1_3.setPaintTicks(true);
		jp1_3.setPaintLabels(true);
		jp1_3.addChangeListener(ls);

		//set a new bar
		
		jp1.add(jp1_1);
		jp1.add(jp1_2);
		jp1.add(jp1_3);

		// add the two scetions blow to jp1

		JButton jb1 = new JButton("functional zone");
		jp1_1.add(jb1);
		String[] btnstr = { "line", "rectangle", "circle", "triangle", "Doraemon", "little octopus","wired triangle","chaos" ,"leaves","maple","ITStree","move ball"};
		for (int i = 0; i < btnstr.length; i++) {
			JButton btn = new JButton(btnstr[i]);
			jp1_1.add(btn);
			btn.addActionListener(ls);
		}
		// create functional zone

		JButton jb2 = new JButton("color");
		JButton jb3 = new JButton("selfchoose");
		jp1_2.add(jb2);
		jp1_2.add(jb3);
		jb3.addActionListener(ls);
		Color[] btnco = { Color.red, Color.blue, Color.yellow, Color.green, Color.black };
		for (int i = 0; i < btnco.length; i++) {
			JButton btn = new JButton("");
			btn.setBackground(btnco[i]);
			btn.setPreferredSize(new Dimension(50, 30));
			jp1_2.add(btn);
			btn.addActionListener(ls);
		}
		// create color zone

		jf.add(jp1, BorderLayout.NORTH);
		jf.add(jp2, BorderLayout.CENTER);
		jf.add(jp3, BorderLayout.SOUTH);
		jf.add(jp4, BorderLayout.EAST);
		jf.add(jp5, BorderLayout.WEST);
		// add jp1 to jp5 to main frame

		jf.setVisible(true);
		// visible

		jp2.addMouseListener(ls);

		
		Graphics2D g2 = (Graphics2D) jp2.getGraphics();
		ls.g2 = g2;
	}

}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.Math;
import java.util.EventListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class Listener3 implements ActionListener, MouseListener, ChangeListener {
	int x1, x2, y1, y2, x0, y0, w0, h0, y3, y4, y5, i = 0;
	int tx[] = { 0, 0, 0 };
	int ty[] = { 0, 0, 0 };
	int x[] = { 0, 0, 0, 0, 0 };
	int y[] = { 0, 0, 0, 0, 0 };
	Graphics2D g2;

	String intest = "";
	int con = 1;
//	BasicStroke size = new BasicStroke(con, BasicStroke.CAP_ROUND , BasicStroke.JOIN_BEVEL);
	// the original size of the pen
//	public interface ChangeListener extends EventListener {
//
//		void stateChanged(ChangeEvent e);
//		
//
//	}

	public void stateChanged(ChangeEvent e) {
		JSlider js = (JSlider) e.getSource();
		con = js.getValue();
//		con=(int) e.getSource();
		System.out.println("当前值: " + con);
		g2.setStroke(new BasicStroke(con, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));

	}

	public void actionPerformed(ActionEvent e) {
		intest = e.getActionCommand();
		System.out.println(" click button ");
		if ("".equals(intest)) {
			JButton btn = (JButton) e.getSource();
			Color c = btn.getBackground();
			g2.setColor(c);
			System.out.println(c);
		} else if ("file".equals(intest)) {

		} else if ("selfchoose".contentEquals(intest)) {
			Color color = new Color(200, 200, 200);
			Color c = JColorChooser.showDialog(null, "please choose your color", color);
			g2.setColor(c);
		}

	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("click");

		if (intest.equals("triangle")) {
			tx[i] = e.getX();
			ty[i] = e.getY();
			i++;
			if (i == 1) {

				g2.drawLine(tx[0], ty[0], tx[0], ty[0]);

			} else if (i == 2) {

				g2.drawLine(tx[0], ty[0], tx[1], ty[1]);

			} else if (i == 3) {

				g2.drawLine(tx[1], ty[1], tx[2], ty[2]);
				g2.drawLine(tx[2], ty[2], tx[0], ty[0]);
				i = 0;

			}
		}
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("press");
		x1 = e.getX();
		y1 = e.getY();

	}

	public void mouseExited(MouseEvent e) {
		System.out.println("leave");

	}

	public void mouseReleased(MouseEvent e) {
		System.out.println("release");
		x2 = e.getX();
		y2 = e.getY();

		if (x1 > x2) {
			x0 = x2;
			w0 = x1 - x2;
		} else {
			x0 = x1;
			w0 = x2 - x1;
		}

		if (y1 > y2) {
			y0 = y2;
			h0 = y1 - y2;
		} else {
			y0 = y1;
			h0 = y2 - y1;
		}

		if (intest.equals("line")) {

			g2.drawLine(x1, y1, x2, y2);

		} else if (intest.equals("rectangle")) {

			g2.drawRect(x0, y0, w0, h0);

		} else if (intest.equals("circle")) {

			g2.drawOval(x0, y0, w0, h0);

		} else if (intest.equals("Doraemon")) {
			drawDoraemon(x0,y0,w0,h0);
		} else if (intest.equals("little octopus")) {

			drawOctopus(x0, y0, w0, h0);

		} else if (intest.equals("wired triangle")) {
			System.out.println("click");
			for (int i = 0; i < x.length; i++) {
				x[i] = (int) (0 + Math.random() * (1200 - 0 + 1));
				y[i] = (int) (0 + Math.random() * (700 - 0 + 1));
				System.out.println(x[i] + "    " + y[i] + "   ");
//				g.fillOval(x[i], y[i], 5, 5);
				g2.drawLine(x[i], y[i], x[i], y[i]);

			}
			for (int i = 0; i < 10000; i++) {

				int c = (int) (0 + Math.random() * (2 - 0 + 1));
				x[x.length - 1] = (int) (x[c] / 2 + x[x.length - 1] / 2);
				y[y.length - 1] = (int) (y[c] / 2 + y[y.length - 1] / 2);
				System.out.println(c);
				g2.drawLine(x[x.length - 1], y[y.length - 1], x[x.length - 1], y[y.length - 1]);
			}
		} else if (intest.equals("chaos")) {
			drawDream(g2);
		} else if (intest.equals("leaves")) {
			drawLeaves(g2, x1, y1);
		} else if (intest.equals("maple")) {
			drawMaple(g2, x1, y1);
		} else if (intest.equals("ITStree")) {
			drawITStree(g2, x1, y1);
		} else if (intest.equals("move ball")) {


			BallGame();
		}
	}

	public void mouseEntered(MouseEvent e) {
		System.out.println("in");
	}
	
	
	private void drawDoraemon(int x0,int y0,int w0,int h0) {
		g2.setColor(Color.blue);
		g2.fillOval(x0, y0, w0, h0);
		// big circle of face
		g2.setColor(Color.white);
		g2.fillOval((int) (x0 + w0 * 0.075), (int) (y0 + h0 * 0.075), (int) (w0 * 0.85), (int) (h0 * 0.85));
		// small circle of face
		g2.setColor(Color.white);
		g2.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		g2.fillOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		g2.setColor(Color.black);
		g2.drawOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		g2.drawOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		// outer eye contour
		g2.setColor(Color.black);
		g2.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05), (int) (h0 * 0.05));
		g2.fillOval((int) (x0 + w0 / 2 - w0 * 0.05), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05),
				(int) (h0 * 0.05));
		// black eyes
		g2.setColor(Color.black);
		g2.drawLine((int) ((x0 + w0 / 2 - w0 * 0.025) + w0 * 0.05 / 2),
				(int) ((y0 * 1.2 + h0 * 0.375) + h0 * 0.05 / 2), (int) (x0 + w0 * 0.25 + w0 * 0.5 / 2),
				(int) (y0 + h0 / 3 + h0 / 2));
		g2.drawArc((int) (x0 + w0 * 0.25), (int) (y0 + h0 / 3), (int) (w0 * 0.5), (int) (h0 / 2), 0, -180);
		// smile
		g2.setColor(Color.red);
		g2.fillOval((int) (x0 + w0 / 2 - w0 * 0.025), (int) (y0 * 1.2 + h0 * 0.375), (int) (w0 * 0.05),
				(int) (h0 * 0.05));
		// nose
		g2.setColor(Color.black);
	}
	
	
	
	private void drawDream(Graphics g) {
		double x = 0f;
		double y = 0f;
		// a,b,c,d
		double a = -1.8, b = -2.0, c = -0.5, d = -0.9;
		for (int i = 0; i < 25500; i++) {
			// formula
			double temx = Math.sin(a * y) + c * Math.cos(a * x);
			double temy = Math.sin(b * x) + d * Math.cos(b * y);
			// change the shape of x1,x2,enlarge them and move to screen coordinates
			int x1 = (int) (temx * 130 + 500);
			int y1 = (int) (temy * 130 + 400);
			System.out.println("x1: " + x1 + " y1: " + y1);
			// colors are deepened according to iteration coefficients
			g.setColor(new Color(0, 0, i / 100));
			g.drawLine(x1, y1, x1, y1);
			x = temx;
			y = temy;
		}
	}

	private void drawOctopus(int x0,int y0,int w0,int h0){
		g2.drawOval(x0, y0, w0, h0);
		// face
		g2.drawOval((int) (x0 + w0 / 2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		g2.drawOval((int) (x0 + w0 / 2 - w0 * 0.2), (int) (y0 * 1.2), (int) (w0 * 0.2), (int) (h0 * 0.2));
		g2.fillOval((int) (x0 + w0 / 2), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05), (int) (h0 * 0.05));
		g2.fillOval((int) (x0 + w0 / 2 - w0 * 0.05), (int) (y0 * 1.2 + h0 * 0.075), (int) (w0 * 0.05),
				(int) (h0 * 0.05));
		// two eyes
		g2.drawOval((int) (x0 + w0 * 0.5 - w0 * 0.25 / 2), (int) (y0 + h0 / 2 + h0 / 4 - h0 * 0.25),
				(int) (w0 * 0.25), (int) (h0 * 0.25));
		g2.drawOval((int) (x0 + w0 * 0.5 - w0 * 0.15 / 2), (int) (y0 + h0 / 2 + h0 / 4 - h0 * 0.1875),
				(int) (w0 * 0.15), (int) (h0 * 0.15));
		// mouth
		g2.drawArc((int) (x0), (int) (y0 + h0 / 2), (int) (w0), (int) (h0 / 2), 0, -180);
		g2.drawArc((int) (x0), (int) (y0 + 3 * h0 / 4), (int) (w0), (int) (h0 / 4), 0, -180);
		g2.drawArc((int) (x0), (int) (y0 + 5 * h0 / 6), (int) (w0), (int) (h0 / 6), 0, -180);
		// legs
	}
	
	private void drawLeaves(Graphics g, int x1, int y1) {
		double x = 0f;
		double y = 0f;
		// a,b,c,d
		double a[] = { 0.0000, 0.7248, 0.1583, 0.3386 };
		double b[] = { 0.2439, 0.0337, -0.1297, 0.3694 };
		double c[] = { 0.0000, -0.0253, 0.3550, 0.2227 };
		double d[] = { 0.3053, 0.7426, 0.3676, -0.0756 };
		double e[] = { 0.0000, 0.2060, 0.1383, 0.0679 };
		double f[] = { 0.0000, 0.2538, 0.1750, 0.0826 };
		for (int i = 0; i < 25500; i++) {

			int t = (int) (0 + Math.random() * (3 - 0 + 1));

			double temx = a[t] * x + b[t] * y + e[t];
			double temy = c[t] * x + d[t] * y + f[t];

			int x6 = (int) (temx * 400 + x1);
			int y6 = (int) (temy * 400 + y1);
			System.out.println("x6: " + x6 + " y6: " + y6);
			//
			g.setColor(new Color(50, (i / 100), 50));
			g.drawLine(x6, y6, x6, y6);
//			g.fillOval(x6, y6, 30, 30);
			x = temx;
			y = temy;
		}
	}

	private void drawMaple(Graphics g, int x1, int y1) {
		double x = 0f;
		double y = 0f;
		// a,b,c,d
		double a[] = { 0.1400, 0.4300, 0.4500, 0.4900 };
		double b[] = { 0.0100, 0.5200, -0.4900, 0.0000 };
		double c[] = { 0.0000, -0.4500, 0.4700, 0.0000 };
		double d[] = { 0.5100, 0.5000, 0.4700, 0.5100 };
		double e[] = { -0.0800, 1.4900, -1.6200, 0.0200 };
		double f[] = { -1.3100, -0.7500, -0.7400, 1.6200 };
		for (int i = 0; i < 25500; i++) {

			int t = (int) (0 + Math.random() * (3 - 0 + 1));
			double temx = a[t] * x + b[t] * y + e[t];
			double temy = c[t] * x + d[t] * y + f[t];

			int x6 = (int) (temx * 80 + x1);
			int y6 = (int) (temy * 80 + y1);
			System.out.println("x6: " + x6 + " y6: " + y6);

			g.setColor(new Color(150, 75, 0));
			g.drawLine(x6, y6, x6, y6);
			x = temx;
			y = temy;
		}
	}

	private void drawITStree(Graphics g, int x1, int y1) {
		double x = 0f;
		double y = 0f;

		double a[] = { 0.0500, -0.0500, 0.0300, -0.0300, 0.5600, 0.1900, -0.3300 };
		double b[] = { 0.0000, 0.0000, -0.1400, 0.1400, 0.4400, 0.0700, -0.3400 };
		double c[] = { 0.0000, 0.0000, 0.0000, 0.0000, -0.3700, -0.1000, -0.3300 };
		double d[] = { 0.4000, -0.4000, 0.2600, -0.2600, 0.5100, 0.1500, 0.3400 };
		double e[] = { -0.0600, -0.0600, -0.1600, -0.1600, 0.3000, -0.2000, -0.5400 };
		double f[] = { -0.4700, -0.4700, -0.0100, -0.0100, 0.1500, 0.2800, 0.3900 };
		for (int i = 0; i < 25500; i++) {

			int t = (int) (0 + Math.random() * (6 - 0 + 1));
			double temx = a[t] * x + b[t] * y + e[t];
			double temy = c[t] * x + d[t] * y + f[t];

			int x6 = (int) (temx * 300 + x1);
			int y6 = (int) (temy * 300 + y1);
			System.out.println("x6: " + x6 + " y6: " + y6);

			g.setColor(new Color(50, (i / 100), 50));
			g.drawLine(x6, y6, x6, y6);
			x = temx;
			y = temy;

		}
	}

	private void BallGame () {
		int changex, changey;
		while(true){
			
			g2.setColor(Color.WHITE);
			g2.fillOval(x2, y2, 100, 100);
			
			x2++;
			y2++;
			

			int r,b,j;
		    r=  (int) (0 + Math.random() * (255 - 0 + 1));
		    b=  (int) (0 + Math.random() * (255 - 0 + 1));
		    j= (int) (0 + Math.random() * (255 - 0 + 1));
		
		    g2.setColor(Color.getHSBColor(r,b,j));
			g2.fillOval(x2, y2, 100, 100);
			
		// sleep 100ms
			try {
		Thread.sleep(100);
			}catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
			
		}
	}

}

效果

java学习笔记->画图板_第5张图片

你可能感兴趣的:(Java学习笔记,java,swing,graphics)