教你如何用java表白你的女神

ゝ一世哀愁。 2021-06-10 20:39 655阅读 0赞

想表白的看过来
代码:
import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;``

class StarShine extends JComponent{
private List stars=new LinkedList();
private static Random random=new Random();
private static Color[][] colors={
{Color.WHITE, Color.ORANGE},
{Color.WHITE, Color.BLUE},
{Color.ORANGE, Color.PINK},
{Color.ORANGE, Color.green},
{Color.PINK,Color.green},
{Color.WHITE,Color.PINK}
};
private String menInfo=””;
public StarShine(){
setBackground(Color.WHITE);

  1. //new Timer(delay,taskPerformer).start();
  2. //构造Timer时要指定一个延迟参数和一个ActionListener
  3. //这里每100ms重绘一次图。
  4. new Timer(100, new ActionListener() {
  5. @Override
  6. public void actionPerformed(ActionEvent evt) {
  7. //随机五角星
  8. int centerX =random.nextInt(getWidth());
  9. //以显示图形框的宽度为上限产生随机整数,作为五角星中心点的横坐标。
  10. int centerY =random.nextInt(getHeight());
  11. //以显示图形框的高度为上限产生随机整数,作为五角星中心点的纵坐标。
  12. double innerSize = 1 + (5 * Math.random());
  13. double outerSize = innerSize + 5 + (15 * Math.random());
  14. stars.add(getStar(centerX,centerY,innerSize,outerSize,5));
  15. //内存信息
  16. long tm=Runtime.getRuntime().totalMemory();
  17. long mm=Runtime.getRuntime().maxMemory();
  18. long fm=Runtime.getRuntime().freeMemory();
  19. long um=tm-fm;
  20. menInfo=String.format("%d / %d MB %d", um/(1024*1024),mm/(1024*1024),stars.size());
  21. repaint();
  22. }
  23. }).start();
  24. }
  25. @Override
  26. protected void paintComponent(Graphics g) {
  27. Graphics2D g2d = (Graphics2D)g;
  28. //天空背景
  29. GradientPaint background = new GradientPaint(0f, 0f, Color.GRAY.darker(),
  30. 0f, (float)getHeight(), Color.GRAY.brighter());
  31. //GradientPaint类提供了使用线性颜色渐变模式填充 Shape 的方法,分周期渐变和非周期渐变两种。
  32. //我们定义的天空是从上到下由深灰到浅灰渐变,地面是从距底部五分一处到底部由黑到深灰渐变。
  33. // GradientPaint (float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)
  34. g2d.setPaint(background);
  35. g2d.fillRect(0, 0, getWidth(), 4*getHeight()/5);
  36. //地面背景
  37. background = new GradientPaint(0f, (float)4*getHeight()/5,
  38. Color.BLACK,
  39. 0f, (float)getHeight(), Color.GRAY.darker());
  40. g2d.setPaint(background);
  41. g2d.fillRect(0, 4*getHeight()/5, getWidth(), getHeight()/5);
  42. //导入图片
  43. Image img = Toolkit.getDefaultToolkit().getImage("D:\\tupian\\表白图片.jpg");
  44. g2d.drawImage(img, 0, 230, 500, 600, Color.LIGHT_GRAY, this);
  45. //开启抗锯齿
  46. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  47. RenderingHints.VALUE_ANTIALIAS_ON);
  48. //画所有的星星
  49. for (Shape star : stars) {
  50. Rectangle rect = star.getBounds();
  51. Point2D center = new Point2D.Float(
  52. rect.x + (float)rect.width / 2.0f,
  53. rect.y + (float)rect.height / 2.0f);
  54. float radius = (float)rect.width / 2.0f;
  55. float[] dist = {0.1f, 0.9f};
  56. //圆形辐射颜色渐变模式
  57. RadialGradientPaint paint = new RadialGradientPaint(center, radius,
  58. dist, colors[random.nextInt(colors.length)]);
  59. //RadialGradientPaint 类提供使用圆形辐射颜色渐变模式填充某一形状的方式。
  60. //用户可以指定两种或多种渐变颜色,此绘制将在颜色与颜色之间提供一个插值。星星就是采用这种渐变方式进行渲染的。
  61. //这是一种非常有趣的渐变方式,通过不同的参数,可以实现绚丽多彩的图形。
  62. g2d.setPaint(paint);
  63. g2d.fill(star);
  64. g2d.clipRect(0,0,1000,520);//限定显示区域
  65. //写字
  66. g2d.setColor(Color.ORANGE);
  67. g2d.setFont(new Font("华文新魏", 2, 66));
  68. g2d.drawString("亲 爱 的 ~", 170, 120);
  69. g2d.setFont(new Font("华文新魏", 3, 100));
  70. g2d.drawString("嫁", 580, 250);
  71. g2d.drawString("给", 800, 350);
  72. g2d.drawString("我", 600, 420);
  73. g2d.drawString("吧", 800, 500);
  74. }
  75. g2d.drawString(menInfo,10, 10);
  76. }
  77. /**
  78. * 获得一个随机边的多边形
  79. * x 中心点X
  80. * y 中心点Y
  81. * innerRadius 内圆半径
  82. * outerRadius 外圆半径
  83. * pointsCount 角数
  84. * 一个多边形
  85. */
  86. private static Shape getStar(double x, double y,
  87. double innerRadius, double outerRadius,int pointsCount) {
  88. GeneralPath path = new GeneralPath();
  89. double outerAngleIncrement = 2 * Math.PI / pointsCount;
  90. double outerAngle = 0.0;
  91. double innerAngle = outerAngleIncrement / 2.0;
  92. x += outerRadius;
  93. y += outerRadius;
  94. float x1 = (float) (Math.cos(outerAngle) * outerRadius + x);
  95. float y1 = (float) (Math.sin(outerAngle) * outerRadius + y);
  96. float x2 = (float) (Math.cos(innerAngle) * innerRadius + x);
  97. float y2 = (float) (Math.sin(innerAngle) * innerRadius + y);
  98. path.moveTo(x1, y1);
  99. //moveTo (float x, float y): 通过移动到指定的坐标(以 float 精度指定),将一个点添加到路径中。
  100. path.lineTo(x2, y2);
  101. //lineTo (float x, float y): 通过绘制一条从当前坐标到指定新坐标(以 float 精度指定)的直线,
  102. //将一个点添加到路径中。
  103. outerAngle += outerAngleIncrement;
  104. innerAngle += outerAngleIncrement;
  105. for (int i = 1; i < pointsCount; i++) {
  106. x1 = (float) (Math.cos(outerAngle) * outerRadius + x);
  107. y1 = (float) (Math.sin(outerAngle) * outerRadius + y);
  108. path.lineTo(x1, y1);
  109. x2 = (float) (Math.cos(innerAngle) * innerRadius + x);
  110. y2 = (float) (Math.sin(innerAngle) * innerRadius + y);
  111. path.lineTo(x2, y2);
  112. outerAngle += outerAngleIncrement;
  113. innerAngle += outerAngleIncrement;
  114. }
  115. path.closePath();
  116. //closePath (): 通过绘制一条向后延伸到最后一个 moveTo 的坐标的直线,封闭当前子路径。
  117. return path;
  118. }
  119. /** 创建界面 */
  120. private static void createAndShowGUI() {
  121. final JFrame f = new JFrame("亲爱的~");
  122. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  123. f.setSize(1000, 800);
  124. f.add(new StarShine());
  125. f.setVisible(true);
  126. f.setLocationRelativeTo(f.getOwner());
  127. }
  128. public static void main(String args[]) {
  129. createAndShowGUI();
  130. }

}

运行结果:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,655人围观)

还没有评论,来说两句吧...

相关阅读