Forums Register Login

Draw [Inequality Number Line] with Java

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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 just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2960 times.
Similar Threads
C++ Or Java For OOP?
Java-4a Please
Help - boolean
How to I know the my URLyBird version?
Equality operator
C# Manifest Files
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:10:15.