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;
}
}