Results 1 to 3 of 3
Thread: Problems with graph plotting.
- 02-05-2009, 06:05 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
Problems with graph plotting.
Hi, first of all i am new in this forum so Hello everybody and thank you very much. I am new in Java. Ive been working with a java gui that plots a function, however i cannot make it plot the axis properly. I set the axis size to vary depending on the value of the functions. However since the Panel for the graph is in a separate class that has no connection with the class where the text field are, i cannot get this and use them to define the plot. As you can see I am using Hyperthread's code for for plotting (the one i modified from the one the teacher gave us is commented). Hyperthread is a senior in this forum. But having some troubles to implement it in my code. The program computes the integral of the given function using the trapezoid method and the Simpson method. Hope this will also help other people with the same problems. I will greatly appreciate if you can take a little look at my code. Thank you very much
now the second part that deals with the plottingJava Code:import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.geom.*; import java.util.Random; import javax.swing.*; public class Main{ public static void main (String[]args){ Frame4 frame=new Frame4(); frame.setDefaultCloseOperation(Frame4.EXIT_ON_CLOSE); frame.setVisible(true); } } class Frame4 extends JFrame{ /*Declare components in Panel*/ JPanel contentPane = new JPanel(); MyPanel myPanel1 = new MyPanel(); //ctg JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JLabel jLabel7 = new JLabel(); JLabel jLabel8 = new JLabel(); JLabel jLabel9 = new JLabel(); JLabel jLabel10= new JLabel(); JLabel jLabel11= new JLabel(); JLabel jLabel12= new JLabel(); JTextField jTextField1 = new JTextField(); JTextField jTextField2 = new JTextField(); JTextField jTextField3 = new JTextField(); JTextField jTextField4 = new JTextField(); JTextField jTextField5 = new JTextField(); JTextField jTextField6 = new JTextField(); JTextField jTextField7 = new JTextField(); JTextField jTextField8 = new JTextField(); JTextField jTextField9 = new JTextField(); JTextField jTextField10= new JTextField(); JTextField jTextField11= new JTextField(); JTextField jTextField12= new JTextField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); /*Construction of Frame*/ public Frame4(){ contentPane=(JPanel)this.getContentPane(); contentPane.setLayout(null); this.setSize(1080,720); //size of Frame this.setTitle("Final Assignment 1: Trapezoid and Simpson"); jLabel1.setText("f(x)=K1sin(Ax)+ K2sin(Bx)+ K3"); jLabel1.setBounds(20,10,200,20); jLabel2.setText("_________ "); jLabel2.setBounds(170,15,70,15); jLabel3.setText(" C^2+x^2 "); jLabel3.setBounds(170,25,70,20); jLabel4.setText("Interval:[xa,xb]"); jLabel4.setBounds(500,10,120,20); jLabel5.setText("Input Parameters"); jLabel5.setBounds(800,130,130,20); jLabel6.setText("xa xb"); jLabel6.setBounds(800,160,100,20); jLabel7.setText("K1 K2"); jLabel7.setBounds(800,190,100,20); jLabel8.setText("K3 C"); jLabel8.setBounds(800,220,100,20); jLabel9.setText("A B"); jLabel9.setBounds(800,250,100,20); jTextField1.setBounds(820,160,30,20); jTextField2.setBounds(880,160,30,20); jTextField3.setBounds(820,190,30,20); jTextField4.setBounds(880,190,30,20); jTextField5.setBounds(820,220,30,20); jTextField6.setBounds(880,220,30,20); jTextField7.setBounds(820,250,30,20); jTextField8.setBounds(880,250,30,20); jTextField9.setBounds(880,280,30,20); /*Setting Button2*/ //////////////////////////////////// /*Setting a Button1*/ jButton1.setText("="); jButton1.setBounds(100,505,50,30); jButton1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ /*Start to calculate points for graphing*/ //variables required for the function double xa = Double.parseDouble(jTextField1.getText()); double xb = Double.parseDouble(jTextField2.getText()); double K1 = Double.parseDouble(jTextField3.getText()); double K2 = Double.parseDouble(jTextField4.getText()); double K3 = Double.parseDouble(jTextField5.getText()); double C = Double.parseDouble(jTextField6.getText()); double A = Double.parseDouble(jTextField7.getText()); double B = Double.parseDouble(jTextField8.getText()); double x = Double.parseDouble(jTextField9.getText()); //variables required for visualizing the graph window and elemnts. double upper= xb; //start to calculate points double lower = xa; int numinterval=500; double h= (upper-lower)/numinterval; // Simpson Srule = new Simpson(); Trapezoid Trap = new Trapezoid(); double S = Srule.integrate(xa,xb,numinterval,K1, K2, K3, A, B, C); double T = Trap.Trapz( xa, xb, numinterval, K1, K2, K3, A, B, C); double fx = K1*Math.sin(A*x)+K2*Math.cos(B*x)+K3/(C*C+x*x); String longResult = new Double(fx).toString(); String longSimpson = new Double(S).toString(); String longTrapez = new Double(T).toString(); String finalResult = longResult; String finalSimpson = longSimpson; String finalTrapez = longTrapez; jLabel10.setText(finalResult); jLabel10.setBounds(500, 600, 100, 20); jLabel11.setText(finalSimpson); jLabel11.setBounds(500,620,100,20); jLabel12.setText(finalTrapez); jLabel12.setBounds(500,640,100,20); FuncVal funcval1 =new FuncVal(); //Curve generator double resultFunc[]=funcval1.calculate(upper,lower,numinterval, K1, K2, K3, A, B, C); myPanel1.x1 =lower; myPanel1.x2=upper; myPanel1.n=numinterval; myPanel1.h = h; myPanel1.resultFunc =resultFunc; myPanel1.setBackground(Color.yellow); myPanel1.setBounds(new Rectangle (25,45,700,350)); //size of panel canvas contentPane.add(myPanel1,null); //ctg.add in action myPanel1.updateUI(); //regraph } }); /*Adding domponents to JPanel*/ contentPane.add(jLabel1,null); contentPane.add(jLabel2,null); contentPane.add(jLabel3,null); contentPane.add(jLabel4,null); contentPane.add(jLabel5,null); contentPane.add(jLabel6,null); contentPane.add(jLabel7,null); contentPane.add(jLabel8,null); contentPane.add(jLabel9,null); contentPane.add(jLabel10,null); contentPane.add(jLabel11,null); contentPane.add(jLabel12,null); contentPane.add(jTextField1,null); contentPane.add(jTextField2,null); contentPane.add(jTextField3,null); contentPane.add(jTextField4,null); contentPane.add(jTextField5,null); contentPane.add(jTextField6,null); contentPane.add(jTextField7,null); contentPane.add(jTextField8,null); contentPane.add(jTextField9,null); contentPane.add(jButton1,null); }//end of method Frame4
This are the classes with all the methods for trapezoid and Simpson althought I still have to work it out a little bit more.Java Code:public class MyPanel extends JPanel{ public double x2,x1,h; public int n; public double []resultFunc; public int[] xPoints = new int[2]; public int[] yPoints = new int[2]; double[] x; double[] y; /** DIDNT WORK< WANTED TO TAKE THE VALUES FROM THE TEXTFIELDS BUT COULDNT MAKE IT // double xMin=Double.parseDouble(jTextField1.getText()); // double x = Double.parseDouble(jTextField9.getText()); // double yMin=K1*Math.sin(A*xMin)+K2*Math.cos(B*xMin)+K3/(C*C+xMin*xMin); // double yMax=K1*Math.sin(A*xMax)+K2*Math.cos(B*xMax)+K3/(C*C+xMax*xMax); // FuncVal yMin=new FuncVal(); // FuncVal left_side = new FuncVal(); // FuncVal middle = new FuncVal(); // FuncVal rite_side = new FuncVal(); // double leftSide = left_side.value( K1,A,K2,B,K3,C,x_lo); //double rightSide = rite_side.value(K1, A, K2, B, K3, C, x_hi); // double mid = middle.value(K1, A, K2, B, K3, C, x_md); */ final int PAD = 20; final boolean DEBUG = false; boolean firstTime; // Set at end of setData method. protected void paintComponent(Graphics g){ super.paintComponent(g); //painting background /**This was the previous code. It worked but as I wanted. g.setColor(Color.black); g.drawLine(40,20,40,320); //Draw Y axis g.drawLine(40, 200, 600, 200); //X axis g.drawString("f(x)",20,20); //Label of X g.drawString("x", 610, 200); //Label of Y g.drawString("0",30,220); //origin g.drawString(" "+x2, 440, 220); //mark "x2" **/ Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h1 = getHeight(); double xScale = (w - 2*PAD)/(xMax - xMin); double yScale = (h1 - 2*PAD)/(yMax - yMin); if(firstTime) System.out.printf("xScale = %.1f yScale = %.1f%n", xScale, yScale); Point2D.Double origin = new Point2D.Double(); // Axes origin. Point2D.Double offset = new Point2D.Double(); // Locate data. if(xMax < 0) { origin.x = w - PAD; offset.x = origin.x - xScale*xMax; } else if(xMin < 0) { origin.x = PAD - xScale*xMin; offset.x = origin.x; } else { origin.x = PAD; offset.x = PAD - xScale*xMin; } if(yMax < 0) { origin.y = h1 - PAD; offset.y = origin.y - yScale*yMax; } else if(yMin < 0) { origin.y = PAD - yScale*yMin; offset.y = origin.y; } else { offset.y = PAD - yScale*yMin; } if(firstTime) { System.out.printf("origin = [%6.1f, %6.1f]%n", origin.x, origin.y); System.out.printf("offset = [%6.1f, %6.1f]%n", offset.x, offset.y); } origin.y = PAD; // Draw abcissa. g2.draw(new Line2D.Double(PAD, origin.y, w-PAD, origin.y)); // Draw ordinate. g2.draw(new Line2D.Double(origin.x, PAD, origin.x, h1-PAD)); g2.setPaint(Color.red); // Mark origin. g2.fill(new Ellipse2D.Double(origin.x-2, origin.y-2, 4, 4)); for(int i=0;i<n;i++){ //Notice:convert of plots positions and function values xPoints [0]= (int)(40+(x1+(i*h1*400/(x2-x1)))); xPoints [1]= (int)(40+(x1+(i+1)*h1*400/(x2-x1))); yPoints [1]= (int)(200-resultFunc[i+1]*200/2); yPoints [0]= (int)(200-resultFunc[i]*200/2); g.setColor(Color.red); g.drawLine(xPoints[0], yPoints[0], xPoints[1], yPoints[1]); } } //paint method ends here } //MyPanel class ends here } //Frame 4 class ends here
ThanksJava Code:class FuncVal { /**list of variables x2: upper limit; x1: lower limit; n : number of interval; h : width of interval (x2-x1)/n*/ double h; public double [] calculate (double x1, double x2, int n, double K1, double K2, double K3,double A,double B, double C){ double result [] = new double [n+1]; h=(x2-x1)/n; for(int i=0;i<n+1;i++){ result[i]=K1*Math.sin(A*(x1+(i*h)))+K2*Math.cos(B*(x1+i*h))+K3/(C*C+((x1+i*h)*(x1+i*h))); } return result; } public double value(double K1, double A,double K2,double B,double K3,double C,double x){ return K1*Math.sin(A*x)+K2*Math.cos(B*x)+K3/(C*C+x*x); } } class Simpson extends FuncVal{ //computes using Simpson /** Integrate a 1-D function using the Simpson Rule. **/ public double integrate (double lo, double hi,int n,double K1, double K2, double K3,double A,double B, double C) { /**if ( hi <= lo || n <= 0) { return 0.0; }*/ double dx = (hi-lo)/n; dx = dx/2; // Use half widths for Simpson rule double area = 0; //double K1 ,K2,K3,B,A,C; double x_lo = lo; double x_md = lo + dx; double x_hi = lo + 2.0 * dx; for (int i=0; i < n; i++) { FuncVal left_side = new FuncVal(); FuncVal middle = new FuncVal(); FuncVal rite_side = new FuncVal(); double leftSide = left_side.value( K1,A,K2,B,K3,C,x_lo); double rightSide = rite_side.value(K1, A, K2, B, K3, C, x_hi); double mid = middle.value(K1, A, K2, B, K3, C, x_md); area += dx * (leftSide + 4.0 * mid + rightSide)/3.0; x_lo = lo + 2.0 * i * dx; x_md = x_lo + dx; x_hi = x_lo + 2.0 * dx; } return area; } } class Trapezoid extends FuncVal{ public double Trapz(double lo, double hi,int n,double K1, double K2, double K3,double A,double B, double C ){ if ( hi <= lo || n <= 0) { return 0.0; } double area, x1,x2; double dx = (hi-lo)/n; area =0; //double x_lo = lo; //double x_hi = hi; FuncVal left_side = new FuncVal(); FuncVal rite_side = new FuncVal(); for (int i=0;i<n-1;i++){ x1 = lo+ i*dx; x2 = lo+(i+1)*dx; double leftSide = left_side.value( K1,A,K2,B,K3,C,x1); double rightSide = rite_side.value(K1, A, K2, B, K3, C, x2); area += dx * (leftSide + rightSide)/2; } return area; } }
- 02-05-2009, 07:30 AM #2
However since the Panel for the graph is in a separate class that has no connection with the class where the text field are, i cannot get this and use them to define the plot.
The general technique is to have the class with the user–input and listeners collect and send the data to the graphic component. Build the graphic component to be ready to render its data at any time.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class DataComm implements ActionListener { DataCommPanel dataCommPanel; JTextField[] fields; public void actionPerformed(ActionEvent e) { // Assemble user-input data. double[] data = new double[fields.length]; for(int i = 0; i < fields.length; i++) { data[i] = Double.parseDouble(fields[i].getText()); } // Tell graphic component to show this data. dataCommPanel.setData(data); } private JPanel getGraphicComponent() { dataCommPanel = new DataCommPanel(); return dataCommPanel; } private JPanel getUIPanel() { fields = new JTextField[4]; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; for(int i = 0; i < fields.length; i++) { fields[i] = new JTextField(8); fields[i].addActionListener(this); if(i == fields.length-1) { gbc.gridwidth = GridBagConstraints.REMAINDER; } panel.add(fields[i], gbc); } JButton button = new JButton("graph these values"); button.addActionListener (this); gbc.gridwidth = fields.length; panel.add(button, gbc); return panel; } public static void main(String[] args) { DataComm test = new DataComm(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getGraphicComponent()); f.add(test.getUIPanel(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class DataCommPanel extends JPanel { double[] data = new double[0]; final int PAD = 20; 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 axes. g2.draw(new Line2D.Double(PAD, h/2, w-PAD, h/2)); g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); Point2D.Double origin = new Point2D.Double(PAD, h/2); if(data.length == 0) { // Avoid division by zero. return; } // Scale data. double xInc = (w - 2*PAD)/(data.length-1); double yScale = (h/2 - PAD)/getMaxValue(); // Plot data. g2.setPaint(Color.red); for(int i = 0; i < data.length; i++) { double x = origin.x + i*xInc; double y = origin.y - yScale*data[i]; g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4)); } } public void setData(double[] data) { this.data = data; repaint(); } private double getMaxValue() { double min = Double.MAX_VALUE; double max = -min; for(int i = 0; i < data.length; i++) { if(data[i] < min) { min = data[i]; } if(data[i] > max) { max = data[i]; } } return Math.max(Math.abs(max), Math.abs(min)); } }
- 02-05-2009, 08:20 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
Thank you very much for your reply. I couldnt figure out how to feed your plot with a function. 1-The class shown below graphs the function. The problem is that I cannot make it self adjust the domain and the range. This is I want the graph to be seen completely in the panel. Right now the panel cuts one piece of the graph because the axis are not rezising itself. Couldnt make this work out. 2- If it is possible, let me know how you made it transparent?. 3- thanks for the provided code, i implemented the TextField fabrication loop for the inputs, yet unfortunatelly I couldnt follow a hundred percent your code. Thanks, really appreciate your help
Java Code:public class MyPanel extends JPanel{ public double x2,x1,h; public int n; public double []result; public int[] xPoints = new int[2]; public int[] yPoints = new int[2]; double[] x; double[] y; protected void paintComponent(Graphics g){ super.paintComponent(g); //painting background g.setColor(Color.red); g.drawLine(40,20,40,320); g.drawLine(40, 200, 600, 200); g.drawString("f(x)",20,20); g.drawString("x", 610, 200); g.drawString("0",30,220); g.drawString(" "+x2, 440, 220); for(int i=0;i<n;i++){ //Since the will pop up in the same window, resize xPoints [0]= (int)(40+(x1+(i*h*400/(x2-x1)))); xPoints [1]= (int)(40+(x1+(i+1)*h*400/(x2-x1))); yPoints [1]= (int)(200-result[i+1]*200/2); yPoints [0]= (int)(200-result[i]*200/2); g.setColor(Color.red); g.drawLine(xPoints[0], yPoints[0], xPoints[1], yPoints[1]); g.drawLine(xPoints[0],200,xPoints[0],yPoints[0]);//paint the area under the curve } } } }
Similar Threads
-
graph plotting
By sirine in forum New To JavaReplies: 5Last Post: 01-25-2009, 03:34 PM -
Bar Graph
By Zosden in forum Advanced JavaReplies: 2Last Post: 04-28-2008, 06:52 AM -
Need Help for Dot Plot Graph
By BHCluster in forum Java 2DReplies: 5Last Post: 04-15-2008, 02:54 PM -
line plotting math
By Ace_Of_John in forum Java 2DReplies: 1Last Post: 01-19-2008, 10:24 AM -
Problems getting Graph to show
By rhivka in forum New To JavaReplies: 6Last Post: 07-31-2007, 05:21 AM


LinkBack URL
About LinkBacks

Bookmarks