Results 1 to 8 of 8
  1. #1
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default How to draw a simple graph and then plot something onto it?

    Hello, I have an assignment that I cannot seem to understand.

    Basically, we have to draw a 40x20 graph and then within the graph, draw a rectangle based on user input of the x,y co-ordinates.

    We cannot use any special classes as far as I know. I can draw a rectangle, but I don't know how I would be able to draw it within a graph.

    Can anyone please help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your request is a bit unclear. What precisely do you mean by "draw a 40x20 graph"? Many here use JFreeChart to draw complex graphs such as bar graphs, line graphs, candlestick graphs, but I don't think that this is what you're talking about, but then I have no idea what you're talking about. Much luck

  3. #3
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Hello,

    Sorry, I guess that was very unclear. This is what the assignment instructs us to do.

    We have to write functions. The graph of 40x20 will be there from default.

    The program will ask the user of x and y co-ordinates of where He wants to draw the rectangle on the graph.

    It will also ask the user the width and height of the rectangle.

    Taking all that information, the program will draw the rectangle on the specified location on the graph.

    So say, if the user says (15,10) on x,y co-ordinates and (4,4) on width and height, the program outs the character "*" of 4x4 on the specificed co-ordinates on the graph of 40x20.

    I have written functions to draw the rectangle. But I don't know how I can put them on that specific location on the graph. Can you please guide me in the right direction?

    Here's my code so far:

    import java.util.Scanner;

    public class Rectangle
    {

    public static void main (String[] args)
    {
    getInput();
    }

    private static void getInput() // ask for a name from the user and call necessary functions to complete the purpose of the program
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter a width for your rectangle: ");
    int width = input.nextInt();

    System.out.print("Now enter a height for your rectangle: ");
    int height = input.nextInt();
    drawRectangle(width, height);
    }

    private static void drawRectangle(int width, int height)
    {
    for (int i = 0; i < height; i++)
    {
    drawLine(width);
    System.out.println();
    }
    }

    private static void drawLine(int length)
    {
    final char STAR = '*';
    int count = 1;

    while (count <= length)
    {
    System.out.print(STAR);
    count++;
    }
    }

    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm still very unclear by what you mean by a "graph", by what you mean by "drawing on the graph".

  5. #5
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    By graph, I mean a labeled grid. The stars represent the rectangle width and height within the graph.

    For example:

    |
    |
    |
    |
    15 +
    |
    |
    |
    |
    10 +
    | ****
    | ****
    | ****
    | ****
    5 +
    |
    |
    |
    |
    0 +====+====+====+====+====+====+====+====+>
    0 5 10 15 20 25 30 35 40

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    So what creates these axises? Your program? Code that's given to you? And if you want to start a rectangle at the 0 x location does it still have to be translated over some to take the Y axis into consideration? I may sound like I'm doing nothing but harping on details, but I'm afraid that details are all-important here.

  7. #7
    lithium002 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Yes, the program creates the graph and draws the rectangle on the x,y co-ordinates on the graph of the inputted width and height.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    To displace your rectangle to the right, you'll need to print x number of spaces (x being the x location of the rectangle) before you print the * of the rectangle. A for loop should do this for you nicely. Put it into a method and call the method from within the for loop you use to write each line of *'s.

Similar Threads

  1. How to plot a graph for two sets of variables....
    By Megatron in forum New To Java
    Replies: 9
    Last Post: 04-20-2009, 09:20 PM
  2. i want plot realtime graph in java
    By santhosh_el in forum New To Java
    Replies: 3
    Last Post: 02-26-2009, 08:32 AM
  3. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 06:49 PM
  4. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 02:57 PM
  5. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 PM

Tags for this Thread

Posting Permissions

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