
Hi I am trying to create a randomly color filled oval but the outcome is a black filled oval this is the code: package assignment2;
import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel;
public class RandomDrawPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
int x = 5;
int y = 7;
Random rcolor = new Random();
int r = rcolor.nextInt(255);
int g1 = rcolor.nextInt(1);
int b = rcolor.nextInt(250);
g.drawOval(x, y, 200, 200);
g.fillOval(x, y, 200, 200);
Color randomColor = new Color(r, g1, b);
}
}
and for the output:
package assignment2;
import javax.swing.JFrame;
public class RandomDrawing {
/**
* @param args
*/
public static void main(String[] args) {
RandomDrawing shape = new RandomDrawing();
shape.go();
}
private void go() {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
RandomDrawPanel oval = new RandomDrawPanel();
frame.getContentPane().add(oval);
/*try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
}*/
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.