fork download
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.function.DoubleUnaryOperator;
  5.  
  6. public class Main extends JFrame {
  7. private JComboBox<String> typeBox;
  8. private JTextField equationField;
  9. private JButton plotButton;
  10. private GraphPanel graphPanel;
  11.  
  12. public Main() {
  13. setTitle("Equation Plotter");
  14. setSize(800, 600);
  15. setDefaultCloseOperation(EXIT_ON_CLOSE);
  16. setLayout(new BorderLayout());
  17.  
  18. // --- Top Panel ---
  19. JPanel topPanel = new JPanel();
  20. topPanel.add(new JLabel("Equation Type:"));
  21. typeBox = new JComboBox<>(new String[]{
  22. "Linear", "Quadratic", "Trigonometric", "Exponential", "Custom"
  23. });
  24. topPanel.add(typeBox);
  25.  
  26. topPanel.add(new JLabel("Equation (use x):"));
  27. equationField = new JTextField("y = 2*x + 3", 20);
  28. topPanel.add(equationField);
  29.  
  30. plotButton = new JButton("Plot");
  31. topPanel.add(plotButton);
  32.  
  33. add(topPanel, BorderLayout.NORTH);
  34.  
  35. // --- Graph Panel ---
  36. graphPanel = new GraphPanel();
  37. add(graphPanel, BorderLayout.CENTER);
  38.  
  39. // --- Button Action ---
  40. plotButton.addActionListener(e -> {
  41. String eq = equationField.getText().replace("y=", "").replace(" ", "");
  42. graphPanel.setEquation(parseEquation(eq));
  43. graphPanel.repaint();
  44. });
  45. }
  46.  
  47. // --- Parse equation into a function ---
  48. private DoubleUnaryOperator parseEquation(String eq) {
  49. try {
  50. if (eq.contains("sin")) {
  51. return x -> Math.sin(evalReplace(eq, x));
  52. } else if (eq.contains("cos")) {
  53. return x -> Math.cos(evalReplace(eq, x));
  54. } else if (eq.contains("tan")) {
  55. return x -> Math.tan(evalReplace(eq, x));
  56. } else {
  57. return x -> evalReplace(eq, x);
  58. }
  59. } catch (Exception e) {
  60. JOptionPane.showMessageDialog(this, "Invalid Equation!", "Error", JOptionPane.ERROR_MESSAGE);
  61. return x -> 0;
  62. }
  63. }
  64.  
  65. // --- Evaluate expression safely ---
  66. private double evalReplace(String eq, double x) {
  67. try {
  68. String replaced = eq.replace("x", "(" + x + ")");
  69. return ((Number) new javax.script.ScriptEngineManager()
  70. .getEngineByName("JavaScript")
  71. .eval(replaced)).doubleValue();
  72. } catch (Exception e) {
  73. return 0;
  74. }
  75. }
  76.  
  77. // --- Graph Panel Class ---
  78. class GraphPanel extends JPanel {
  79. private DoubleUnaryOperator function = x -> 0;
  80.  
  81. void setEquation(DoubleUnaryOperator func) {
  82. this.function = func;
  83. }
  84.  
  85. protected void paintComponent(Graphics g) {
  86. super.paintComponent(g);
  87.  
  88. int w = getWidth();
  89. int h = getHeight();
  90.  
  91. g2.setColor(Color.WHITE);
  92. g2.fillRect(0, 0, w, h);
  93.  
  94. g2.setColor(Color.LIGHT_GRAY);
  95. for (int i = 0; i < w; i += 20)
  96. g2.drawLine(i, 0, i, h);
  97. for (int j = 0; j < h; j += 20)
  98. g2.drawLine(0, j, w, j);
  99.  
  100. g2.setColor(Color.BLACK);
  101. g2.setStroke(new BasicStroke(2));
  102. g2.drawLine(0, h / 2, w, h / 2); // X-axis
  103. g2.drawLine(w / 2, 0, w / 2, h); // Y-axis
  104.  
  105. g2.setColor(Color.RED);
  106. double scale = 40; // pixels per unit
  107.  
  108. for (int i = 1; i < w; i++) {
  109. double x1 = (i - w / 2.0) / scale;
  110. double x2 = (i + 1 - w / 2.0) / scale;
  111. double y1 = function.applyAsDouble(x1);
  112. double y2 = function.applyAsDouble(x2);
  113.  
  114. int px1 = i;
  115. int py1 = (int) (h / 2 - y1 * scale);
  116. int px2 = i + 1;
  117. int py2 = (int) (h / 2 - y2 * scale);
  118.  
  119. g2.drawLine(px1, py1, px2, py2);
  120. }
  121. }
  122. }
  123.  
  124. public static void main(String[] args) {
  125. SwingUtilities.invokeLater(() -> new Main().setVisible(true));
  126. }
  127. }
Success #stdin #stdout #stderr 0.28s 67208KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:197)
	at java.desktop/java.awt.Window.<init>(Window.java:538)
	at java.desktop/java.awt.Frame.<init>(Frame.java:423)
	at java.desktop/java.awt.Frame.<init>(Frame.java:388)
	at java.desktop/javax.swing.JFrame.<init>(JFrame.java:180)
	at Main.<init>(Main.java:12)
	at Main.lambda$main$6(Main.java:126)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)