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: Need to draw something with lines and loops

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Need to draw something with lines and loops

    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Color;
     
    public class MyDrawing_Start extends JFrame {
    	public MyDrawing_Start() {
    		add (new MyPanel());
    	}
     
    	public static void main (String [] args){
    		MyDrawing_Start frame = new MyDrawing_Start();
    		frame.setTitle("My drawing demo");
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    	}
    }
     
    class MyPanel extends JPanel {
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
     
    		// For color use the either the next 2 lines or g.setColor(Color.BLUE);
    		Color myColor = new Color(0, 0, 255);
    		g.setColor(Color.RED);
    		for(int i= 20, j=20; i<=100; i+=5, j +=10 )
    {
      g.drawLine(100,150,100,50);
     
     
     
    }
     
    		g.setColor(Color.BLUE);
    		for(int i=100, j=100; i<=200; i++, j+= 10)
     
     
    g.drawLine(i,j,50,50);
     
     
     
     
    }
     
     
     
    }


    --- Update ---

    Hi I need help to draw something with lines and loops. I have the looping down but I have never drawn anything with this. The lines go out of control and don't form anything coherent. Can someone help me draw a smiley face, 2 lines for the eyes and one for the mouth, I tried and my lines were never ending in some cases.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to draw something with lines and loops

    Do you have a question about the code you posted?

    The code needs formatting. Too many }s are at the start of the line. }s should be inline beneath the start of the line with the pairing {, not in the first column.
    }s also should be beneath the start of the statements they belong to or at the end of the line.


    help me draw a smiley face
    Take a piece of paper, draw the design you want. Then assign the x and y values for the ends of the lines or the locations of the circles you want to draw.
    When you have the full set of x and y values for the drawing, write the code using those x and y values.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Need to draw something with lines and loops

    Quote Originally Posted by Norm View Post
    Do you have a question about the code you posted?

    The code needs formatting. Too many }s are at the start of the line. }s should be inline beneath the start of the line with the pairing {, not in the first column.
    }s also should be beneath the start of the statements they belong to or at the end of the line.



    Take a piece of paper, draw the design you want. Then assign the x and y values for the ends of the lines or the locations of the circles you want to draw.
    When you have the full set of x and y values for the drawing, write the code using those x and y values.
    Oh sorry for ambigious in my first post. But What I was wondering is how to loop it, effectively, and what are the coordinates. For example (x,x,x,x). What do each of those x's represent when i want to draw a line

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need to draw something with lines and loops

    What do each of those x's represent when i want to draw a line
    The best place to see the definition of the args for a method is the API doc. Here is a link to the API doc:
    Java Platform SE 7 API docs

    Find the class with the method in the lower left frame, click on the link and the doc for the class will be shown in the main frame. Scroll down to where the method you are interested in is described.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Using loops and control statements to draw lines
    By john.adam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 19th, 2012, 09:51 AM
  2. Re: Draw labyrinth ( I need help)
    By tubluforu in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2012, 12:08 AM
  3. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  4. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM