/********************************************** * Swing application for polynomial graphs * * Created by: Vivian Synteta * * (synteta8@etu.unige.ch) * * Date: 12/06/2000 (updated 13/06/2000) * * For staf2x course (Daniel Schneider) * **********************************************/ import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class graph extends Applet implements ActionListener, ChangeListener { Plotter plotter; JTextField x_min, x_max; JTextField y_min, y_max; JLabel function; JSlider [] slider = new JSlider[3]; double [] coefs = new double[3]; public Component createComponents() { // define general panel JPanel pane = new JPanel(); pane.setBackground(Color.white); pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2)); pane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints (); // ------- components -------- // title JLabel title = new JLabel("Vivian's Polynomial Plotter"); title.setFont(new Font("Dialog",Font.BOLD,16)); title.setForeground(Color.red); // constraints for title gbc.insets = new Insets(5,5,5,5); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 5; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; // add title pane.add(title, gbc); // constraints for plotter plotter = new Plotter(); plotter.coefs = coefs; gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 5; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.CENTER; pane.add(plotter, gbc); // constraints for function // ----------------------------------- function = new JLabel("f(x) = 0"); function.setFont(new Font("Dialog",0,20)); function.setBorder(BorderFactory.createEmptyBorder(2,2,2,2)); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 5; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(function, gbc); //---------------------------------------- //---------------------------------------- // constraints for limits JLabel limits = new JLabel("LIMITS"); limits.setForeground(Color.red); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 2; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(limits, gbc); // constraints for label x_min JLabel labelmin = new JLabel("Xmin"); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(labelmin, gbc); // constraints for x_min x_min = new JTextField("-10"); plotter.x_min = -10; plotter.x_max = 10; x_min.addActionListener(this); gbc.gridx = 2; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(x_min, gbc); // constraints for label x_max JLabel labelmax = new JLabel("Xmax"); gbc.gridx = 3; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(labelmax, gbc); // constraints for x_max x_max = new JTextField("10"); x_max.addActionListener(this); gbc.gridx = 4; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(x_max, gbc); // constraints for label y_min labelmin = new JLabel("Ymin"); gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(labelmin, gbc); // constraints for y_min y_min = new JTextField("-10"); plotter.y_min = -10; plotter.y_max = 10; y_min.addActionListener(this); gbc.gridx = 2; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(y_min, gbc); // constraints for label y_max labelmax = new JLabel("Ymax"); gbc.gridx = 3; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(labelmax, gbc); // constraints for y_max y_max = new JTextField("10"); y_max.addActionListener(this); gbc.gridx = 4; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.WEST; pane.add(y_max, gbc); for (int i=0; i= 0.0) func = func + " + " + coefs[i]; else func = func + coefs[i]; if (i==coefs.length-1) break; func = func + " x^" + (coefs.length-i-1) + " "; } function.setText(func); } public void actionPerformed(ActionEvent event) { if (event.getSource() == x_min) { plotter.x_min = Double.valueOf(x_min.getText()).doubleValue(); plotter.repaint(); } if (event.getSource() == x_max) { plotter.x_max = Double.valueOf(x_max.getText()).doubleValue(); plotter.repaint(); } if (event.getSource() == y_min) { plotter.y_min = Double.valueOf(y_min.getText()).doubleValue(); plotter.repaint(); } if (event.getSource() == y_max) { plotter.y_max = Double.valueOf(y_max.getText()).doubleValue(); plotter.repaint(); } } // ----------- for the applet ---------------- // ------------------------------------------- public void stateChanged(ChangeEvent event) { for (int i=0; i