I had all of this program finished except for the plotting like a week ago...Ever since then, I've been trying to figure out how to plot the stupid polynomials..I thought I had it, but the program seems to go crazy whenever it's suppose to repaint..It's like repainting components on top of other panels and stuff..This program is in two seperate classes, it's pretty small...If you don't mind, see if you can't get this working properly, and make sure that I'm on the right track in trying to plot this thing.

Main Class

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class RoboPlot extends JFrame implements ChangeListener
{
	JSlider[] numberSlider1;
	
	JTabbedPane tabbedPane;
	
	int x4;
	int x3;
	int x2;
	int x1;
	int x0;
	
	ImagePlotter ip;
	
	
	public RoboPlot()
	{
		ip = new ImagePlotter();
		tabbedPane = new JTabbedPane();
		
		JPanel tab1Panel = new JPanel();
		tab1Panel.setBackground(Color.white);
		tabbedPane.addTab("Line 1", tab1Panel);
		
		int max = 20;
		int min = -20;
		int init = 0;
		
		numberSlider1 = new JSlider[5];
		JLabel[] lblCoef = new JLabel[5];
		int count = 4;
		for (int i=0; i<5;i++)
		{
			lblCoef[i] = new JLabel("     a(x)^" + count);
			count--;
			numberSlider1[i] = new JSlider(min, max, init);
			numberSlider1[i].setMajorTickSpacing(5);
			numberSlider1[i].setMinorTickSpacing(1);
			numberSlider1[i].setPaintLabels(true);
	        numberSlider1[i].setPaintTicks(true);
	        numberSlider1[i].setSnapToTicks(true);
	        numberSlider1[i].setBackground(Color.white);
	        numberSlider1[i].addChangeListener(this);
			tab1Panel.add(lblCoef[i]);
			
			tab1Panel.add(numberSlider1[i]);
		}		
	
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		setBackground(Color.white);
		pane.add(ip,BorderLayout.NORTH);
		pane.add(tabbedPane);
		setSize(300,620);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("RoboPlot");
		setContentPane(pane);
		setVisible(true);	
	
	}
	
	public void stateChanged(ChangeEvent e)
	{
		for (int i=0; i<numberSlider1.length; i++)
		{
			if (e.getSource() == numberSlider1[0])
			{
				x4 = numberSlider1[0].getValue();
				
				ip.x4 = x4;
				ip.fOfX(x4);
			}
			 else if (e.getSource() == numberSlider1[1])
			{
				x3 = numberSlider1[1].getValue();
				
				ip.x3 = x3;
				ip.fOfX(x3);
			}
			 else if (e.getSource() == numberSlider1[2])
			{
				x2 = numberSlider1[2].getValue();
				
				ip.x2 = x2;
				ip.fOfX(x2);	
			}
			 else if (e.getSource() == numberSlider1[3])
			{
				x1 = numberSlider1[3].getValue();
				
				ip.x1 = x1;
				ip.fOfX(x1);
			}
			 else if (e.getSource() == numberSlider1[4])
			{
				x0 = numberSlider1[4].getValue();
				
				ip.x0 = x0;
				ip.fOfX(x0);
			}
			
		}
		
	}
	public static void main(String[] args)
	{
		RoboPlot rp = new RoboPlot();
	}
}

External drawing class

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;

public class ImagePlotter extends JPanel
{
		Graphics2D g2d;
		
		int[] xPoints;
		int[] yPoints;
		
		int x4;
		int x3;
		int x2;
		int x1;
		int x0;
		
		boolean graph;
		
	public ImagePlotter()
	{
		setPreferredSize(new Dimension(300,300));
		
		xPoints = new int[301];
		yPoints = new int[301];
		
	}
	public void paintComponent(Graphics g)
	{
		 g2d = (Graphics2D)g;
	
		g2d.setColor(Color.red);
		g2d.drawLine(150,0,150,300);
		g2d.drawLine(0,150,300,150);
		
			
			if (graph)
			{
				for (int i=0; i<=300; i++)
				{
					xPoints[i] = i;
					yPoints[i] = fOfX(i);
					
					g2d.drawPolyline(xPoints,yPoints,xPoints.length);
					
					graph = false;
					
				}
			}
	}
	public int fOfX(int x)
	{
		int yCoord = x4*x^4 + x3*x^3 + x2*x^2 + x1^1 + x0^0;
		graph = true;
		repaint();
		
		return yCoord;
	}
}

Recommended Answers

All 3 Replies

well, i think i got the problem, the problem is you are thinking your origin is in the center of the cross you draw, but the origin is top-left corner of the screen, so you need to map your results to fit the screen, i don't use graphics stuff but it seems the problem...
and you need to clear the screen before repainting...
another program is x0,x1,x2,x3,x4 stuff, it will be much easy if you use an array.... you can get rid of the big if-else block in stateChange by using arrays....

public int fOfX(int x)
{
int yCoord = x4*x^4 + x3*x^3 + x2*x^2 + x1^1 + x0^0;
graph = true;
repaint();

return yCoord;
}

here fOfX calculates wrong, you need write like this,

int yCoord = x4*(x^4) + x3*(x^3) + x2*(x^2) + x1*(x^1) + x0*(x^0);

now, i am going to try to make a polynomial plotterby myself, so we can check our program better...

I messed up your code a bit, it has some mistakes but does the job you want...
i try to put some comments to make things clear.

ImagePlotter.java

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;

public class ImagePlotter extends JPanel
{
	private Graphics2D g2d;

	private int[] xPoints;
	private int[] yPoints;
	private int[] coeff;

	public ImagePlotter(int coeff)
	{
		setPreferredSize(new Dimension(300,300));

		this.xPoints = new int[301];
		this.yPoints = new int[301];
		this.coeff = new int[coeff];

	}
	public void paintComponent(Graphics g)
	{
	 	g2d = (Graphics2D)g;

		g2d.setColor(Color.red);
		g2d.drawLine(150,0,150,300);
		g2d.drawLine(0,150,300,150);

/*drawing of the polynomial starts here, but since numbers are really big you
need to give smaller numbers in order to make it sense.
also this is not a very good way to give static numbers like 150 and 300 here
because when you make it full screen, it looks very odd,
*/
		for (int i=0; i<=300; i++)
		{
//this commentted part doesn't work...
/*		xPoints[i] = i - 150;
		yPoints[i] = fOfX(i - 150);
		g2d.drawPolyline(xPoints,yPoints,xPoints.length);*/
			int y=fOfX(i-150);
			g2d.drawLine(i,150,i,150-y);
		}
	}

	public int fOfX(int x)
	{
		int yCoord = 0;
		for (int i=0; i<coeff.length;i++) {
			yCoord += coeff[i] * (int)Math.pow((double)x,(double)i);
		}
		return yCoord;
	}

//Coefficient Setter
	public void setCoeff(int coeffNumber,int value) {
		coeff[coeffNumber] = value;
	}
}

RoboPlot.java

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class RoboPlot extends JFrame implements ChangeListener
{
	JSlider[] numberSlider1;

	JTabbedPane tabbedPane;

	ImagePlotter ip;

	//our constant values
	static final int root = 5; //number of roots of the polynomial

	public RoboPlot()
	{
//creates an imagePlotter with roots
		ip = new ImagePlotter(root);
		tabbedPane = new JTabbedPane();

		JPanel tab1Panel = new JPanel();
		tab1Panel.setBackground(Color.white);
		tabbedPane.addTab("Line 1", tab1Panel);
/*you can make this max and min constant so you can use them outside the constructor
for checking or something like that.
*/
		int max = 20;
		int min = -20;
		int init = 0;

		numberSlider1 = new JSlider[root];
		JLabel[] lblCoef = new JLabel[root];
/*
your first numberSlider is your first coefficients and so on
*/
		for (int i=0; i<root;i++)
		{
			lblCoef[i] = new JLabel("     a(x)^" + i);
			numberSlider1[i] = new JSlider(min, max, init);
			numberSlider1[i].setMajorTickSpacing(5);
			numberSlider1[i].setMinorTickSpacing(1);
			numberSlider1[i].setPaintLabels(true);
			numberSlider1[i].setPaintTicks(true);
			numberSlider1[i].setSnapToTicks(true);
			numberSlider1[i].setBackground(Color.white);
			numberSlider1[i].addChangeListener(this);
			tab1Panel.add(lblCoef[i]);
			tab1Panel.add(numberSlider1[i]);
		}

		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		setBackground(Color.white);
		pane.add(ip,BorderLayout.NORTH);
		pane.add(tabbedPane);
		setSize(300,620);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("RoboPlot");
		setContentPane(pane);
		setVisible(true);

	}

	public void stateChanged(ChangeEvent e)
	{
		for (int i=0; i<root;i++) {
			if (e.getSource() == numberSlider1[i]) {
				//the change event fired from ith slider...
				//so change its value for the plotter..
				ip.setCoeff(i,numberSlider1[i].getValue());
			}
		}
		ip.repaint();
	}
	public static void main(String[] args)
	{
	RoboPlot rp = new RoboPlot();
	}
}

Thanks man, this program was a headache!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.