Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By quad64bit

Thread: How to create Graph of points from array

  1. #1
    enk1du is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default How to create Graph of points from array

    Hi,

    I have to create a Graph from list with two columns of floats. So i have this:


    Java Code:
    List<Float> getFromFile = new ArrayList<Float>();
          Scanner sccan = new Scanner(new FileReader("D:\\Docs\\Java\\test.txt"));
          while (sccan.hasNext()) {
    			
      		getFromFile.add(sccan.nextFloat());
          }
      	  sccan.close();
              Object maxValue = Collections.max(getFromFile );
    	  Object minValue = Collections.min(getFromFile );
    And i need a help with drawing points on a chart. I made simple frame with axes.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class Paint extends Canvas {
    	
    	public Paint() {
    		Frame frame = new Frame("Canvas");
    		frame.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent we) {
    				System.exit(0);
    			}
    		});
    		
    		//Window
    		setSize(900, 600);
    		frame.add(this, "West");
    
    		frame.pack();
    		frame.setResizable(false);
    		frame.setVisible(true);
    		
    		Panel panel1 = new Panel();
    		panel1.setLayout(new GridLayout(3,1));
    		panel1.add(new Checkbox("Wyb\u00f3r 1",true));
    		frame.add(panel1, "Center");
    	}
    
     public void paint(Graphics g) {
    	 
    	 super.paint(g);
    
    	 //axes
    	 g.drawLine(400, 0, 400, 600);
    	 g.drawLine(0, 300, 793, 300);
    
    	 //scale
    	 for (int i = 0; i < 800; i+=40) {
    		 g.drawLine(i, 305, i, 295);
    	 }
    
    	 for (int j = 20; j < 600; j+=40) {
    		 g.drawLine(395, j, 405, j);
    	 }
    	 
    	  //arrows
    	  g.drawLine(395, 5, 400, 0);
    	  g.drawLine(405, 5, 400, 0);
    	  g.drawLine(789, 295, 793, 299);
    	  g.drawLine(789, 305, 793, 299);
    }
    }
    I have problem with getting a max and rest of the points from getFromFile.
    Last edited by enk1du; 11-20-2012 at 01:37 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: How to create Graph of points from array

    Why are you using the AWT Canvas which was superseded by the Swing JPanel more than 10 years ago?

    Recommended tutorials: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and Lesson: Getting Started with Graphics (The Java™ Tutorials > 2D Graphics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    enk1du is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to create Graph of points from array

    Quote Originally Posted by DarrylBurke View Post
    Why are you using the AWT Canvas which was superseded by the Swing JPanel more than 10 years ago?

    Recommended tutorials: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and Lesson: Getting Started with Graphics (The Java™ Tutorials > 2D Graphics)

    db
    Yeah, I red that, but there is nothing about drawing points from the ArrayList...
    I'm beginner and I think JPanel is useless for me if I don't know how to draw in AWT.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: How to create Graph of points from array

    It's a lot easier than AWT.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    enk1du is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to create Graph of points from array

    OK, I will learn it but any help with implementation of Scanner in AWT?
    Last edited by enk1du; 11-20-2012 at 08:25 PM.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create Graph of points from array

    Why are you using AWT again? It's deprecated. You should be doing it in Swing. AWT is over a decade old, there is no use in learning it at this point, it is more trouble than it is worth.
    DarrylBurke likes this.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: How to create Graph of points from array

    Either way, you seem to have a Collection of Floats.
    So pass that to your JPanel (OK, Canvas).

    Then, when you (re)paint you can simply refer to the collection.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. create moving points with respect to time
    By life4menu in forum Java Applets
    Replies: 44
    Last Post: 03-15-2012, 06:30 PM
  2. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  3. Java Sorting Array of points!
    By Jcbconway in forum Advanced Java
    Replies: 26
    Last Post: 10-27-2010, 04:07 AM
  4. Replies: 5
    Last Post: 10-01-2010, 09:26 PM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

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