Results 1 to 3 of 3
  1. #1
    jackwong0099 is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Cool Creating a graph which receives values from input sensors

    Hi, I'm new to java :) sorry if my code is hard to read. I want to create a graph which I can plot my coordinates on, however I'm currently having troubles, when I execute the program it doesn't start at the center of my screen even if I used the code
    Java Code:
    setLocationRelativeTo(null);
    Description
    I'm trying to create a program which will plot the graph automatically from the inputs from my micro-controller(Arduino another language). I have an open source which allow me to communicate with java and arduino (in the form of strings).
    Comments, suggestion and improvements are appreciated, also for your information this is not related to my school work/any assignments all these are just based on my personal interest. Also I'm kind of stuck not really sure what to do next any suggestion?

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Graphs extends JPanel{
    	
    	int[] data = new int[1080]; // because the input sensor's range from 0 - 1080
    	long[] timeseconds;  
    	final int xaxis = 300; //5 minutes, 300 seconds
    	final int yaxis = 1080;
    	final int POO = 200;
    			
    	
    	public 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();
    	      
    	      g2.draw(new Line2D.Double(POO, POO, POO, h-POO));
    	      g2.draw(new Line2D.Double(POO, h-POO, w-POO, h-POO));
    	      
    	        double xInc = (double)(w - 2*POO)/(data.length-1);
    	        double scale = (double)(h - 2*POO)/getMax();
    	       // g2.setPaint(Color.red);
    	     //   for(int i = 0; i < data.length; i++) {
    	      //      double x = POO + i*xInc;
    	     //       double y = h - POO - data[i];
    	      //      g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
    	      //  }
    	}
    	        
    	private int getMax() {
    	            int max = -Integer.MAX_VALUE;
    	            for(int i = 0; i < data.length; i++) {
    	            if(data[i] > max)
    	            	max = data[i];
    	}
    	            return max;
    	}
    
    	public static void main(String[] args) {
    		
            JFrame J = new JFrame();
            J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            J.setLocationRelativeTo(null);
            J.add(new Graphs());
            J.setSize(1200,1200);
            J.setLocation(200,200);
            J.setVisible(true);
    	}
    
    }
    Last edited by jackwong0099; 06-10-2013 at 12:54 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Creating a graph which receives values from input sensors

    I recommend doing several things to center the app on the screen.

    1. Set the dimensions in your panel (not frame) using setPreferredSize().
    2. Then just before you do setLocationRelativeTo(null) and setVisible(true), use J.pack(). This causes the layout manager to properly size the components. The frame will now hold the JPanel and since the frame has size it can be properly positioned.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Creating a graph which receives values from input sensors

    Moved from New to Java

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

Similar Threads

  1. scaling of negative values in graph
    By noobplus in forum New To Java
    Replies: 15
    Last Post: 04-07-2012, 06:37 AM
  2. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. creating a graph from a text file
    By chinolee in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 08:36 AM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 04:45 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
  •