Results 1 to 17 of 17
  1. #1
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default drawing scattered plot from 2D array

    I wanted to draw a graph(just points) reading from array. the numbers of array have to be doubles because of accuracy.
    In the main class, I attached two files and saved into 2D arrays and draw graph in another class. the x axis will be CorrectData[i][0] and y axis is CorrectData[i][1].
    Can someone tell me how to read from array and draw scattered plot and fix the code below? Thank you!!! I really need a quick answer!!!


    Java Code:
    // code for Graph class
    class Graph extends Frame
    {
    	public test data= new test();
     private final int sizeH=600;
     private final int sizeW=1000;
     
     Color color=Color.red;
      
     double[] data1 = new double[data.num];
     double[] data2 = new double[data.num];
    
    
     Graph()
     {
    	
      setSize(sizeW,sizeH);
      setVisible(true);
      addWindowListener(new WindowAdapter()
      {
       public void windowClosing(WindowEvent e)
       {
        System.exit(0);
       }
      });
      
      repaint2();
      
     }
    
     private void getValue(){
    	 for (int i=0; i<data.num; i++){
    		 data1[i] = data.CorrectData[i][0];
    		 data2[i] = data.CorrectData[i][1];
    	 }
     }
     
     
     
     public void repaint2()
     {
      Graphics2D g=(Graphics2D)g;
     
    
       this.getValue();
      
      for (int i=0;i<=sizeW ;i=i+100 )
      {
    	  g.fill(new Ellipse2D.Double(data1[i], data2[i], 4, 4));
    
      }
     }
     
     }

    Java Code:
    // Code for test class (main)
    public class test {
    	static double CorrectData[][]= new double[1000][5];
    	static String RawData[][] = new String[1000][3];
    	static int num;
    	public static void doit() throws IOException{
    		FileInputStream fstream1 = new FileInputStream("test1.txt");
    		DataInputStream in1 = new DataInputStream(fstream1);
    		BufferedReader br1= new BufferedReader (new InputStreamReader(in1));
    	
    	
                
    			int Row = 0 ;
    			
    			int Column = 0;
    			
    			
    			int count=0;
    			String StoreString[] = new String[100];
    			
    			while((StoreString[count]=br1.readLine())!= null){
    				System.out.println(StoreString[count]);
    				StringTokenizer words = new StringTokenizer(StoreString[count],"  ");
    				count++;
    				
    				words=new StringTokenizer(StoreString[Row]);
    				for (Column=0; Column<3; Column++)
    				{
    				RawData[Row][Column]=(words.nextToken());
    				System.out.println(RawData[Row][Column]);
    				}
    				
    				Row++;
    				
    				
    			}
    			num = Row;
    			System.out.println(Row);
    			System.out.println("hello");
    			
    	// CorrectData Column 4
    			for (int i=0; i<Row; i++)
    			{
    				    
    					CorrectData[i][0] = sciNotationToDouble(RawData[i][1])*(-1000);
    					System.out.println(CorrectData[i][0]);
    				
    			}
    	//CorrectData Column 5
    			for (int i=0; i<Row; i++)
    			{
    				int x = 64;
    				double y = 0.0314;
    				CorrectData[i][1] = CorrectData[i][0]-sciNotationToDouble(RawData[i][0])*x*y;
    				System.out.println(CorrectData[i][1]);
    			}
    	//CorrectData Column 6
    			for (int i=0; i<Row; i++)
    			{
    				CorrectData[i][2] = Math.log10(sciNotationToDouble(RawData[i][1]));
    				System.out.println(CorrectData[i][2]);
    			}
    	
    			
    		
    			fstream1.close();
    			in1.close();
    			br1.close();
    	}
    	public static void main(String args[])throws IOException
    	{
       
       //created new file to store three files' data...
       // appended three files here...
    
    				  
                                          test.doit();	
    					
    				
    			Graph graph = new Graph();
    					 
    		}
    	
    		
    		
    }
    Moderator Edit: Code tags added
    Last edited by yeahwa; 06-05-2010 at 03:19 PM. Reason: Moderator Edit: Code tags added

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    Repost your code using the code tags and get rid of all that useless vertical whitespace.

    I doubt anyone will try to read the unformatted code. I know I won't.

    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. Since this was your first post, I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  4. #4
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks,Fubarable! It was my first time post here~
    I know it's long code, but pretty please someone help me~

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by yeahwa View Post
    Thanks,Fubarable! It was my first time post here~
    I know it's long code, but pretty please someone help me~
    I think that many will be better able to help you if you give more detail on just what your problem is. Currently you have posted code and asked others to fix this, but this type of post usually has a low rate of getting an answer that will satisfy you. Much better would be to tell the details about what is working, what isn't working, what errors are occurring, etc. We like to say that posters should put in as much effort asking their question as they would like helpers to apply in solving their question.

    This link can help you ask a better question: How To Ask Programming Questions

  6. #6
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I appended three files(which has three columns of numbers(double)) and saved in a 2d array RawData[1000][3] and got new 2d array CorrectData[1000][3] calculating from RawData[1000][3]. What I want to do is draw x, y axis and scattered plot from CorrectData[i][0] as x, CorrectData[i][1] as y. The numbers are like this: 0.0017406615763012215
    -0.0034883278458213473
    -0.0052430554123718345
    0.005682847330363273
    -0.007446482167864335
    -0.009661145212398569
    0.01188715973164805
    I got the numbers in double format succefully from string, but in graph, I only got window opened, and nothing is drawn. Did I get the data from main class correctly? How can I draw scattered plot?

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Your code doesn't compile for me so there's limited help I can give. Problems as I see them:

    1) Using AWT instead of Swing.
    2) Not drawing correctly, not using paint method. You'll need to read the tutorials to see how to do this correctly, but again, I recommend that you read the tutorials for Swing graphics, not AWT, and that you draw in a JPanel's paintcomponent method (the tutorials will show you how).
    3) Use of static variables and methods rather than instance classes with objects.

  8. #8
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Does anyone know draw points with double format data? it seems all require integer to draw shape in Graphics2D.

  9. #9
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by yeahwa View Post
    Does anyone know draw points with double format data? it seems all require integer to draw shape in Graphics2D.
    Most Shape classes will use double just fine, and in fact I see that you're using Ellipse2D which can be created with double data easily via the Double(....) constructor (as you're already doing).

    Any comments about my previous points? Do they make sense? Do you disagree with them? Are you trying to implement them?

  10. #10
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks curmudgeon! it does make sense, but it just does not draw any points. I was trying to draw the points filling small ellipses but Nothing on screen! Can you see where's the problem?

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You're doing your graphics all wrong. For one, there's no valid paint method present. But again, if my previous post makes sense, you'll know that you don't want to use AWT, that you'll use Swing, draw in a JPanel, and override its paintComponent method. You should scrap your code above and re-do it, me thinks.

  12. #12
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I fixed the code. I tested and found out it reads numbers from correctly from "test" class but it still does not draw anything. the numbers read from CorrectData[i][0] and CorrectData[i][0] are like this:
    -1004.9999999999999
    -1004.0
    -1000.0
    -996.0000000000001
    -992.0
    -988.0000000000001
    -987.0
    -983.0000000000001
    -978.0
    -973.0000000000001
    -972.0000000000001
    -968.0
    -963.0000000000001
    -958.0000000000001
    -0.0017406615763012215
    -0.0034883278458213473
    -0.0052430554123718345
    -0.005682847330363273
    -0.007446482167864335
    -0.009661145212398569
    any ideas?
    Java Code:
    class Graph extends Frame implements Runnable
    {
    	
     private final int sizeH=1200;
     private final int sizeW=1200;
     
     boolean is=true;
     Color color=Color.red;
     
     double x;
     double y;
     int i=0;
     
     Graph()
     {
    	
      setSize(sizeW,sizeH);
      setVisible(true);
      addWindowListener(new WindowAdapter()
      {
       public void windowClosing(WindowEvent e)
       {
        System.exit(0);
       }
      }); 
     }
     
     public void run()
     {
      while (is)
      {
      graph();
       repaint();
       try
       {
        Thread.sleep(10);
       }
       catch (Exception e)
       {
       }
      }
     }
    
     public void graph()
     {
    	 
    	x=Math.abs(test.CorrectData[i][0]);
    	y=Math.abs(test.CorrectData[i][1]);
    	i++;
    	if(i == test.getNum())
    	{
    		is= false;
    	}
     }
     
     public void repaint()
     {
    	 Graphics g=getGraphics();
    	 g.setColor(color);	
    	 g.fillOval((int)x,(int)y,5,5);
     }
     
    
    }

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by yeahwa View Post
    I fixed the code. I tested and found out it reads numbers from correctly from "test" class but it still does not draw anything. the numbers read from CorrectData[i][0] and CorrectData[i][0] are like this:
    -1004.9999999999999
    -1004.0
    -1000.0
    -996.0000000000001
    -992.0
    -988.0000000000001
    -987.0
    -983.0000000000001
    -978.0
    -973.0000000000001
    -972.0000000000001
    -968.0
    -963.0000000000001
    -958.0000000000001
    -0.0017406615763012215
    -0.0034883278458213473
    -0.0052430554123718345
    -0.005682847330363273
    -0.007446482167864335
    -0.009661145212398569
    any ideas?
    That is not a very nice range of numbers. If you want to draw them on, say, a JPanel with dimensions 800x800 most of those numbers fall out of that JPanel. You have to scale all your numbers to be in the range of the JPanel dimensions. You have to find xmin, xmax, ymin and ymax for that list of numbers. If the dimensions of your JPanel are X and Y draw the points:

    (x-xmin)/(xmax-xmin)*X, (y-ymin)/(ymax-ymin)*Y

    for all values of (x,y) in your data list.

    kind regards,

    Jos

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You still haven't read the graphics tutorials yet, or if you have have not implemented their recommendations. I'm not sure how we can help you til you help yourself first.

  15. #15
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I don't know if I did it correctly... I just read sun java tutorial for Swing...
    test class
    Java Code:
    public static double getData1(int i){
    		return (CorrectData[i][0]-ymin)/(ymax-ymin)*800;
    	}
    	public static double getData2(int i)
    	{
    		return (CorrectData[i][1]-xmin)/(xmax-xmin)*800;
    	}
    SwingPaint class
    Java Code:
    public class SwingPaint {
    	
        public static void main(String[] args) {
        	
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	
                    createAndShowGUI(); 
                }
            });
        }
    
        private static void createAndShowGUI() {
            
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new MyPanel());
            f.pack();
            f.setVisible(true);
        }
    }
    class MyPanel extends JPanel {
    	 	
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(800,800);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
           Graphics2D g2 = (Graphics2D)g;
            // Draw Text
            g.drawString("This is my custom Panel!",10,20);
            
            for(int i=0;i<test.getNum();i++){
            g2.fill(new Ellipse2D.Double(test.getData1(i),test.getData2(i),5,5));
            	        }
                }  
    }
    it still does not draw anything... where's the problem? Thank you all... I know it lasts too long...
    Last edited by yeahwa; 06-07-2010 at 06:43 AM.

  16. #16
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    in SwingPaint Class, getData1(i) function only gets one number 400.00 no matter what "i" is. but in the test class, it gets the right numbers. and if I remove "for" loop and give certain number on getData1(like 3, 4, 5) function it will always draw in the same position 400.00.
    Can anyone tell me the problems?
    many many thanks!!!

  17. #17
    yeahwa is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I solved it! Thank you very very much, curmudgeon and JosAH! It was my first time to write Java Graphic program!

Similar Threads

  1. Replies: 16
    Last Post: 05-22-2010, 03:51 AM
  2. How to plot this contour ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 05:00 PM
  3. Plot Program
    By lsmitherman in forum New To Java
    Replies: 5
    Last Post: 02-07-2010, 04:07 PM
  4. Drawing 3d graph from array table
    By Mekonom in forum Java 2D
    Replies: 3
    Last Post: 12-26-2009, 09:42 AM
  5. Replies: 5
    Last Post: 08-02-2008, 08:05 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
  •