# line graphs

Nicole Tan

Greenhorn

Posts: 4

Eddie Vanda

Ranch Hand

Posts: 281

posted 11 years ago

- 0

Nicole,

Look at overriding JComponent's paintComponent method (you can do that in a JPanel) and the drawLine (...) method in the Graphics class.

Ed

Look at overriding JComponent's paintComponent method (you can do that in a JPanel) and the drawLine (...) method in the Graphics class.

Ed

The nice thing about Standards is that there are so many to choose from!

Nicole Tan

Greenhorn

Posts: 4

Craig Wood

Ranch Hand

Posts: 1535

posted 11 years ago

- 0

JSpinner is a Swing component introduced in SDK1.4. If you are using an older sdk/jre they won't work.

Nicole Tan

Greenhorn

Posts: 4

posted 11 years ago

- 0

cool, that's what i needed. ok... i got the graph part sorted out, but there's just this small problem.... i need to display a polynomial eqn line for each generation, how do i destroy the line for the generation b4? and since my program will run for many generation, how do i display the lines on the graph one by one without having all at one go? it seems that my program will keep running and running until it stops, then only the lines(in this case all the lines) are displayed.

Craig Wood

Ranch Hand

Posts: 1535

Nicole Tan

Greenhorn

Posts: 4

posted 11 years ago

- 0

i cut out most of the spinner and selection coding as i only need the graph..

here.. i'll explain waht i'm doing, i writing genetic algorithm to fit a curve, u do know what GA is right? well.. just in case, genetic algorithm works in a way that it will produce a whole set of random equations(by random generating sets of coefficients), called the population. then this population of coefficient sets will go through some operations... each time it goes through these operation, a new population will be form from the old one.. so i will have new lines everytime... going through the operations once is call one generation... so there will be tons of generations before the lines evolve themselves into better ones..

what i want is, for each generation, i want to get the best fitted line to display on the screen... but everytime i run the program... it keep evolving the population(which keeps the pc busy) and dun have time to display the graphs. it will only show the graph but no lines are produced as the algorithm is working too fast and repaiting is too slow.

this is what's left of the code, and i called the setConstant() when one generation is done in the algorithm

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.text.*;

import javax.swing.*;

import javax.swing.event.*;

/**

*

* @author TAN

*/

class Graph extends JPanel

{

final int PAD = 15;

NumberFormat nf;

double[] no = new double[4];

double a, b, c, d;

public Graph()

{

a = 0;

b = 0;

c = 0;

d = 0;

nf = NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(2);

nf.setGroupingUsed(false);

setBackground(Color.white);

}

protected 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();

// ordinate

g2.draw(new Line2D.Double(w/2, PAD, w/2, h - PAD));

float yInc = (h - 2*PAD)/50f;

float x1 = w/2;

float y1 = PAD;

for(int i = 0; i <= 50; i++)

{

if(i % 5 == 0)

{

g2.draw(new Line2D.Double(x1 - 2, y1, x1 + 2, y1));

if(i != 25)

g2.drawString(String.valueOf(5 - i/5), x1 - 15, y1 + 5);

}

else

g2.draw(new Line2D.Double(x1 - 1, y1, x1, y1));

y1 += yInc;

}

// abcissa

g2.draw(new Line2D.Double(PAD, h/2, w - PAD, h/2));

float xInc = (w - 2*PAD)/50f;

x1 = PAD;

y1 = h/2;

for(int i = 0; i <= 50; i++)

{

if(i % 5 == 0)

{

g2.draw(new Line2D.Double(x1, y1 - 2, x1, y1 + 2));

if(i != 25)

g2.drawString(String.valueOf(i/5 - 5), x1 - 3, y1 + 15);

}

else

g2.draw(new Line2D.Double(x1, y1, x1, y1 + 1));

x1 += xInc;

}

// polynomial

g2.setPaint(Color.red);

double cube, square, linear, constant, y;

double yLast = 0;

double x = PAD;

for(int i = -25; i <= 25; i++)

{

cube = square = linear = constant = 0;

cube = a * Math.pow(i/5f, 3) * (h/2 - PAD)/5;

square = b * Math.pow(i/5f, 2) * (h/2 - PAD)/5;

linear = c * i * (h/2 - PAD)/25;

constant = d * (h/2 - PAD);

y = h/2 - (cube + square + linear + constant);

if(i > -25) // no line on

g2.draw(new Line2D.Double(x - xInc, yLast, x, y)); // first pass

x += xInc;

yLast = y;

}

}

public void setConstant(double[] coe)

{

no = coe;

a = no[0];

b = no[1];

c = no[2];

d = no[3];

repaint();

}

}

here.. i'll explain waht i'm doing, i writing genetic algorithm to fit a curve, u do know what GA is right? well.. just in case, genetic algorithm works in a way that it will produce a whole set of random equations(by random generating sets of coefficients), called the population. then this population of coefficient sets will go through some operations... each time it goes through these operation, a new population will be form from the old one.. so i will have new lines everytime... going through the operations once is call one generation... so there will be tons of generations before the lines evolve themselves into better ones..

what i want is, for each generation, i want to get the best fitted line to display on the screen... but everytime i run the program... it keep evolving the population(which keeps the pc busy) and dun have time to display the graphs. it will only show the graph but no lines are produced as the algorithm is working too fast and repaiting is too slow.

this is what's left of the code, and i called the setConstant() when one generation is done in the algorithm

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.text.*;

import javax.swing.*;

import javax.swing.event.*;

/**

*

* @author TAN

*/

class Graph extends JPanel

{

final int PAD = 15;

NumberFormat nf;

double[] no = new double[4];

double a, b, c, d;

public Graph()

{

a = 0;

b = 0;

c = 0;

d = 0;

nf = NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(2);

nf.setGroupingUsed(false);

setBackground(Color.white);

}

protected 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();

// ordinate

g2.draw(new Line2D.Double(w/2, PAD, w/2, h - PAD));

float yInc = (h - 2*PAD)/50f;

float x1 = w/2;

float y1 = PAD;

for(int i = 0; i <= 50; i++)

{

if(i % 5 == 0)

{

g2.draw(new Line2D.Double(x1 - 2, y1, x1 + 2, y1));

if(i != 25)

g2.drawString(String.valueOf(5 - i/5), x1 - 15, y1 + 5);

}

else

g2.draw(new Line2D.Double(x1 - 1, y1, x1, y1));

y1 += yInc;

}

// abcissa

g2.draw(new Line2D.Double(PAD, h/2, w - PAD, h/2));

float xInc = (w - 2*PAD)/50f;

x1 = PAD;

y1 = h/2;

for(int i = 0; i <= 50; i++)

{

if(i % 5 == 0)

{

g2.draw(new Line2D.Double(x1, y1 - 2, x1, y1 + 2));

if(i != 25)

g2.drawString(String.valueOf(i/5 - 5), x1 - 3, y1 + 15);

}

else

g2.draw(new Line2D.Double(x1, y1, x1, y1 + 1));

x1 += xInc;

}

// polynomial

g2.setPaint(Color.red);

double cube, square, linear, constant, y;

double yLast = 0;

double x = PAD;

for(int i = -25; i <= 25; i++)

{

cube = square = linear = constant = 0;

cube = a * Math.pow(i/5f, 3) * (h/2 - PAD)/5;

square = b * Math.pow(i/5f, 2) * (h/2 - PAD)/5;

linear = c * i * (h/2 - PAD)/25;

constant = d * (h/2 - PAD);

y = h/2 - (cube + square + linear + constant);

if(i > -25) // no line on

g2.draw(new Line2D.Double(x - xInc, yLast, x, y)); // first pass

x += xInc;

yLast = y;

}

}

public void setConstant(double[] coe)

{

no = coe;

a = no[0];

b = no[1];

c = no[2];

d = no[3];

repaint();

}

}

I agree. Here's the link: http://aspose.com/file-tools |