Problems making a Graph

From: JS (dsa._at_asdf.com)
Date: 11/29/04


Date: Mon, 29 Nov 2004 23:22:59 +0100

I would like to make a graph like this one:

http://www.cis.ksu.edu/~schmidt/CIS200/gifV6html/Ch5/bargraph.gif

This is what I have, but I am pretty lost on how to get any further.
setBar1 - setBar6 are supposed to be each of the bars and I guess I am
supposed to use setAxes in setBar but I am not sure how.

hope someone can help

----------------------------------------------------------------------------
--------------
import java.awt.*;
import javax.swing.*;
import java.text.*;

class BarGraphWriter extends JPanel
{ private int width;
  private int height;
  private String sentence1 = "";
  private String sentence2 = "";
  private String sentence3 = "";
  private String sentence4 = "";
  private String sentence5 = "";
  private String sentence6 = "";
  private int x_position;
  private int y_position;

public BarGraphWriter(int w, int h)
  { width = w;
    height = h;
    x_position = width / 5;
    y_position = height / 2;
    JFrame my_frame = new JFrame();
    my_frame.getContentPane().add(this);
    my_frame.setTitle("Graph");
    my_frame.setSize(width, height);
    my_frame.setVisible(true);
  }

public void paintComponent(Graphics g)
  { super.paintComponent(g);
    g.setColor(Color.red);
    g.drawString(sentence1, x_position, y_position);
    g.drawString(sentence2, x_position, y_position);
    g.drawString(sentence3, x_position, y_position);
    g.drawString(sentence4, x_position, y_position);
    g.drawString(sentence5, x_position, y_position);
    g.drawString(sentence6, x_position, y_position);
  }

public void setAxes(int x_pos, int y_pos, String top_label, int y_height)
{
}

public void setBar1(String label, int height, Color c)
{
}

public void setBar2(String label, int height, Color c)
{
}

public void setBar3(String label, int height, Color c)
{
}

public void setBar4(String label, int height, Color c)
{
}

public void setBar5(String label, int height, Color c)
{
}

public void setBar6(String label, int height, Color c)
{
}

}
public class TestGraph
{ public static void main(String[] a)
  { BarGraphWriter e = new BarGraphWriter();
    e.setTitle("Days in first four months of the year");
    e.setAxes(20, 120, "30", 90);

    int scale_factor = 3;
    e.setBar1("Jan", 31 * scale_factor, Color.red);
    e.setBar2("Feb", 28 * scale_factor, Color.white);
    e.setBar3("Mar", 31 * scale_factor, Color.blue);
    e.setBar4("Apr", 30 * scale_factor, Color.red);
  }
}



Relevant Pages

  • Re: Java API sound
    ... I was using mixer but port is better solution; ... private void CreateInterface ... public void actionPerformed{ ... int d = -1; ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... private void init() { ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)
  • Graphs disappear when I minimize/maximize my frame
    ... {private int width; ... Bar bar1 = initBar; ... public void setBar1(String label, int height, Color c) ...
    (comp.lang.java.help)
  • JFrame Resize Issues (Redux)
    ... However, when the LookAndFeel is set, Java's Window Manager ... public int OffSetX = 0; ... private Rectangle positRectangle; ... public void componentHidden{ ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... */ public class FlowDOMView extends JPanel {private DefaultTreeModel model = null; ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)