CirclesPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: CirclesPanel.java,v 1.1 2005/05/25 23:13:26 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.demo.login.romain;
  8. import java.awt.Color;
  9. import java.awt.GradientPaint;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. import java.awt.Paint;
  13. import java.awt.Rectangle;
  14. import java.awt.RenderingHints;
  15. import java.awt.Shape;
  16. import java.awt.geom.AffineTransform;
  17. import java.awt.geom.Area;
  18. import java.awt.geom.CubicCurve2D;
  19. import java.awt.geom.Ellipse2D;
  20. import java.awt.geom.GeneralPath;
  21. public class CirclesPanel extends GradientPanel {
  22. private RenderingHints hints;
  23. private int counter = 0;
  24. public CirclesPanel() {
  25. hints = new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
  26. RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  27. hints.put(RenderingHints.KEY_ANTIALIASING,
  28. RenderingHints.VALUE_ANTIALIAS_ON);
  29. hints.put(RenderingHints.KEY_COLOR_RENDERING,
  30. RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  31. hints.put(RenderingHints.KEY_INTERPOLATION,
  32. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  33. hints.put(RenderingHints.KEY_RENDERING,
  34. RenderingHints.VALUE_RENDER_QUALITY);
  35. }
  36. public void paintComponent(Graphics g) {
  37. counter++;
  38. Graphics2D g2 = (Graphics2D) g;
  39. g2.setRenderingHints(hints);
  40. super.paintComponent(g2);
  41. float width = getWidth();
  42. g2.translate(0, -30);
  43. drawCurve(g2,
  44. 20.0f, -10.0f, 20.0f, -10.0f,
  45. width / 2.0f - 40.0f, 10.0f,
  46. 0.0f, -5.0f,
  47. width / 2.0f + 40, 1.0f,
  48. 0.0f, 5.0f,
  49. 50.0f, 5.0f, false);
  50. g2.translate(0, 30);
  51. drawCircles(g2, 45.0f, 30.0f, 16.0f, 1.0f, true, false);
  52. drawCircles(g2, 40.0f, 24.0f, 12.0f, 2.0f, false, false);
  53. drawCircles(g2, getHeight() - 25.0f, 50.0f, 0.0f, 1.0f, false, true);
  54. drawCircles(g2, getHeight() - 40.0f, 70.0f, 20.0f, 2.0f, true, true);
  55. g2.translate(width - 150, 0);
  56. drawCircles(g2, getHeight() - 110.0f, 300.0f, width, 0.0f, false, true);
  57. g2.translate(-width + 150, 0);
  58. }
  59. private void drawCircles(Graphics2D g2, float y, float diameter,
  60. float gap, float speed, boolean invertDirection,
  61. boolean invertGradient) {
  62. float width = getWidth();
  63. Color start = new Color(255, 255, 255, 150);
  64. Color end = new Color(255, 255, 255, 0);
  65. GradientPaint painter = new GradientPaint(0, y,
  66. invertGradient ? end : start,
  67. 0, y + diameter,
  68. invertGradient ? start : end);
  69. Paint oldPainter = g2.getPaint();
  70. g2.setPaint(painter);
  71. float block = diameter + gap;
  72. float startX = invertDirection ? -(counter % block) * speed :
  73. (counter % block) * speed - speed * block;
  74. for (float x = startX; x < width; x += block) {
  75. Ellipse2D circle = new Ellipse2D.Double(x, y, diameter, diameter);
  76. g2.fill(circle);
  77. }
  78. g2.setPaint(oldPainter);
  79. }
  80. private void drawCurve(Graphics2D g2, 
  81. float y1, float y1_offset,
  82. float y2, float y2_offset,
  83. float cx1, float cx1_offset,
  84. float cy1, float cy1_offset,
  85. float cx2, float cx2_offset,
  86. float cy2, float cy2_offset,
  87. float thickness,
  88. float speed,
  89. boolean invert) {
  90. float width = getWidth();
  91. float height = getHeight();
  92. double offset = Math.sin(counter / (speed * Math.PI));
  93. float start_x = 0.0f;
  94. float start_y = y1 + (float) (offset * y1_offset);
  95. float end_x = width;
  96. float end_y = y2 + (float) (offset * y2_offset);
  97. float ctrl1_x = (float) offset * cx1_offset + cx1;
  98. float ctrl1_y = cy1 + (float) (offset * cy1_offset);
  99. float ctrl2_x = (float) (offset * cx2_offset) + cx2;
  100. float ctrl2_y = (float) (offset * cy2_offset) + cy2;
  101. CubicCurve2D curve = new CubicCurve2D.Double(start_x, start_y, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, end_x, end_y);
  102. GeneralPath path = new GeneralPath(curve);
  103. path.lineTo(width, height);
  104. path.lineTo(0, height);
  105. path.closePath();
  106. Area thickCurve = new Area((Shape) path.clone());
  107. AffineTransform translation = AffineTransform.getTranslateInstance(0, thickness);
  108. path.transform(translation);
  109. thickCurve.subtract(new Area(path));
  110. Color start = new Color(255, 255, 255, 200);
  111. Color end = new Color(255, 255, 255, 0);
  112. Rectangle bounds = thickCurve.getBounds();
  113. GradientPaint painter = new GradientPaint(0, curve.getBounds().y,
  114. invert ? end : start,
  115. 0, bounds.y + bounds.height,
  116. invert ? start : end);
  117. Paint oldPainter = g2.getPaint();
  118. g2.setPaint(painter);
  119. g2.fill(thickCurve);
  120. g2.setPaint(oldPainter);
  121. }
  122. }