944,116 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5073
  • Java RSS
Feb 24th, 2005
0

polynomials

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4.  
  5. public class RoboPlot extends JFrame implements ChangeListener
  6. {
  7. JSlider[] numberSlider1;
  8.  
  9. JTabbedPane tabbedPane;
  10.  
  11. int x4;
  12. int x3;
  13. int x2;
  14. int x1;
  15. int x0;
  16.  
  17. ImagePlotter ip;
  18.  
  19.  
  20. public RoboPlot()
  21. {
  22. ip = new ImagePlotter();
  23. tabbedPane = new JTabbedPane();
  24.  
  25. JPanel tab1Panel = new JPanel();
  26. tab1Panel.setBackground(Color.white);
  27. tabbedPane.addTab("Line 1", tab1Panel);
  28.  
  29. int max = 20;
  30. int min = -20;
  31. int init = 0;
  32.  
  33. numberSlider1 = new JSlider[5];
  34. JLabel[] lblCoef = new JLabel[5];
  35. int count = 4;
  36. for (int i=0; i<5;i++)
  37. {
  38. lblCoef[i] = new JLabel(" a(x)^" + count);
  39. count--;
  40. numberSlider1[i] = new JSlider(min, max, init);
  41. numberSlider1[i].setMajorTickSpacing(5);
  42. numberSlider1[i].setMinorTickSpacing(1);
  43. numberSlider1[i].setPaintLabels(true);
  44. numberSlider1[i].setPaintTicks(true);
  45. numberSlider1[i].setSnapToTicks(true);
  46. numberSlider1[i].setBackground(Color.white);
  47. numberSlider1[i].addChangeListener(this);
  48. tab1Panel.add(lblCoef[i]);
  49.  
  50. tab1Panel.add(numberSlider1[i]);
  51. }
  52.  
  53. Container pane = getContentPane();
  54. pane.setLayout(new BorderLayout());
  55. setBackground(Color.white);
  56. pane.add(ip,BorderLayout.NORTH);
  57. pane.add(tabbedPane);
  58. setSize(300,620);
  59. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. setTitle("RoboPlot");
  61. setContentPane(pane);
  62. setVisible(true);
  63.  
  64. }
  65.  
  66. public void stateChanged(ChangeEvent e)
  67. {
  68. for (int i=0; i<numberSlider1.length; i++)
  69. {
  70. if (e.getSource() == numberSlider1[0])
  71. {
  72. x4 = numberSlider1[0].getValue();
  73.  
  74. ip.x4 = x4;
  75. ip.fOfX(x4);
  76. }
  77. else if (e.getSource() == numberSlider1[1])
  78. {
  79. x3 = numberSlider1[1].getValue();
  80.  
  81. ip.x3 = x3;
  82. ip.fOfX(x3);
  83. }
  84. else if (e.getSource() == numberSlider1[2])
  85. {
  86. x2 = numberSlider1[2].getValue();
  87.  
  88. ip.x2 = x2;
  89. ip.fOfX(x2);
  90. }
  91. else if (e.getSource() == numberSlider1[3])
  92. {
  93. x1 = numberSlider1[3].getValue();
  94.  
  95. ip.x1 = x1;
  96. ip.fOfX(x1);
  97. }
  98. else if (e.getSource() == numberSlider1[4])
  99. {
  100. x0 = numberSlider1[4].getValue();
  101.  
  102. ip.x0 = x0;
  103. ip.fOfX(x0);
  104. }
  105.  
  106. }
  107.  
  108. }
  109. public static void main(String[] args)
  110. {
  111. RoboPlot rp = new RoboPlot();
  112. }
  113. }

External drawing class
Java Syntax (Toggle Plain Text)
  1. import java.awt.image.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4.  
  5. public class ImagePlotter extends JPanel
  6. {
  7. Graphics2D g2d;
  8.  
  9. int[] xPoints;
  10. int[] yPoints;
  11.  
  12. int x4;
  13. int x3;
  14. int x2;
  15. int x1;
  16. int x0;
  17.  
  18. boolean graph;
  19.  
  20. public ImagePlotter()
  21. {
  22. setPreferredSize(new Dimension(300,300));
  23.  
  24. xPoints = new int[301];
  25. yPoints = new int[301];
  26.  
  27. }
  28. public void paintComponent(Graphics g)
  29. {
  30. g2d = (Graphics2D)g;
  31.  
  32. g2d.setColor(Color.red);
  33. g2d.drawLine(150,0,150,300);
  34. g2d.drawLine(0,150,300,150);
  35.  
  36.  
  37. if (graph)
  38. {
  39. for (int i=0; i<=300; i++)
  40. {
  41. xPoints[i] = i;
  42. yPoints[i] = fOfX(i);
  43.  
  44. g2d.drawPolyline(xPoints,yPoints,xPoints.length);
  45.  
  46. graph = false;
  47.  
  48. }
  49. }
  50. }
  51. public int fOfX(int x)
  52. {
  53. int yCoord = x4*x^4 + x3*x^3 + x2*x^2 + x1^1 + x0^0;
  54. graph = true;
  55. repaint();
  56.  
  57. return yCoord;
  58. }
  59. }
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 25th, 2005
0

Re: polynomials

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....
Quote ...
Java Syntax (Toggle Plain Text)
  1. public int fOfX(int x)
  2. {
  3. int yCoord = x4*x^4 + x3*x^3 + x2*x^2 + x1^1 + x0^0;
  4. graph = true;
  5. repaint();
  6.  
  7. return yCoord;
  8. }
here fOfX calculates wrong, you need write like this,

Java Syntax (Toggle Plain Text)
  1. 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...
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Feb 25th, 2005
0

Re: polynomials

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
Java Syntax (Toggle Plain Text)
  1. import java.awt.image.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4.  
  5. public class ImagePlotter extends JPanel
  6. {
  7. private Graphics2D g2d;
  8.  
  9. private int[] xPoints;
  10. private int[] yPoints;
  11. private int[] coeff;
  12.  
  13. public ImagePlotter(int coeff)
  14. {
  15. setPreferredSize(new Dimension(300,300));
  16.  
  17. this.xPoints = new int[301];
  18. this.yPoints = new int[301];
  19. this.coeff = new int[coeff];
  20.  
  21. }
  22. public void paintComponent(Graphics g)
  23. {
  24. g2d = (Graphics2D)g;
  25.  
  26. g2d.setColor(Color.red);
  27. g2d.drawLine(150,0,150,300);
  28. g2d.drawLine(0,150,300,150);
  29.  
  30. /*drawing of the polynomial starts here, but since numbers are really big you
  31. need to give smaller numbers in order to make it sense.
  32. also this is not a very good way to give static numbers like 150 and 300 here
  33. because when you make it full screen, it looks very odd,
  34. */
  35. for (int i=0; i<=300; i++)
  36. {
  37. //this commentted part doesn't work...
  38. /* xPoints[i] = i - 150;
  39. yPoints[i] = fOfX(i - 150);
  40. g2d.drawPolyline(xPoints,yPoints,xPoints.length);*/
  41. int y=fOfX(i-150);
  42. g2d.drawLine(i,150,i,150-y);
  43. }
  44. }
  45.  
  46. public int fOfX(int x)
  47. {
  48. int yCoord = 0;
  49. for (int i=0; i<coeff.length;i++) {
  50. yCoord += coeff[i] * (int)Math.pow((double)x,(double)i);
  51. }
  52. return yCoord;
  53. }
  54.  
  55. //Coefficient Setter
  56. public void setCoeff(int coeffNumber,int value) {
  57. coeff[coeffNumber] = value;
  58. }
  59. }

RoboPlot.java
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4.  
  5. public class RoboPlot extends JFrame implements ChangeListener
  6. {
  7. JSlider[] numberSlider1;
  8.  
  9. JTabbedPane tabbedPane;
  10.  
  11. ImagePlotter ip;
  12.  
  13. //our constant values
  14. static final int root = 5; //number of roots of the polynomial
  15.  
  16. public RoboPlot()
  17. {
  18. //creates an imagePlotter with roots
  19. ip = new ImagePlotter(root);
  20. tabbedPane = new JTabbedPane();
  21.  
  22. JPanel tab1Panel = new JPanel();
  23. tab1Panel.setBackground(Color.white);
  24. tabbedPane.addTab("Line 1", tab1Panel);
  25. /*you can make this max and min constant so you can use them outside the constructor
  26. for checking or something like that.
  27. */
  28. int max = 20;
  29. int min = -20;
  30. int init = 0;
  31.  
  32. numberSlider1 = new JSlider[root];
  33. JLabel[] lblCoef = new JLabel[root];
  34. /*
  35. your first numberSlider is your first coefficients and so on
  36. */
  37. for (int i=0; i<root;i++)
  38. {
  39. lblCoef[i] = new JLabel(" a(x)^" + i);
  40. numberSlider1[i] = new JSlider(min, max, init);
  41. numberSlider1[i].setMajorTickSpacing(5);
  42. numberSlider1[i].setMinorTickSpacing(1);
  43. numberSlider1[i].setPaintLabels(true);
  44. numberSlider1[i].setPaintTicks(true);
  45. numberSlider1[i].setSnapToTicks(true);
  46. numberSlider1[i].setBackground(Color.white);
  47. numberSlider1[i].addChangeListener(this);
  48. tab1Panel.add(lblCoef[i]);
  49. tab1Panel.add(numberSlider1[i]);
  50. }
  51.  
  52. Container pane = getContentPane();
  53. pane.setLayout(new BorderLayout());
  54. setBackground(Color.white);
  55. pane.add(ip,BorderLayout.NORTH);
  56. pane.add(tabbedPane);
  57. setSize(300,620);
  58. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. setTitle("RoboPlot");
  60. setContentPane(pane);
  61. setVisible(true);
  62.  
  63. }
  64.  
  65. public void stateChanged(ChangeEvent e)
  66. {
  67. for (int i=0; i<root;i++) {
  68. if (e.getSource() == numberSlider1[i]) {
  69. //the change event fired from ith slider...
  70. //so change its value for the plotter..
  71. ip.setCoeff(i,numberSlider1[i].getValue());
  72. }
  73. }
  74. ip.repaint();
  75. }
  76. public static void main(String[] args)
  77. {
  78. RoboPlot rp = new RoboPlot();
  79. }
  80. }
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Feb 25th, 2005
0

Re: polynomials

Thanks man, this program was a headache!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: PageFormat
Next Thread in Java Forum Timeline: array required, but java.lang.String found





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC