| | |
polynomials
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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
External drawing class
Main Class
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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; } }
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....
here fOfX calculates wrong, you need write like this,
now, i am going to try to make a polynomial plotterby myself, so we can check our program better...
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....
•
•
•
•
Java Syntax (Toggle Plain Text)
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; }
Java Syntax (Toggle Plain Text)
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
RoboPlot.java
i try to put some comments to make things clear.
ImagePlotter.java
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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(); } }
![]() |
Similar Threads
- Can anyone help me with adding 2 polynomials together? (C++)
- linked lists polynomials (C++)
- Polynomials (C++)
- help with polynomials (C++)
Other Threads in the Java Forum
- Previous Thread: PageFormat
- Next Thread: array required, but java.lang.String found
Views: 3091 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database detection draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object objects oracle oriented os panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






