今天的内容呢emmm,关于图像形成网址问题,pc端浏览器不知道怎么就打不开了(若有大佬知道怎么弄出来,请务必踹我一下(抱拳!!!))
传送门:http://paulbourke.net/fractals/ifs/
网上扒了一张图,大家凑合着先看哈,一会给大家看效果图
写这类图像代码感受:
- 都是以Math类中的方法作为媒介
- 通过迭代目标的方式进行操作
- 通过循环的方式不断让图像凝实(其实这是人类视觉的错觉,因为所有的图像都是由矢量图根据 数量的增加和距离的把控,让我们有视觉上的冲击)
代码构建:
1、搭建图形窗口的框架
public class Task extends JFrame {
public void initUI() {
JFrame jFrame = this;//构建窗体
jFrame.setTitle("迭代分形");//窗体左上角命名
jFrame.setSize(800, 800);//组件大小
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//窗口关闭停止运行
jFrame.setLocationRelativeTo(null);//窗体居中
jFrame.setVisible(true);//显示窗体
}
2、写入我们要写入的图形方法中的参数
public void drawIFS(Graphics graphics) {
double a = -1.8, b = -2.0, c = -0.5, d = -0.9;
double x = 0d, y = 0d;//设定初始值
for (int i = 0; i < 100000; i++) {
double nx = Math.sin(a * y) + c * Math.cos(a * x);
double ny = Math.sin(x * b) + d * Math.cos(b * y);
x = nx;
y = ny;//迭代目标
int px = (int) (x * 100);//放大目标100倍 控制图像的大小
int py = (int) (y * 100);
graphics.setColor(Color.blue);//给图像上色
graphics.fillRect(px + 400, py + 400, 1, 1);
}
3、重写"绘制容器" paint()方法
public void paint(Graphics graphics)//重绘必须重写paint方法,不然画不出来
{
super.paint(graphics);
}
4、主方法中调用
public static void main(String[] args) {
new Task().initUI();
}
5、在paint方法中调用写好的函数方法
public void paint(Graphics graphics)
{
super.paint(graphics);
drawIFS(graphics);
}
效果图如下:
还有其他的几组图片,把方法中的参数换一下即可
public void DrawPicture(Graphics graphics){
double x=0d,y=0d;
double a=-1.7,b=1.3,c=-0.1,d=-1.2;
for (int i=0;i<100000;i++){
double nx=Math.sin(a*y)+c*Math.cos(a*x);
double ny=Math.sin(b*x)+d*Math.cos(b*y);
x=nx;
y=ny;
int px=(int)(x*50);
int py=(int)(y*50);
graphics.setColor(Color.black);
graphics.fillRect(px+400,py+400,1,1);
}
效果图:
public void drawFen(Graphics graphics)
{
double x = 0d, y=0d;
double a = 1,b = 4,c = 60;
for (int i = 0; i < 1000000000; i++) {
double nx = y - Math.signum(x) * Math.sqrt(Math.abs(b*x-c));
double ny = a-x;
x= nx;
y= ny;
int px = (int)(x);
int py = (int)(y);
graphics.setColor(Color.cyan);
graphics.fillRect(px+400,py+400,1,1);
}
}
public void DrawFen1(Graphics graphics)
{
double a =0.4,b=1,c=0;
double x=0d,y=0d;
for (int i = 0; i < 1000000; i++) {
double nx = y-Math.signum(x)*Math.sqrt(Math.abs(b*x-c));
double ny = a-x;
x=nx;
y=ny;
int px = (int)(x*50);
int py = (int)(y*50);
graphics.setColor(Color.green);
graphics.fillRect(px+400,py+400,1,1);
}
}
}
网址上面还有很多,目前只选了其中几个进行制作,网页打不开求大佬指点(抱拳!!!)