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 4 of 4

Thread: Java applet drawing curved lines using nested for loop

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java applet drawing curved lines using nested for loop

    Hello,

    How can I draw the bottom left and right corner of this image? I'm able to draw them separately, but when putting them together the graphic is incorrect.
    First program is the bottom left corner. Second for the bottom right.
    https://imgur.com/a/1vtVM2A
    import java.awt.*;
    import java.applet.*;
     
     
    public class Lab05v80 extends Applet
    {
     
       public void init()
       {
          setSize(1000, 650); // width, height
       } 
     
    	public void paint(Graphics g)
    	{
    		int width = 980;
    		int height = 630;
    		int x1 = 10;
    		int y1 = 640;
    		int x2 = 990;
    		int y2 = 640;
    		g.drawRect(10,10,width,height);
     
     
    		for (int lines = 0; lines <= 50; lines++)     
                    {              
                       g.drawLine(x1,y1,x2,y2);
                       y2 -= height/49;
                       x1 += width/49;
                     }
     
     
        }
     
    }
    import java.awt.*;
    import java.applet.*;
     
     
    public class Lab05v80 extends Applet
    {
     
       public void init()
       {
          setSize(1000, 650); // width, height
       } 
     
    	public void paint(Graphics g)
    	{
    		int width = 980;
    		int height = 630;
    		int x1 = 10;
    		int y1 = 640;
    		int x2 = 990;
    		int y2 = 640;
    		g.drawRect(10,10,width,height);
     
     
    		for (int lines = 0; lines <= 50; lines++)     
                    {              
                       g.drawLine(x1,y1,x2,y2);
                       y2 -= height/49;
                       x1 += width/49;
     
                     }
                        for (int line1 = 0; line1 <= 50; line1++)
                        {   g.drawLine(x2,y1,x1,y2);
                             y2 -= height/49;
                             x2 -= width/49;
                        }
        }
     
    }
    This is the bottom left corner

    g.drawLine(x2,y1,x1,y2);
    y2 -= height/49;
    x2 -= width/49;

    For the second for loop, is the indentation incorrect or the block structure?
    Last edited by novdecscv22; December 16th, 2020 at 05:38 AM. Reason: incorrect format

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,200
    Thanks
    53
    Thanked 2,626 Times in 2,576 Posts

    Default Re: Java applet drawing curved lines using nested for loop

    How can I draw ...
    You need to find the correct formulas and algorithm for doing the drawings. When you have them, then try writing the java code for it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java applet drawing curved lines using nested for loop

    I don't understand why algorithm is necessary for this. Isn't the control structure(nested for loop) sufficient to draw the curved lines?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,200
    Thanks
    53
    Thanked 2,626 Times in 2,576 Posts

    Default Re: Java applet drawing curved lines using nested for loop

    Isn't the control structure(nested for loop) sufficient to draw the curved lines?
    I don't know what is needed to draw the lines without the design/algorithm for how the code show work.
    There will probably be some loops but without a design I can not say what they would be.

    Here is the algorithm for the posted code:
    select two end points (1 & 2) for a line
    start loop -  several times
       draw a line between the end points
       move end point 1 to the right
       move end point 2 up
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Drawing Lines Loop
    By 02s2k in forum Loops & Control Statements
    Replies: 1
    Last Post: May 3rd, 2013, 11:17 AM
  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. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  4. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM

Tags for this Thread