// filename: Circles1.java
import java.applet.*;
import java.awt.*;
public class Circles1 extends Applet
{
public void paint(Graphics g)
{
int i, y, width, height, size;
width = getSize().width;
height = getSize().height;
size = width / 10; y = height / 2;
for (i = 0; i < 10; i = i + 1)
{
if (i % 2 == 0)
g.setColor(Color.red);
else
g.setColor(Color.green);
g.fillOval(i * size, y, size, size);
}
/* more efficient version using addition operation
for (i = 0, x = 0; i < 10; i = i + 1, x = x + size)
{
if (i % 2 == 0)
g.setColor(Color.red);
else
g.setColor(Color.green);
g.fillOval(x, y, size, size);
}
// Alternating Between Three Colors
for (i = 0, x = 0; i < 10; i = i + 1, x = x + size)
{
if (i % 3 == 0) g.setColor(Color.red);
else if (i % 3 == 1) g.setColor(Color.green);
else if (i % 3 == 2) g.setColor(Color.blue);
g.fillOval(x, y, size, size);
}
*/
}
}
// filename: Circles5.java
import java.applet.*;
import java.awt.*;
public class Circles5 extends Applet
{
public void paint(Graphics g)
{
int row, col, x, y, width, height, sizeX, sizeY;
width = getSize().width;
height = getSize().height;
sizeX = width / 10; sizeY = height / 5;
for (col = 0; col < 10; col = col + 1)
for (row = 0; row < 5; row = row + 1)
g.drawOval(col * sizeX, row * sizeY, sizeX, sizeY);
/* more efficient version
for (x = 0; x < width; x = x + sizeX)
for (y = 0; y < height; y = y + sizeY)
g.drawOval(x, y, sizeX, sizeY);
*/
}
}
// filename: Circles8.java
import java.applet.*;
import java.awt.*;
public class Circles8 extends Applet
{
public void paint(Graphics g)
{
int x, y, r, width, height, sizeX, sizeY;
width = getSize().width;
height = getSize().height;
sizeX = width / 10; sizeY = height / 5;
int color = 0;
for (x = 0; x < width; x = x + sizeX)
for (y = 0; y < height; y = y + sizeY)
for (r = 10; r > 0; r = r - 2, color = color+1)
{
if (color % 3 == 0) g.setColor(Color.red);
else if (color % 3 == 1) g.setColor(Color.green);
else if (color %3 == 2) g.setColor(Color.blue);
g.drawOval(x + r, y + r, sizeX - 2*r, sizeY - 2*r);
}
}
}
// filename: AnimationApplet.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AnimationApplet extends Applet implements ActionListener
{
public Button b;
public int width, size, x, y;
public void init( )
{
b = new Button("Animate Now");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e)
{
int i;
Graphics g = getGraphics();
g.setColor(Color.blue);
width = getSize().width;
y = getSize().height/2;
size = width / 10;
for (x = 0; x + size < width; x = x+1)
{
g.fillOval(x, y, size, size);
for (i = 0; i < 1000000; i = i+1);
}
}
}