dimanche 8 novembre 2015

Draw ovals in random places with random colors that will not intersect

I have to write a code that will do the following- that will draw 10 ovals in random places with random colors. And they must not intersect. The hardest part of the assignment is to make a code that will wrote the names, or the numbers(for example 255,255,255) of that random color in the top of the ovals. I wrote something like this enter code here`

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;


public class Frame extends JFrame {
Frame(){
    super();
    setSize(500, 500);
    setVisible(true);
}
int a[]=new int[9];
int b[]=new int[9];
@Override
public void paint(Graphics g) {
    super.paint(g);
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
a[j]=(int)(20+Math.random()*440);
b[j]=(int)(20+Math.random()*440);
if(a[j]!=a[i]+21 && a[j]!=a[i]-21 && b[j]!=b[i]+21 && b[j]!=b[i]-21)
    g.drawOval(a[j],b[j], 20, 20);
g.setColor(new Color ((int)(1+Math.random()*255),(int)(1+Math.random()*255),(int)(1+Math.random()*255)));

g.drawString("//here must be the name of color of the random oval", a[j], b[j]);

}
    }

}
}




Aucun commentaire:

Enregistrer un commentaire