Results 1 to 4 of 4
  1. #1
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default Drawing 3d graph from array table

    Hi all. I have an table like [][]tablo and it includes 3 value x,y,z . and there is about 600 lines . How can I draw a table from this values? I saw on that on java has 2d graph with x,y but how can i draw 3d? x,y,z ?

    0.0000 0.0000 55.0000
    208.9600 -99.9630 55.0000
    208.9600 -99.9630 14.0520
    208.9600 -100.3960 13.6300
    208.9600 -100.8530 13.2350
    208.9600 -101.3330 12.8690
    208.9600 -101.8350 12.5320

    this is out put of table .

  2. #2
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Hi again .I thought that 3D graph will be hard and decided to make it 2D .But there is only 1 problem ..I have 2 class and I must take tablo[][] from second class to Grap class .here codes

    Java Code:
    public class CnsPro {
    
        static float[] coordiante = {0, 0, 0};
        static float [][] tablo;
        static float xf, yf, zf;
        static String x_str;
        static String y_str;
        static String [] z_str;
        static String txt;
         static float GX,GY,GZ ;
     // static  FileInput file = new FileInput();
    
        static void doit() {
            Pattern x = Pattern.compile("X([+-]?[0-9.]*)");
            Pattern y = Pattern.compile("Y([+-]?[0-9.]*)");
            Pattern z = Pattern.compile("Z([+-]?[0-9.]*)");
            Pattern n =Pattern.compile("");
             txt="% O1005 N1 G94 G90 T1 M6 N2 G0 G56 X0 Y0 N3 G43 H01 Z100 N4 X208.96 Y-99.963 N5 G1 G94 Z14.052 F300. N6 Y-100.396 Z13.63 N7 Y-100.853 Z13.235 N8 Y-101.333 Z12.869 N9 Y-101.835 Z12.532 N10 Y-102.355 Z12.226 N11 Y-102.894 Z11.952 N12 Y-103.448 Z11.712 N13 Y-104.015 Z11.505 N14 Y-104.594 Z11.332 N15 Y-105.183 Z11.195 N16 Y-105.778 Z11.094 N17 Y-106.379 Z11.029 N18 Y-106.982 Z11. N19 Y-150.078 F96. N20 X208.935 Y-150.675 N21 X208.869 Y-151.272 N22 X208.763 Y-151.87 N23 X208.625 Y-152.428 N24 X208.42 Y-153.064 N25 X208.178 Y-153.661 N26 X207.884 Y-154.258 N27 X207.53 Y-154.855 N28 X207.11 Y-155.453 N29 X206.609 Y-156.05 N30 X206.237 Y-156.433 N31 X205.639 Y-156.963 N32 X205.042 Y-157.407 N33 X204.445 Y-157.78 N34 X203.848 Y-158.092 N35 X203.25 Y-158.35 N36 X202.653 Y-158.558 N37 X202.056 Y-158.721 N38 X201.459 Y-158.841 N39 X200.862 Y-158.919 N40 X200.265 Y-158.956 N41 X-.078 Y-158.96 N42 X-.675 Y-158.935 N43 X-1.272 Y-158.869 N44 X-1.87 Y-158.763 N45 X-2.467 Y-158.614 N46 X-3.012 Y-158.439 N47 X-3.661 Y-158.178 N48 X-4.258 Y-157.884 N49 X-4.856 Y-157.53 N50 X-5.273 Y-157.244 N51 X-6.008 Y-156.647 N52 X-6.609 Y-156.05 N53 X-7.11 Y-155.453 N54 X-7.531 Y-154.855 N55 X-7.841 Y-154.336 N56 X-8.178 Y-153.661 N57 X-8.439 Y-153.012 N58 X-8.614 Y-152.467 N59 X-8.763 Y-151.87 N60 X-8.869 Y-151.272 N61 X-8.935 Y-150.675 N62 X-8.96 Y-150.078 N63 Y-.121 N64 X-8.947 Y.478  % ";
             z_str=txt.split("N");
     Matcher mN = n.matcher(txt);
    
     			GX = GY = GZ = 0;
     			tablo = new float[z_str.length - 1][3];
     			for(int j=1;j<z_str.length;j++){
                     	Matcher mX = x.matcher(z_str[j]);
                     	Matcher mY = y.matcher(z_str[j]);
                     	Matcher mZ = z.matcher(z_str[j]);
                	    if (mX.find())
                	    	GX = Float.parseFloat(mX.group(1));
                	    tablo[j-1][0]= GX;
                	    if (mY.find())
                	    	GY = Float.parseFloat(mY.group(1));
                	    tablo[j-1][1]= GY;
                	    if (mZ.find())
                	    	GZ = Float.parseFloat(mZ.group(1));
                	    tablo[j-1][2]= GZ;
                 }
                        for(int i=0;i <tablo.length;i++) {
    
     	          for(int j=0;j<3;j++){
     	              System.out.print(" "+tablo[i][j]);
     	          }
     	          System.out.print("\n");
     	     }
           for(int i=0;i <tablo.length;i++) {
    
     	          for(int j=0;j<3;j++){
     	              System.out.print(" "+tablo[i][j]);
     	          }
     	          System.out.print("\n");
     	     }
       System.out.print("TOTAL NO : "+tablo.length);
        }
    
    
        public static void main(String[] args) throws IOException {
    
     new CnsPro().doit();
    new Graph("GRAPH",500, 500/2);
    
        }
    }
    And Graph class
    Java Code:
    public class Graph extends JFrame {
    
        public CnsPro coor = new CnsPro();
        int n, k;
        int total_Points = 6;
        int[] X_Coor;
        int[] Y_Coor;
    
        public Graph(String title,/*int [] X_Coor, int [] Y_Coor ,int tPoints,*/ int n, int k) {
    
            this.n = n;
            this.k = k;
            this.setTitle(title);
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            this.getContentPane().setLayout(new BorderLayout());
            this.setSize(500, 500);
            this.repaint();
    //this.setResizable(false);
            this.setVisible(true);
        }
    
        private void getValue() {   // HERE IT MUST TAKE X VALUES FROM TABLE
            for (int i = 0; i < coor.tablo.length; i++) {
                X_Coor[i] = (int) coor.tablo[i][0];
            }
            for (int i = 0; i < coor.tablo.length; i++) { // HERE IT MUST TAKE Y VALUES FROM TABLE
                Y_Coor[i] = (int) coor.tablo[i][1];
            }
    
        }
    
        public void paint(Graphics g) {
    
            this.drawCoordSystem(g, n / k);
            this.getValue();
    
            g.drawPolyline(X_Coor, Y_Coor, coor.tablo.length);
    
        }
    
        private void drawCoordSystem(Graphics g, int ylabel) {
            g.drawLine(50, 450, 50, 50); //Y coordinate
            g.drawLine(45, 55, 50, 50);
            g.drawLine(50, 50, 55, 55);
            g.drawLine(48, 300, 52, 300);
            g.drawLine(48, 150, 52, 150);
    
            g.drawString(Integer.toString(ylabel), 15, 155);
            g.drawString(Integer.toString(ylabel / 2), 15, 305);
            g.drawLine(50, 450, 450, 450); // X coordinate
            g.drawLine(445, 445, 450, 450); // X arrow
            g.drawLine(450, 450, 445, 455); //X arrow
            g.drawLine(200, 448, 200, 452);
            g.drawLine(350, 448, 350, 452);
            g.drawString("1", 345, 475);
            g.drawString("0.5", 195, 475);
            g.drawString("Y", 15, 50);
            g.drawString("X", 450, 475);
            g.drawString("0", 15, 475);
        }
    }

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class CPGraph extends JFrame {
    
        // This new instance is not the instance you instantiated in the
        // [i]main[/i] method in the CP class. You want to pass the instance of
        // CP created there into this class. Or, get rid of that instance
        // and call [i]doit[/i] on the instance created here. Or, don't use the
        // [i]main[/i] method in the CP class but put one in this class.
    //    public CP coor = new CP();
        CP coor;  // coor declaration
        int n, k;
        int total_Points = 6;
        int[] X_Coor;
        int[] Y_Coor;
    
        public CPGraph(String title, CP coor, int n, int k) {
            this.coor = coor;  // coor instantiation
            this.n = n;
            this.k = k;
            ...
        }
    
        private void getValue() {   // HERE IT MUST TAKE X VALUES FROM TABLE
            // Instantiate these member variables.
            X_Coor = new int[coor.tablo.length];
            Y_Coor = new int[coor.tablo.length];
            for (int i = 0; i < coor.tablo.length; i++) {
                // You get a NullPointer here because you did not
                // instantiate the X-Coor array; you only declared
                // it (as a member variable).
                X_Coor[i] = (int) coor.tablo[i][0];
            }
            for (int i = 0; i < coor.tablo.length; i++) { // HERE IT MUST TAKE Y 
    
    VALUES FROM TABLE
                Y_Coor[i] = (int) coor.tablo[i][1];
            }
        }
    
        public void paint(Graphics g) {
    
            this.drawCoordSystem(g, n / k);
            // You only need to call this next method one time.
            // You could move it to the class constructor.
            this.getValue();
    
            g.drawPolyline(X_Coor, Y_Coor, coor.tablo.length);
        }
        ...
    }
    Java Code:
    public class CP {
        ...
        public static void main(String[] args) {
            // Create an instance of CP nd save a reference
            // to it in local variable "cp".
            CP cp = new CP();
            // Use variable "cp" to call [i]doit[/i] method.
            cp.doit();
            // Pass reference (variable "cp") to CPGraph class.
            new CPGraph("GRAPH", cp, 500, 500/2);
        }
    }

  4. #4
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

Similar Threads

  1. Drawing Graph and Add to JPanel
    By halo3 in forum NetBeans
    Replies: 17
    Last Post: 09-22-2009, 02:31 PM
  2. ECG Graph
    By bharath333 in forum Java Applets
    Replies: 4
    Last Post: 02-14-2009, 11:26 PM
  3. graph plotting
    By sirine in forum New To Java
    Replies: 5
    Last Post: 01-25-2009, 04:34 PM
  4. Bar Graph
    By Zosden in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2008, 07:52 AM
  5. Help with graph code example
    By silvia in forum Java 2D
    Replies: 1
    Last Post: 07-29-2007, 11:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •