Оглавление |
Назад
// =======================================
// ColorInterface.java
//
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// =======================================
import java.applet.*;
import java.awt.*;
// ============================================
// ColorInterface
// ============================================
public class ColorInterface extends Applet
{
DrawRect m_DrawRectThread = null;
public String getAppletInfo()
{
return "Name: Color Rectangles";
}
public void paint(Graphics g)
{
Dimension dimAppWndDimension = size();
g.setColor(Color.yellow);
g.fillRect(0, 0,
dimAppWndDimension.width - 1,
dimAppWndDimension.height - 1);
g.setColor(Color.black);
g.drawRect(0, 0,
dimAppWndDimension.width - 1,
dimAppWndDimension.height - 1);
}
public void start()
{
if (m_DrawRectThread == null)
{
m_DrawRectThread = new DrawRect(this);
m_DrawRectThread.start();
}
}
public void stop()
{
if (m_DrawRectThread != null)
{
m_DrawRectThread.stop();
m_DrawRectThread = null;
}
}
}
// ============================================
// DrawRect
// ============================================
class DrawRect extends Thread
{
Graphics g;
Dimension dimAppWndDimension;
public DrawRect(Applet Appl)
{
g = Appl.getGraphics();
dimAppWndDimension = Appl.size();
}
public void run()
{
MyColorRect rc = new MyColorRect();
while (true)
{
rc.setRandomSize(dimAppWndDimension);
rc.setRandomColor();
rc.drawMyRect(g);
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
stop();
}
}
}
}
// ============================================
// DrawMyRectangles
// ============================================
interface DrawMyRectangles
{
void drawMyRect(Graphics g);
}
// ============================================
// RandomValue
// ============================================
interface RandomValue
{
int getRandomValue(int maxValue);
}
// ============================================
// MyColorRect
// ============================================
class MyColorRect
extends MyRect
implements DrawMyRectangles
{
protected Color rectColor;
MyColorRect()
{
super();
rectColor = Color.black;
}
public void setRandomColor()
{
int rColor, gColor, bColor;
rColor = getRandomValue(255);
gColor = getRandomValue(255);
bColor = getRandomValue(255);
rectColor = new Color(
rColor, gColor, bColor);
}
public void drawMyRect(Graphics g)
{
g.setColor(rectColor);
g.fillRect(x, y, width, height);
}
}
// ============================================
// MyRect
// ============================================
class MyRect
implements DrawMyRectangles, RandomValue
{
protected int x;
protected int y;
protected int width;
protected int height;
MyRect()
{
x = y = width = height = 0;
}
public void setRandomSize(
Dimension dimAppWndDimension)
{
x = getRandomValue(
dimAppWndDimension.width);
y = getRandomValue(
dimAppWndDimension.height);
width = getRandomValue(
dimAppWndDimension.width) / 2;
height = getRandomValue(
dimAppWndDimension.height) / 2;
}
public void drawMyRect(Graphics g)
{
g.fillRect(x, y, width, height);
}
public int getRandomValue(int maxValue)
{
return (int)(maxValue * Math.random());
}
}
Назад |