Java学习笔记(五):在JFrame中用Graphics类实现画板

一、画板功能

  • 实现一个可以选择画直线,矩形、圆、三角形、多边形等可改变颜色的图形的画板
  • 事件监听器和重绘原理

二、初步实现

简单粗暴的实现画笔功能:
监听器+画板界面
class SampleDraw:

  • 实现的是画笔界面显示,包含了各个按钮(button),画笔(Graphics)等。
  • 要注意的是画笔要放在界面可见的后面
package com.Graphics;

public class SampleDraw {
	public static void main(String [] args){
		SampleDraw draw=new SampleDraw();
		draw.showUI();
	}
	public void showUI(){
		javax.swing.JFrame jf=new javax.swing.JFrame();
		jf.setTitle("简单画板");
		jf.setSize(600, 500);
		jf.setResizable(false);//界面不可改变大小
		jf.setDefaultCloseOperation(3);//关闭时退出程序
		jf.setLocationRelativeTo(null);//居中显示
		//流式布局
		java.awt.FlowLayout flow=new java.awt.FlowLayout();
		jf.setLayout(flow);
		
		
		javax.swing.JButton line=new javax.swing.JButton("直线");
		javax.swing.JButton rect=new javax.swing.JButton("矩形");
		javax.swing.JButton arc=new javax.swing.JButton("圆");
		javax.swing.JButton text=new javax.swing.JButton("TEXT");
		javax.swing.JButton san=new javax.swing.JButton("三角形");
		javax.swing.JButton xpc=new javax.swing.JButton("橡皮擦");
		javax.swing.JButton dbx=new javax.swing.JButton("多边形");
		javax.swing.JButton fs=new javax.swing.JButton("放射直线");
		javax.swing.JButton color1=new javax.swing.JButton("green");
		javax.swing.JButton color2=new javax.swing.JButton("blue");
		javax.swing.JButton color3=new javax.swing.JButton("red");
		jf.add(line);//划线按钮
		jf.add(rect);//矩形按钮
		jf.add(arc);//圆形按钮
		jf.add(text);//文本按钮
		jf.add(san);//三角形按钮
		jf.add(dbx);//多边形按钮
		jf.add(xpc);//橡皮擦按钮
		jf.add(fs);
		jf.add(color1);
		jf.add(color2);
		jf.add(color3);
		color1.setBackground(java.awt.Color.green);
		color2.setBackground(java.awt.Color.blue);
		color3.setBackground(java.awt.Color.red);
		java.awt.Container pane=jf.getContentPane();
		pane.setBackground(java.awt.Color.white);	
		jf.setVisible(true);//可见 可见要加在画笔前面

	
		//从窗体上获取画笔
		//获取窗体在屏幕上占据的区域,这块区域是可以改变颜色的
		java.awt.Graphics g=jf.getGraphics();//画笔,一定要放在可见之后
		DrawListener dlis=new DrawListener(g);
		
		
		
		//jf.setBackground(java.awt.Color.white);
		line.addActionListener(dlis);
		rect.addActionListener(dlis);
		arc.addActionListener(dlis);
		text.addActionListener(dlis);
		san.addActionListener(dlis);
		xpc.addActionListener(dlis);
		dbx.addActionListener(dlis);
		fs.addActionListener(dlis);
		color1.addActionListener(dlis);
		color2.addActionListener(dlis);
		color3.addActionListener(dlis);
		jf.addMouseListener(dlis);//鼠标监听
		jf.addMouseMotionListener(dlis);
		
	}
	
}

监听事件:
class DrawListener:
根据按钮的e.getActionCommand()判断选择的是画什么东西,三角形是先画一条边再话一个点,点与边的两点相连

  • 存在的问题是不能拖动显示
  • 画出的界面只要重绘之后就清除了
package com.Graphics;

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 java.awt.event.MouseMotionListener;
import java.lang.Math;;

public class DrawListener implements MouseListener,MouseMotionListener ,ActionListener {
	private int x1, y1, x2, y2; 
	private Graphics g;  	// 记录画板上的画布对象
	private String flag,color;
	private int san = 0, x10, x20, y10, y20;	// 三角形
	private int df = 0, dx0, dy0, dx, dy, count = 0;
	private int line_flag=0,lx=0,ly=0;
	public DrawListener(Graphics g) {
		this.g = g;
	}
	public void actionPerformed(ActionEvent e){ // 按钮监听
		//flag = e.getActionCommand();
		System.out.println(e.getActionCommand());
		
		if(e.getActionCommand().equals("三角形")){
			flag="三角形";
		}else if(e.getActionCommand().equals("多边形")){
			flag="多边形";
		}else if(e.getActionCommand().equals("直线")){
			flag="直线";
		}else if(e.getActionCommand().equals("矩形")){
			flag="矩形";
		}else if(e.getActionCommand().equals("圆")){
			flag="圆";
		}else if(e.getActionCommand().equals("TEXT")){
			flag="TEXT";
		}else if(e.getActionCommand().equals("橡皮擦")){
			flag="橡皮擦";
		}else if(e.getActionCommand().equals("放射直线")){
			flag="放射直线";
		}		
		if(e.getActionCommand().equals("green")){
			color="green";
			
		}else if(e.getActionCommand().equals("blue")){
			color="blue";
			
		}else if(e.getActionCommand().equals("red")){
			color="red";
			
		}
	if(color.equals("green")){
			g.setColor(java.awt.Color.green);
		}else if(color.equals("blue")){		
			g.setColor(java.awt.Color.blue);
		}else if(color.equals("red")){			
			g.setColor(java.awt.Color.red);
		}else
			g.setColor(java.awt.Color.white);
	}

	public void mouseDragged(MouseEvent e){
		 int lf=0;
		 System.out.println("Dragged");
		 if(flag.equals("橡皮擦")){
			 g.setColor(java.awt.Color.white);
			 g.fillRect(e.getX(),e.getY(), e.getX()+1, e.getY()+1);
		 }
		 else if(flag.equals("直线")&&line_flag==1&&lf==0){
			 g.drawLine(x1, y1, e.getX(), e.getY());
			 lf=1;
		 }else if(flag.equals("放射直线")){
			 g.drawLine(x1, y1, e.getX(), e.getY());
		 }
		 if(lf==1){
			 lf=0;
			 java.awt.Color c=g.getColor();
			 g.setColor(java.awt.Color.white);
			 g.drawLine(x1, y1,lx,ly);
			 lx=e.getX();
			 ly=e.getY();
			 g.setColor(c);
		 }
	 }
    
	 public void mouseMoved(MouseEvent e){		
		 System.out.println("mouseMoved"); 
	 }

	// 鼠标按下时点的坐标
	public void mousePressed(MouseEvent e) {
		// 记录第一次点击的x,y:通过事件e得到
		x1 = e.getX();
		y1 = e.getY();
		System.out.println("按下");
		if (flag.equals("三角形") && san == 0) {
			x10 = e.getX();
			y10 = e.getY();
		} else if (flag.equals("TEXT")) {
			g.drawString("TEXT", x1, y1);
		} else if (flag.equals("多边形") && df == 0) {
			dx0 = e.getX();
			dy0 = e.getY();
			df = 1;
		}else if(flag.equals("直线")){
			line_flag=1;
		}


	}
	// 鼠标释放开时点的坐标

	public void mouseReleased(MouseEvent e) {
		// 记录松开的坐标
		System.out.println("松开");
		x2 = e.getX();
		y2 = e.getY();
		// 得到两次(按下松开)坐标,调用画布对象的方法划线
	
		if (flag.equals("三角形") && san == 0) {
			x20 = e.getX();
			y20 = e.getY();
			g.drawLine(x10, y10, x20, y20);
			san = 1;
		} else if (flag.equals("直线")) {
			g.drawLine(x1, y1, x2, y2);
			line_flag=0;
		} else if (flag.equals("矩形")) {
			if (x2 > x1) {
				g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
			} else {
				g.drawRect(x2, y2, Math.abs(x2 - x1), Math.abs(y2 - y1));
			}
		} else if (flag.equals("圆")) {
			if (x2 > x1) {
				g.drawArc(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1), 0, 360);
			} else {
				g.drawArc(x2, y2, Math.abs(x2 - x1), Math.abs(y2 - y1), 0, 360);
			}
		} else if (flag.equals("多边形") && df == 1) {
			df = 2;
			dx = x2;
			dy = y2;
			g.drawLine(x1, y1, x2, y2);
			System.out.println("("+x1+","+y1+")"+"("+x2+","+y2+")");
		}
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("点击");
		if (flag.equals("TEXT")) {
			g.drawString("TEXT", x1, y1);
		} else if (flag.equals("三角形") && san == 1) {
			g.drawLine(x1, y1, x10, y10);
			g.drawLine(x20, y20, x1, y1);
			san = 0;
		} else if (flag.equals("多边形") && df > 0) {
			if (e.getX() == dx && e.getY() == dy && df == 2) {// 如果双击
				count++;
				if (count == 2) {
					g.drawLine(dx0, dy0, x1, y1);
					df = 0;
					System.out.println("("+dx+","+dy+")"+"("+x1+","+y1+")");
					count = 0;
				}
			} else {// 单击更新坐标
				if (df == 2) {
					g.drawLine(x1, y1, dx, dy);
					System.out.println("("+x1+","+y1+")"+"("+dx+","+dy+")");
					dx=e.getX();
					dy=e.getY();
				}
			}
		}
	}

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

	public void mouseExited(MouseEvent e) {
		System.out.println("退出");
	}
}


三、优化提升

优化代码、添加按钮图片、重绘
在自己的win10画图板上截取相关图片,用swing组件添加到窗体的north上。

  • 所用到的组件是ImageIcon图标
String[] shape={"直线","曲线","矩形","圆","三角形","多边形","放射线","橡皮擦"};
		for(int i=0;i<shape.length;i++){
			ImageIcon image=new ImageIcon(this.getClass().getResource(shape[i]+".png"));
			JButton jbu =new JButton(image);//
			jbu.setActionCommand(shape[i]);//添加字符串
			jbu.setPreferredSize(new Dimension(25, 25));
			northPlane.add(jbu);
			jbu.addActionListener(mouse);
		}
  • 重绘原理:把监听器监听到的所有鼠标动作(画出来的线),记录下来,当窗体重绘的时候再次显示。定义一个repaint类,包含画图方式name,颜色,按下放开两点(三角形三个点),我们用一个很大的数组来保存每一次画图记录
  • 每个组件都有对应的paint方法,可以直接继承JFrame窗体类,重写paint方法
public void paint(Graphics g){
		//g=jf.();
		super.paint(g);
		//取出数组中的图形对象
		for(int i=0;i<repaint.length;i++){
			Repaint rp=repaint[i];
			if(rp!=null){
				rp.Drawpaint(g);
			}else break;
		}
	}
package com.Graphics02;

import java.awt.Color;
import java.awt.Graphics;

//图形类
public class Repaint {
	private int x1,y1,x2,y2,x3,y3;
	private String name;
	private Color c;
	public Repaint(String name,Color c,int x1,int y1,int x2,int y2){
		this.name=name;
		this.x1=x1;
		this.x2=x2;
		this.y1=y1;
		this.y2=y2;
		this.c=c;
	}
	public void setsan(int x,int y){
		this.x3=x;
		this.y3=y;
	}
	public void Drawpaint(Graphics g){
		g.setColor(c);
		switch (name){
		case "直线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "多边形":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "矩形":
			g.drawRect(Math.min(x2, x1), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
			break;
		case "圆":
			g.drawArc(Math.min(x1, x2), Math.min(y2, y1), Math.abs(x1 - x2), Math.abs(y1 - y2), 0, 360);
			break;
		case "三角形":
			g.drawLine(x1, y1, x2, y2);
			g.drawLine(x1, y1, x3, y3);
			g.drawLine(x3, y3, x2, y2);
			break;
		case "橡皮擦":
			g.clearRect(x1, y1, x2, y2);;
			break;
		case "曲线":
			g.drawLine(x1, y1, x2, y2);
			break;
		}
	}
}

有了重绘功能就可以实现拖动了,在我们电脑里面的画图板都是在画图的时候可以拖动看见线条的,可以用重绘来实现,在监听器中再继承一个MouseMotionListener可监听鼠标移动,拖动

  • 在鼠标拖动方法里面添加不断划线,要用白线覆盖原来画出来的线,再画一个新的线
  • 拖动时的线条覆盖问题:每次在要划线前面添加重绘
public void mouseDragged(MouseEvent e) {// 拖动
		//g.setColor(color);
		if(shape != null){
		for(int i=0;i<shape.length;i++){
			Repaint rp=shape[i];
			if(rp!=null){
				rp.Drawpaint(g);
			}else break;
		}
		}
		//Color c = g.getColor();
		if (name.equals("直线") || name.equals("三角形")) {
			g.setColor(Color.white);
			g.drawLine(x1, y1, x3, y3);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawLine(x1, y1, x3, y3);
		} else if (name.equals("矩形")) {
			g.setColor(Color.white);
			g.drawRect(Math.min(x1, x3), Math.min(y1, y3), Math.abs(x1 - x3), Math.abs(y1 - y3));
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawRect(Math.min(x1, x3), Math.min(y1, y3), Math.abs(x1 - x3), Math.abs(y1 - y3));
		}else if (name.equals("圆")){
			g.setColor(Color.white);
			g.drawArc(Math.min(x1, x3), Math.min(y3, y1), Math.abs(x1 - x3), Math.abs(y1 - y3), 0, 360);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawArc(Math.min(x1, x3), Math.min(y3, y1), Math.abs(x1 - x3), Math.abs(y1 - y3), 0, 360);
			
		}
		else if (name.equals("橡皮擦")) {
			// Color c=g.getColor();
			g.setColor(Color.white);
		//	g.clearRect(e.getX(), e.getY(), e.getX() + 1, e.getY() + 1);
			Repaint rp=new Repaint(name,g.getColor(),e.getX(),e.getY(),1,1);
			shape[index++]=rp;
			g.setColor(color);
		} else if (name.equals("曲线")) {
			g.setColor(color);
			g.drawLine(e.getX(), e.getY(), x1, y1);
			Repaint rp=new Repaint(name,g.getColor(),e.getX(),e.getY(),x1,y1);
			shape[index++]=rp;
			// g.drawLine(x1, y1, x3, y3);
			x1 = e.getX();
			y1 = e.getY();

		}

	}

  • 在电脑的画图板里面,多边形没有拖动显示,只要点击了一个点就有连接到,可以在鼠标移动的方法里面添加多边形的绘制
public void mouseMoved(MouseEvent e) {// 移动
		
		if(shape != null){
		for(int i=0;i<shape.length;i++){
			Repaint rp=shape[i];
			if(rp!=null){
				rp.Drawpaint(g);
			}else break;
		}
	}
		Color c = g.getColor();
		if (name.equals("多边形") && duo_flag == 1) {
			g.setColor(Color.white);
			g.drawLine(dx, dy, x3, y3);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(c);
			g.drawLine(dx, dy, x3, y3);
		}
	
	}

效果图
在这里插入图片描述

四、最终版本

在上一次的基础加入可选择线条粗细的功能,不过貌似效果不是很好,再次选择粗细的时候画曲线会卡顿。
可选择画笔粗细的是Graphics2D类,在上一个份代码中修改Graphic类就可以了,添加可选择粗细的按钮,方法同上,要注意的是重绘方法依旧是获取画笔Graphic这个类的画笔,这里需要强制转型:

public void paint(Graphics g){
		Graphics2D g0=(Graphics2D)g;
		super.paint(g);
		//取出数组中的图形对象
		System.out.println("重绘函数");
		for(int i=0;i<repaint.length;i++){
			Repaint rp=repaint[i];
			if(rp!=null){
				rp.Drawpaint(g0);
			}else break;
		}
		
	}
  • 为了让我们画出来的线不能越界(画到按钮上),使用边框布局,所以我们在窗体类中应该要继承JPanel获取center的画笔,重绘center
  • show
package com.Graphics03;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Draw extends JPanel{
	private Repaint[] repaint=new Repaint[100000];//重绘对象数组
//	Graphics g;
//	private Graphics2D g=(Graphics2D) this.getGraphics();
	
	public static void main(String [] args){
		Draw draw=new Draw();
		draw.showUI();
	}
	
	public void showUI(){
		//创建窗体对象
		JFrame jf=new JFrame(); //改成this 子类
		jf.setSize(800, 600);
		jf.setTitle("画板界面");
		//设置退出进程方法
		jf.setDefaultCloseOperation(3);
		//设置居中
		jf.setLocationRelativeTo(null);
		//设置背景色
		jf.getContentPane().setBackground(Color.white);
		jf.setLayout(new BorderLayout());//设置边框布局
		//三
		JPanel northPlane = new JPanel();  //容器
		northPlane.setBackground(Color.white); //
		northPlane.setPreferredSize(new Dimension(0, 40));
		jf.add(northPlane,BorderLayout.NORTH);
		
	//	JPanel centerPlane = new JPanel();
		this.setBackground(Color.WHITE);
		jf.add(this,BorderLayout.CENTER);
		//san
		
		DrawMouse mouse=new DrawMouse(this);//新建鼠标监听器
		//事件监听器
				//1.事件源:当前动作所发生的组件
				//2.确定监听器方法:鼠标监听器方法
				//3.绑定事件处理类
		String[] shape={"直线","曲线","矩形","圆","三角形","多边形","放射线","橡皮擦","1","2","3"};
		for(int i=0;i<shape.length;i++){
			ImageIcon image=new ImageIcon(this.getClass().getResource(shape[i]+".png"));
			JButton jbu =new JButton(image);
			jbu.setActionCommand(shape[i]);//添加字符串
			jbu.setPreferredSize(new Dimension(25, 25));
			northPlane.add(jbu);
			jbu.addActionListener(mouse);
		}
		Color[] color={Color.BLUE,Color.red,Color.green};
		for(int i=0;i<color.length;i++){
			JButton jbu =new JButton();
			jbu.setBackground(color[i]);
			jbu.setPreferredSize(new Dimension(30,30));
			northPlane.add(jbu);
			jbu.addActionListener(mouse);
		}
		jf.setVisible(true);
		
	//	mouse.setgra(g);
		mouse.setRepaint(repaint);
		this.addMouseListener(mouse);//鼠标监听方法
		this.addMouseMotionListener(mouse);//鼠标拖动事件
	
		
		
	}
	/**
	 * 重写绘制组件的方法
	 */
	public void paint(Graphics g){
		
		Graphics2D g0=(Graphics2D)g;
		super.paint(g);
		//取出数组中的图形对象
		System.out.println("重绘函数");
		for(int i=0;i<repaint.length;i++){
			Repaint rp=repaint[i];
			if(rp!=null){
				rp.Drawpaint(g0);
			}else break;
		}
		
	}
}

监听器:

package com.Graphics03;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

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

public class DrawMouse implements MouseListener, ActionListener, MouseMotionListener {
	Color color = Color.BLACK;
	private int x1, y1, x2, y2, x3, y3, sx0, sy0, sx, sy, dx, dy, dx0, dy0;
	private int san_flag = 0, duo_flag = 0;
	private float wi = 1;
	private String name = "直线";
	private Repaint[] shape;
	private int index = 0;
	private Draw draw;
	private Graphics2D g;// 保存传递的画笔

	public DrawMouse(Draw draw) {
		this.draw = draw;

	}

	public void setRepaint(Repaint[] shape) {
		this.shape = shape;
	}

	public void actionPerformed(ActionEvent e) {// 按键监听器
		// g=draw.getGraphics();
		if (e.getActionCommand().equals("")) {
			JButton jbu = (JButton) e.getSource();// 获取当前事件源
			color = jbu.getBackground();
			g.setColor(color);
			// System.out.println(color);
		} else if (e.getActionCommand().equals("1")) {
			wi = 1.0f;
		} else if (e.getActionCommand().equals("2")) {
			wi = 2.0f;
		} else if (e.getActionCommand().equals("3")) {
			wi = 3.0f;
		} else {
			name = e.getActionCommand();
		}
	}

	public void mouseClicked(MouseEvent e) {// 点击

		// g=draw.getGraphics();
		if (name.equals("三角形") && san_flag == 1) {
			g.drawLine(e.getX(), e.getY(), sx0, sy0);
			g.drawLine(e.getX(), e.getY(), sx, sy);
			Repaint rp = new Repaint(name, g.getColor(), wi, sx0, sy0, sx, sy);
			rp.setsan(e.getX(), e.getY());
			shape[index++] = rp;
			san_flag = 0;
		} else if (name.equals("多边形") && duo_flag == 1) {
			g.drawLine(x1, y1, dx, dy);
			Repaint rp = new Repaint(name, g.getColor(), wi, x1, y1, dx, dy);
			shape[index++] = rp;
			if (dx == e.getX() && dy == e.getY()) {
				g.drawLine(dx0, dy0, x1, y1);
				Repaint rp0 = new Repaint(name, g.getColor(), wi, x1, y1, dx0, dy0);
				shape[index++] = rp0;
				duo_flag = 0;
			} else {
				dx = e.getX();
				dy = e.getY();
			}
		}
	}

	public void mousePressed(MouseEvent e) {// 按下
		// g = draw.getGraphics();
		g.setColor(color);
		x1 = e.getX();
		y1 = e.getY();
		System.out.println(x1 + " " + y1);

	}

	public void mouseReleased(MouseEvent e) {// 放开
		// g=draw.getGraphics();
		x2 = e.getX();
		y2 = e.getY();
		// g.setColor(color);
		System.out.println(g.getColor());
		System.out.println(x2 + " " + y2);

		if (name.equals("直线")) {
			g.drawLine(x1, y1, x2, y2);
			Repaint rp = new Repaint(name, g.getColor(), wi, x1, y1, x2, y2);
			shape[index++] = rp;
		} else if (name.equals("矩形")) {
			g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
			Repaint rp = new Repaint(name, g.getColor(), wi, x1, y1, x2, y2);
			shape[index++] = rp;
		} else if (name.equals("圆")) {
			g.drawArc(Math.min(x1, x2), Math.min(y2, y1), Math.abs(x1 - x2), Math.abs(y1 - y2), 0, 360);
			Repaint rp = new Repaint(name, g.getColor(), wi, x1, y1, x2, y2);
			shape[index++] = rp;
		} else if (name.equals("三角形") && san_flag == 0) {
			san_flag = 1;
			g.drawLine(x1, y1, x2, y2);
			sx0 = x1;
			sy0 = y1;
			sx = x2;
			sy = y2;

		} else if (name.equals("多边形") && duo_flag == 0) {
			duo_flag = 1;
			g.drawLine(x1, y1, x2, y2);
			Repaint rp = new Repaint(name, g.getColor(), wi, x1, y1, x2, y2);
			shape[index++] = rp;
			dx0 = x1;
			dy0 = y1;
			dx = x2;
			dy = y2;
		}
	}

	public void mouseEntered(MouseEvent e) {// 进入
		g = (Graphics2D) draw.getGraphics();
		g.setStroke(new BasicStroke(wi));

	}

	public void mouseExited(MouseEvent e) {// 退出
	}

	public void mouseDragged(MouseEvent e) {// 拖动
		// g.setColor(color);
		if (shape != null && (!name.equals("橡皮擦"))) {
			for (int i = 0; i < shape.length; i++) {
				Repaint rp = shape[i];
				if (rp != null) {
					rp.Drawpaint(g);
				} else
					break;
			}
		}
		// Color c = g.getColor();
		if (name.equals("直线") || name.equals("三角形")) {
			g.setColor(Color.white);
			g.drawLine(x1, y1, x3, y3);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawLine(x1, y1, x3, y3);
		} else if (name.equals("矩形")) {
			g.setColor(Color.white);
			g.drawRect(Math.min(x1, x3), Math.min(y1, y3), Math.abs(x1 - x3), Math.abs(y1 - y3));
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawRect(Math.min(x1, x3), Math.min(y1, y3), Math.abs(x1 - x3), Math.abs(y1 - y3));
		} else if (name.equals("圆")) {
			g.setColor(Color.white);
			g.drawArc(Math.min(x1, x3), Math.min(y3, y1), Math.abs(x1 - x3), Math.abs(y1 - y3), 0, 360);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(color);
			g.drawArc(Math.min(x1, x3), Math.min(y3, y1), Math.abs(x1 - x3), Math.abs(y1 - y3), 0, 360);

		} else if (name.equals("橡皮擦")) {
			// Color c=g.getColor();
			g.setColor(Color.white);
			g.drawLine(e.getX(), e.getY(), x1, y1);
			Repaint rp = new Repaint(name, g.getColor(), wi, e.getX(), e.getY(), x1, y1);
			shape[index++] = rp;
			// g.drawLine(x1, y1, x3, y3);
			x1 = e.getX();
			y1 = e.getY();
		} else if (name.equals("曲线")) {
			g.setColor(color);
			g.drawLine(e.getX(), e.getY(), x1, y1);
			Repaint rp = new Repaint(name, g.getColor(), wi, e.getX(), e.getY(), x1, y1);
			shape[index++] = rp;
			// g.drawLine(x1, y1, x3, y3);
			x1 = e.getX();
			y1 = e.getY();

		}

	}

	public void mouseMoved(MouseEvent e) {// 移动

		if (shape != null && name.equals("多边形")) {
			for (int i = 0; i < shape.length; i++) {
				Repaint rp = shape[i];
				if (rp != null) {
					rp.Drawpaint(g);
				} else
					break;
			}
		}
		Color c = g.getColor();
		if (name.equals("多边形") && duo_flag == 1) {
			g.setColor(Color.white);
			g.drawLine(dx, dy, x3, y3);
			x3 = e.getX();
			y3 = e.getY();
			g.setColor(c);
			g.drawLine(dx, dy, x3, y3);
		}

	}
}

最终效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值