• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How to write the class BarGrapher based on the class TestGraph?  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write an application that helps the user plot a bar graph of her own. The application first asks the user for the title of the graph. Next, the application asks for the largest value that will be plotted as a bar---this will be used as the label on the graph's y-axis. Then, the application asks the user for the values of the six bars to be drawn. (If the user does not want one of the six bars to appear, she types 0 for its value and newline by itself for the bar's label.) The application displays the bar graph as its answer.
I have already written this but I don't know how to continue.....



 
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Don't do that, well, not now.

Go back to basics. Your bar graph will have four rectangles, axes which are lines, and various labels. You shoud do one thing at a time and finish that before you try something different. You need a bar graph class (actually, I think it should be called Histogram), and you need to paint that. Start small, maybe only painting one rectangle. Then paint two rectangles. Enhance the class gradually until you are painting everything you want. Your code will look something like this:-Now you can gradually develop the Histogram class until you have it working correctly. Then consider how to get that sort of input.

Line 8 starts a new thread, as described here. Lines 15‑23 create an anonymous class which will paint your Histogram, assuming it has an accessible paint(Graphics) method. You may need to scale and translate the Graphics object. Remember that y goes downwards from the top left, and x goes let to right from the top left
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
public class TestGraph
{ public static void main(String[] a)
 { BarGrapher e = new BarGrapher(600, 600);
   e.setTitle("Days in first four months of the year");
   e.setAxes(20, 120, "30", 90);      // x- and y-axes start at  20, 120
               // graph is 90 pixels high; top of graph is labelled "30"
   int scale_factor = 3;  
   e.setBar1("Jan", 31 * scale_factor, Color.red);   // Jan has 31 days
   e.setBar2("Feb", 28 * scale_factor, Color.white); // etc.
   e.setBar3("Mar", 31 * scale_factor, Color.blue);
   e.setBar4("Apr", 30 * scale_factor, Color.red);
 }
}
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Thank you for posting the code; I have copied it with the code button, ad it looks much better now Please start all statements on their own line; look at lines 3‑6 here:-I am afraid I don't know what the graph class is supposed to do; please show us what you have written already.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Staff note (Knute Snortum):

I fixed this image. When using img tags, be sure your URL points to an actual image. Use the Preview button (next to the Submit button) before posting.

 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The graph is supposed to write four bar charts. I can't upload the image but you can google bar graphs in java and look at the simple ones.
Content minimized. Click to view
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sunny Flower wrote:The graph is supposed to write four bar charts. I can't upload the image but you can google bar graphs in java and look at the simple ones.


class BarGraphWriter helps a user draw bar graphs
Methods
setAxes(int x_pos, int y_pos, String top_label, int y_height)   : draw the x- and y-axes of the graph. The pair, x_pos, y_pos, state the coordinates on the window where the two axes begin. The height of the y-axis, stated in pixels, is y_height. (The length of the x-axis will be exactly long enough to display 6 bars.) The label placed at the top of the y-axis is top_label. (The label placed at the bottom of the y_axis is always 0.) See the picture above.
setBar1(String label, int height, Color c) : draw the first bar in the graph, where the label underneath the bar is label, the height of the bar, in pixels, is height, and the bar's color is c. See the picture above.
setBar2(String label, int height, Color c) : draw the second bar in the graph, where the arguments are used in the same way as in setBar1
setBar3(String label, int height, Color c), setBar4(String label, int height, Color c), setBar5(String label, int height, Color c), setBar6(String label, int height, Color c) :draw the third through sixth bars of the graph
Here is the application that drew the above graph:
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Were you given that specification as part of an assignment?
You would usually expect that to be a Swing/JavaFX exercise, and you would draw rectangles of the appropriate size to fit into the dispay. Which is it, Swing or FX? You have been given a very detailed requirement; please show us how you interpret the specification.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is part of an assignment. I have to write the class BarGrapher which contains the methods: setTitle, setAxes setBar1,  setBar2, setBar3 and setBar4. I have to write them something like public void setTitle( String s) { <here I must type the instructions so that this method writes the title> } but I'm lost here, I don't know what to do.
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start small. Forget all those method or the time being, and give your class a paint(Graphics) method that simply fills one rectangle. When you have that working, consider a setBar1() method, and supply the information for bar1. When that is working, consider adding bar 2.

I think your two threa‍ds should be merged into one.
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . I think your two threa‍ds should be merged into one.

See the first two posts in this combined thre‍ad.
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The class should have these methods:
class BarGraphWriter helps a user draw bar graphs
Methods:
setAxes(int x_pos, int y_pos, String top_label, int y_height) (draw the x- and y-axes of the graph. The pair, x_pos, y_pos, state the coordinates on the window where the two axes begin. The height of the y-axis, stated in pixels, is y_height. (The length of the x-axis will be exactly long enough to display 6 bars.) The label placed at the top of the y-axis is top_label. (The label placed at the bottom of the y_axis is always 0.) See the picture above.
setBar1(String label, int height, Color c) (draw the first bar in the graph, where the label underneath the bar is label, the height of the bar, in pixels, is height, and the bar's color is c.) See the picture above.
setBar2(String label, int height, Color c) (draw the second bar in the graph, where the arguments are used in the same way as in setBar1)
setBar3(String label, int height, Color c),
setBar4(String label, int height, Color c),
setBar5(String label, int height, Color c),
setBar6(String label, int height, Color c)

Here is the application that drew the above graph:
Staff note (Knute Snortum):

If you use img tags, be sure that the URL is actually an image. To test this, press the Preview button (next to the Submit button) before you post.

 
Rancher
Posts: 3116
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sunny,

in a previous post in another topic you had your DrawChart class extending JPanel. I think that that is a good idea.

What I would do to make the work a little less difficult, is to give that DrawChart class a BorderLayout as LayoutManager.
You can set the title of the Graph simply as a JLabel in BorderLayout.PageStart.
In BorderLayout.Center I would place a normal JPanel that will draw the Bars for you. I would most certainly use user-coordinates for this panel, so that drawing the bars will be pretty easy,  letting Java do all the scaling for you. The way to achieve this is to use an AffineTransform. To get the scaling precise, you need the maximum and the minimum values of all the bars that you have. So have a look at the AffineTransform class, it really will make your work easier, by avoiding using oixel coordinates.

In BorderLayout.LineStart you can set your top-level label. If you use similar user coordinates as in the center panel, placing the top label will be easy.

In BorderLayout.PageEnd (the bottom part) you can place the labels that come with the bars.

And for the bar-data in the method 'setBarx' I would use a static innner class 'Bar', containing its value, color, label, and perhaps some other fields (for instance a boolean 'mustBeDrawn', that can be set to false if it shouldn't be drawn).
From the method 'setBarx(...)' you can directly create an instance of Bar.

You could have a List<Bar> in your DrawChart-panel, that you use to draw the Bars, and to determine the maximum and minimum values of the Bars. You do not need then to input these values, and you don't need a scaling factor.

So, in essence, say your center drawing panel has a size of 500 by 500 pixels. In pixel coordinates, the top left point would be (0, 0), the bottom right corner would have coordinates (499, 499). If the maximum and minimum values of the bars are 20 and -10, then we could define a coordinate system where the bottom left corner would have coordinates (0, -10) and the top right corner would have coordinates (20, 20).
The first bar can then be drawn as a rectangle with (x, y) being (0, 0) and width = 1, height = the bars given height. The second bar can be drawn at location (2, 0) with width = 1, height = the given value of that bar, et cetera. As said, letting Java do all the scalings for you.

So, in this scheme I wouldn't use the coordinates of the origin, what would be an advantage, since how would you determine what pixel coordinates to use for that point? So, you could simply neglect these values.

To recap:

setAxes(int x_pos, int y_pos, String top_label, int y_height): I would use the top_label to set the label as I described, I would use y_height only in case one of the Bars had a value that is quite an outlier compared to the other Bars values, otherwise I would derive max and min from the Bar data.
setBarx(String label, int height, Color c): I would create instances of the inner Bar class.

For the center panel: if you have the max_y and min_y value, then drawing the axis is simply to draw a line2D from (0, 0) to (20, 0), and from (0, min_y) to (0, max_y).

As a last note: in the previous I said that the with of the drawing panel is 20, in user coordinates. That is arbitrary, but with this Bar1 would be drawn with x-coordinates 0 and 1, Bar2 with x-xoordinates 2 and 3, et cetera, each bar being thus 1 unit wide, with a one unit gap between them. Enough room for 10 bars.

Well, think about if this makes any sense to you. But at least, do have a look at the AffineTransform class. It would eliminate using dreadful pixel coordinates, using tailor made user coordinates instead.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,
Thank you so much for your time and effort to help me.
Unfortunately, this is an assignment for which the teacher said we can't use classes that are already written to write bars and axes. So, I must write the methods myself.
For example, in class we've written this method:

In this assignment I have to write methods similar to this one but I'm confused because I can't think of anything that would draw lines, bars etc.
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anduena Smith wrote:. . . the teacher said we can't use classes that are already written to write bars and axes. . . . .

Why didn't you tell us that earlier? The picture you showed on Saturday with the pretty bars and shadows are quite inconsistent with that instruction. That makes for dreadfully awkward code. You are going to have to write 33+ lines, the first being the headings. Look at line 2 of your code. I hope you aren't being taught to write { and statements on the same line.

I think you are going to have to go back to pencil and paper. Draw an output for your four months as a histogram. Decide whether you want the bars vertical or (much easier) horizontal.

I am going to merge your threads again because I think they constitute several stages of the same problem.
 
Piet Souris
Rancher
Posts: 3116
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anduena,

indeed, that is quite something else! So, no JPanels, no colors, just a lot of println's. Hmm, I agree partly with Campbell, this is nasty and ugly. Enough moaning. To draw a bar, 30 high with a label, you can think of something like:

That is not so difficult, but printing more than one bar, side by side, will be less easy.

But before going on, is this indeed what you are supposed to do? No nice graphics, no nice colors, and looking at you printBee method, what other methods not related to bar diagrams are you supposed to implement?
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . I agree partly with Campbell, this is nasty and ugly. . . .

Unless it is an exercise in writing loops. Does anybody ever write loops complicated enough to draw that sort of thing?

To draw a bar, 30 high . . .

Is that a bar 30 high from the bottom, or 30 deep starting from the top? If you want to start from the bottom, that will make the code even more awkward

But before going on, is this indeed what you are supposed to do? No nice graphics, no nice colors, and looking at you printBee method, what other methods not related to bar diagrams are you supposed to implement?

Agree. Please contact your teachers and ask them for the full details.
Also think of an object‑oriented way to do that, with Bar objects modelling their sizes and headings.
 
Piet Souris
Rancher
Posts: 3116
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(...)Unless it is an exercise in writing loops. Does anybody ever write loops complicated enough to draw that sort of thing?


I've seen that kind of Bar Graphs before, like in SAS Base, I always found thm ugly, but it proofs that it is possible.

Campbel wrote:Is that a bar 30 high from the bottom, or 30 deep starting from the top?


or both! Say you have the average temperature of the first 6 days of January. Some are likely to be positive, and some will be negative.

But indeed, I too wonder what this teacher is up to... I hope that Anduena will keep us informed, because somehow this assignment is intriguing.    
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . or both! . . .

Now, that really would spoil your day, wouldn't it

A bidirectional graph bar would require an OO approach, I would think.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm really confused right now. I'll post the whole assignment and maybe you can figure it out. I'm sorry that I can't explain it very well but I don't really know what I'm supposed to do. Here's the assignment:

1. Write a class that helps a user display a bar graph that displays up to 6 separate bars. Here is an example of such a graph: class BarGraphWriter:
The class should have these methods:
class BarGraphWriter helps a user draw bar graphs
The class should have these methods:
Methods
setAxes(int x_pos, int y_pos, String top_label, int y_height) [draw the x- and y-axes of the graph. The pair, x_pos, y_pos, state the coordinates on the window where the two axes begin. The height of the y-axis, stated in pixels, is y_height. (The length of the x-axis will be exactly long enough to display 6 bars.) The label placed at the top of the y-axis is top_label. (The label placed at the bottom of the y_axis is always 0.) See the picture above.]
setBar1(String label, int height, Color c) [draw the first bar in the graph, where the label underneath the bar is label, the height of the bar, in pixels, is height, and the bar's color is c. See the picture above.]
setBar2(String label, int height, Color c) [draw the second bar in the graph, where the arguments are used in the same way as in setBar1]
setBar3(String label, int height, Color c),
setBar4(String label, int height, Color c),
setBar5(String label, int height, Color c),         [draw the third through sixth bars of the graph]
setBar6(String label, int height, Color c),    

A) Here is the application that drew the above graph:

Test your coding of class BarGraphWriter with TestGraph.

B) Write an application that helps the user plot a bar graph of her own. The application first asks the user for the title of the graph. Next, the application asks for the largest value that will be plotted as a bar---this will be used as the label on the graph's y-axis. Then, the application asks the user for the values of the six bars to be drawn. (If the user does not want one of the six bars to appear, she types 0 for its value and newline by itself for the bar's label.) The application displays the bar graph as its answer.
Notice that the application decides for itself the location of the x- and y-axes. Here is how the interaction between the application and its user might go (the user's responses are stated in italics):

Please type the title of your graph: Days in the months
Please type the value of the largest bar you will draw: 31
Please type the name of the first bar: Jan
Please type the value of the first bar: 31
 ... etc. ...

Note: the picture with the graph is in an earlier reply in this post.
P.s. Thank you all for the help, I really appreciate it!
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using the Color class (is it this class?), that suggests you are supposed to do it on a GUI after all. You have been given a badly‑designed assignment, if you have methods with numbers at the end of them. That suggests your graph class is to be designed poorly, with the methods exposing implementation details, in this case that the class supports six bars. Please show us the picture you have been given as an example; that will be more helpful than anything else. It may also explain what it means about not using graph design software.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks the same as before. So back to what I told you at first. Start by drawing one rectangle. I presume you allowed to use the methods of the Graphics class (and Graphics2D); I can't think of another way to do it.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finished it! Thank you so much! You're a life saver๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š
P.s Happy New Year๐Ÿฅ‚
 
Campbell Ritchie
Marshal
Posts: 63790
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us how you managed it.

And happy new year to you too.
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I drew everything using Graphics.
 
Piet Souris
Rancher
Posts: 3116
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anduena,

well done to get it going! I  was out unfortunately, so was unable to respond.

In another topic, about ScatterPlots, I just gave an example how to use AffineTransforms, to avoid nasty pixel calculations. I also started on your assignment, as a ScatterGraph instead of a Bar graph, but I haven't finished that yet,
If I have, and you are interested, let me know (but only after your teacher has approved your code!).
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Post,
He approved it. I got 25 out of 30 points. I would really want to see it if you can finish it. All the best!
 
Anduena Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet*,( sorry, the autocorrect did it)
He approved it. I got 25 out of 30 points. I would really want to see it if you can finish it. All the best!
 
That is a really big piece of pie for such a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!