Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thank you for all you useful answers guys, I solved this problem as following : </p> <blockquote> <p>CreateShapes.java</p> </blockquote> <pre><code>import java.awt.Color; import java.awt.Graphics; public class CreateShapes { private Graphics g; private Color myColor; private int x1, x2, x3, x4; public CreateShapes(int x1, int x2, int x3, int x4, Color myColor){ this.x1 = x1; this.x2 = x2; this.x3 = x3; this.x4 = x4; this.myColor = myColor; } public void draw(Graphics g){ g.setColor(myColor); g.drawLine(x1, x2, x3, x4); } } </code></pre> <blockquote> <p>DrawShapes.java</p> </blockquote> <pre><code>import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel; public class DrawShapes extends JPanel{ private Random randomNumber = new Random(); private CreateShapes[] shapes; private int shapeCounter; public DrawShapes(){ setBackground(Color.WHITE); shapes = new CreateShapes[1+randomNumber.nextInt(15)]; for(int i = 0 ; i &lt; shapes.length ; i++){ int x1 = randomNumber.nextInt(600); int x2 = randomNumber.nextInt(600); int x3 = randomNumber.nextInt(600); int x4 = randomNumber.nextInt(600); Color color = new Color(randomNumber.nextInt(255), randomNumber.nextInt(255), randomNumber.nextInt(255), randomNumber.nextInt(255)); shapes[i] = new CreateShapes(x1, x2, x3, x4, color); } shapeCounter = shapes.length; } public int getShapesNumber(){ return shapeCounter; } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); for(CreateShapes shape : shapes){ shape.draw(g); } } } </code></pre> <blockquote> <p>RandomShapesTest.java</p> </blockquote> <pre><code>import java.awt.BorderLayout; import javax.swing.JLabel; import javax.swing.JFrame; public class RandomShapesTest { public static void main(String[] args){ DrawShapes drawShapes = new DrawShapes(); JFrame application = new JFrame(); String status = String.format("Lines : %d", drawShapes.getShapesNumber()); JLabel statusBar = new JLabel(status); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(drawShapes); application.add(statusBar,BorderLayout.SOUTH); application.setSize(600,600); application.setVisible(true); } } </code></pre> <p>Regards,,,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload