## Forum Stats

• 3,874,444 Users
• 2,266,734 Discussions

Discussions

# Graphing and GUIs

Member Posts: 1
edited Jun 8, 2017 4:07PM

Hi,

I have an assignment that requires me to graph some functions.
I'm new to Java, and getting a GUI setup is rather challenging as I don't know how to set up anything except for a Jframe, and some shapes.

I'd like some direction as to where to begin to setup an x/y axis, and graphing a curved function (as opposed to drawLine method from the Graphics library).

If it helps, this is what I'm trying to accomplish

I don't even know where to look for the information on how to complete this, so anything is appreciated.

• edited May 12, 2017 12:08PM

I don't even know where to look for the information on how to complete this, so anything is appreciated.

The Java Tutorials has several trails about how to use the 2D graphics functionality

# Drawing Arbitrary Shapes

`You have already learned how to draw most of shapes represented in the `java.awt.geom` package. To create more complicated geometry, such as polygons, polylines, or stars you use another class from this package, `GeneralPath`.`

As your own 'problem statement' says you will have to compute the 'n' end points of the lines you need (typically into an array) and then loop thru the array drawing each line segment.

Complex shapes are drawn as a series of connected line segments.

You should be able to find PLENTY of other examples on the web.

• Member Posts: 457
edited Jun 8, 2017 4:07PM

I know this is too late for the person that just dumped this in the forum and left, but if you ever hope of finishing a programming class and possibly ever becoming a programmer you have to learn how to do an analysis of the problem.  Everything is given in the problem on how to set things up.

"Yo need to draw n line segments joing the successive points..." -- therein you are actually told there are no curves involved, but just lines connecting point a to point b, it even tell you how many points to use: "Experiemnt with values of n=10, 25, 50, 100, 200 to make the best choice."

if gives you the function and the interval to use, so you can calculate the endpoints easily.

for example part "a" gives an cubic formula and a interval from 0 to 4--that is start at 0 on the X axis and run to 4. that will give Y values at the endpoint of -3 and 65, so you have to create an X that has 4 units and a Y that has 68 units.

For the steps you have on the X if you try 10 points, you get 4/10 or 0.4 units increment along the X axis and a corresponding change in Y value that will be bounded by your endpoints of -3 and 65.

If you read the Java API, I know heaven forbid you actually look something up yourself, you'll see a JPanel startes in the upper left corner at (0, 0), so if you are working with a BufferedImage of say 600x600, then you will have 600/4=125 or 1 unit along your X axis will scale by a factor of 125 when plotting the output. and your Y will scale by 600/68 or approximately 8.8.

There in you have the setup for the problem, now all you have to do is code it.

This discussion has been closed.