Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to draw a random color ellipse on random places with Rectangle2D() function in Java

1 Answer

0 votes
package javaapplication1;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import java.awt.Shape;

public class Example extends JPanel {
   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);

      Graphics2D g2d = (Graphics2D) g;

      Random r = new Random();
      int x, y;
       
      for (int i = 1; i <= 10; i++) {
          x = r.nextInt(150) + 1;
          y = r.nextInt(150) + 1;
          g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
          Shape s1 = new Ellipse2D.Double(x, y, 60, 60);
          g2d.fill(s1);
      }
   }

   public static void main(String[] args) {
      JFrame f = new JFrame("Draw With Java");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new Example());
      f.setSize(300, 350);
      f.setVisible(true);
   }
}
 
/*
run:
 
// see the random color ellipse on the run screen
 
*/

 



answered Jan 11, 2016 by avibootz
edited Jan 11, 2016 by avibootz
...