This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Draw [Inequality Number Line] with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Draw [Inequality Number Line] with Java " Watch "Draw [Inequality Number Line] with Java " New topic
Author

Draw [Inequality Number Line] with Java

Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi all,

Does anyone knows what Java tools can I use to create the following Inequality Number Line? I do not want Java Applets.



All advices will be greatly appreciated.

Thanks in advance!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Check out the API for the Graphics class. You will find methods to draw lines and text (hint)
For your requirement, essentially what you need to do is
1) Subclass a container like JPanel
2) Override the paintComponent
3) Inside this overridden method, use the Graphics instance to do all the drawing and painting you require. Just don't forget to invoke super.paintComponent() in the first line of your overridden method.
If you run into any problem, show us your code and we can help point you in the right direction

Apple Khoa wrote: I do not want Java Applets.

Huh? What had applets got to do with your requirement?


Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Maneesh Godbole wrote:Check out the API for the Graphics class. You will find methods to draw lines and text (hint)
1) Subclass a container like JPanel


Hi Maneesh Godbole,

Thanks for your prompt reply.

I am aware that Java Graphics class has a drawLine function which allows users to draw a straight line. But from what I understand, it doesn't show the axis values even if I draw the x-axis as a straight line.

You mentioned about JPanel. Can that be implemented into servlets? My ultimate objective is to insert the code into my servlet which will then generate the inequality number line as a pdf file for output.

I learnt from the Internet about JGraphT. May I ask if that is applicable for my case (since JGraphT produces postscript, can that be implemented into servlets?)

Thank you very much for your kind help.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Now you say you need it in a servlet

You might need to use something like JFreeChart http://www.jfree.org/jfreechart/ or Cweolf http://cewolf.sourceforge.net/new/index.html to draw the graph and maybe iText to generate the PDF. If you google around I am sure you fill find more libraries.

I think JGraphT would not be suitable. What you need is a charting library and not a graphing one.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Apple Khoa wrote:You mentioned about JPanel. Can that be implemented into servlets? My ultimate objective is to insert the code into my servlet which will then generate the inequality number line as a pdf file for output.

If you need to do this in an application without a GUI, then you'll not be using JPanel. If you're going to do the drawing yourself (not using some chart or graph library), then you can create a BufferedImage and call getGraphics on it, which will give you a Graphics2D object that you can use to draw on the image (just like you would draw on a JPanel). You can use the ImageIO class to save the image to a png, jpeg or other graphics format file.
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi Jesper De Jong,

May I know what do you mean by "do the drawing yourself"?

Actually, I intend to write the mathematical logic for the topic Inequalities, and the inequality should be shown on the number line as the final answer. Hence, the 'chart' should change according to the inequality. All these will be included in servlets, which will output pdf file.

In my case, is BufferedImage and getGraphics still applicable?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Maneesh and you were talking about libraries, such as JFreeChart. Those libraries are specifically for creating charts and graphs. You just pass the data to the library, tell it what kind of chart to draw, and it will produce an image for you with the chart.

You could try to use such a library, or not. If you don't, then you'll have to draw all the lines, numbers etc. yourself. If you are doing it in an application that doesn't have a GUI (such as a servlet), then you could create a BufferedImage, which is just an image in the computer's memory. You can draw lines, numbers, etc. on a BufferedImage in the same way as you would draw on a JPanel (using a Graphics2D object). When you're done drawing, you can save the BufferedImage in a file using the ImageIO class.

Here's a simple example:
Apple Khoa
Ranch Hand

Joined: Jul 27, 2011
Posts: 37
Hi Jesper De Jong,

Thank you so much for your clear explanation. This is my first time attempting graphs/charts with Java, so pardon me if I ask a lot of questions.

From what I understand in your explanation, the libraries and BufferedImage (with getGraphics) draw graphics that are 'permanent' in the sense that the graphics cannot change according to the Inequalities which will vary (e.g, sometimes X>2, sometimes x<25 etc). Kindly correct me if I am wrong.

My program will set questions and solve the inequalities, and then display it on the number line.
E.g
x - 3 > 5
x > 8
(display x >8 on number line)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Draw [Inequality Number Line] with Java