java - How Can I Make My Code Better and Run Faster? -


i have created graphing program takes user inputted y or x intercept form equation , graphs matching line on grid. works, incredibly slow graph lines. fast before added code uses user's input, know inefficiency part. appreciate if take little time through code , give me suggestions/tips on how can make run faster.

this code main frame class:

package src;  import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.keyevent; import java.awt.geom.line2d; import java.text.decimalformat;  import javax.swing.abstractaction; import javax.swing.actionmap; import javax.swing.borderfactory; import javax.swing.inputmap; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.keystroke;  /*  * class responsible making program's frame  */  public class mainframe  {     public static int width = 1001;     public static int height = 1001;     public static string equation;     public static boolean drawline = false;     //public static string equation;      public static void main(string[] args)      {         jframe frame = new jframe();         equationconverter ec = new equationconverter();          //these jpanels put window         jpanel northpane = new jpanel();         jpanel southpane = new jpanel();         jpanel eastpane = new jpanel();         jpanel westpane = new jpanel();          color panelcolor = new color(230, 230, 230);         jtextfield textfield = new jtextfield();         //jtextpane textpane = new jtextpane();          //set window's layout         frame.setlayout(new borderlayout());          //create grid panel         @suppresswarnings("serial")         jpanel graphpane = new jpanel()         {             @override             protected void paintcomponent(graphics g)             {                 super.paintcomponent(g);                 graphics2d g2 = (graphics2d)g;                 setpreferredsize(new dimension(width, height));                 setbackground(color.white);                 g2.setcolor(color.light_gray);                  int x1 = 0;                 int y1 = 0;                 int x2 = 0;                 int y2 = height;                  //draw horizontal lines                 (int = 1; <= 20; i++)                 {                     g2.drawline(x1, y1, x2, y2);                     x1 += width/20;                     x2 = x1;                 }                 g2.drawline(width-1, 0, width-1, height);                  x1 = 0;                 y1 = 0;                 x2 = width;                 y2 = 0;                  //draw vertical lines                 (int = 1; <= 20; i++)                 {                     g2.drawline(x1, y1, x2, y2);                     y1 += height/20;                     y2 = y1;                 }                 g2.drawline(0, height-1, width, height-1);                  //make x , y axis lines bigger ,                 //a darker color grid lines                 g2.setstroke(new basicstroke(4));                 g2.setcolor(color.black);                 g2.drawline(500, 0, 500, 1001);                 g2.drawline(0, 500, 1001, 500);                  //draw numbers along y axis                 g2.setfont(new font("defualt", font.plain, 20));                 int sx = 475;                 int sy = 60;                 string si;                 (int = 9; >= -9; i--)                 {                     if (i != 0)                     {                         si = integer.tostring(i);                         g2.drawstring(si, sx, sy);                     }                     sy += 50;                 }                  sx = 40;                 sy = 525;                 //draw numbers along x axis                 (int = -9; <= 9; i++)                 {                     if (i != 0)                     {                         si = integer.tostring(i);                         g2.drawstring(si, sx, sy);                     }                     sx += 50;                 }                  if (drawline == true)                 {                     double x = 0;                     double y = 0;                     string newequation = "";                     string finalequation = "";                     decimalformat df = new decimalformat("#.####");                     g2.setcolor(color.red);                      equation = textfield.gettext();                     equation = equation.tolowercase().replace(" ", "");                     if (!equation.equals(""))                     {                         if (equation.charat(0) == 'y' && equation.charat(1) == '=')                         {                                equation = equation.substring(2);                             newequation = ec.editstr(equation, 'x');                               //draws lines using y intercept form                             (x = -10; x < 10; x+=0.0001)                             {                                 //y = (x);                                 finalequation = ec.replacex(newequation, df.format(x), 'x');                                 y = double.parsedouble(ec.solveequation(finalequation));                                 g2.draw(new line2d.double(width/2+50*x, height/2-50*y,                                             width/2+50*x, height/2-50*y));                             }                         }                         else if (equation.charat(0) == 'x' && equation.charat(1) == '=')                         {                             equation = equation.substring(2);                             newequation = ec.editstr(equation, 'y');                              //draws lines using x intercept form                             (y = -10; y < 10; y+=0.0001)                             {                                 //x = (y);                                 finalequation = ec.replacex(newequation, df.format(y), 'y');                                 x = double.parsedouble(ec.solveequation(finalequation));                                 g2.draw(new line2d.double(width/2+50*x, height/2-50*y,                                             width/2+50*x, height/2-50*y));                             }                         }                     }                 }                 else                 {                     drawline = false;                 }             }         };           //settings graph panel         frame.add(graphpane, borderlayout.center);             graphpane.setborder(borderfactory.createlineborder(color.black));           //settings north panel         frame.add(northpane, borderlayout.north);             northpane.setpreferredsize(new dimension(1500, 30));             northpane.setbackground(panelcolor);           //settings south panel         southpane.add(textfield);             textfield.setpreferredsize(new dimension(700, 80));             textfield.setfont(new font("default", font.plain, 60));             textfield.setborder(borderfactory.createlineborder(color.black));             textfield.addactionlistener(new actionlistener()             {                 public void actionperformed(actionevent e)                 {                     drawline = true;                     graphpane.repaint();                 }             });           //settings east panel         frame.add(eastpane, borderlayout.east);             westpane.setpreferredsize(new dimension(247, 1500));                 westpane.setbackground(panelcolor);           //settings west panel         frame.add(westpane, borderlayout.west);             eastpane.setpreferredsize(new dimension(247, 1500));             eastpane.setbackground(panelcolor);           //settings frame         frame.settitle("custom graphs");         frame.setsize(1501, 1501);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setlocationrelativeto(null);         frame.setresizable(false);         frame.setvisible(true);     } } 

and code class contains methods converting user's equation string computer can use graph line:

package src;  import javax.script.scriptengine; import javax.script.scriptenginemanager;  /*  * class converts user's equation use computer  */  public class equationconverter {     //this method adds multiplication signs @ necessary points in sting     protected string editstr(string str, char xory)     {         string newstr = str;         (int = 0; < str.length(); i++)         {             //adds multiplicative sign between variables , digits             if (i != 0 && str.charat(i) == xory && character.isdigit(str.charat(i-1)))             {                     newstr = str.substring(0, i) + "*" + str.substring(i);             }             //adds multiplicative sign between parentheses             else if (i != 0 && str.charat(i) == '(' && str.charat(i-1) == ')')             {                 newstr = str.substring(0, i) + "*" + str.substring(i);             }             str = newstr;         }         return newstr;     }      //this method replaces character x numeric value of x     protected string replacex(string str, string number, char xory)     {         string newstr = str;         (int = 0; < str.length(); i++)         {             if (i == 0 && str.charat(i) == xory)             {                 if (str.length() == 1)                 {                     newstr = number;                 }                 else                 {                     newstr = number + str.substring(i+1);                 }             }             else if (i != 0 && str.charat(i) == xory)             {                 //add parentheses there 2 minus signs                 if (i+1 == str.length())                 {                     newstr = str.substring(0, i) + "(" + number + ")";                 }                 else                 {                     newstr = str.substring(0, i) + "(" + number + ")" + str.substring(i+1);                 }             }             str = newstr;         }         return newstr;     }      //this method solves string equation , returns numeric value     protected string solveequation(string str)     {         scriptenginemanager mgr = new scriptenginemanager();         scriptengine engine = mgr.getenginebyname("javascript");         try         {             return engine.eval(str).tostring();         }          catch (exception e)          {             system.exit(0);             return "";         }     } } 


Comments