欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java简单的画图工具,包括简单的图形绘制,排序可视化,颜色按钮

程序员文章站 2022-06-24 19:43:17
...

图形绘制

1.创建窗体;
2.给按钮加上监听;
3.添加组件;
4.在监听器中加入按钮响应;
5.绘制图形;`

窗体代码

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class DrawWindow {
	
	public static void main(String[] args) {
	//创建窗体对象
	DrawWindow dw=	new DrawWindow();
	dw.outUI();
	}
	// 图形按钮 
	String[] btnstrs = {"直线","矩形","圆","3D图形","三角"};
	public void outUI(){
		//创建按钮监听对象
		MyDrawListen bn=new MyDrawListen();
		JFrame hf=new JFrame();
		//设置标题
		hf.setTitle("图形绘制");
		//设置窗体大小
		hf.setSize(1200,900);
		FlowLayout fl = new FlowLayout();
		hf.setLayout(fl);
		for(int i=0;i<btnstrs.length;i++) {
			//加载图形按钮在窗体上
			JButton btn = new  JButton();
			btn.setText(btnstrs[i]);
			hf.add(btn);
			btn.addActionListener(bn);//加上监听
		}
		
		
		
		hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		hf.setVisible(true);
		
		hf.addMouseListener(bn);
		
		
		bn.addMouseListener(bn);
		
		//画笔对象
		//将画笔对象传入监听器
		Graphics j = hf.getGraphics();
		bn.f1=j;
		
		
				
	}
}

监听器代码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public  class  MyDrawListen implements MouseListener ,java.awt.event.ActionListener{
	
	Graphics f1=null;
	int x1,y1,x2,y2;
	public int abs(int a) {
		if(a>=0){
			return a;
		}
		else
		return -a;
	}
	public int min(int a,int b) {
		if(a>=b)
			return b;
		else
		return a;
	}
	//绘制等腰三角形
	public void drawTra1(int x1,int y1,int x2,int y2) {
		int a;
		a=x1-(x2-x1);
		f1.drawLine(x1,y1,x2,y2);
		f1.drawLine(x1,y1,a, y2);
		f1.drawLine(a, y2, x2, y2);
	}
	public void mousePressed(MouseEvent e) {
		x1=e.getX();y1=e.getY();
	}
	
	public void mouseReleased(MouseEvent e) {
		x2=e.getX();y2=e.getY();
	
		
		if(shapstr.equals("直线")) {
			f1.drawLine(x1, y1, x2, y2);
		}
		else if(shapstr.equals("圆"))
		{
			if(x2-x1>0&&y2-y1>0)
			f1.drawOval(x1, y1, x2, y2);
			else if(x2-x1<0&&y2-y1<0){
			}
		}
		else if(shapstr.equals("矩形")){
			f1.drawRect(min(x1,x2), min(y1,y2), abs(x2-x1), abs(y2-y1));
		}
		else if(shapstr.equals("3D图形")){
			for(int i=0;i<200;i++) {
				Color color = new Color(i,0,0);
				f1.setColor(color);
				f1.drawOval(x1, y1, i+=10, i+=10);
			}
			
		}
		else if(shapstr.equals("三角")) {
			drawTra1(x1, y1, x2, y2);
		}
		String bitstr,shapstr;//shapstr用来存放读取的按钮上读取的汉字
	public void actionPerformed(ActionEvent e) {
		bitstr=e.getActionCommand();
		if(bitstr.equals("")) {
			//将窗体中的按钮传入监听器
		javax.swing.JButton btn =(javax.swing.JButton)	e.getSource();
		    //获取按钮背景色
			Color color = btn.getBackground();
			f1.setColor(color);
		}else {
		 shapstr=bitstr;
		}
	}

颜色按钮-排序可视化

创建颜色数组 
Color[]   colors ={Color.black , Color.red };
生成按钮
加上监听器 
加载到窗体上
	
通过按钮上是否有字符串 来区分是图形类型按钮 还是 颜色按钮

处理颜色按钮
1、先获取按钮对象  get事件源对象 
2、获取按钮的背景色
3、将这个颜色设置给画笔


可视化排序 

根据初始化的数组 
开始排序,每排一个就将数组绘制一次 图形
绘制之前最好清空,填充一个矩形  fill ;

窗体代码

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class DrawWindow {
	
	public static void main(String[] args) {
	//创建窗体对象
	DrawWindow dw=	new DrawWindow();
	dw.outUI();
	}
	// 图形按钮 
	String[] btnstrs = {"直线","矩形","圆","3D图形","三角","冒泡排序","选择排序"};
	//颜色按钮
	Color[] color = {Color.black,Color.red,Color.blue,Color.PINK};
	
	
	public void outUI(){
		//创建按钮监听对象
		MyDrawListen bn=new MyDrawListen();
		JFrame hf=new JFrame();
		//设置标题
		hf.setTitle("图形绘制");
		//设置窗体大小
		hf.setSize(1200,900);
		FlowLayout fl = new FlowLayout();
		hf.setLayout(fl);
		for(int i=0;i<btnstrs.length;i++) {
			//加载图形按钮在窗体上
			JButton btn = new  JButton();
			btn.setText(btnstrs[i]);
			hf.add(btn);
			btn.addActionListener(bn);//加上监听
		}
		for(int i=0;i<color.length;i++) {
			JButton btn2 = new JButton();
			//设置按钮 颜色
			btn2.setBackground(color[i]);
			java.awt.Dimension dim = new java.awt.Dimension(30,30);
			btn2.setPreferredSize(dim);
			
			hf.add(btn2);
			btn2.addActionListener(bn);
			
		}
		
		
		
		hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		hf.setVisible(true);
		
		hf.addMouseListener(bn);
		
		
		bn.addMouseListener(bn);
		
		//画笔对象
		//将画笔对象传入监听器
		Graphics j = hf.getGraphics();
		bn.f1=j;
	}
}

监听器代码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public  class  MyDrawListen implements MouseListener ,java.awt.event.ActionListener{
	
	Graphics f1=null;
	int x1,y1,x2,y2;
	public int abs(int a) {
		if(a>=0){
			return a;
		}
		else
		return -a;
	}
	public int min(int a,int b) {
		if(a>=b)
			return b;
		else
		return a;
	}
	//绘制等腰三角形
	public void drawTra1(int x1,int y1,int x2,int y2) {
		int a;
		a=x1-(x2-x1);
		f1.drawLine(x1,y1,x2,y2);
		f1.drawLine(x1,y1,a, y2);
		f1.drawLine(a, y2, x2, y2);
	}
	//排序可视化
	public void sort1() {
		int a[] = new int[300];
		Random ran = new Random();
		//用随机数初始化数组
		for (int i = 0; i < a.length; i++) {
			a[i]=ran.nextInt(1000);
		}
		int temp;
		for(int i=0;i<a.length;i++) {
			Color color = Color.blue;
			f1.setColor(color);
			f1.fillRect(0+i*3,100,2,a[i]);
		}
		for(int i=0;i<a.length;i++) {
			for(int j=i+1;j<a.length;j++) {
				if(a[i]>a[j]) {
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
			
			}
			//延时。使排序可视化
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			f1.setColor(Color.black);
			f1.fillRect(0,100,1200,1100);
			for (int k = 0; k < a.length; k++) {
				f1.setColor(Color.green);
				f1.fillRect(0+k*3,100,2,a[k]);
			}
			
		}
	}
	//选择排序
	public void sort2() {
		int b[] = new int[300];
		Random ran2 = new Random();
		for (int i = 0; i < b.length; i++) {
			b[i]=ran2.nextInt(1000);
		}
		int temp;
		for(int i=0;i<b.length;i++) {
			Color color = Color.blue;
			f1.setColor(color);
			f1.fillRect(0+i*3,100,2,b[i]);
		}
		for(int i=0;i<b.length;i++) {
			int min=i;
			for(int j=i+1;j<b.length;j++) {
				if(b[j]<b[min]) {
					min=j;
				}
			}
			if(min!=i) {
				temp=b[i];
				b[i]=b[min];
				b[min]=temp;
			}
			try {
				Thread.sleep(80);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//			Color color = Color.yellow;
			f1.setColor(Color.black);
			f1.fillRect(0,100,1200,1100);
			for (int k = 0; k < b.length; k++) {
				f1.setColor(Color.green);
				f1.fillRect(0+k*3,100,2,b[k]);
			}
		}
	}
	
	public void mousePressed(MouseEvent e) {
		x1=e.getX();y1=e.getY();
	}
	
	public void mouseReleased(MouseEvent e) {
		x2=e.getX();y2=e.getY();
	
		
		if(shapstr.equals("直线")) {
			f1.drawLine(x1, y1, x2, y2);
		}
		else if(shapstr.equals("圆"))
		{
			if(x2-x1>0&&y2-y1>0)
			f1.drawOval(x1, y1, x2, y2);
			else if(x2-x1<0&&y2-y1<0){
			}
		}
		else if(shapstr.equals("矩形")){
			f1.drawRect(min(x1,x2), min(y1,y2), abs(x2-x1), abs(y2-y1));
		}
		else if(shapstr.equals("3D图形")){
			for(int i=0;i<200;i++) {
				Color color = new Color(i,0,0);
				f1.setColor(color);
				f1.drawOval(x1, y1, i+=10, i+=10);
			}
			
		}
		else if(shapstr.equals("三角")) {
			drawTra1(x1, y1, x2, y2);
		}
		else if(shapstr.equals("冒泡排序")) {
			sort1();
		}
		else if(shapstr.equals("选择排序")){
			sort2();
		}
		
		
	}
	
	
	String bitstr,shapstr;//shapstr用来存放读取的按钮上读取的汉字
	public void actionPerformed(ActionEvent e) {
		bitstr=e.getActionCommand();
		if(bitstr.equals("")) {
			//将窗体中的按钮传入监听器
		javax.swing.JButton btn =(javax.swing.JButton)	e.getSource();
		    //获取按钮背景色
			Color color = btn.getBackground();
			f1.setColor(color);
		}else {
		 shapstr=bitstr;
		}
	}
相关标签: Java java