• JAVA实现数学函数图像


    JAVA实现数学函数图像

    基础知识

    在java.lang包中有个public final Math类,包含了基本的数字操作,如指数、对数、平方根和三角函数。

    【Java的默认包是java.lang,即该包中的所有类不需要导包,不需要再写import,是默认导入的,其它包必须导入。Java就这么定义的,记住就可以了】。

      java.lang.Math类中包含E和PI两个静态常量,以及进行科学计算的类(static)方法,可以直接通过类名调用。

      public static final Double E = 2.7182818284590452354

      public static final Double PI = 3.14159265358979323846

      public static long abs(double x):传回 x 的绝对值。X也可int long float

      public static long sin(double x): 传回x径度的正弦函数值 

      public static long cos(double x):传回x径度的余弦函数值  

      public static long tan(double x): 传回x径度的正切函数值

      public static long asin(double x):传回x值的反正弦函数值。

      public static long acos(double x):传回x值的反余弦函数值。

      public static long atan(double x):传回x值的反正切函数值。

      public static long atan2(double x, double y):传回极坐标(polar)的θ值

      public static long floor(double x):传回不大于x的最大整数值

      public static long ceil(double x):传回不小于x的最小整数值。

      public static long exp(double x):传回相当于ex值

      public static long log(double x):传回x的自然对数函数值

      public static long max(double x,double y):传回x、y较大数

      public static long min(double x,double y):传回x、y较小数

      public static long pow(double x,double y):传回x的y次幂值

      public static long sqrt(double x): 传回x开平方值

      public static long rint(double x):传回最接近x的整数值

      public static long round(double x):传回x的四舍五入值

      public static long toDegrees(double angrad):传回将angrad径度转换成角度

      public static long toRadians(double angdeg): 传回将angdeg角度转换成径度

      public static long random():传回随机数值,产生一个0-1之间的随机数(不包括0和1)

    关于 Java图形用户界面 可参见 https://blog.csdn.net/cnds123/article/details/113251233

    下面给出多个示例代码,取自网络。

    一、绘制sin函数图像

    先看效果图

    源码文件DrawSin.java内容如下:

    1. import java.awt.Color;
    2. import java.awt.Graphics;
    3. import javax.swing.JFrame;
    4. public class DrawSin extends JFrame {
    5. private static int SCALE_X = 40; // X轴缩放倍数
    6. private static int SCALE_Y = 100; // Y轴缩放倍数
    7. private static int ORIGIN_X = 50; // 原点X
    8. private static int ORIGIN_Y = 0; // 原点Y
    9. private static int END_ARC = 360 * 2; // 画多长
    10. public void paint(Graphics g) {
    11. double ox = 0, oy = 0, x = 0, y = 0, arc = 0;
    12. super.paint(g);
    13. ORIGIN_Y = this.getHeight() / 2;
    14. // 画坐标轴
    15. g.drawLine(ORIGIN_X, ORIGIN_Y, this.getWidth(), ORIGIN_Y); // 横轴
    16. g.drawLine(ORIGIN_X, 0, ORIGIN_X, this.getHeight()); // 纵轴
    17. // 每90度画个标尺
    18. for (int i = 0; i < END_ARC; i += 90) {
    19. arc = Math.PI * i * 2 / 360;
    20. x = ORIGIN_X + arc * SCALE_X;
    21. g.drawLine((int) x, ORIGIN_Y - 10, (int) x, ORIGIN_Y + 10);
    22. }
    23. // 画正弦曲线
    24. g.setColor(Color.RED);
    25. for (int i = 0; i < END_ARC; i += 10) {
    26. arc = Math.PI * i * 2 / 360;
    27. x = ORIGIN_X + arc * SCALE_X;
    28. y = ORIGIN_Y + Math.sin(arc) * SCALE_Y;
    29. if (arc > 0) {
    30. g.drawLine((int) ox, (int) oy, (int) x, (int) y);
    31. }
    32. ox = x;
    33. oy = y;
    34. }
    35. }
    36. public static void main(String[] args) {
    37. DrawSin wnd = new DrawSin();
    38. wnd.setSize(600, 500);
    39. wnd.setVisible(true);
    40. }
    41. }

    二、画函数图像

    先看效果图

    源码文件DrawFn.java 内容如下:

    1. import java.awt.Color;
    2. import java.awt.Frame;
    3. import java.awt.Graphics;
    4. import java.awt.Graphics2D;
    5. import java.awt.GridLayout;
    6. import java.awt.event.ActionEvent;
    7. import java.awt.event.ActionListener;
    8. import java.awt.event.ItemEvent;
    9. import java.awt.event.ItemListener;
    10. import java.awt.geom.Line2D;
    11. import java.awt.geom.Point2D;
    12. import javax.swing.BorderFactory;
    13. import javax.swing.JButton;
    14. import javax.swing.JComboBox;
    15. import javax.swing.JFrame;
    16. import javax.swing.JLabel;
    17. import javax.swing.JPanel;
    18. import javax.swing.JTextField;
    19. import javax.swing.border.LineBorder;
    20. public class DrawFn extends JFrame implements ItemListener{
    21. private JTextField txt_c;
    22. private JTextField txt_b;
    23. private JTextField txt_a;
    24. public JComboBox chooseFun;
    25. //draw_actionAdapter adapter;
    26. public int A;
    27. public drawFnPanel panel = new drawFnPanel();
    28. public static void main(String[] args) {
    29. DrawFn frame=new DrawFn();
    30. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    31. frame.setSize(800, 600);
    32. frame.setLocationRelativeTo(null);
    33. frame.setVisible(true);
    34. frame.setBackground(Color.BLACK);
    35. }
    36. public DrawFn() {
    37. super("画函数图像");
    38. final JLabel aLabel = new JLabel();
    39. aLabel.setForeground(Color.WHITE);
    40. aLabel.setText("a=");
    41. aLabel.setBounds(650, 10, 21, 18); //设置位置,大小
    42. getContentPane().add(aLabel);
    43. txt_a = new JTextField();
    44. txt_a.setBounds(680, 8, 60, 18);
    45. getContentPane().add(txt_a);
    46. //
    47. final JLabel bLabel = new JLabel();
    48. bLabel.setForeground(Color.WHITE);
    49. bLabel.setText("b=");
    50. bLabel.setBounds(650, 30, 21, 18);
    51. getContentPane().add(bLabel);
    52. txt_b = new JTextField();
    53. txt_b.setBounds(680, 28, 60, 18);
    54. getContentPane().add(txt_b);
    55. //
    56. final JLabel cLabel = new JLabel();
    57. cLabel.setForeground(Color.WHITE);
    58. cLabel.setText("c=");
    59. cLabel.setBounds(650, 50, 32, 18);
    60. getContentPane().add(cLabel);
    61. //this.setContentPane(cLabel);
    62. txt_c = new JTextField();
    63. txt_c.setBounds(680, 48, 60, 18);
    64. getContentPane().add(txt_c);
    65. //this.setContentPane(txt_c);
    66. //设置按钮
    67. final JButton button = new JButton();
    68. button.addActionListener(new draw_actionAdapter(this));//什么意思?
    69. button.setText("绘制");
    70. button.setBounds(680, 80, 60, 18);
    71. getContentPane().add(button);
    72. //this.setContentPane(button);
    73. //定义下拉菜单
    74. final JLabel choose = new JLabel();
    75. choose.setForeground(Color.WHITE);
    76. choose.setText("请选择函数类型:");
    77. choose.setBounds(20, 5, 120, 20);
    78. getContentPane().add(choose);
    79. JPanel jp1=new JPanel();
    80. String []fun={"ax^2+bx+c","ae^bx+c","a*sin(PIx+b)+c","a*b^x+c","a*x^b+c","敬请期待"};
    81. chooseFun=new JComboBox(fun);
    82. chooseFun.setBounds(20, 30, 120, 20);
    83. jp1.add(chooseFun);
    84. chooseFun.setMaximumRowCount(3);
    85. getContentPane().add(chooseFun);
    86. chooseFun.addItemListener(this);
    87. getContentPane().add(panel);
    88. //this.setContentPane(panel);
    89. }
    90. public void itemStateChanged(ItemEvent e) {
    91. if(e.getStateChange()==e.SELECTED)
    92. {
    93. A=chooseFun.getSelectedIndex();
    94. //System.out.println(A);
    95. }
    96. }
    97. public void paintFn(ActionEvent e){
    98. panel.paintFn(Integer.parseInt(txt_a.getText()), Integer.parseInt(txt_b.getText()), Integer.parseInt(txt_c.getText()));
    99. }
    100. class draw_actionAdapter implements ActionListener{
    101. private DrawFn adapter;
    102. public draw_actionAdapter(DrawFn adapter){
    103. this.adapter=adapter;
    104. }
    105. public void actionPerformed(ActionEvent e){
    106. //adapter.getA(e);
    107. adapter.paintFn(e);
    108. adapter.repaint();
    109. }
    110. }
    111. class drawFnPanel extends JPanel{
    112. private float fa;
    113. private float fb;
    114. private float fc;
    115. private int UnitLength=100;//可以任意改变该像素值
    116. public void paintFn(int a,int b,int c){
    117. fa=a;
    118. fb=b;
    119. fc=c;
    120. }
    121. public double Fun(double x){
    122. //System.out.println("A="+DrawFn.A);
    123. if(A==0)
    124. return fa*x*x+fb*x+fc;
    125. else if(A==1)
    126. return fa*Math.pow(Math.E, fb*x)+fc ;//这里可以输入任何函数
    127. else if(A==2)
    128. return fa*Math.sin(Math.PI*x+fb)+fc;
    129. else if(A==3)
    130. return fa*Math.pow(fb, x)+fc;
    131. else if(A==4)
    132. return fa*Math.pow(x,fb)+fc;
    133. else
    134. return 0;
    135. }
    136. int width,height;
    137. int X,Y;
    138. //重载paintComponent函数
    139. public void paintComponent(Graphics g)
    140. {
    141. g.setColor(Color.BLACK);
    142. width = this.getWidth();//获得宽度
    143. height = this.getHeight();//获得高度
    144. X=width/2;
    145. Y=height/2;//获得原点坐标
    146. this.drawAxes(g);
    147. this.function(g);
    148. }
    149. //画坐标轴
    150. private void drawAxes(Graphics g)
    151. {
    152. g.setColor(Color.WHITE);
    153. g.drawLine(0, Y, width, Y);
    154. g.drawLine(X, 0, X, height);
    155. g.drawString("0",X + 2,Y +12); //画原点数字
    156. for(int i=1;i*UnitLength
    157. {
    158. g.drawLine(X+i*UnitLength,Y-1,X+i*UnitLength,Y-6);//画X轴正向的小竖线
    159. g.drawLine(X - i*UnitLength, Y-1, X - i*UnitLength, Y-6);//画X轴负向的小竖线
    160. g.drawString(String.valueOf(i), X + i*UnitLength-3, Y + 12); // x轴正向数字
    161. g.drawString(String.valueOf(i*-1), X - i*UnitLength-3, Y + 12); // x轴负向数字
    162. //画Y轴
    163. g.drawLine(X+1,Y+i*UnitLength,X+6,Y+i*UnitLength);
    164. g.drawLine(X+1,Y-i*UnitLength,X+6,Y-i*UnitLength);
    165. g.drawString(String.valueOf(i), X-12, Y - i*UnitLength-3);
    166. g.drawString(String.valueOf(i*-1), X-12, Y + i*UnitLength-3);
    167. }
    168. }
    169. //实现任意函数函数图像
    170. public void function(Graphics g1)
    171. {
    172. Point2D temp1,temp2;
    173. double x,y;//我们看到的坐标值
    174. Graphics2D g = (Graphics2D)g1;
    175. g.setColor(Color.WHITE);
    176. x = -1.0*X/UnitLength;
    177. //temp1返回面板的实际坐标值(以像素为单位)
    178. y = Fun(x);
    179. temp1 = new Point2D.Double(this.alterX(x * UnitLength), this.alterY(y * UnitLength));
    180. for(int i = 0 ; i < width; i++){
    181. x =x + 1.0/UnitLength;//前进一个像素
    182. y = Fun(x);
    183. if ( Math.abs(y) < Y){
    184. temp2 = new Point2D.Double(this.alterX(x * UnitLength), this.alterY(y * UnitLength));
    185. g.draw(new Line2D.Double(temp1, temp2));
    186. temp1 = temp2;
    187. }
    188. }
    189. //repaint();
    190. }
    191. //新坐标对应的原坐标
    192. private double alterX(double x){
    193. return x + X;
    194. }
    195. private double alterY(double y){
    196. return -1 *( y - Y);
    197. }
    198. }
    199. }

    三、平面直角坐标系函数图像

    源码文件UI.java和MyPanel.java组成

    效果图如下:

    提示:“输入函数”功能尚未实现!

    源码文件UI.java 内容如下

    1. package math;
    2. import java.awt.BorderLayout;
    3. import java.awt.Dimension;
    4. import java.awt.GridLayout;
    5. import java.awt.Toolkit;
    6. import javax.swing.ButtonGroup;
    7. import javax.swing.JButton;
    8. import javax.swing.JComboBox;
    9. import javax.swing.JFrame;
    10. import javax.swing.JLabel;
    11. import javax.swing.JPanel;
    12. import javax.swing.JRadioButton;
    13. import javax.swing.JTextField;
    14. public class UI extends JFrame
    15. {
    16. MyPanel mp;
    17. JPanel pl = new JPanel();
    18. JPanel pl1 = new JPanel(),
    19. pl2 = new JPanel(),
    20. pl3 = new JPanel(),
    21. pl4 = new JPanel();
    22. JRadioButton rb1,rb2;
    23. ButtonGroup bg = new ButtonGroup();
    24. JTextField tf = new JTextField(16);
    25. String[] s = {"y = sin(x)", "y = cos(x)", "y = tan(x)",
    26. "y = pow(x, 2)", "y = pow(x, 3)", "y = log(x)",
    27. "y = pow(2, x)", "y = sqrt(x)", "r = a(sita)"};
    28. JComboBox cb;
    29. JButton bn1 = new JButton("变宽"),
    30. bn2 = new JButton("变窄"),
    31. bn3 = new JButton("拉长"),
    32. bn4 = new JButton("压短"),
    33. bn = new JButton("绘图"),
    34. exit = new JButton("退出"),
    35. bn5 = new JButton("左移"),
    36. bn6 = new JButton("右移"),
    37. bn7 = new JButton("上移"),
    38. bn8 = new JButton("下移");
    39. public UI()
    40. {
    41. mp = new MyPanel(this);
    42. pl1.setLayout(new GridLayout(1, 2));
    43. pl2.setLayout(new GridLayout(1, 2));
    44. pl3.setLayout(new GridLayout(1, 2));
    45. pl4.setLayout(new GridLayout(1, 2));
    46. pl1.add(bn1); bn1.setEnabled(false);
    47. pl1.add(bn2); bn2.setEnabled(false);
    48. pl2.add(bn3); bn3.setEnabled(false);
    49. pl2.add(bn4); bn4.setEnabled(false);
    50. pl3.add(bn5); bn5.setEnabled(false);
    51. pl3.add(bn6); bn6.setEnabled(false);
    52. pl4.add(bn7); bn7.setEnabled(false);
    53. pl4.add(bn8); bn8.setEnabled(false);
    54. pl.setLayout(new GridLayout(20, 1));
    55. rb1 = new JRadioButton("输入函数");
    56. rb2 = new JRadioButton("选择已有函数");
    57. rb2.setSelected(true);
    58. tf.setEnabled(false);
    59. bg.add(rb1);
    60. bg.add(rb2);
    61. rb1.addActionListener(mp);
    62. rb2.addActionListener(mp);
    63. pl.add(rb1);
    64. pl.add(tf);
    65. pl.add(rb2);
    66. cb = new JComboBox(s);
    67. pl.add(cb);
    68. pl.add(new JLabel());
    69. pl.add(pl1); pl.add(pl2);
    70. pl.add(pl3); pl.add(pl4);
    71. pl.add(bn);
    72. pl.add(exit);
    73. bn1.addActionListener(mp);
    74. bn2.addActionListener(mp);
    75. bn3.addActionListener(mp);
    76. bn4.addActionListener(mp);
    77. bn5.addActionListener(mp);
    78. bn6.addActionListener(mp);
    79. bn7.addActionListener(mp);
    80. bn8.addActionListener(mp);
    81. bn.addActionListener(mp);
    82. exit.addActionListener(mp);
    83. this.setLayout(new BorderLayout());
    84. this.add(mp, BorderLayout.CENTER);
    85. this.add(pl, BorderLayout.EAST);
    86. this.setTitle("平面直角坐标系函数图像");
    87. this.setSize(797, 600 + 37);
    88. Dimension dn = Toolkit.getDefaultToolkit().getScreenSize();
    89. this.setLocation((dn.width - 797) / 2, (dn.height - 637) / 2);
    90. this.setVisible(true);
    91. this.setDefaultCloseOperation(3);
    92. }
    93. public static void main(String[] args)
    94. {
    95. new UI();
    96. }
    97. }

     源码文件MyPanel.java 内容如下

    1. package math;
    2. import java.awt.Color;
    3. import java.awt.Graphics;
    4. import java.awt.Graphics2D;
    5. import java.awt.Point;
    6. import java.awt.event.ActionEvent;
    7. import java.awt.event.ActionListener;
    8. import java.awt.event.MouseEvent;
    9. import java.awt.event.MouseMotionListener;
    10. import java.awt.geom.Ellipse2D;
    11. import java.awt.geom.Line2D;
    12. import javax.swing.JOptionPane;
    13. import javax.swing.JPanel;
    14. public class MyPanel extends JPanel implements ActionListener,MouseMotionListener
    15. {
    16. UI ui;
    17. int flag;
    18. double h_times;
    19. int w_times;
    20. int dx;
    21. int dy;
    22. String str;
    23. Point pt = new Point(0, 0);
    24. void init()
    25. {
    26. flag = -1;
    27. h_times = Math.PI / 100;
    28. w_times = 100;
    29. dx = 300;
    30. dy = 300;
    31. }
    32. public MyPanel(UI ui)
    33. {
    34. this.addMouseMotionListener(this);
    35. init();
    36. this.ui = ui;
    37. }
    38. public void paintComponent(Graphics g)
    39. {
    40. super.paintComponent(g);
    41. Graphics2D g2 = (Graphics2D)g;
    42. drawCoordinate(g2);
    43. Line2D line;
    44. g2.setColor(Color.BLUE);
    45. g2.drawString("(" + (pt.x - 300) + ", " + (300 - pt.y) + ")", pt.x + 20, pt.y + 20);
    46. switch(flag)
    47. {
    48. case 0:
    49. g2.drawString("y = Asin(Bx + C) + D", 105, 60);
    50. for(double i = 0; i < 600; i += 0.01)
    51. {
    52. line = new Line2D.Double(i, dy - Math.sin(getReal_X(i)) * w_times, i + 1, dy - Math.sin(getReal_X(i + 1)) * w_times);
    53. g2.draw(line);
    54. }
    55. break;
    56. case 1:
    57. g2.drawString("y = Acos(Bx + C) + D", 105, 60);
    58. for(double i = 0; i < 600; i += 0.01)
    59. {
    60. line = new Line2D.Double(i, dy - Math.cos(getReal_X(i)) * w_times, i + 1, dy - Math.cos(getReal_X(i + 1)) * w_times);
    61. g2.draw(line);
    62. }
    63. break;
    64. case 2:
    65. g2.drawString("y = Atan(Bx + C) + D", 105, 60);
    66. for(double i = 0; i < 600; i += 0.01)
    67. {
    68. line = new Line2D.Double(i, dy - Math.tan(getReal_X(i)) * w_times, i + 1, dy - Math.tan(getReal_X(i + 1)) * w_times);
    69. g2.draw(line);
    70. }
    71. break;
    72. case 3:
    73. g2.drawString("y = Apow(Bx + C, 2) + D", 105, 60);
    74. for(double i = 0; i < 600; i += 0.01)
    75. {
    76. line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 2) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 2) * w_times);
    77. g2.draw(line);
    78. }
    79. break;
    80. case 4:
    81. g2.drawString("y = Apow(Bx + C, 3) + D", 105, 60);
    82. for(double i = 0; i < 600; i += 0.01)
    83. {
    84. line = new Line2D.Double(i, dy - Math.pow(getReal_X(i), 3) * w_times, i + 1, dy - Math.pow(getReal_X(i + 1), 3) * w_times);
    85. g2.draw(line);
    86. }
    87. break;
    88. case 5:
    89. g2.drawString("y = Alog(Bx + C) + D", 105, 60);
    90. for(double i = 0; i < 600; i += 0.01)
    91. {
    92. line = new Line2D.Double(i, dy - Math.log(getReal_X(i)) * w_times, i + 1, dy - Math.log(getReal_X(i + 1)) * w_times);
    93. g2.draw(line);
    94. }
    95. break;
    96. case 6:
    97. g2.drawString("y = Apow(2, Bx + C) + D", 105, 60);
    98. for(double i = 0; i < 600; i += 0.01)
    99. {
    100. line = new Line2D.Double(i, dy - Math.pow(2, getReal_X(i)) * w_times, i + 1, dy - Math.pow(2, getReal_X(i + 1)) * w_times);
    101. g2.draw(line);
    102. }
    103. break;
    104. case 7:
    105. g2.drawString("y = Asqrt(Bx + C) + D", 105, 60);
    106. for(double i = 0; i < 600; i += 0.01)
    107. {
    108. line = new Line2D.Double(i, dy - Math.sqrt(getReal_X(i)) * w_times, i + 1, dy - Math.sqrt(getReal_X(i + 1)) * w_times);
    109. g2.draw(line);
    110. }
    111. break;
    112. case 8:
    113. g2.drawString("y = a(sita)", 105, 60);
    114. for(double i = 0; i < 600; i += 0.01)
    115. {
    116. line = new Line2D.Double(getReal_X(i) * Math.cos(getReal_X(i)), dy - getReal_X(i) * Math.sin(getReal_X(i)) * w_times, getReal_X(i) * Math.cos(getReal_X(i + 1)), dy - getReal_X(i) * Math.sin(getReal_X(i + 1)) * w_times);
    117. g2.draw(line);
    118. }
    119. break;
    120. }
    121. if(flag != -1)
    122. {
    123. g2.drawString("A = " + w_times, 105, 90);
    124. g2.drawString("B= " + h_times, 105, 120);
    125. g2.drawString("C= " + (300 - dx), 105, 150);
    126. g2.drawString("D= " + (300 - dy), 105, 180);
    127. }
    128. }
    129. private double getReal_X(double x)
    130. {
    131. return (x - dx) * h_times;
    132. }
    133. private void drawCoordinate(Graphics2D g2)
    134. {
    135. int len = 20;
    136. Line2D line;
    137. for(int i = 0; i <= 600 / len; i++)
    138. {
    139. g2.setColor(Color.PINK.darker());
    140. if(i == 300 / len)
    141. g2.setColor(Color.RED);
    142. else;
    143. line = new Line2D.Double(0, i * len, 600, i * len);
    144. g2.draw(line);
    145. line = new Line2D.Double(i * len, 0, i * len, 600);
    146. g2.draw(line);
    147. }
    148. drawPoint(g2, 300, 300);
    149. }
    150. private void drawPoint(Graphics2D g2, double x, double y)
    151. {
    152. g2.setColor(Color.YELLOW);
    153. Ellipse2D circle = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
    154. g2.fill(circle);
    155. }
    156. public void actionPerformed(ActionEvent e)
    157. {
    158. if(e.getSource() == ui.rb1)
    159. {
    160. ui.tf.setEnabled(true);
    161. ui.cb.setEnabled(false);
    162. flag = -1;
    163. }
    164. if(e.getSource() == ui.rb2)
    165. {
    166. ui.tf.setEnabled(false);
    167. ui.cb.setEnabled(true);
    168. }
    169. if(e.getSource() == ui.bn1)
    170. {
    171. h_times /= 1.1;
    172. }
    173. if(e.getSource() == ui.bn2)
    174. {
    175. h_times *= 1.1;
    176. }
    177. if(e.getSource() == ui.bn3)
    178. {
    179. // ui.bn4.setEnabled(true);
    180. w_times += 10;
    181. // if(w_times >= 300)
    182. // ui.bn3.setEnabled(false);
    183. }
    184. if(e.getSource() == ui.bn4)
    185. {
    186. // ui.bn3.setEnabled(true);
    187. w_times -= 10;
    188. // if(w_times <= 0)
    189. // ui.bn4.setEnabled(false);
    190. }
    191. if(e.getSource() == ui.bn5)
    192. {
    193. dx -= 10;
    194. }
    195. if(e.getSource() == ui.bn6)
    196. {
    197. dx += 10;
    198. }
    199. if(e.getSource() == ui.bn7)
    200. {
    201. // ui.bn8.setEnabled(true);
    202. dy -= 10;
    203. // if(dy <= 0)
    204. // ui.bn7.setEnabled(false);
    205. }
    206. if(e.getSource() == ui.bn8)
    207. {
    208. // ui.bn7.setEnabled(true);
    209. dy += 10;
    210. // if(dy >= 600)
    211. // ui.bn8.setEnabled(false);
    212. }
    213. if(e.getSource() == ui.bn)
    214. {
    215. if(ui.tf.isEnabled() == true)
    216. {
    217. str = ui.tf.getText();
    218. if(str == null || str.length() == 0)
    219. {
    220. ui.bn1.setEnabled(false);
    221. ui.bn2.setEnabled(false);
    222. ui.bn3.setEnabled(false);
    223. ui.bn4.setEnabled(false);
    224. ui.bn5.setEnabled(false);
    225. ui.bn6.setEnabled(false);
    226. ui.bn7.setEnabled(false);
    227. ui.bn8.setEnabled(false);
    228. JOptionPane.showMessageDialog(this, "请输入函数表达式 !");
    229. return;
    230. }else
    231. JOptionPane.showMessageDialog(this, "本功能尚未实现 !");
    232. }else
    233. flag = -2;
    234. ui.bn1.setEnabled(true);
    235. ui.bn2.setEnabled(true);
    236. ui.bn3.setEnabled(true);
    237. ui.bn4.setEnabled(true);
    238. ui.bn5.setEnabled(true);
    239. ui.bn6.setEnabled(true);
    240. ui.bn7.setEnabled(true);
    241. ui.bn8.setEnabled(true);
    242. init();
    243. if(ui.cb.isEnabled() == true)
    244. {
    245. flag = ui.cb.getSelectedIndex();
    246. }
    247. }
    248. if(e.getSource() == ui.exit)
    249. System.exit(0);
    250. repaint();
    251. }
    252. public void mouseDragged(MouseEvent arg0)
    253. {
    254. }
    255. public void mouseMoved(MouseEvent e)
    256. {
    257. pt = e.getPoint();
    258. repaint();
    259. }
    260. }

    OK! 

  • 相关阅读:
    PC商城开发
    /var/lib/docker/aufs/mnt/ 空间占用高问题解决\查看磁盘空间
    PHP:回退(Backed)枚举
    微电子领域常见概念(九)势垒层
    【PHY】3GPP UE能力类别的变化
    jmeter压测
    c语言第一个爱心程序
    【OpenCV 图像处理 Python版】图像处理的基本操作
    【Linux】进程与服务
    数据链路层(以太网帧与ARP协议)
  • 原文地址:https://blog.csdn.net/cnds123/article/details/127699945