Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java graphic problem

 
Morgan mor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I want to write two random methods.
1: I want to draw a rectangle and a ellipse 100 times
and the random method should give them different
location and different size.

2: I want to fill them whit different color by help of the
random method
How can I write these methods?

public void paint(Graphics g)
{
x = (int)((Math.random()
 
jessie puls
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this maybe?



you would probably want to set some maximum value for the randoms generated on the size and position of the shapes.
[ November 24, 2004: Message edited by: jessie puls ]
 
Naani Mui
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope the following code would do the one you expected.

import java.applet.*;
import java.awt.*;
import java.util.*;
/*
<Applet Code="Randoms.class" width="600" height="450">
</Applet>
*/
public class Randoms extends Applet implements Runnable
{
Thread t;
int rc,gc,bc;
Color c;

public void init()
{
t = new Thread(this);
t.start();
}

public void run()
{
try
{
for ( int i = 0; i<100; i++)
{
repaint();
t.sleep(500);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}

}
public void paint(Graphics g){
rc = (int)Math.floor(Math.random() * 256);
gc = (int)Math.floor(Math.random() * 256);
bc = (int)Math.floor(Math.random() * 256);
c = new Color(rc,gc,bc);
g.setColor(c);
g.fillRect( (int)Math.floor(Math.random()*400),(int)Math.floor(Math.random()*250),(int)Math.floor(Math.random()*200),(int)Math.floor(Math.random()*200));
rc = (int)Math.floor(Math.random() * 256);
gc = (int)Math.floor(Math.random() * 256);
bc = (int)Math.floor(Math.random() * 256);
c = new Color(rc,gc,bc);
g.setColor(c);
g.fillOval( (int)Math.floor(Math.random()*400),(int)Math.floor(Math.random()*250),(int)Math.floor(Math.random()*200),(int)Math.floor(Math.random()*200));
}
}

Good luck.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic