This code has a method to draw a heart.
import java.applet.Applet; import java.awt.*;
public class heartpatterns extends Applet
{
public void paint (Graphics g)
{
heart (25, 75, Color.pink);
}
public void heart (int x, int y, Color c)
{
Graphics g = getGraphics ();
g.setColor (c);
g.fillOval (x, y, 10, 10);
g.fillOval (x + 8, y, 10, 10);
int xArray [] = {x, x + 8, x + 18};
int yArray [] = {y + 6, y + 15, y + 6};
g.fillPolygon (xArray, yArray, 3);
}
}
In the heart method:
g.fillOval (x, y, 10, 10);
g.fillOval (x + 8, y, 10, 10);
int xArray [] = {x, x + 8, x + 18};
int yArray [] = {y + 6, y + 15, y + 6};
g.fillOval (x, y, 10, 10);
g.fillOval (x + 8, y, 10, 10);
int xArray [] = {x, x + 8, x + 18};
int yArray [] = {y + 6, y + 15, y + 6};
Adapt the program in the following ways.
(A) Make a red heart, then a green one, then a blue one. All three should be on the screen at the same time. You will need to do this in paint.
(B) Change the paint method so it puts 25 hearts on the screen in a row.
int x = 0; int y = 0; int i = 0;while (i < 25) { heart (x, y, Color.pink); x += 20; i++;}
(C) Make a loop that puts 25 hearts hearts in a row and then 25 rows of 25 hearts. (put a loop inside a loop). This should happen in the paint method.
int x = 0;
int y = 0;
int i = 0;
int j = 0;
while (i < 50)
{
while (j < 50)
{
heart (x, y, Color.pink);
x += 20;
j++;
}
x = 0;
y += 16;
i++;
j = 0;
}