import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; /** * class EulerPanel. This class creates the JPanel that runs the EulersMethod.class, with text fields * for entering functions of F, timestep h, starting point x0, and final time limit T. There are also * buttons to zoom in and out of the graph created * * @author Allison Gooch - agooch3 */ public class EulerPanel extends JPanel { // fields and buttons private JButton startButton, zoomIn, zoomOut; private JTextField function1, function2, h, x0, T; // arraylists for use by EulersMethod.class public static ArrayList xResults1, yResults1, xResults2, yResults2, xResults3, yResults3; // instantiates intervals for use by zoom buttons private double intervalX = 10, intervalY = 10; // EulerPanel constructor instantiates and adds all buttons and fields public EulerPanel(){ setBackground(Color.white); setPreferredSize(new Dimension(600, 600)); // creates buttons and fields startButton = new JButton("Click to do Euler's Method"); zoomIn = new JButton("Zoom in"); zoomOut = new JButton("Zoom out"); function1 = new JTextField(); function2 = new JTextField(); h = new JTextField(); x0 = new JTextField(); T = new JTextField(); // creates arraylists xResults1 = new ArrayList(); yResults1 = new ArrayList(); xResults2 = new ArrayList(); yResults2 = new ArrayList(); xResults3 = new ArrayList(); yResults3 = new ArrayList(); // adds panel of zoom buttons JPanel p4 = new JPanel(); p4.setPreferredSize(new Dimension(600,30)); p4.setBackground(Color.white); p4.add(zoomIn); p4.add(zoomOut); // adds panel of labels for fields JPanel p3 = new JPanel(); p3.setPreferredSize(new Dimension(600,30)); p3.setBackground(Color.white); JLabel l1 = new JLabel("Enter f"); l1.setPreferredSize(new Dimension(150,30)); JLabel l2 = new JLabel("Enter g"); l2.setPreferredSize(new Dimension(150,30)); JLabel l3 = new JLabel("Timestep h"); l3.setPreferredSize(new Dimension(75,30)); JLabel l4 = new JLabel("x0 as 'x,y'"); l4.setPreferredSize(new Dimension(75,30)); JLabel l5 = new JLabel("Final time T"); l5.setPreferredSize(new Dimension(75,30)); p3.add(l1); p3.add(l2); p3.add(l3); p3.add(l4); p3.add(l5); // adds panel of fields JPanel p2 = new JPanel(); p2.setPreferredSize(new Dimension(600,30)); p2.setBackground(Color.white); function1.setPreferredSize(new Dimension(150,25)); function2.setPreferredSize(new Dimension(150,25)); h.setPreferredSize(new Dimension(75,25)); x0.setPreferredSize(new Dimension(75,25)); T.setPreferredSize(new Dimension(75,25)); p2.add(function1); p2.add(function2); p2.add(h); p2.add(x0); p2.add(T); // adds panel for start button JPanel p = new JPanel(); p.setPreferredSize(new Dimension(600,30)); p.setBackground(Color.white); p.add(startButton); // adds panels to main panel add(p3); add(p2); add(p); add(p4); // adds button listener to all buttons ButtonListener b = new ButtonListener(); startButton.addActionListener(b); function1.addActionListener(b); function2.addActionListener(b); h.addActionListener(b); x0.addActionListener(b); T.addActionListener(b); ZoomListener z = new ZoomListener(); zoomIn.addActionListener(z); zoomOut.addActionListener(z); } // end JacobiPanel constructor public void paintComponent (Graphics page){ super.paintComponent(page); // sets default page look Font font = new Font("Comic Sans", Font.BOLD, 12); page.setFont(font); page.setColor(Color.black); // draws key to the graph above graph page.drawString("Key: Red graph = h", 420, 155); page.drawString("Blue graph = h/10", 450, 170); page.drawString("Green graph = h/100", 450, 185); // draws graph axes and labels them page.drawLine(300, 190, 300, 590); page.drawLine(50, 390, 550, 390); page.drawString("X", 540, 405); page.drawString("Y", 305, 200); // creates reference variables for graphing int originX = 300; int originY = 390; // graphing algorithm uses interval and arraylist components to graph all graphs // red = h for (int i = 0; i < xResults1.size(); i++){ try { page.setColor(Color.red); page.drawLine((int)(Math.round(originX + xResults1.get(i)*intervalX)), (int)(Math.round(originY + (-1)*yResults1.get(i)*intervalY)), (int)(Math.round(originX + xResults1.get(i+1)*intervalX)), (int)(Math.round(originY + (-1)*yResults1.get(i+1)*intervalY))); } catch (Exception e){}; } // blue = h/10 for (int i = 0; i < xResults2.size(); i++){ try { page.setColor(Color.blue); page.drawLine((int)(Math.round(originX + xResults2.get(i)*intervalX)), (int)(Math.round(originY + (-1)*yResults2.get(i)*intervalY)), (int)(Math.round(originX + xResults2.get(i+1)*intervalX)), (int)(Math.round(originY + (-1)*yResults2.get(i+1)*intervalY))); } catch (Exception e){}; } // green = h/100 for (int i = 0; i < xResults3.size(); i++){ try { page.setColor(Color.green); page.drawLine((int)(Math.round(originX + xResults3.get(i)*intervalX)), (int)(Math.round(originY + (-1)*yResults3.get(i)*intervalY)), (int)(Math.round(originX + xResults3.get(i+1)*intervalX)), (int)(Math.round(originY + (-1)*yResults3.get(i+1)*intervalY))); } catch (Exception e){}; } } // end paintComponent private class ButtonListener implements ActionListener { String fInput, gInput, hInput, x0Input, TInput, xInput, yInput; double xVal, yVal; int commaLoc; public void actionPerformed (ActionEvent e){ // gets input from text fields once start button is clicked if (e.getSource() == startButton){ try { fInput = function1.getText(); gInput = function2.getText(); hInput = h.getText(); x0Input = x0.getText(); TInput = T.getText(); // parses x0 input into a double for x and y components try { commaLoc = x0Input.indexOf(","); xInput = x0Input.substring(0, commaLoc); yInput = x0Input.substring(commaLoc+1, x0Input.length()); xVal = Double.parseDouble(xInput); yVal = Double.parseDouble(yInput); } catch(Exception b){ x0.setText("Format error"); } // calls EulersMethod with the given input data new EulersMethod(fInput, gInput, hInput, xVal, yVal, TInput); repaint(); } catch(Exception c){ new EulersMethod("0", "0", "0", 0, 0, "0"); repaint(); } } } // end actionPerformed } // end ButtonListener private class ZoomListener implements ActionListener { public void actionPerformed (ActionEvent e){ // if zoomIn is clicked, make the interval 10 pixels larger then repaint if (e.getSource() == zoomIn){ intervalX = intervalX + 10; intervalY = intervalY + 10; repaint(); } // if zoomOut is clicked, make the interval 10 pixels smaller then repaint // doesn't go smaller than 10 pixels if (e.getSource() == zoomOut){ if (intervalX == 10 && intervalY ==10){ intervalX = 10; intervalY = 10; } else { intervalX = intervalX - 10; intervalY = intervalY - 10; } repaint(); } } // end actionPerformed } // end ZoomListener } // end EulerPanel