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

• 11-15-2009, 11:13 PM
lithium002
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.

• 11-15-2009, 11:19 PM
Fubarable
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
• 11-15-2009, 11:54 PM
lithium002
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:

Quote:

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);
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++;
}
}

}
• 11-16-2009, 12:32 AM
Fubarable
I'm still very unclear by what you mean by a "graph", by what you mean by "drawing on the graph".
• 11-16-2009, 12:59 AM
lithium002
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
• 11-16-2009, 01:14 AM
Fubarable
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.
• 11-16-2009, 01:46 AM
lithium002
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.
• 11-16-2009, 02:22 AM
Fubarable
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.