Results 1 to 10 of 10
Thread: How to draw a line?
- 10-27-2011, 03:42 PM #1
How to draw a line?
Hi everyone.
I am trying to make a program that I give some points.
And then I would like to get a line graph in a GUI screen.
Something like this:
I already got the window where I am gonna put the line into.
Next to that I also have looked at Line2D, but there I can only make one line from pointA to pointB.
But I would Like one line from pointA to pointB to pointC etc.
Can anyone help me how to do that?
For a line from pointA to pointB I have:
I know I need to get rid of the Double.Java Code:Line2D line1 = new Line2D.Double(0, 0, 200, 200);
But it appears you can only put there a Double or a Float.Last edited by Lund01; 10-27-2011 at 03:46 PM.
- 10-27-2011, 03:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: How to draw a line?
Read all about it in the Path2D class ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-27-2011, 03:58 PM #3
Re: How to draw a line?
I will.
Thanks.
-
Re: How to draw a line?
I would create an ArrayList<Point> to hold your data points, and simply iterate through the List in your paintComponent method. This way, you can create your lines and also the little boxes and diamonds if desired.
- 10-27-2011, 04:34 PM #5
Re: How to draw a line?
I know how to make that arrayList, but how do I get those points to be printed in the frame?
My arraylist example:
Java Code:ArrayList<Double> arrayList = new ArrayList<Double>(); arrayList.add(0.0); arrayList.add(200.0); arrayList.add(100.0);
-
Re: How to draw a line?
You would need an ArrayList that holds two data bits -- the x-value and the y-value of each data point, and then simply re-scale and translate the data as needed and draw using the standard Graphics2D library.
- 10-27-2011, 04:56 PM #7
Re: How to draw a line?
But how do I let the arrayList know that 2 values are with each other?
Like this x-value and y-value. And that they are not sepperate.
-
Re: How to draw a line?
Tell us more. What is the domain of your data? Based on your picture above, it appears that the domain type is month and that the entire domain contains the 12 months of the year.
And what is the range of the data points? Again, based on the image above it appears that the range type is numeric, perhaps int, and going from 0 to 100. If so, then you could use ArrayList<Point> as I suggested above, with the x part of point corresponding to month and the y part corresponding with the function value, 0 to 100.
Then in your drawing method (perhaps best done using Graphics2D extracted from a BufferedImage, you will have to rescale your x values so that 1 scales to 1/12th of the width of the BufferedImage and the y values so that 1 rescales to 1/100th the height of the BufferedImage.
It's not much more than basic algebra and geometry. I suggest you play with some code and see what you can come up with.
- 10-27-2011, 06:03 PM #9
Re: How to draw a line?
I adjusted somethings you said, and I now have the folowing code:
Java Code:/** * @Autheur: Ilse * Datum: Oktober 2011 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.util.*; import java.lang.*; public class Opdracht4B extends Frame { // Just for testing if you can draw a line: Line2D line1 = new Line2D.Double(0, 0, 200, 200); Stroke drawingStroke = new BasicStroke(2); public void paint (Graphics g) { ArrayList<Point> arrayList = new ArrayList<Point>(); arrayList.add(new java.awt.Point(0, 0)); arrayList.add(new java.awt.Point(200, 200)); arrayList.add(new java.awt.Point(100, 100)); Graphics2D graph = (Graphics2D)g; graph.setStroke(drawingStroke); // Give the line from ....... the color blue. graph.setPaint(Color.blue); graph.draw(line1); // TEST if (!arrayList.isEmpty()) { for (int i=0; i < arrayList.size(); i++) System.out.print(arrayList.get(i) + "," ); System.out.println(); } else { System.out.println("ArrayList is empty"); } } public static void main(String[] args) { // Create a window and give it a title. Frame frame = new Opdracht4B(); frame.setTitle("Naam pathway nog invullen"); // Set the size of the window. frame.setSize(800, 800); // Center the window on the screen. frame.setLocationRelativeTo(null); // Exit the program when the window is closed. //frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE); // Make the window visible to the user. frame.setVisible(true); } }Last edited by Lund01; 10-27-2011 at 06:07 PM.
-
Re: How to draw a line?
Some suggestions:
- Use Swing if at all possible, not AWT.
- Again, draw in a BufferedImage, not on a component, and certainly not directly on a top-level container such as a Frame or JFrame.
- The ArrayList creation and its filling with points is likely going to be done separate from the drawing code. The drawing code simply uses the ArrayList and its data.
Similar Threads
-
How to draw line with animation
By hnchnc in forum Java 2DReplies: 1Last Post: 11-18-2010, 10:49 PM -
Draw line
By janes in forum Java 2DReplies: 6Last Post: 03-25-2010, 10:48 PM -
Draw more than one line on run time
By aiman in forum Java AppletsReplies: 3Last Post: 12-10-2009, 02:44 AM -
How to Draw line in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:08 PM -
How to draw a thick line
By johnt in forum Java 2DReplies: 1Last Post: 05-31-2007, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks