用JAVA做一个简单的画图软件

您所在的位置:网站首页 用线画圆怎么画图片视频 用JAVA做一个简单的画图软件

用JAVA做一个简单的画图软件

2024-07-13 17:14| 来源: 网络整理| 查看: 265

#用JAVA做一个简单的画图软件 写在前面

第一次用Java写东西,未免有疏漏之处,请多多指正。

首先 建立画布。

这里利用JFrame窗体界面创建一个DrawerFrame类,在主函数中实例化DrawerFrame类,并调用showUI方法,画出程序的基本框架。

public class DrawerFrame { public static void main(String[] args){ DrawerFrame df= new DrawerFrame(); df.showUI(); } public void showUI(){ //窗体 MyFrame jf=new MyFrame(); jf.setSize(900,900); jf.setTitle("画图界面"); //居中显示 jf.setLocationRelativeTo(null); //退出进程 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置可见 jf.setVisible(true); }

现在效果如下 在这里插入图片描述

第二步 往画板上面增加画图功能

这是目标 在这里插入图片描述

从最简单的画直线开始 由于画板的画图功能是通过鼠标出发,所以要增加一个MouseListener,实现对鼠标信息的获取。 //监听方法 DrawerMouse m=new DrawerMouse(); jf.addMouseListener(m); 这里的DrawerMouse是由MouseListener(抽象类,每个接口都必须实现)重写而来。(tips: 在这里插入图片描述 如果不知道这些接口的作用,可以像这样做,然后一边操控鼠标一边在控制面板查看输出) 画直线段需要知道起始点(X1,Y1)和终点(X2,Y2)的坐标,在鼠标按下的时候,此时光标的位置即为起点。松开时,则得到终点。 public void mousePressed (MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseReleased (MouseEvent e){ x2 = e.getX(); y2 = e.getY(); //画图 gr.drawLine(x1, y1, x2, y2); } 画矩形十分简单,但是要考虑矩形的方向。下面讲讲怎么画曲线和喷枪。画曲线要检测到鼠标的拖动动作,这里还是用到现成的ActionListener,同MouseListener一样,需要对其接口重写。画曲线可以理解为画无数很短的直线,这样的话,如果我们不断更新直线的起点和终点,就能通过画直线的方式画出曲线。 public void mouseDragged(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); if( ActionCommand.equals("曲线")){ gr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; }

喷枪功能与画直线类似,只不过画的是点而不是直线,并且这些点是不连续的、随机的。

喷枪功能与画直线类似,只不过画笔的颜色要与画板背景颜色一致。 按照以上步骤实现DrawerMouse类大致如下:

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.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.UIManager; public class DrawerMouse implements MouseListener,ActionListener,MouseMotionListener { public Graphics2D gr; public int x1=0,y1=0,x2=0,y2=0; public int pre_x1,pre_y1,pre_x2,pre_y2,source_x,source_y;//pre_x1,pre_y1,pre_x2,pre_y2记录上一次的(x1.y1) (x2,y2);(source_x,source_y)是多边形的第一个点 public String ActionCommand,Colorcommand; public boolean FirstEdgeDone;//判断多边形第一条边是否画出 public ArrayList date=new ArrayList (); public void mouseClicked (MouseEvent e){ //画图 if(ActionCommand=="三角形"){ //三角形 if(pre_x1!=pre_x2&&pre_y1!=pre_y2){ gr.drawLine(x1, y1, pre_x1, pre_y1); Shape s1=new Shape(x1, y1,pre_x1, pre_y1,gr.getColor(),"直线"); date.add(s1); gr.drawLine(x2, y2, pre_x2, pre_y2); Shape s2=new Shape(x1, y1,pre_x2, pre_y2,gr.getColor(),"直线"); date.add(s2); } } else ; } public void mousePressed (MouseEvent e){ pre_x1=x1; pre_y1=y1; x1 = e.getX(); y1 = e.getY(); } public void mouseReleased (MouseEvent e){ pre_x2=x2; pre_y2=y2; x2 = e.getX(); y2 = e.getY(); //画图 if(ActionCommand.equals("矩形")){ //矩形 gr.drawRect(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x2-x1),Math.abs(y2-y1)); Shape s=new Shape(x1, y1, x2, y2,gr.getColor(),"矩形"); date.add(s); } else if(ActionCommand.equals("直线")||ActionCommand=="三角形") { //画直线,或者画三角形的第一条边 gr.drawLine(x1, y1, x2, y2); Shape s=new Shape(x1, y1, x2, y2,gr.getColor(),"直线"); date.add(s); } else if(ActionCommand=="多边形"&&!FirstEdgeDone){ //画多边形的第一条边 source_x=x1; source_y=y1; gr.drawLine(x1, y1, x2, y2); Shape s=new Shape(x1, y1, x2, y2,gr.getColor(),"直线"); date.add(s); FirstEdgeDone=true; } else if(ActionCommand=="多边形"){ //多边形 if(e.getClickCount()==2){ gr.drawLine(x1, y1, source_x, source_y); Shape s=new Shape(x1, y1,source_x, source_y,gr.getColor(),"直线"); date.add(s); FirstEdgeDone=false; } else { gr.drawLine(x1, y1, pre_x2, pre_y2); Shape s=new Shape(x1, y1,pre_x2, pre_y2,gr.getColor(),"直线"); date.add(s); } } else ; } public void mouseEntered (MouseEvent e){ } public void mouseExited (MouseEvent e){ } //实现MouseMotionListener的方法 public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); if( ActionCommand.equals("曲线")){ gr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } else if( ActionCommand.equals("橡皮擦")){ gr.setColor(UIManager.getColor("control")); gr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } else if( ActionCommand.equals("喷枪")){ Random r=new Random(); for(int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3