夏望平,三棵树涂料股份有限公司,厕所装修
今天闲来无事,教大家一个哄妹子的小case。我们需要创建一个心形图案,按照心形图案的位置和长度,对所创建的字符串进行截断并在所需的位置上输出,最终能呈现在屏幕上满满的爱心。废话不多说,直接上源码看效果 ~
package ddd; import java.awt.*; import javax.swing.jframe; public class cardioid extends jframe { //获取屏幕大小 private static final int width = 500; private static final int height = 500; private static int window_width = toolkit.getdefaulttoolkit().getscreensize().width; private static int window_height = toolkit.getdefaulttoolkit().getscreensize().height; public cardioid(){ super("i love you");//设置窗口标题 this.setbackground(color.black); this.setlocation((window_width-width)/2,(window_height-height)/2);//设置窗口位置 this.setsize(width, height);//设置窗口大小 this.setlayout(getlayout());//设置窗口布局 this.setvisible(true);//设置窗口可见 this.setdefaultcloseoperation(dispose_on_close);//设置窗口默认关闭方式 } public void paint(graphics g){ double x,y,r;//横纵坐标以及半径 image image = this.createimage(width, height); graphics pic = image.getgraphics(); for (int i = -2; i < 90; i++) { for (int j = -2; j < 90; j++) { r=math.pi/45+math.pi/45*i*(1-math.sin(math.pi/45*j))*18; x=r*math.cos(math.pi/45*j)*math.sin(math.pi/45*i)+width/2; y=-r*math.sin(math.pi/45*j)+height/3; pic.setcolor(color.magenta); pic.filloval((int)x, (int)y, 2, 2); } g.drawimage(image,0,0,this);//生成图片 } } public static void main(string[] args) { new cardioid(); } }
实现效果如下所示:
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论