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

Thread: Please help me loop this method

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me loop this method

    I have some code I wrote, it works, I would like to know how to make this loop without writing certain things 10 times and cant seem to do it. Can you help?

    package linesrects;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.Random;
    import javax.swing.JPanel;

    public class LinesRectsPanel extends JPanel
    {
    static int i;


    int x; //// = (int) ((Math.random() * (1800 - 100)) + 10); //lat
    int y;// = (int) ((Math.random() * (800 - 100)) + 100); // long
    int w; //= (int) ((Math.random() * (300 - 100)) + 100); // width // x lat, y long, ,width, hieght
    int h; //= (int) ((Math.random() * (100 - 50)) + 50); // hiegth
    int x1; //= x+20;
    int x2; //= x+100 ;
    int y1; //= y+80;
    int y2; //= y+45;
    int y3;
    int str;
    int t1;
    int t2;
    int w1;
    int w2;
    int w3;
    int w4;

    int xx;



    public void paintComponent(Graphics g)
    {
    super.paintComponent (g);
    for(int i=0; i<=4; i++);

    Graphics2D g2 = (Graphics2D) g;

    g2.setStroke(new BasicStroke());
    //BasicStroke bs = new BasicStroke();

    g2.drawLine(x1, y2, x2, y2);
    Color c = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color d = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color e = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color f = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color gg = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color j = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color k = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color ll = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color m = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color n = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    Color p = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
    this.setBackground( Color.WHITE ); // keep


    //g.setColor(c); //make random color
    // g.drawLine(x1, y2, x2, y2); // Identify which int affects which make random but within parameters



    g.setColor(d); // set color, then choose shape randomize
    //g2.setStroke(int 10);
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    //g.drawLine(x1, y2, x2, y2);
    g.setColor(e);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;



    g.setColor(e); //randomize

    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(gg);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;

    g.setColor(f); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(ll);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(gg); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(m);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(j); //randomize
    g.drawLine(x1, y2, x2, y2);
    g.setColor(n);

    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4); //Identify which int affects which make random but within parameters

    g.setColor(n);

    g.drawRect( x, y, w, h);
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(k); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(p);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(ll); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(d);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(m); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(e);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(n); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(f);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (7 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;


    g.setColor(p); //randomize
    g2.setStroke(new BasicStroke(str));
    g2.drawLine(x1, y2, x2, y2);
    g2.drawLine(x1, y2, t1, t2);
    g2.drawLine(w1, w2, w3, w4);
    g.setColor(gg);

    g.drawRect( x, y, w, h); //Identify which int affects which make random but within parameters
    g.setColor(Color.BLACK);
    g.drawString(" TARGET AQUIRED", x, y3);
    str = (int) ((Math.random() * (10 - 1)) + 1);
    x= (int) ((Math.random() * (1800 - 100)) + 100); //number up to
    y= (int) ((Math.random() * (800 - 100)) + 100);
    w= (int) ((Math.random() * (300 - 100)) + 100);
    h= (int) ((Math.random() * (100 - 50)) + 50);
    x1 = x+45;
    x2 = x+90 ;
    y1 = y+80;
    y2 = y+35;
    y3 = y-2;
    t1 = x1-5;
    t2 = y2 - 5;
    w1 = t1 + 30;
    w2= t2 - 5;
    w3 = x1 + 50;
    w4 = y2 + 10;
    // Identify which int affects which make random but within parameters




    // make loop ___ times835
    g.drawString(" DRAG BOTTOW EDGE OF PANEL TO RANDOMIZE", 700, 820);
    g.drawRect(0, 830, 1900, 500);
    g.setColor(Color.GREEN);
    g.fillRect(0, 830, 1900, 500);

    }

    }


    package linesrects;

    import java.awt.Color;
    import javax.swing.JFrame;
    import static linesrects.LinesRectsPanel.i;

    public class LinesRects
    {


    public static void main (String [] args)
    {

    // Color e = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));


    JFrame window = new JFrame ( " Aquire Target Lock"); // Make JFRAME

    //LinesRectsPanel linesrects = new LinesRectsPanel();


    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE); // close operation on X



    LinesRectsPanel go1 = new LinesRectsPanel(); // instance through method


    window.add( go1 ); // add shapes to JFRAME




    window.setSize(1900, 910); // size
    window.setVisible(true); //set vis




    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,371
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Please help me loop this method

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

    Please don't create duplicate threads. The other thread was deleted.

  3. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    183
    My Mood
    Sleepy
    Thanks
    17
    Thanked 32 Times in 28 Posts

    Default Re: Please help me loop this method

    After you have done what Greg suggested above - here are some things to ponder:

    Why does it need to loop?
    Where do you need the loop to begin and end?
    What sort of loop do you need (for, do-while.while etc)?
    How many loops do you think it needs?

    Tidy up the post and give us clear ideas about how you want the algorithm to work.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  4. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (June 6th, 2014)

  5. #4
    Junior Member
    Join Date
    Jul 2014
    Location
    Philly area
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help me loop this method

    Well, I know for sure that this is a problem:

    for(int i=0; i<=4; i++);

    This is a for loop that does nothing, five times. You likely want to surround the lines that follow it with brackets, so it actually does stuff in each iteration.

    for(int i=0; i<=4; i++)  {
        //Stuff to do goes here
    }
    Last edited by aliteralmind; July 2nd, 2014 at 03:19 PM.

  6. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me loop this method

    I think, this can be one of the way:

     
    // GB: code tags fixed, code removed

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,371
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Please help me loop this method

    @deepak garg: Please review this forum's position on providing help in The Problem With Spoon-feeding.

Similar Threads

  1. Do while loop skipping method 3 times
    By TheoMuffin in forum What's Wrong With My Code?
    Replies: 23
    Last Post: November 25th, 2013, 05:39 PM
  2. how to loop a method?
    By LeslieInNorthPole in forum Object Oriented Programming
    Replies: 8
    Last Post: August 9th, 2012, 05:57 PM
  3. Help bolean method and while loop !!!!
    By pip0 in forum Loops & Control Statements
    Replies: 15
    Last Post: April 2nd, 2012, 08:29 PM
  4. POST method in a loop
    By kollyisrealisaac in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 26th, 2011, 11:29 AM
  5. private method , loop , switch , help?
    By wolfgar in forum Loops & Control Statements
    Replies: 9
    Last Post: November 8th, 2009, 10:04 PM