# Thread: Plotting a scatter diagram

1. Member Join Date
Dec 2014
Posts
4
Rep Power
0

## 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?

Anybody...  Reply With Quote

2. ## Re: Plotting a scatter diagram  Reply With Quote

3. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Plotting a scatter diagram Originally Posted by SHMAI 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  Reply With Quote

4. Member Join Date
Dec 2014
Posts
4
Rep Power
0

## Re: Plotting a scatter diagram

I don't know how and where to start from Originally Posted by Norm   Reply With Quote

5. ## 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.  Reply With Quote

6. Member Join Date
Dec 2014
Posts
4
Rep Power
0

## 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.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.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.  Reply With Quote

7. ## Re: Plotting a scatter diagram

- 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.  Reply With Quote

8. Member Join Date
Dec 2014
Posts
4
Rep Power
0

## Re: Plotting a scatter diagram

OK. Thanks  Reply With Quote

9. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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  Reply With Quote

#### Posting Permissions

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