Hi everyone:

I ahve this program to plot a graph using x and y coordinates. I cna plot the graph but I will also like to plot the x and y vlaues and name the xx and y coordinates. Anyone can help me ? Here is my code.

// Bifurcationlo.java

import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.Graphics;

public class Bifurcationlo extends Frame 
{
  public Bifurcationlo()
  {
  setSize(600,500);
  addWindowListener(new WindowAdapter()
  { public void windowClosing(WindowEvent event)
  { System.exit(0); }}); }

  public void paint(Graphics g)
  {
  int xmax = 600; int ymax = 400;
  int j, k, m, n;
  double x, xplot, yplot; 
  double r = 2.0;  // bifurcation parameter
  while(r <= 4.0)
  {
  xplot = xmax*(r-2.0)/2.0;
  x = 0.5;
  for(j=0;j<400;j++) { x = r*x*(1.0-x); }
  for(k=0;k<400;k++)
  {
  x = r*x*(1.0-x);
  yplot = ymax*(1.0-x);
  m = (int) Math.round(xplot);
  n = 50 + (int) Math.round(yplot);
  g.drawLine(m,n,m,n);
  }
  r += 0.0005;
  }  // end while
  }

  public static void main(String[] args)
  {
  Frame f = new Bifurcationlo(); 
  f.setVisible(true);
  }
}

Recommended Answers

All 4 Replies

I imagine you want to use drawString from Graphics.

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

You'll need to turn what you want to display into a String and pass that String, along with the coordinates to display them at, to the drawString function:

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

From Graphics class:

drawString

public abstract void drawString(String str,
                                int x,
                                int y)

    Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

    Parameters:
        str - the string to be drawn.
        x - the x coordinate.
        y - the y coordinate. 
    Throws:
        NullPointerException - if str is null.
    See Also:
        drawBytes(byte[], int, int, int, int), drawChars(char[], int, int, int, int)

You can turn integers or doubles into Strings using the toString () method from the Integer and Double classes.

From Integer class:

toString

public static String toString(int i)

    Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.

    Parameters:
        i - an integer to be converted. 
    Returns:
        a string representation of the argument in base 10.

I imagine you want to use drawString from Graphics.

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

You'll need to turn what you want to display into a String and pass that String, along with the coordinates to display them at, to the drawString function:

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

From Graphics class:

drawString

public abstract void drawString(String str,
                                int x,
                                int y)

    Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

    Parameters:
        str - the string to be drawn.
        x - the x coordinate.
        y - the y coordinate. 
    Throws:
        NullPointerException - if str is null.
    See Also:
        drawBytes(byte[], int, int, int, int), drawChars(char[], int, int, int, int)

You can turn integers or doubles into Strings using the toString () method from the Integer and Double classes.

From Integer class:

toString

public static String toString(int i)

    Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.

    Parameters:
        i - an integer to be converted. 
    Returns:
        a string representation of the argument in base 10.

Thanks for the reply. I will read on the String class but I wanted to make sure i was understood. I am able to draw my graph but what I am not able to is to draw an x-axis and a y-axis to show the coordinates of ths points in my graph. It will be like creating a chart with excel, but our professor wants to find out how to do it with Java. Kinda like the picture I attached. http://en.wikipedia.org/wiki/File:Cartesian-coordinate-system.svg

The axes are just lines you draw like any other and you use drawString() for the labeling as VernonDozier already mentioned. You have to draw everything yourself. There is nothing built in to support charting.

The axes are just lines you draw like any other and you use drawString() for the labeling as VernonDozier already mentioned. You have to draw everything yourself. There is nothing built in to support charting.

Thank you I just wanted to be certain that is what I had to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.