画板(DrawBoard)

本文介绍了使用Java编程实现一个简单的画板应用。通过主函数启动,Test类和DrawBoard类作为主要组件,实现了基本的绘图功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主函数,Test类

package DrawBoard2;


public class Test {
		public static void main(String[] args){
			DrawBoard db = new DrawBoard();
			db.initFrame();
		} 
}


DrawBoard类

package DrawBoard2;

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

import javax.swing.JFrame;
import javax.swing.JPanel;


public class DrawBoard extends JFrame {
	
	Graphics2D g;

	public void initFrame(){
			
			this.setSize(800,600);//窗体大小
			this.setDefaultCloseOperation(3);//关闭后立即停止运行
			this.setLocationRelativeTo(null);//窗体居中
			this.setTitle("画板");//窗体标题
			
			//边框布局
			this.setLayout(new BorderLayout());
			
			PanelLeft panelLeft = new PanelLeft(g);//创建左边面板
			PanelCenter panelCenter = new PanelCenter();//创建中间面板
			PanelDown panelDown= new PanelDown(g,panelLeft);//创建下边面板
			this.add(panelLeft,BorderLayout.WEST);
			this.add(panelCenter,BorderLayout.CENTER);
			this.add(panelDown,BorderLayout.SOUTH);
			
			panelLeft.click();//调用左边面板的方法
			panelDown.clickColor();//调用下边面板的方法
			
			//画板可见
			this.setVisible(true);
			g =(Graphics2D) panelCenter.getGraphics();//中间面板取画笔,并将画笔强制转型成子类
			panelLeft.g = g;//画笔传递
			panelDown.g = g;
			DrawListener mouse = new DrawListener(g,panelLeft);//添加两个鼠标行为的监听器
			panelCenter.addMouseListener(mouse);
			panelCenter.addMouseMotionListener(mouse);	
		}		
}

左边面板PanelLeft类

package DrawBoard2;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class PanelLeft extends JPanel {
	
	String actionCommand = "6";//默认按钮选择铅笔
	int n;
	public Graphics2D g;
	Color color = Color.black;//默认画笔颜色为黑色
	
	public PanelLeft(Graphics2D g){
		this.g = g;
		setPreferredSize(new Dimension(70,0));
	}
	
	public void click(){
		//给按钮添加监听器
		ActionListener actionListener = new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				
				actionCommand = e.getActionCommand();				
				//橡皮
				if("2".equals(actionCommand)){
					g.setColor(Color.white);//设置颜色
					g.setStroke(new BasicStroke(20));//设置画笔粗细
				}
				//刷子
				else if("7".equals(actionCommand)){
					g.setColor(color);
					g.setStroke(new BasicStroke(20));	
				}
				//其他
				else{
					n=0;
					g.setColor(color);
					g.setStroke(new BasicStroke(1));//还原画笔粗细
				}
			 }	
		};
		
		//添加按钮
		for(int i = 0;i < 16;i++){
			JButton button = new JButton();
			ImageIcon image1 = new ImageIcon("images/draw"+i+".jpg");
			ImageIcon image2 = new ImageIcon("images/draw"+i+"-1.jpg");
			ImageIcon image3 = new ImageIcon("images/draw"+i+"-2.jpg");
			ImageIcon image4 = new ImageIcon("images/draw"+i+"-3.jpg");
			//给按钮添加图标
			button.setIcon(image1);
			button.setRolloverIcon(image2);
			button.setPressedIcon(image3);
			button.setSelectedIcon(image4);
			
			button.setPreferredSize(new Dimension(25,25));
			this.add(button);
			button.addActionListener(actionListener);//按钮添加动作监听器
			button.setActionCommand(i+"");
		}
	}
}


下边面板PanelDown类

package DrawBoard2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class PanelDown extends JPanel {
	Color[] colors = { Color.black, Color.gray, Color.red, Color.yellow,Color.blue };
	Color color = Color.black;
	Graphics2D g;
	PanelLeft panelLeft;
	
	public PanelDown(Graphics2D g,PanelLeft pl){
			this.g = g;
			this.panelLeft = pl;//传参,将画笔与左边面板传入
		}
	public void clickColor(){
			
			ActionListener listener = new ActionListener() {
				
				public void actionPerformed(ActionEvent e) {

					String actionCommandColor = e.getActionCommand();
					int i = Integer.valueOf(actionCommandColor);// String -> int							//将字符串型转换为整型
					color = colors[i];
					g.setColor(color);
					panelLeft.color = color;
					if("2".equals(panelLeft.actionCommand)){
						g.setColor(Color.white);
					}
				}
			};
			for (int i = 0; i < colors.length; i++){
				// 创建颜色按钮
				JButton buttoncolor = new JButton();
				buttoncolor.setBackground(colors[i]);
			    
				buttoncolor.setPreferredSize(new Dimension(25, 25));
				buttoncolor.addActionListener(listener);//颜色按钮添加动作监听器
				buttoncolor.setActionCommand(i + "");
				
				this.add(buttoncolor);
			}
		}
}

中间面板PanelCenter类

package DrawBoard2;

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

import javax.swing.JPanel;

public class PanelCenter extends JPanel {

	public PanelCenter(){
			setBackground(Color.white);//设置面板背景色
		}		
}


鼠标监听器DrawListener类
package DrawBoard2;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

public class DrawListener implements MouseMotionListener,MouseListener {

	int x1,y1,x2,y2,a,b,xS,yS;
	PanelLeft panelLeft;
	Graphics2D g;
	Random random = new Random();
	
	public DrawListener(Graphics2D g,PanelLeft pl){
		this.g = g;
		this.panelLeft = pl;
	}
	
	
	public void mouseClicked(MouseEvent e) {
			int clickCount = e.getClickCount();//获取点击次数
			if(clickCount==1)
			{
				
			}
			else if(clickCount==2)
			{
				if("13".equals(panelLeft.actionCommand)){
					g.drawLine(x2, y2, xS, yS);//双击使多边形自动完成
					panelLeft.n = 0;
				}
			}
	}

	
	public void mousePressed(MouseEvent e) {
		
		x1 = e.getX();
		y1 = e.getY();
		
		if("13".equals(panelLeft.actionCommand)){
			
			if(panelLeft.n != 0){
				x1 = a;
				y1 = b;
			}
			 if(panelLeft.n == 0){
				xS = e.getX();//记录按下坐标初始值
				yS = e.getY();
			}
		}
	}

	
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		
		if("10".equals(panelLeft.actionCommand)){
			//直线
			g.drawLine(x1, y1, x2, y2);
		
		}
		else if("12".equals(panelLeft.actionCommand)){
			//矩形
			g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
		
		}
		else if("14".equals(panelLeft.actionCommand)){
			// 椭圆
			g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
		
		}	
		else if("13".equals(panelLeft.actionCommand)){
			//多边形
			
			g.drawLine(x1, y1, x2, y2);
			a = x2;
			b = y2;
			panelLeft.n++;
		}
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	
	public void mouseExited(MouseEvent e) {
		
	}

	public void mouseDragged(MouseEvent e) {
		if("6".equals(panelLeft.actionCommand)){
			x2 = e.getX();
			y2 = e.getY();
			//绘制铅笔
			g.drawLine(x1, y1, x2, y2);
			x1 = x2;
			y1 = y2;
		}
		else if("2".equals(panelLeft.actionCommand)){
			x2 = e.getX();
			y2 = e.getY();
			//橡皮
			g.drawLine(x1, y1, x2, y2);
			x1 = x2;
			y1 = y2;
		}
		else if ("8".equals(panelLeft.actionCommand))
		{
			x2 = e.getX();
			y2 = e.getY();
			// 喷枪
			for (int i = 0; i < 30; i++) {
				// 生成的随机数
				int nextInt1 = random.nextInt(8) - 4;// [0,9]->[-5,4]
				int nextInt2 = random.nextInt(8) - 4;// [0,9]->[-5,4]
				// 画点
				g.drawLine(x2 + nextInt1, y2 + nextInt2, x2+ nextInt1, y2 + nextInt2);
			}
	    }
		else if("7".equals(panelLeft.actionCommand)){
			x2 = e.getX();
			y2 = e.getY();
			//刷子
			g.drawLine(x1, y1, x2, y2);
			x1 = x2;
			y1 = y2;
		}
	}
	
	public void mouseMoved(MouseEvent e) {
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值