//Evan Warner //This class is the GUI and driver for the project import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JCheckBoxMenuItem; import javax.swing.JLabel; import javax.swing.JFormattedTextField; import javax.swing.ButtonGroup; import javax.swing.BorderFactory; import java.awt.Graphics; import java.awt.TextField; import java.awt.Image; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.text.NumberFormat; public class SchwarzChristoffel extends JPanel { private static JFrame frame; private JFrame windowframe; private static final int W=600, H=600; private static BufferedImage myImage, axesImage; private static Graphics myBuffer, axesBuffer; private Graph graph; private JMenuBar menubar; private JMenu file, view; private JMenuItem exit, window, zoomIn, zoomOut, normalWindow; private JCheckBoxMenuItem axesOn; private JLabel fequals, alphaequals, betaequals; private TextField functionline,alphaline,betaline; private static double xmin=-3, xmax=3, ymin=-3, ymax=3; private static ComplexFunction theFunction; private Complex a, b; private double alpha, beta; private boolean axeson=true, firstpt=true; private JPanel south; public SchwarzChristoffel() //Sets up GUI { theFunction = new ComplexFunction("z"); alpha = 0.0; beta = 0.0; a = new Complex(-1,1); b = new Complex(1,2); setLayout(new BorderLayout()); myImage = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB); myBuffer = myImage.createGraphics(); axesImage = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB); axesBuffer = axesImage.createGraphics(); axesBuffer.setColor(Color.white); axesBuffer.fillRect(0,0,W,H); graph = new Graph(); graph.addMouseListener(new MouseListener()); add(graph, BorderLayout.CENTER); south = new JPanel(); fequals = new JLabel("f = "); south.add(fequals); functionline = new TextField(20); functionline.addActionListener(new FunctionListener()); functionline.setText(theFunction.toString()); south.add(functionline); alphaequals = new JLabel(" alpha = "); south.add(alphaequals); alphaline = new TextField(4); alphaline.addActionListener(new AlphaListener()); alphaline.setText(""+alpha); south.add(alphaline); betaequals = new JLabel(" beta = "); south.add(betaequals); betaline = new TextField(4); betaline.addActionListener(new BetaListener()); betaline.setText(""+beta); south.add(betaline); add(south, BorderLayout.SOUTH); menubar = new JMenuBar(); file = new JMenu("File"); menubar.add(file); exit = new JMenuItem("Exit"); exit.addActionListener(new ExitListener()); file.add(exit); view = new JMenu("View"); menubar.add(view); zoomIn = new JMenuItem("Zoom in"); zoomIn.addActionListener(new ZoomInListener()); zoomOut = new JMenuItem("Zoom out"); zoomOut.addActionListener(new ZoomOutListener()); window = new JMenuItem("Set viewing window..."); window.addActionListener(new WindowListener()); normalWindow = new JMenuItem("Reset default settings"); normalWindow.addActionListener(new NormalWindowListener()); view.add(zoomIn); view.add(zoomOut); view.add(window); axesOn = new JCheckBoxMenuItem("Axes On"); axesOn.setState(true); axesOn.addActionListener(new AxesListener()); view.add(axesOn); view.add(normalWindow); add(menubar,BorderLayout.NORTH); drawaxes(); graph(); calculate(); } //Gets mouse click events and calculates path integrals in the complex plane private class MouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(firstpt) a = new Complex(convertToNormalX(e.getX()),convertToNormalY(e.getY())); else { b = new Complex(convertToNormalX(e.getX()),convertToNormalY(e.getY())); graph(); calculate(); } firstpt=!firstpt; } } //Clears graph surface public void clear() { myImage = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB); myBuffer = myImage.createGraphics(); repaint(); } //Draws path integral line on graph surface private void graph() { clear(); myBuffer.setColor(Color.blue.darker()); myBuffer.drawLine(convertToGraphX(a.real()),convertToGraphY(a.imag()),convertToGraphX(b.real()),convertToGraphY(b.imag())); repaint(); } //Calls other classes to calculate path integrals private void calculate() { GaussQuad gq = new GaussQuad(theFunction,alpha,beta,a,b); for(int i=1;i<10;i++) System.out.println("Answer is " + gq.integrate(i)); } //Gets the String written in the function line and creates a ComplexFunction private class FunctionListener implements ActionListener { public void actionPerformed(ActionEvent e) { functionline.selectAll(); theFunction = new ComplexFunction(functionline.getText()); functionline.setText(theFunction.toString()); } } //Gets the String written in the "alpha = " line and stores it as a double private class AlphaListener implements ActionListener { public void actionPerformed(ActionEvent e) { alphaline.selectAll(); try { double alpha = Double.parseDouble(alphaline.getText()); calculate(); } catch(NumberFormatException n) { alphaline.setText("!!SYNTAX_ERROR!!"); } } } //Gets the String written in the "beta = " line and stores it as a double private class BetaListener implements ActionListener { public void actionPerformed(ActionEvent e) { betaline.selectAll(); try { double beta = Double.parseDouble(betaline.getText()); calculate(); } catch(NumberFormatException n) { betaline.setText("!!SYNTAX_ERROR!!"); } } } //Draws the images on the central panel of the GUI private class Graph extends JPanel { public void paintComponent(Graphics g) { g.drawImage(axesImage, 0, 0, W, H, null); g.drawImage(myImage, 0, 0, W, H, null); } } //Responds to changes in the state of the "Axes On" radio button private class AxesListener implements ActionListener { public void actionPerformed(ActionEvent e) { axeson=axesOn.getState(); drawaxes(); } } //Draws the axes, using the current window parameters private void drawaxes() { axesImage = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB); axesBuffer = axesImage.createGraphics(); if(axeson) { axesBuffer.setColor(Color.red); axesBuffer.drawLine(convertToGraphX(0),convertToGraphY(ymin),convertToGraphX(0),convertToGraphY(ymax)); axesBuffer.drawLine(convertToGraphX(xmin),convertToGraphY(0),convertToGraphX(xmax),convertToGraphY(0)); double xsize=(xmax-xmin)/20; double ysize=(ymax-ymin)/20; for(double x = xmin+xsize; x<-xsize/2; x+=xsize) { axesBuffer.drawLine(convertToGraphX(x),convertToGraphY(0)+5,convertToGraphX(x),convertToGraphY(0)-5); int temp = (int)Math.round(x*10); double theX = ((double)temp)/10; axesBuffer.drawString(""+theX,convertToGraphX(x)-12,convertToGraphY(0)-6); } for(double x = xsize; x