超经漂亮验证码,绘制函数图象小工具
分类:微服架构

介绍:

这是一个数字钟表程序,主要功能是从系统中获取当前的系统时间然后再将其显示在数字时钟上,由于整个的数字时钟都是用函数构成的,所以它可以实现一般的数字时钟所不具有的功能,比如说它可以被鼠标指针拖动到窗口的任意位置,除此之外它还可以实现钟表大小随鼠标滚轮的滚动而变大变小的操作。```java

复制代码
、、、
1 package com.zly.xsp.image;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.util.Random;
11
12 import javax.imageio.ImageIO;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
16
17 public class CreateImageCode {
18 // 图片的宽度。
19 private int width = 160;
20 // 图片的高度。
21 private int height = 40;
22 // 验证码字符个数
23 private int codeCount = 4;
24 // 验证码干扰线数
25 private int lineCount = 20;
26 // 验证码
27 private String code = null;
28 // 验证码图片Buffer
29 private BufferedImage buffImg = null;
30 Random random = new Random();
31
32 public CreateImageCode() {
33 creatImage();
34 }
35
36 public CreateImageCode(int width, int height) {
37 this.width = width;
38 this.height = height;
39 creatImage();
40 }
41
42 public CreateImageCode(int width, int height, int codeCount) {
43 this.width = width;
44 this.height = height;
45 this.codeCount = codeCount;
46 creatImage();
47 }
48
49 public CreateImageCode(int width, int height, int codeCount, int lineCount) {
50 this.width = width;
51 this.height = height;
52 this.codeCount = codeCount;
53 this.lineCount = lineCount;
54 creatImage();
55 }
56
57 // 生成图片
58 private void creatImage() {
59 int fontWidth = width / codeCount;// 字体的宽度
60 int fontHeight = height - 5;// 字体的高度
61 int codeY = height - 8;
62
63 // 图像buffer
64 buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
65 Graphics g = buffImg.getGraphics();
66 //Graphics2D g = buffImg.createGraphics();
67 // 设置背景色
68 g.setColor(getRandColor(200, 250));
69 g.fillRect(0, 0, width, height);
70
71
72
73 // 设置字体
74 //Font font1 = getFont(fontHeight);
75 Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
76 g.setFont(font);
77
78 // 设置干扰线
79 for (int i = 0; i < lineCount; i++) {
80 int xs = random.nextInt(width);
81 int ys = random.nextInt(height);
82 int xe = xs + random.nextInt(width);
83 int ye = ys + random.nextInt(height);
84 g.setColor(getRandColor(1, 255));
85 g.drawLine(xs, ys, xe, ye);
86 }
87
88 // 添加噪点
89 float yawpRate = 0.01f;// 噪声率
90 int area = (int) (yawpRate * width * height);
91 for (int i = 0; i < area; i++) {
92 int x = random.nextInt(width);
93 int y = random.nextInt(height);
94
95 buffImg.setRGB(x, y, random.nextInt(255));
96 }
97
98
99 String str1 = randomStr(codeCount);// 得到随机字符
100 this.code = str1;
101 for (int i = 0; i < codeCount; i++) {
102 String strRand = str1.substring(i, i + 1);
103 g.setColor(getRandColor(1, 255));
104 // g.drawString(a,x,y);
105 // a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处
106
107 g.drawString(strRand, ifontWidth+3, codeY);
108 }
109
110
111 }
112
113 // 得到随机字符
114 private String randomStr(int n) {
115 String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
116 String str2 = "";
117 int len = str1.length() - 1;
118 double r;
119 for (int i = 0; i < n; i++) {
120 r = (Math.random())
len;
121 str2 = str2 + str1.charAt((int) r);
122 }
123 return str2;
124 }
125
126 // 得到随机颜色
127 private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
128 if (fc > 255)
129 fc = 255;
130 if (bc > 255)
131 bc = 255;
132 int r = fc + random.nextInt(bc - fc);
133 int g = fc + random.nextInt(bc - fc);
134 int b = fc + random.nextInt(bc - fc);
135 return new Color(r, g, b);
136 }
137
138 /**
139 * 产生随机字体
140 /
141 private Font getFont(int size) {
142 Random random = new Random();
143 Font font[] = new Font[5];
144 font[0] = new Font("Ravie", Font.PLAIN, size);
145 font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
146 font[2] = new Font("Fixedsys", Font.PLAIN, size);
147 font[3] = new Font("Wide Latin", Font.PLAIN, size);
148 font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);
149 return font[random.nextInt(5)];
150 }
151
152 // 扭曲方法
153 private void shear(Graphics g, int w1, int h1, Color color) {
154 shearX(g, w1, h1, color);
155 shearY(g, w1, h1, color);
156 }
157
158 private void shearX(Graphics g, int w1, int h1, Color color) {
159
160 int period = random.nextInt(2);
161
162 boolean borderGap = true;
163 int frames = 1;
164 int phase = random.nextInt(2);
165
166 for (int i = 0; i < h1; i++) {
167 double d = (double) (period >> 1)
168
Math.sin((double) i / (double) period
169 + (6.2831853071795862D * (double) phase)
170 / (double) frames);
171 g.copyArea(0, i, w1, 1, (int) d, 0);
172 if (borderGap) {
173 g.setColor(color);
174 g.drawLine((int) d, i, 0, i);
175 g.drawLine((int) d + w1, i, w1, i);
176 }
177 }
178
179 }
180
181 private void shearY(Graphics g, int w1, int h1, Color color) {
182
183 int period = random.nextInt(40) + 10; // 50;
184
185 boolean borderGap = true;
186 int frames = 20;
187 int phase = 7;
188 for (int i = 0; i < w1; i++) {
189 double d = (double) (period >> 1)
190 * Math.sin((double) i / (double) period
191 + (6.2831853071795862D * (double) phase)
192 / (double) frames);
193 g.copyArea(i, 0, 1, h1, 0, (int) d);
194 if (borderGap) {
195 g.setColor(color);
196 g.drawLine(i, (int) d, i, 0);
197 g.drawLine(i, (int) d + h1, i, h1);
198 }
199
200 }
201
202 }
203
204
205
206 public void write(OutputStream sos) throws IOException {
207 ImageIO.write(buffImg, "png", sos);
208 sos.close();
209 }
210
211 public BufferedImage getBuffImg() {
212 return buffImg;
213 }
214
215 public String getCode() {
216 return code.toLowerCase();
217 }
218
219 //使用方法
220 /public void getCode3(HttpServletRequest req, HttpServletResponse response,HttpSession session) throws IOException{
221 // 设置响应的类型格式为图片格式
222 response.setContentType("image/jpeg");
223 //禁止图像缓存。
224 response.setHeader("Pragma", "no-cache");
225 response.setHeader("Cache-Control", "no-cache");
226 response.setDateHeader("Expires", 0);
227
228
229 CreateImageCode vCode = new CreateImageCode(100,30,5,10);
230 session.setAttribute("code", vCode.getCode());
231 vCode.write(response.getOutputStream());
232 }
/
233
234 }

1、创建一个Graphics对象
BufferedImage bi = new BufferedImage(120,120, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
2、控制图形边缘反锯齿

Graphics2D .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
3、线段 / 折线

       这是一款绘制函数图象的轻量级小程序,希望他可以给大家对于简单绘图带来很大的便捷;目前版本支持各类简单函数,及复合函数图象的绘制,且在细节上比较符合我们平时绘图的习惯,提供函数图象的导出保存功能,理论上可以同时在一张面板上绘制的图象数量是不受限制的,但还是建议不要超过30个。今天是个特殊的日子,也希望我的小程序可以带给大家一些快乐。

package TheClock; import java.awt.; import javax.swing.; import java.lang.; import java.awt.event.; import java.util.; public class TheClock extends JApplet implements Runnable,MouseMotionListener,MouseListener,MouseWheelListener{ private Thread t; boolean dstatus=true; public static final double PI=Math.PI/180; Image offScreenImage=null; Graphics offScreenBuffer=null; int width=1440; int height=900; double R=90; int cx=250,cy=150; int x2,y2; public void init(){ t=new Thread(this); t.start(); offScreenImage=this.createImage(width,height); offScreenBuffer=offScreenImage.getGraphics(); addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(this); } public void update(Graphics g){ paint(g); } public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseDragged(MouseEvent e){ x2=e.getX(); y2=e.getY(); cx=x2; cy=y2; repaint(); } public void mouseMoved(MouseEvent e){} public void mouseWheelMoved(MouseWheelEvent e){ int count=e.getWheelRotation(); if(count>0){ R+=count; }else if(count<0){ R+=count; } } public void run(){ while(true){ try{ t.sleep(1000); }catch(Exception e){} repaint(); } } public void paint(Graphics g){ int s,m,h; //创建时间 Date rightNow=new Date(); //获取系统时间 String today=rightNow.toLocaleString(); s=rightNow.getSeconds(); m=rightNow.getMinutes(); h=rightNow.getHours(); g.drawImage(offScreenImage,0,0,this); g.setColor(Color.orange); g.fillOval((int)(cx-R), (int)(cy-R),(int)(2R),(int)(2R)); g.setColor(Color.black); g.drawOval((int)(cx-R), (int)(cy-R),(int)(2R),(int)(2R)); //画刻度(1,2,4,5,7,8,10,11) g.drawString("12", cx-8, (int)(cy-R+18)); g.drawString("3", (int)(cx+R-18), cy+4); g.drawString("6",cx-4,(int)(cy+R-10)); g.drawString("9", (int)(cx-R+12), cy+4); g.drawString("我的钟表", cx-20, (int)(cy-R-20)); g.drawString(today,cx-55,(int)(cy+R+30)); g.setFont(new Font("TimesRoman",Font.PLAIN,14)); offScreenBuffer.clearRect(0,0,width,height); drawCircle(g,cx,cy,R,s,m,h); } public void drawCircle(Graphics g,double x,double y,double R,double s,double m,double h){ double x1,y1; x1=x+RMath.cos((s6-90)PI); y1=y+RMath.sin((s6-90)PI); //画秒针 g.setColor(Color.blue); g.drawLine((int)x, (int)y, (int)x1, (int)y1); //画分针 g.setColor(Color.green); g.drawLine((int)x, (int)y, (int)(x+(R/43)Math.cos((m6-90)PI)), (int)(y+(R/43)Math.sin((m6-90)PI))); //画时针 g.setColor(Color.red); g.drawLine((int)x, (int)y, (int)(x+(R/2)Math.cos((h30+m6/12-90)PI)), (int)(y+(R/2)Math.sin((h30+m6/12-90)PI))); //画刻度(3,6,9,12) g.setColor(Color.black); g.drawLine((int)x,(int)(y-R+10),(int)x,(int)(y-R)); g.drawLine((int)(x+R-10),(int)y,(int)(x+R),(int)y); g.drawLine((int)x,(int)(y+R-10),(int)x,(int)(y+R)); g.drawLine((int)(x-R+10),(int)y,(int)(x-R),(int)y); g.drawLine((int)(x+(R-5)Math.cos(30PI)),(int)(y+(R-5)Math.sin(30PI)),(int)(x+RMath.cos(30PI)),(int)(y+RMath.sin(30PI))); g.drawLine((int)(x+(R-5)Math.cos(60PI)),(int)(y+(R-5)Math.sin(60PI)),(int)(x+RMath.cos(60PI)),(int)(y+RMath.sin(60PI))); g.drawLine((int)(x+(R-5)Math.cos(120PI)),(int)(y+(R-5)Math.sin(120PI)),(int)(x+RMath.cos(120PI)),(int)(y+RMath.sin(120PI))); g.drawLine((int)(x+(R-5)Math.cos(150PI)),(int)(y+(R-5)Math.sin(150PI)),(int)(x+RMath.cos(150PI)),(int)(y+RMath.sin(150PI))); g.drawLine((int)(x+(R-5)Math.cos(210PI)),(int)(y+(R-5)Math.sin(210PI)),(int)(x+RMath.cos(210PI)),(int)(y+RMath.sin(210PI))); g.drawLine((int)(x+(R-5)Math.cos(240PI)),(int)(y+(R-5)Math.sin(240PI)),(int)(x+RMath.cos(240PI)),(int)(y+RMath.sin(240PI))); g.drawLine((int)(x+(R-5)Math.cos(300PI)),(int)(y+(R-5)Math.sin(300PI)),(int)(x+RMath.cos(300PI)),(int)(y+RMath.sin(300PI))); g.drawLine((int)(x+(R-5)Math.cos(330PI)),(int)(y+(R-5)Math.sin(330PI)),(int)(x+RMath.cos(330PI)),(int)(y+RMath.sin(330*PI))); } }

、、、

//两点绘制线段
g2d.drawLine(50,50,200,50);
//多点绘制折线 点(50, 100), 点(100, 130), 点(150, 70), 点(200, 100)
int[] xpoints = new int[]{50,100,150,200};
int[] ypoints = new int[]{100,130,70,100};
int npoints=4;
g2d.drawPolyline(xpoints,ypoints,npoints);
g2d.dispose();
//两点绘制线段(设置线宽为5px): 点(50, 150), 点(200, 150)
BasicStroke bs1 = new BasicStroke(5);
g2d.setStroke(bs1);
g2d.drawLine(50,150,200,150);
//绘制虚线: 将虚线分为若干段( 实线段 和 空白段 都认为是一段), 实线段 和 空白段 交替绘制,
// 绘制的每一段(包括 实线段 和 空白段)的 长度 从 dash 虚线模式数组中取值(从首
// 元素开始循环取值), 下面数组即表示每段长度分别为: 5px, 10px, 5px, 10px, ...
float[] dash = new float[]{5,10};
BasicStroke bs2=new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
g2d.setStroke(bs2);
g2d.drawLine(50,200,200,200);

部分源码:package FunctionImage_1;

```

g2d.dispose();

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
import javax.xml.transform.Templates;

  4、矩形

public class MyPanelCenter extends JPanel {
    Point oPoint = null; // 坐标原点
    FPoint resultPoint[] = null; // 函数图象点集
    int space = 10;// 坐标系周围间距
    int sl = 5;// 标准最小刻度像素点大小
    int snl = 5;// 标准坐标上数字间间距刻度大小,1个数学单位之间有snl个刻度
    double nSize = 1;// 每个坐标数学单位对应实际函数中的数学单位
    double pointsize = 1000;// 要计算点的个数
    double xstart, xend;// 函数现行定义域
    int flag = 0;// 特殊缩小标记
    double rangeXstart, rangeXend;// 初始定义域
    int lineSizeFlag = 1;// 默认线条为细
    ReadExPre readExPre;// 解读表达式的类
    ArrayList<FunctionExpression> expressionsList;// 存放函数表达式的顺序表

// 1. 绘制一个矩形: 起点(30, 20), 宽80, 高100
g2d.drawRect(30,20,80,100);

    public MyPanelCenter() {
        super();
        readExPre = new ReadExPre();
        expressionsList = new ArrayList<>();
    }

// 2. 填充一个矩形
g2d.fillRect(30,20,80,100);

    public void changeBiger() {// 把坐标系变大
        if (flag == 1) {
            nSize *= 2;
            if (nSize == 1) {
                flag = 0;
            }
        } else {
            sl += 5;
        }
    }

// 3. 绘制一个圆角矩形: 起点(30, 150), 宽80, 高100, 圆角宽30, 圆角高30
g2d.drawRoundRect(30,150,80,100,30,30);

    public void changeLittler() {// 把坐标系变小,实际表示范围扩大
        if (sl > 5) {// 未达到最小刻度
            sl -= 5;
        } else {// 达到最小刻度
            flag = 1;
            nSize /= 2;
        }
    }

// 4. 绘制一个多边形(收尾相连): 点(140, 150), 点(180, 250), 点(220, 200)
int[] xpoints = new int[]{140,180,220};
int[] ypoints = new int[]{150,250,200};
int npoints = 3;
g2d.drawPolygon(xpoints,ypoints,npoints);

    public void clear(Graphics g) {
        // 覆盖原来的
        g.setColor(Color.white);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }

g2d.dispose();

    public void paint(Graphics g) {
        // 覆盖原来的
        clear(g);
        // 画坐标系
        paintTheXoY(g);
        // 绘制函数列表里的函数图象
        paintAllFun(g);
        // 绘制函数标识
        paintTheFunFlag(g);
    }

  5、圆弧、扇形

    public void paintTheXoY(Graphics g) {// 画坐标系
        if (oPoint == null) {
            oPoint = new Point(this.getWidth() / 2, this.getHeight() / 2);
        }
        // 画坐标轴
        g.setColor(Color.GREEN);
        g.drawLine(oPoint.x, space, oPoint.x, this.getHeight() - 2 * space);
        g.drawLine(space, oPoint.y, this.getWidth() - 2 * space, oPoint.y);
        g.drawString("y", oPoint.x, space - 2);
        g.drawString("x", this.getWidth() - space, oPoint.y);
        // y箭头
        g.drawLine(oPoint.x, space, oPoint.x - 5, space + 5);
        g.drawLine(oPoint.x, space, oPoint.x + 5, space + 5);
        // x箭头
        g.drawLine(this.getWidth() - 2 * space - 5, oPoint.y + 5,
                this.getWidth() - 2 * space, oPoint.y);
        g.drawLine(this.getWidth()

// 1. 绘制一条圆弧: 椭圆的外切矩形 左上角坐标为(0, 0), 宽100, 高100,
// 弧的开始角度为0度, 需要绘制的角度数为-90度,
// 椭圆右边水平线为0度, 逆时针为正角度, 顺时针为负角度
g2d.drawArc(0,0,100,100,0,-90);
// 2. 绘制一个圆: 圆的外切矩形 左上角坐标为(120, 20), 宽高为100
g2d.drawArc(120,20,100,100,0,360);
// 3. 填充一个扇形
g2d.fillArc(80, 150, 100, 100, 90, 270);

  • 2 * space - 5, oPoint.y - 5,
                    this.getWidth() - 2 * space, oPoint.y);
            // 画坐标
            // x
            int xlength = oPoint.x;
            // 左侧
            for (int temp = sl; temp + space < oPoint.x; temp += sl) {
                if (temp % (sl * snl) == 0) {
                    g.drawString("-" + temp / (sl * snl * nSize) + "", xlength
                            - temp - 5, oPoint.y + 15);
                    g.drawLine(xlength - temp, oPoint.y + 3, xlength - temp,
                            oPoint.y - 3);
                    // rangeXstart = -temp / (sl * snl * nSize);
                } else {
                    g.drawLine(xlength - temp, oPoint.y + 1, xlength - temp,
                            oPoint.y - 1);
                }
            }
            rangeXstart = -(oPoint.x - space) / (sl * snl * nSize);
            if (oPoint.x < 0) {// 原点移至panel左侧外
                rangeXstart = (0 + space - oPoint.x) / (sl * snl * nSize);// 取左侧作为当前定义域上端
            }
            // 右侧
            for (int temp = sl; temp + space < this.getWidth() - oPoint.x - sl; temp += sl) {
                if (temp % (sl * snl) == 0) {
                    g.drawString(temp / (sl * snl * nSize) + "",
                            xlength + temp - 5, oPoint.y + 15);
                    g.drawLine(xlength + temp, oPoint.y + 3, xlength + temp,
                            oPoint.y - 3);
                    // rangeXend = temp / (sl * snl * nSize);
                } else {
                    g.drawLine(xlength + temp, oPoint.y + 1, xlength + temp,
                            oPoint.y - 1);
                }
            }
            rangeXend = (this.getWidth() - space - oPoint.x) / (sl * snl * nSize);
            if (oPoint.x > this.getWidth()) {// 原点移至panel右侧外
                rangeXend = (oPoint.x - this.getWidth() - space)
                        / (sl * snl * nSize);// 取右侧目前坐标值作为当前定义域上端
            }
            // y
            int ylength = oPoint.y;
            // 上侧
            for (int temp = sl; temp + space < oPoint.y; temp += sl) {
                if (temp % (sl * snl) == 0) {
                    g.drawString(temp / (sl * snl * nSize) + "", oPoint.x
  • 25,
                            ylength - temp + 5);
                    g.drawLine(oPoint.x + 3, ylength - temp, oPoint.x - 3, ylength
                            - temp);
                } else {
                    g.drawLine(oPoint.x + 1, ylength - temp, oPoint.x - 1, ylength
                            - temp);
                }
            }
            // 下侧
            for (int temp = sl; temp + space < this.getHeight() - oPoint.y - sl; temp += sl) {
                if (temp % (sl * snl) == 0) {
                    g.drawString("-" + temp / (sl * snl * nSize) + "",
                            oPoint.x - 25, ylength + temp + 5);
                    g.drawLine(oPoint.x + 3, ylength + temp, oPoint.x - 3, ylength
                            + temp);
                } else {
                    g.drawLine(oPoint.x + 1, ylength + temp, oPoint.x - 1, ylength
                            + temp);
                }
            }
        }

g2d.dispose();

    public void paintTheFunFlag(Graphics g) { // 画函数识别标记
        Iterator<FunctionExpression> iterator = expressionsList.iterator();
        int FstrLocationY = 10, FstrLocationX = 10;
        while (iterator.hasNext()) {
            FunctionExpression temFunctionExpression = iterator.next();
            if (FstrLocationY < this.getHeight() - 20) {
                g.setColor(temFunctionExpression.color);
                g.fillRect(FstrLocationX, FstrLocationY, 10, 2);
                g.drawString(temFunctionExpression.expression,
                        FstrLocationX + 13, FstrLocationY + 5);
                FstrLocationY += 10;
            }
        }
    }

  6、椭圆

    public void clearTheFunFlag(Graphics g) { // 覆盖掉之前的函数识别标记
        Iterator<FunctionExpression> iterator = expressionsList.iterator();
        int FstrLocationY = 10, FstrLocationX = 10;
        while (iterator.hasNext()) {
            FunctionExpression temFunctionExpression = iterator.next();
            if (FstrLocationY < this.getHeight() - 20) {
                g.setColor(Color.white);
                g.fillRect(FstrLocationX, FstrLocationY, 10, 2);
                g.drawString(temFunctionExpression.expression,
                        FstrLocationX + 13, FstrLocationY + 5);
                FstrLocationY += 10;
            }
        }
    }

// 1. 绘制一个圆: 圆的外切矩形 左上角坐标为(0, 0), 宽高为100
g2d.drawOval(0,0,100,100);
// 2. 填充一个椭圆
g2d.fillOval(120, 100, 100, 150);
g2d.dispose();

    public void paintAllFun(Graphics g) {
        Iterator<FunctionExpression> iterator = expressionsList.iterator();
        while (iterator.hasNext()) {
            FunctionExpression temFunctionExpression = iterator.next();
            try {
                paintOneFun(g, temFunctionExpression.expression,
                        temFunctionExpression.color);
            } catch (IllegalExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

  7、图片

    public void paintOneFun(Graphics g, String expression, Color color)
            throws IllegalExpressionException {// 画一个函数的图像
        // 计算点
        createFPoints(expression);
        // 描点连线
        paintFunction(g, color);
    }

String filepath = "C:/Users/LENOVO/Desktop/demo.jpg";
Image image = Toolkit.getDefaultToolkit().getImage(filepath);
g2d.drawImage(image,50,50,image.getWidth(this),image.getHeight(this),this);

    public void paintFunction(Graphics g, Color color) { // 描点连线
        FPoint temStart, temEnd;
        g.setColor(color);
        for (int i = 0; i < resultPoint.length - 1; i++) {
            temStart = resultPoint[i];
            // System.out.println("(" + temStart.x + " , " + temStart.y + ")");
            temEnd = resultPoint[i + 1];
            // System.out.println("(" + temStart.x + " , " + temStart.y + ")");
            if (temEnd != null && temStart != null&&temEnd.flag==1&&temStart.flag==1) {
                // if (pointInXoY(temEnd) || pointInXoY(temStart)) {
                // if (lineSizeFlag == 1) {
                g.drawLine(temStart.x, temStart.y, temEnd.x, temEnd.y);
                // }
                if (lineSizeFlag == 2) {// 把线条加粗
                    // g.drawLine(temStart.x - 1, temStart.y, temEnd.x,
                    // temEnd.y);
                    // g.drawLine(temStart.x, temStart.y, temEnd.x + 1,
                    // temEnd.y);
                    g.drawLine(temStart.x, temStart.y + 1, temEnd.x,
                            temEnd.y + 1);
                    g.drawLine(temStart.x + 1, temStart.y, temEnd.x + 1,
                            temEnd.y);
                    // g.drawLine(temStart.x, temStart.y-1, temEnd.x,
                    // temEnd.y-1);
                }
                // }
            }
        }
        resultPoint = null;// 内存留待回收
    }

g2d.dispose();

    public boolean pointInXoY(Point p) {// 点是否在坐标系里
        if (p.x >= 0 && p.y >= 0 && p.x <= getWidth() && p.y <= getHeight()) {
            return true;
        }
        return false;
    }

  8、文本

    public void createFPoints(String expression)
            throws IllegalExpressionException { // 生成一组点集
        setRange(expression);// 纠正定义域
        double xRange = xend

// 设置字体样式, null 表示使用默认字体, Font.PLAIN 为普通样式, 大小为 25px
g2d.setFont(new Font(null,Font.PLAIN,25));

  • xstart;// 域宽
            double oneStepL = xRange / pointsize;// 初始化步长
            resultPoint = new FPoint[(int) pointsize + 1];
            int index = 0;
            double temy;
            int temypie = Integer.MAX_VALUE;
            for (double i = xstart; i <= xend; i += oneStepL, index++) {
                temy = compute(i, expression);
                    if (!Double.isNaN(temy)) {
                    if (temy == Double.POSITIVE_INFINITY) {
                        resultPoint[index] = new FPoint(0,0,0);//加入一个无效点
                        index++;
                        temypie = Integer.MAX_VALUE;
                    } else if (temy == Double.NEGATIVE_INFINITY) {
                        temypie = Integer.MIN_VALUE;
                    } else {
                        temypie = (int) (temy * sl * snl * nSize);
                    }
                    resultPoint[index] = new FPoint((int) (i * sl * snl * nSize)
                            + oPoint.x, oPoint.y - temypie, 1);
                }
            }
        }

// 绘制文本, 其中坐标参数指的是文本绘制后的 左下角 的位置
// 首次绘制需要初始化字体, 可能需要较耗时
g2d.drawString("Hello world!",20,60);
g2d.drawString("你好,世界!",20,120);

    public void setRange(String expression) {
        if (expression.equals("logx") || expression.equals("lgx")) {
            if (rangeXstart <= 0) {
                xstart = 1.3e-300;
            }
            xend = rangeXend;
        } else if (expression.equals("cosh(x)") || expression.equals("sinh(x)")) {
            xstart = -1;
            xend = 1;
        } else {
            xstart = rangeXstart;
            xend = rangeXend;
        }
    }

 

    public double compute(double x, String expression)
            throws IllegalExpressionException {// 计算函数值
        double y = Double.NaN;// 初始值为nan
        y = readExPre.readExpression(new StringBuffer(expression), x);
        // double y = Math.pow(x, 5) + 2 * x * x;
        // double y=Math.tan(x);
        // double y=Math.sin(x)/x;
        // double y=-Math.sin(x)*x*x*9.8/2;
        // System.out.println("(x" + x + " , y" + y + ")");
        // System.out.println("(x" + (int)(x*sl*snl) + " , y" + (int)(y*sl*snl)
        // + ")");
        return y;
    }

  9、绘制太极图形

    public boolean moveToXY(double x, double y) {
        // 计算(x,y)的像素坐标
        double temx = x * sl * snl * nSize + oPoint.x, temy = oPoint.y - y * sl
                * snl * nSize;
        if (temx > Integer.MAX_VALUE || temy > Integer.MAX_VALUE
                || temx < Integer.MIN_VALUE || temy < Integer.MIN_VALUE) {
            return false;
        }
        int xpie = (int) (temx);
        int ypie = (int) (temy);
        // 移动原点
        oPoint.x -= xpie - getWidth() / 2;
        oPoint.y -= ypie - getHeight() / 2;
        repaint();
        return true;
    }

private void paintTAiJi(Graphics g){
mf.setTitle("太极");
Graphics2D g2d = (Graphics2D) g.create();

    public double getPointsize() {
        return pointsize;
    }

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    public void setPointsize(double pointsize) {
        this.pointsize = pointsize;
    }

Shape lefthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape righthalfCircle = new Ellipse2D.Double(10,10,300,300);
Shape innerCircle1 = new Ellipse2D.Double(85,10,150,150);
Shape innerCircle2 = new Ellipse2D.Double(85,160,150,150);

    public void addExp(String exp, Color color) {
        expressionsList.add(new FunctionExpression(exp, color));
    }

Shape rectangel1 = new Rectangle2D.Double(160,10,150,300);
Shape rectangle2 = new Rectangle2D.Double(10,10,150,300);

    public void removeExp(String exp) {
        Iterator<FunctionExpression> iterator = expressionsList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().expression.equals(exp)) {
                iterator.remove();
            }
        }
    }

Area left = new Area(lefthalfCircle);
Area right = new Area(righthalfCircle);

    public void paintOnePie(Graphics g, int x, int y) {
        g.setColor(Color.blue);
        g.drawOval(x - 5, y - 5, 10, 10);
        g.drawOval(x - 3, y - 3, 6, 6);
    }
}

Area area1 = new Area(rectangel1);
Area area2 = new Area(rectangle2);
//左半圆白色,右半圆黑色
left.subtract(area1);
right.subtract(area2);

class FunctionExpression {
    String expression;
    Color color;

Area inner1 = new Area(innerCircle1);
Area inner2 = new Area(innerCircle2);

    public FunctionExpression(String exp, Color color) {
        expression = exp;
        this.color = color;
    }
}

left.add(inner1);
right.add(inner2);

class FPoint {
    int x;
    int y;
    int flag = 1;// 有效点

right.subtract(inner1);
//小白圆
Shape minorWhiteCircle = new Ellipse2D.Double(150,70,20,20);
//小黑圆
Shape minorBalckCircle = new Ellipse2D.Double(150,230,20,20);

    public FPoint(int x, int y, int flag) {
        this.x = x;
        this.y = y;
        this.flag = flag;
    }
}

//
g2d.setPaint(Color.WHITE);
g2d.fill(left);
g2d.setPaint(Color.black);
g2d.fill(right);

  程序截图:图片 1

g2d.fill(minorWhiteCircle);
g2d.setPaint(Color.WHITE);
g2d.fill(minorBalckCircle);

这里是下载地址:

}

10、单一颜色填充

private void singleColor(Graphics g){
mf.setTitle("8.画笔颜色设置");
Graphics2D g2d = (Graphics2D) g.create();

g2d.setPaint(Color.blue);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

11、设置渐变色

private void setGradient(Graphics g){
mf.setTitle("9.渐变色填充");
Graphics2D g2d = (Graphics2D) g.create();

Color secondColor = new Color(99,153,255);
//水平方向填充
//GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,this.getWidth(),0,secondColor);
//竖直方向填充
GradientPaint hLinePaint = new GradientPaint(0,0,Color.BLACK,0,this.getHeight(),secondColor);
g2d.setPaint(hLinePaint);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());

}

12、圆周径向渐变颜色填充

private void radialGradientPaint(Graphics g){
mf.setTitle("10.圆周径向渐变颜色填充");
Graphics2D g2d = (Graphics2D) g.create();

float cx = this.getWidth()/2;
float cy = this.getHeight()/2;//(cx,cy为圆心坐标)
float radius = Math.min(cx,cy);//圆半径
float[] fractions = new float[]{0.1f,0.5f,1.0f};//色彩渐变关键帧位置
Color[] colors = new Color[]{Color.red,Color.GREEN,Color.black};//颜色数组
RadialGradientPaint rgp = new RadialGradientPaint(cx,cy,radius,fractions,colors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
g2d.setPaint(rgp);

g2d.fillRect(0,0,this.getWidth(),this.getHeight());
}

 

 

 

本文由10bet手机官网发布于微服架构,转载请注明出处:超经漂亮验证码,绘制函数图象小工具

上一篇:0泛型类的创建和使用,深度剖析C 下一篇:没有了
猜你喜欢
热门排行
精彩图文