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
}
}