1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java画图工具

Java画图工具

时间:2021-08-01 22:44:20

相关推荐

Java画图工具

Java画图工具的运用

实现功能基本的画图功能:

1.铅笔 2.直线 3.三角形 4.长方形 5.椭圆 还有一些分形 也可以做到改变颜色

导入的包有:

import java.awt.Dimension;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import javax.swing.JButton;

import javax.swing.JFrame;

画图界面:

思路和实现:

1.基本构建

首先需要创建窗台来现实出画图工具界面,然后在界面上有需要按钮来实现铅笔、直线等各种功能 所以就用到了动作监听器,最后还有如何保存各个已经画出来的铅笔和直线

2.代码的实现

2.1 窗体

public class DrawFrame {public static void main(String[] args){DrawFrame df = new DrawFrame();df.showFrame();}//显示画图工具的界面public void showFrame(){JFrame jf = new JFrame();//像素>分辨率jf.setSize(1000, 800);jf.setTitle("画图工具");//居中显示jf.setLocationRelativeTo(null);//设置退出进程的方法jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置流式布局管理器FlowLayout flow = new FlowLayout();jf.setLayout(flow);jf.setVisible(true);}

2.2 添加按钮并实现监听功能(接口)

添加按钮用JButton来创建一个新的对象并加入到窗体(窗体.add(按钮))中,但如何实现监听功能呢?这里我们用到了动作监听器(按钮.addActionListener)用来监听按钮是否被点击。

DrawMouse mouse = new DrawMouse();String[] arrary = {"铅笔","直线","三角形","长方形","椭圆","分形","Leaf IFS"};for (int i = 0; i < arrary.length; i++) {JButton jbu = new JButton(arrary[i]);jf.add(jbu);jbu.addActionListener(mouse);}

由于接口中的方法都是抽象方法,没有实际的操作行为,我们得创建新的子类用来实现(继承)接口中的方法。一个子类可实现(继承)多个接口,接口直接用逗号隔开。接口ActionListener用来监听按钮,接口MouseListener/MouseMotionListener用来获取鼠标的位置/轨迹。

最最需要注意的是,创建接口的子类时,需要重写接口中所有的抽象方法

public class DrawMouse implements ActionListener,MouseListener,MouseMotionListener{public void actionPerformed(ActionEvent e){}//ActionListener中的抽象方法public void mouseClicked(MouseEvent e) {}//MouseListener中的抽象方法public void mousePressed(MouseEvent e){}//MouseListener中的抽象方法public void mouseReleased(MouseEvent e){}//MouseListener中的抽象方法public void mouseEntered(MouseEvent e){}//MouseListener中的抽象方法public void mouseExited(MouseEvent e){}//MouseListener中的抽象方法public void mouseDragged(MouseEvent e){}//MouseMotionListener中的抽象方法public void mouseMoved(MouseEvent e){}//MouseMotionListener中的抽象方法}

2.3 画笔和鼠标位置的交互

由于画笔是画在窗体上的,画笔就从窗体上获取而且获取画笔得在窗体可视之后。

jf.setVisible(true);Graphics g = jf.getGraphics();jf.addMouseListener(mouse); //用来获取鼠标位置jf.addMouseMotionListener(mouse); //获取鼠标轨迹

如果就上段代码进行实现,会发现画笔仅只是窗体上的一个Graphics对象并不能实现任何功能,所以我们需要传递对象g到接口的子类中,换句话说,就是把g变成接口子类中的属性,这样就可以做到交互功能。

jf.setVisible(true);Graphics g = jf.getGraphics();jf.addMouseListener(mouse); //用来获取鼠标位置jf.addMouseMotionListener(mouse); //获取鼠标轨迹mouse.gr = g;

接口的子类中的属性

public class DrawMouse implements ActionListener,MouseListener,MouseMotionListener{public Graphics gr;public void actionPerformed(ActionEvent e){}//ActionListener中的抽象方法..//各个接口中的所有抽象方法public void mouseMoved(MouseEvent e){}//MouseMotionListener中的抽象方法}

3.选择功能

ActionListener激活后,程序是如何判断铅笔、直线或三角形等功能呢?

根据上图的画图界面,明显的可以看出每个按钮的名称,没有名称的按钮也可以知道相应的颜色,由此可以e.getActionCommand()来判断按钮上的内容来实现不同功能。名称和颜色也得作为接口子类的属性来保存。

public class DrawMouse implements MouseListener,ActionListener,MouseMotionListener{public Graphics gr;public Color color;public String name;public void actionPerformed(ActionEvent e){JButton button = (JButton) e.getSource();//getSource返回事件源类型,也可以用getActionCommand返回String类型//如果为图形按钮 if(button.getActionCommand() != ""){name = e.getActionCommand();}else{color = button.getBackground(); } }}

3.1 实现铅笔功能

用到的接口:MouseMotionListener

用到的方法:mousePressed, mouseDragged

确定起点,当鼠标移动时方法mouseDragged会被不停的调用,所以在方法mouseDragged中,可以画直线来表示点因为每个点之间距离很小,再把终点更新为起点就可以实现铅笔功能

public void mousePressed(MouseEvent e){System.out.println("按下");x1 = e.getX();y1 = e.getY();}public void mouseDragged(MouseEvent e) {//获取鼠标位置if ("铅笔".equals(name) ){x2=e.getX();y2=e.getY();gr.setColor(color);gr.drawLine(x1, y1, x2, y2);x1 = x2;y1 = y2;

3.2 实现直线功能

确定起点、终点,然后画一条直线。

用到的接口:MouseListener

用到的方法:mousePressed,mouseReleased

public void mousePressed(MouseEvent e){System.out.println("按下");x1 = e.getX();y1 = e.getY();}public void mouseReleased(MouseEvent e){System.out.println("松开");//画线if ("直线".equals(name) ){x2 = e.getX();y2 = e.getY();gr.setColor(color);gr.drawLine(x1, y1, x2, y2);}

3.3 实现三角形功能

确定起点、终点,画出一条直线,然后再选一个可以组成三角形的点与起点和终点画一条直线形成三角形

用到的接口:MouseListener

用到的方法:mousePressed, mouseReleased, mouseClicked

public void mousePressed(MouseEvent e){System.out.println("按下");x1 = e.getX();y1 = e.getY();}public void mouseReleased(MouseEvent e){System.out.println("松开");//画线if ("三角形".equals(name)&& i <1 ){x2 = e.getX();y2 = e.getY();i+=1;gr.setColor(color);gr.drawLine(x1, y1, x2, y2);}public void mouseClicked(MouseEvent e){System.out.println("点击");if ("三角形".equals(name) && i == 1) {x3 = e.getX();y3 = e.getY();gr.drawLine(x3, y3, x1, y1);gr.drawLine(x3, y3, x2, y2);i=0;

3.4 实现长方形(矩形)功能(椭圆同理)

确定起点,再由终点到起点的x,y轴距离决定长和宽。

单纯的减可能会出现问题,因为这个公式只支持向下和向右画。改变一下内部参数的公式便可以解决此问题。

用到的接口:MouseListener

用到的方法:mousePressed, mouseReleased

public void mousePressed(MouseEvent e){System.out.println("按下");x1 = e.getX();y1 = e.getY();}public void mouseReleased(MouseEvent e){System.out.println("松开");//画线if ("长方形".equals(name) ){x2 = e.getX();y2 = e.getY();gr.setColor(color);gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); //椭圆同理

3.5 实现分形功能

分形是根据一些特定的公式不停的迭代更新后然后画出的图形。

这个“分形” 叫做谢尔宾斯三角形。

功能的操作步骤:首先点击分形按钮,再选4个点,再点击分形按钮。

用到的接口:MouseListener

用到的方法:actionPerformed,mouseClicked

public void actionPerformed(ActionEvent e){if ("分形".equals(name) && stage == 4) {//System.out.println(stage);for (int j = 0; j< 10000; j++) {k+=1;//System.out.println(k);String[] parrary = {"A","B","C"};Random r = new Random();text = parrary[r.nextInt(parrary.length)];if (text.equals("A")) {xp = (x1+xp)/2;yp = (y1+yp)/2;gr.setColor(color);gr.drawLine(xp, yp, xp, yp);}else if (text.equals("B")) {xp = (x2+xp)/2;yp = (y2+yp)/2;gr.setColor(color);gr.drawLine(xp, yp, xp, yp);}else if (text.equals("C")) {xp = (x3+xp)/2;yp = (y3+yp)/2;gr.setColor(color);gr.drawLine(xp, yp, xp, yp);}}stage = 0;}public void mouseClicked(MouseEvent e){if ("分形".equals(name)) {if (stage == 0) {x1 = e.getX();y1 = e.getY();stage +=1;gr.drawLine(x1, y1, x1, y1);System.out.println(stage);}else if (stage == 1) {x2 = e.getX();y2 = e.getY();stage +=1;gr.drawLine(x2, y2, x2, y2);System.out.println(stage);}else if (stage == 2) {x3 = e.getX();y3 = e.getY();stage +=1;gr.drawLine(x3, y3, x3, y3);System.out.println(stage);}else if (stage == 3) {xp = e.getX();yp = e.getY();stage +=1;gr.drawLine(xp, yp, xp, yp);System.out.println(stage);}}}}}

3.5 实现Leaf IFS功能

下图是实际效果和公式

用到的接口:MouseListener

用到的方法:actionPerformed,mouseClicked

public void mouseClicked(MouseEvent e){else if ("Leaf IFS".equals(name) & stage ==0) {x4 = e.getX();y4 = e.getY();x0 = e.getX();y0 = e.getY();int x2 = (int) x4;int y2 = (int) y4;gr.drawLine(x2, y2, x2, y2);stage = 1;}}public void actionPerformed(ActionEvent e){if ("Leaf IFS".equals(name) && stage ==1) {Random rand = new Random();for(int i=0;i<50000;i++){double[][] cs= {{0.0000,0.2439,0.0000,0.3053,0.0000,0.0000},{0.7248,0.0337,-0.0253,0.7426,0.2060,0.2538},{0.1583,-0.1297,0.3550,0.3676,0.1383,0.1750},{0.3386,0.3694,0.2227,-0.0756,0.0679,0.0826}};int j=rand.nextInt(4);x4p = cs[j][0]*x4 + cs[j][1]*y4 + cs[j][4];y4p = cs[j][2]*x4 + cs[j][3]*y4 + cs[j][5];int x1 = (int) (x4p*130+x0) ;int y1 = (int) (-y4p*130+y0) ;gr.setColor(color);gr.drawLine(x1,y1,x1,y1);x4 = x4p;y4 = y4p;}stage = 0;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。