Results 1 to 10 of 10
  1. #1
    Megatron's Avatar
    Megatron is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default How to plot a graph for two sets of variables....

    hi.......I am new to java....I ve been trying to plot a graph for two sets of variables....its a problem like this.....

    x=[ 2 , 4 , 12 , 15 , 20];
    y=[ 1 , 3 , 8 , 33 , 35 ];


    user will give the values of x and y..
    I ve done this using Mat-lab...But I am not being able to do this on java....
    Is there any way of doing this....can anyone help me...

    thnx...

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Google paintComponent...

    Let me know if you get stuck...
    Who Cares... As Long As It Works...

  3. #3
    Megatron's Avatar
    Megatron is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Thnx for ur kind help.....
    but i am getting stuck again and again...
    I took help from one of the threads..but its not working...
    while compiling ..it is showing error.......
    I am giving it here....let me know what i have done wrong.....





    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class LineGraph extends JPanel {

    //int[] data = {
    //, 500, 1000, 2000, 4000
    //};

    int[] dataX = {

    250, 500, 1000, 2000, 4000
    };

    int dataY[] = {
    0, 10, 20, 30, 40, 50, 60
    };

    final int PAD = 20;

    public LineGraph() {
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    } catch (Exception e) {
    System.err.println("Unable to find and load driver");
    System.exit(1);
    }
    }


    protected void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,

    RenderingHints.VALUE_ANTIALIAS_ON);

    int w = getWidth();
    int h = getHeight();
    // Draw ordinate.
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));
    // Draw abcissa.
    g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));
    // Draw labels.
    Font font = g2.getFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("0", frc);
    float sh = lm.getAscent() + lm.getDescent();
    // Ordinate label.
    String s = "Decibel Level Db";
    float sy = PAD + ((h - 2 * PAD) - s.length() * sh) / 2 + lm.getAscent();
    for (int i = 0; i < s.length(); i++) {
    String letter = String.valueOf(s.charAt(i));
    float sw = (float) font.getStringBounds(letter, frc).getWidth();
    float sx = (PAD - sw) / 2;
    g2.drawString(letter, sx, sy);
    sy += sh;
    }
    // Abcissa label.
    s = "Frequency Hz";
    sy = h - PAD + (PAD - sh) / 2 + lm.getAscent();
    float sw = (float) font.getStringBounds(s, frc).getWidth();
    float sx = (w - sw) / 2;
    g2.drawString(s, sx, sy);
    // Draw lines.
    double xInc = (double) (w - 2 * PAD) / (dataX.length - 1);
    double scale = (double) (h - 2 * PAD) / getMax();
    g2.setPaint(Color.green.darker());
    for (int i = 0; i < dataX.length - 1; i++) {
    //double x1 = PAD + i * xInc;
    double x1 = PAD + dataX[ i ];
    //double y1 = h - PAD - scale * data;
    double y1 = h - PAD - scale * dataY;
    //double x2 = PAD + (i + 1) * xInc;
    double x2 = PAD + dataX[i+1];
    //double y2 = h - PAD - scale * data[i + 1];
    double y2 = h - PAD - scale * dataY[i + 1];

    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    }
    // Mark data points.
    g2.setPaint(Color.red);
    for (int i = 0; i < dataX.length; i++) {
    //double x = PAD + i * xInc;
    double x = PAD + dataX[ i ];
    //double y = h - PAD - scale * data;
    double y = h - PAD - scale * dataY;
    g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
    }
    }

    private int getMax() {
    int max = -Integer.MAX_VALUE;
    for (int i = 0; i < dataX.length; i++) {
    if (dataX > max) {
    max = dataX;
    }
    }
    return max;
    }
    }

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Can you post the error you are getting...
    Who Cares... As Long As It Works...

  5. #5
    Megatron's Avatar
    Megatron is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    init:
    deps-jar:
    Compiling 1 source file to F:\New Document folder\NetBeansProjects\2set\build\classes
    F:\New Document folder\NetBeansProjects\2set\src\da.java:78: operator * cannot be applied to double,int[]
    double y1 = h - PAD - scale * dataY;
    F:\New Document folder\NetBeansProjects\2set\src\da.java:92: operator * cannot be applied to double,int[]
    double y = h - PAD - scale * dataY;
    F:\New Document folder\NetBeansProjects\2set\src\da.java:100: operator > cannot be applied to int[],int
    if (dataX > max) {
    F:\New Document folder\NetBeansProjects\2set\src\da.java:101: incompatible types
    found : int[]
    required: int
    max = dataX;
    4 errors
    BUILD FAILED (total time: 0 seconds)

  6. #6
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Your first error lies in this code:

    Java Code:
    double y = h - PAD - scale * dataY
    dataY is an array and everything else is not...

    Your last couple of errors lie in this code:

    Java Code:
    private int getMax() { 
    int max = -Integer.MAX_VALUE; 
    for (int i = 0; i < dataX.length; i++) { 
    if (dataX > max) { 
    max = dataX; 
    } 
    } 
    return max; 
    }

    dataX is also an array...

    You need to make dataX and dataY into dataX[?] and data[?]...
    Who Cares... As Long As It Works...

  7. #7
    Megatron's Avatar
    Megatron is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    after fixing the errors..........when it comes to run..........it shows......

    class "LineGraph" does not have a main method...........


    how can i run the code now.....

  8. #8
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Your LineGraph is a class that you will need to create an object in your main method and add the object to a JFrame...


    Like so:

    Java Code:
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
             //Do JFrame Stuff...
             LineGraph  graff = new LineGraph ();
    
             frame.add(graff);
                      
             //Finish Frame Stuff...
     
        }
    
    }

    Then run your code...
    Who Cares... As Long As It Works...

  9. #9
    Megatron's Avatar
    Megatron is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    It works................... :)))

    Thnx a million..........

  10. #10
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

Similar Threads

  1. How to plot graph in java for given samples
    By annesteve31 in forum New To Java
    Replies: 27
    Last Post: 11-29-2009, 10:27 PM
  2. i want plot realtime graph in java
    By santhosh_el in forum New To Java
    Replies: 3
    Last Post: 02-26-2009, 09:32 AM
  3. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 07:49 PM
  4. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 03:57 PM
  5. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 03:54 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
  •