Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Graphing/ GUI issues

  1. #1
    patman is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Graphing/ GUI issues

    So I am working on a graphing calculator for school. This may be a dumb question but i want to plot data from a array but i create the array in another class and it is giving me errors and i dont know what to do.

    Java Code:
    package graphing;
    
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import cp213.*;
    
    public class Input extends JFrame {
    
    	private static final ActionListener Listener = null;
    	private JPanel TPanel;
    	private JTextField Fun;
    	private JTextField Var;
    	private JTextField Start;
    	private JTextField End;
    	private int points = 100;
    	
    	public Input(){
    		Fun = new JTextField("Function", 12);
    		Var = new JTextField("Variable", 12);
    		Start = new JTextField("Start", 12);
    		End = new JTextField("End", 12);
    		ActionListener buttonListener = new ButtonListener();
    		JButton button = new JButton("Graph");
    		button.addActionListener(buttonListener);
    		TPanel = new JPanel(null);
    		TPanel.setLayout(new GridLayout(1, 5));
    		TPanel.add(Fun);
    		TPanel.add(Var);
    		TPanel.add(Start);
    		TPanel.add(End);
    		TPanel.add(button);
            add(TPanel);
    	}
    	
    	public double[] evaluate(Poly p, int s, int e){
    		double[] data;
    		data = new double[points];
    		double len = (e - s) / points;
    		double eval = s;
    		for (int i = 0; i < points; i++ ){
    			double temp = p.evalAt(eval);
    			data[i] = temp;
    			eval += len;
    		}
    		return data;
    	}
    	
    class ButtonListener implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e) {
    		try {
    			Poly p = new Poly(Fun.getText());
    			int start = Integer.parseInt(Start.getText());
    			int end = Integer.parseInt(End.getText());
    			double[] data = evaluate(p, start, end);
    			paintComponent(Graphics g, data)
    		} catch (PolyException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (OperatorException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    	}
    
    	
    }	
    }
    I get the errors in this class and nothing works

    Java Code:
    package graphing;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import cp213.*;
     
    public class GraphingPanel extends JPanel{
    	
    	private int points = 100;
    	
        public GraphingPanel(double[] data){
            setBounds(50, 50, 500, 400);
            setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
            setBackground(Color.WHITE);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            // Draw ordinate.
            g2.draw(new Line2D.Double(points, points, points, h-points));
            // Draw abcissa.
            g2.draw(new Line2D.Double(points, h-points, w-points, h-points));
            double xInc = (double)(w - 2*points)/(data.length-1);
            double scale = (double)(h - 2*points)/getMax(data);
            // Mark data points.
            g2.setPaint(Color.red);
            for(int i = 0; i < data.length; i++) {
                double x = points + i*xInc;
                double y = h - points - scale*data[i];
                g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
            }
        }
     
        private double getMax(double[] data) {
            double max = -Integer.MAX_VALUE;
            for(int i = 0; i < data.length; i++) {
                if(data[i] > max)
                    max = data[i];
            }
            return max;
        }
    }
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Graphing/ GUI issues

    If you have errors that you want us to help you with, it would be nice of you to post the error messages. We're pretty smart, but we can't see that which is not shown to us.
    DarrylBurke likes this.

Similar Threads

  1. New in Java Graphing
    By roquelexio in forum New To Java
    Replies: 2
    Last Post: 12-07-2011, 05:00 PM
  2. Need help graphing
    By rperalta in forum Java 2D
    Replies: 6
    Last Post: 09-24-2011, 06:59 PM
  3. Graphing GUI
    By spyroth in forum NetBeans
    Replies: 3
    Last Post: 02-12-2011, 03:52 AM
  4. Scientific Graphing
    By shruthi Nadiger in forum AWT / Swing
    Replies: 2
    Last Post: 03-15-2010, 12:37 AM
  5. Help with graphing problem
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •