当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现表白小程序

Java实现表白小程序

2019年07月19日  | 移动技术网IT编程  | 我要评论

夏望平,三棵树涂料股份有限公司,厕所装修

今天闲来无事,教大家一个哄妹子的小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(); 
  } 
} 

 实现效果如下所示:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网