椭圆随机彩色同心圆
大家好,直接进入正题,我们今想画一个彩色的椭圆,并且是同心圆状,从圆心到圆外,有不同的颜色的萧条组成。这个题我们可以用java应用小程序Applet,也可以用画板Graphic来完成,不过我们今天试验用Applet尝试来完成。那么,我直接放代码:
这个代码片是调用画板的方法,设置画板的宽度,高度,以及x轴.y轴的对应的坐标点位
import java.awt.Graphics;
public class Ellipse{
int x, y, width, height;
Ellipse(int X, int Y, int w, int h) {
x = X;
y = Y;
width = w;
height = h;
}
public void draw(Graphics g) {
g.drawOval(x, y, width, height);
}
}
第二张代码片,就是通过继承Applet,写方法来实现,因为我们的这个元是睡着高度,宽度的变化而变化的,因此高度、宽度都/2。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Ellipse1 extends Applet{
public void paint(Graphics g) {
int ww = getSize().width / 2;
int hh = getSize().height / 2;
int rr = 0;
while (rr < hh) {
Ellipse cir = new Ellipse(ww - rr, hh - rr, 2 * rr, 2 * rr);
g.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
cir.draw(g);
rr += 8;
}
}
}
好了,这个小实验达成,希望大家喜欢,能对大家有所帮助,java需要长期的坚持。
下面是可爱风的效果图: