Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Drawing Lines Loop

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing Lines Loop

    I am a beginner, I am in my first java course so please take it easy on me. But I have written this code drawing 15 lines. I am stuck on how to make another loop to start the lines again in each corner.
    Here is my code thus far, and the result I am looking for. If anyone can help me or write show me the extra input needed to reach this picture please let me know.

    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;

    public class DrawOneSetOfLines extends JPanel
    {
    public static void main(String args[])
    {
    DrawOneSetOfLines panel = new DrawOneSetOfLines();


    JFrame application = new JFrame();


    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);

    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
    }

    public void paintComponent(Graphics g)
    {

    super.paintComponent(g);

    int linesToDraw = 15;
    int width = getWidth();
    int height = getHeight();
    int number, y, x, dy, dx;
    x = 0;
    y = height;
    number = 15;
    dx = width / number;
    dy = height / number;
    for( int i = 1; i < number; i++ )
    {
    x += dx;
    y -= dy;
    g.drawLine( 0, 0, x, y );


    }
    }
    }
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing Lines Loop

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Is there an algorithm that will draw the lines to make the image that you want drawn? Please post it so it can be compared to the code that was posted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with three lines
    By heythisgreg in forum Java Theory & Questions
    Replies: 9
    Last Post: February 17th, 2013, 05:40 PM
  2. [SOLVED] Drawing lines using angles in Java HELP
    By shadysback in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 02:10 PM
  3. How would you get a JTextArea to know how many lines it had?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2011, 07:58 PM
  4. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  5. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM