This question already has an answer here:
Design and implement an applet that draws 3 circles of random radius (a radius between 20 and 100 inclusively) and 3 rectangles of random width and height (a width between 50 and 150 inclusively and a height between 30 and 90 inclusively) in random locations. Draw a string with your name on the bottom right corner of the applet. Use a width of 400 and a height of 300 for your applet.
import javax.swing.JApplet;
import java.awt.*;
import java.util.Random;
public class Applet extends JApplet
{
public void paint(Graphics circle)
{
// Height and width of window
final int MAX_WIDTH = 400;
final int MAX_HEIGHT = 300;
resize(MAX_WIDTH, MAX_HEIGHT);
// Creates random diameter for each circle
Random rand = new Random();
int diameter1 = rand.nextInt(81) + 20;
int diameter2 = rand.nextInt(81) + 20;
int diameter3 = rand.nextInt(81) + 20;
// Draws circles
circle.drawOval(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_HEIGHT),
diameter1, diameter1);
circle.drawOval(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_HEIGHT),
diameter2, diameter2);
circle.drawOval(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_HEIGHT),
diameter3, diameter3);
// Creates random heights and widths for rectangles
int randWidth1 = rand.nextInt(101) + 50;
int randWidth2 = rand.nextInt(101) + 50;
int randWidth3 = rand.nextInt(101) + 50;
int randHeight1 = rand.nextInt(61) + 30;
int randHeight2 = rand.nextInt(61) + 30;
int randHeight3 = rand.nextInt(61) + 30;
// Draws rectangles
circle.drawRect(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_HEIGHT),
randWidth1, randHeight1);
circle.drawRect(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_WIDTH),
randWidth2, randHeight2);
circle.drawRect(rand.nextInt(MAX_WIDTH), rand.nextInt(MAX_WIDTH),
randWidth3, randHeight3);
}
}
Aucun commentaire:
Enregistrer un commentaire