Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: Plotting a scatter diagram

  1. #1
    SHMAI is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Question Plotting a scatter diagram

    Hello, I am just learning java and I want to plot a scatter diagram and I need to do this as soon as possible.

    The graph is to make use of JComponent only.
    The diagram title should show the Title: Graph of y on x
    The scattergram should show points (x,y) of Npoints.
    The diagram should calculate and draw a "line of best fit" for the data.
    The diagram should also state the equation for the line of best fit.

    I have installed eclipse and is it possible to get a code to run this?

    Please someone help!

    Anybody...

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Plotting a scatter diagram

    Do you have any questions about your program? Post the code you are having problems with and ask your questions about the code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Plotting a scatter diagram

    Quote Originally Posted by SHMAI View Post
    is it possible to get a code to run this?
    Sure. But you have to write it yourself. Then we can answer questions about any problems
    you may be having.

    BTW, are you familiar with linear regression?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    SHMAI is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: Plotting a scatter diagram

    I don't know how and where to start from

    Quote Originally Posted by Norm View Post
    Do you have any questions about your program? Post the code you are having problems with and ask your questions about the code.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Plotting a scatter diagram

    I don't know how and where to start from
    First write down the algorithm.
    Then make a list of the steps a program needs to take to implement the algorithm.
    Then start with the first step in the list: code, compile and test it.
    When it works, move to the next step in the list.
    SHMAI likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    SHMAI is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: Plotting a scatter diagram

    I've done something below but it doesn't work. It just makes a black space.

    package Pract;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.IOException;
    import java.util.Arrays;

    import javax.swing.JFrame;
    import javax.swing.JPanel;



    public class Graphs extends JPanel {
    int count = 20;

    double[] xaxis = {65.0, 67.0, 40.0, 51.0, 25.0, 81.0, 66.0, 60.0, 68.0, 64.0, 55.0, 45.0, 60.0, 53.0, 81.0, 61.0, 73.0, 58.0, 36.0, 62.0};
    double[] yaxis = {74.0, 65.0, 38.0, 57.0, 43.0, 66.0, 64.0, 59.0, 66.0, 45.0, 55.0, 52.0, 58.0, 53.0, 69.0, 60.0, 70.0, 61.0, 32.0, 62.0};


    private final static int width = 4; // Defaults
    private final static int height = 4;
    private Graphics g;

    static int x = 0;
    static int y = 0;

    private void drawXYAxes(Graphics g) {
    int hBound = 600 / 2;
    int vBound = 600 / 2;
    int tic = 600 / 100;

    g.drawLine(-hBound,0,hBound,0);
    for (int k = -hBound; k <= hBound; k+=10)
    g.drawLine(k, tic, k, -tic);
    g.drawLine(0, vBound, 0, -vBound);
    for (int k = -vBound; k <= vBound; k+=10)
    g.drawLine(-tic, k, +tic, k);
    } // drawXYAxes()

    public void paintComponent(Graphics graphics) {
    Graphics2D g = (Graphics2D) graphics;
    g.setColor(Color.black);
    for (int i = 0; i < count; i++) {
    x = (int) (xaxis[i] - width/2);
    y = (int) (yaxis[i] - height/2);

    }
    g.setColor(getBackground());
    drawXYAxes(g); // Draw the X and Y axes
    g.fillRect(x, y, width, height); // Draw a point
    g.translate(600 / 2, 600 / 2);// Relocate origin
    g.setColor(Color.black);


    } // paintComponent()


    public static void main(String args[]) {
    Graphs u = new Graphs();

    JFrame f = new JFrame("Scatter diagram");
    f.setTitle("Plotting a scatter diagram ");
    f.setSize(600,600);
    f.add(new Graphs());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    ((Graphics) u.g).fillRect(x, y, width, height); // Draw a point
    // u.paintComponent();

    } // main()


    }// Graph
    Last edited by SHMAI; 12-17-2014 at 04:24 AM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Plotting a scatter diagram

    Please use [code][/code] tags to format code. This is hardly readable.

    - Before you call drawXYAxes(g), yu set the paintcolor to getBackground(). This means the lines are drawn using the backgroundcolor, and thus are invisibe. Set the color to a proper color before painting.
    - In main you create an instance of Graphs, but you don't use it in the JFrame. Remove the instance named "u".
    - The fillRect after setVisible(true) causes a NullPointerException because g.u is not initiliazed yet. Remove this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    SHMAI is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: Plotting a scatter diagram

    OK. Thanks

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Plotting a scatter diagram

    The for loop is a problem. You just keep setting x and y over and over again. And first you need to invoke super.paintComponent(g) in your paintComponent() method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with UML diagram
    By Patricia6946 in forum New To Java
    Replies: 2
    Last Post: 03-29-2013, 12:21 PM
  2. UML diagram
    By JackDaniels in forum New To Java
    Replies: 7
    Last Post: 03-08-2011, 03:11 AM
  3. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2010, 11:21 PM
  4. plz i need help ((ER diagram))
    By sweet angle in forum JDBC
    Replies: 9
    Last Post: 02-01-2009, 10:28 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
  •