Java 画图板

用Java做一个画图板,主要分四个步骤:

  1. 创建画布,并设置画布可见
  2. 给画布添加菜单栏(我是以菜单的形式做的各种功能)
  3. 设置监听器
  4. 用监听器监测事件,绘图

创建画布

//面板参数
JFrame jf = new JFrame("画图板");
jf.setSize(1800,900);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setLayout(new BorderLayout());


//设置可见
jf.setVisible(true);
Graphics g = jf.getGraphics();

加菜单栏

//创建菜单
JMenuBar jb = new JMenuBar();
jb.setSize(1800,50);
jf.add(jb,BorderLayout.NORTH);

//添加形状栏
JMenu menu1 = new JMenu("形状");
jb.add(menu1);
//形状栏下子目录
String[] arr = {"直线","曲线","椭圆","矩形","等腰三角形"};
//使用for循环将其导入形状
for (int i=0;i<arr.length;i++){
	JMenuItem menuItem = new JMenuItem(arr[i]);
	menu1.add(menuItem);
}

设置监听器

//创建监听器
DrawListener dl = new DrawListener();
//将监听器添加到各个组件上面
for (int i=0;i<arr.length;i++){
	JMenuItem menuItem = new JMenuItem(arr[i]);
	menu1.add(menuItem);
	menuItem.addActionListener(dl);
	……
//给窗体加监听器
if.addMouseListener(dl);
jf.addMouseMotionListener(dl);
dl.g = g;
}

监听事件&颜色板

//利用type接受事件信息
String action = e.getActionCommand();
type=action;

//判断颜色
if(action.equals("")){
	Object srcobj = e.getSource();
	JButton srcBtn = (JButton) srcobj;
	Color color = srcBtn.getBackGround();
	g.setColor(color);
}

画曲线

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		x1=e.getX();
		y1=e.getY();
		}
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		x3=e.getX();
		y3=e.getY();
		if("曲线".equals(type)){
			x3=e.getX();
			y3=e.getY();
			g.drawLine(x1, y1, x3, y3);
			x1=x3;
			y1=y3;
		}
	}

下面附上全部代码:

DrawUI

package drawing_board2;

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

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

public class DrawUI extends JFrame{

    //创建监听器
  	DrawListener dl = new DrawListener();
	
  	public void showUI(){
		//设置画图板
		JFrame jf = new JFrame("画图板");
		jf.setSize(1800, 900);
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jf.setLayout(new BorderLayout());
			
		
		//设置菜单栏
		JMenuBar jb = new JMenuBar();
		jb.setSize(1800, 50);
		//将菜单栏对象放在窗体的菜单栏中
		jf.add(jb,BorderLayout.NORTH);
		
		//形状栏
		JMenu menu1 = new JMenu("形状");
		jb.add(menu1);
		String[] arr = {"直线","曲线","椭圆","矩形","等腰三角形"};
		for (int i = 0; i < arr.length; i++){
			JMenuItem menuItem = new JMenuItem(arr[i]);
			menu1.add(menuItem);
			menuItem.addActionListener(dl);		
		}
		
		//分形
		JMenu menu2 = new JMenu("分形图案");
		jb.add(menu2);
		String[] arr2 = {"分形1","分形2","分形3","分形4"};
		for (int i=0;i<arr2.length;i++){
			JMenuItem menuItem2 = new JMenuItem(arr2[i]);
			menu2.add(menuItem2);
			menuItem2.addActionListener(dl);
		}
		
		//清屏
		JMenu menu3 = new JMenu("清屏");
		jb.add(menu3);
		JMenuItem menuItem = new JMenuItem("清屏");
		menu3.add(menuItem);
		menuItem.addActionListener(dl);
		
		//颜色按钮
		Color[] ColorBtn = {Color.BLUE,Color.cyan,Color.GRAY,Color.orange,Color.PINK,Color.YELLOW};
		Dimension ColorBtnSize = new Dimension(30,20);
		for(int i=0;i<ColorBtn.length;i++){
			JButton color = new JButton();
			color.setPreferredSize(ColorBtnSize);
			color.setBackground(ColorBtn[i]);
			jb.add(color);
			color.addActionListener(dl);
		}
		
		//增加滑杆
		
		
		//给窗体加监听器
		jf.addMouseListener(dl);
		jf.addMouseMotionListener(dl);
		
		//设置可见
		jf.setVisible(true);
		Graphics g = jf.getGraphics();
		
		dl.g = g;
		
	}

	public void paint (Graphics g){
		super.paint(g);
	}
	
	public static void main(String[] args){
		DrawUI ui = new DrawUI();
		ui.showUI();
	}
}

DrawListener

package drawing_board2;

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

import javax.swing.JButton;

public class DrawListener implements MouseListener,ActionListener,MouseMotionListener{
	
	Graphics g;
	String type="";
	
	int x1,y1,x2,y2,x3,y3,x4,y4;
	
	public void setGraphics(Graphics arg){
		g=arg;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String action = e.getActionCommand();
		type=action;
		
		//调颜色
		if(action.equals("")){
			Object srcobj = e.getSource();
			JButton srcBtn = (JButton) srcobj;
			Color color = srcBtn.getBackground();
			g.setColor(color);
		}
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		x1=e.getX();
		y1=e.getY();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		x2=e.getX();
		y2=e.getY();
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		x3=e.getX();
		y3=e.getY();
		if("曲线".equals(type)){
			x3=e.getX();
			y3=e.getY();
			g.drawLine(x1, y1, x3, y3);
			x1=x3;
			y1=y3;
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
		if("直线".equals(type)){
			g.drawLine(x1,y1,x2,y2);
		}
		else if("椭圆".equals(type)){
			g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x1-x2),Math.abs(y1-y2));
		}
		else if("矩形".equals(type)){
			g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x1-x2),Math.abs(y1-y2));
		}
		else if("等腰三角形".equals(type)){
			g.drawLine(x1, y1, x2, y1);
			g.drawLine(x1, y1, (x1+x2)/2, y2);
			g.drawLine(x2, y1, (x1+x2)/2, y2);
		}
		//分形
		else if("分形1".equals(type)){
			double x = 0;
			double y = 0;
			double a= -1.8,b=-2.0,c=-0.5,d=-0.9;
			for(int i=0;i<25500;i++){
				double temx = Math.sin(a*y)+c*Math.cos(a*x);
				double temy = Math.sin(b*x)+d*Math.cos(b*y);
				int x1=(int)(temx*130+500);
				int y1=(int)(temy*130+400);
				
				g.drawLine(x1, y1, x1, y1);
				x=temx;
				y=temy;
			}}
		
		else if("分形2".equals(type)){
			for(int i=0;i<=180;i++){     
				for(int j=0;j<=180;j++){   
					double r=Math.PI/45*i*(1-Math.sin(Math.PI/45*j))*20;   
					double x=r*Math.cos(Math.PI/45*j)*Math.sin(Math.PI/45*i)+300;  
					double y=-r*Math.sin(Math.PI/45*j)+200;    
					Color c=Color.getHSBColor(i*j/8100.0f, 0.9999f,0.9999f);   
					g.setColor(c);      
					g.drawOval((int)(2*x/3+300), (int)(2*y/3+50), 1,1);   
//					try{    Thread.sleep(1);    
//					}
//					catch(Exception e1){}       
					}
			}}
		
		else if("分形3".equals(type)){
			double x = 0;
			double y = 0;
			double a= 1.7,b=1.7,c=0.6,d=1.2;
			for(int i=0;i<50000;i++){
				double temx = Math.sin(a*y)+c*Math.cos(a*x);
				double temy = Math.sin(b*x)+d*Math.cos(b*y);
				int x1=(int)(-temx*130+800);
				int y1=(int)(-temy*130+400);
				g.setColor(new Color(i/250,i/500,i/250));
				g.drawLine(x1, y1, x1, y1);
				x=temx;
				y=temy;
		}
		}
		
		else if ("清屏".equals(type)){
			g.clearRect(0, 70, 1800,900);
		}
	}
	}

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

发布了45 篇原创文章 · 获赞 14 · 访问量 2495

猜你喜欢

转载自blog.csdn.net/qq_44357371/article/details/99515008