Results 1 to 10 of 10
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Question 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:
    Java Code:
     
    Line2D line1 = new Line2D.Double(0, 0, 200, 200);
    I know I need to get rid of the Double.
    But it appears you can only put there a Double or a Float.
    Last edited by Lund01; 10-27-2011 at 03:46 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to draw a line?

    Read all about it in the Path2D class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: How to draw a line?

    I will.
    Thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default 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);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to draw a line?

    Quote Originally Posted by Lund01 View Post
    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.
    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.

  9. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default 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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. How to draw line with animation
    By hnchnc in forum Java 2D
    Replies: 1
    Last Post: 11-18-2010, 10:49 PM
  2. Draw line
    By janes in forum Java 2D
    Replies: 6
    Last Post: 03-25-2010, 10:48 PM
  3. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 02:44 AM
  4. How to Draw line in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •