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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: CurvesPanel.java,v 1.1 2005/05/25 23:13:25 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.GeneralPath;
  20. public class CurvesPanel extends GradientPanel {
  21. private RenderingHints hints;
  22. private int counter = 0;
  23. public CurvesPanel() {
  24. hints = new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
  25. RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  26. hints.put(RenderingHints.KEY_ANTIALIASING,
  27. RenderingHints.VALUE_ANTIALIAS_ON);
  28. hints.put(RenderingHints.KEY_COLOR_RENDERING,
  29. RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  30. hints.put(RenderingHints.KEY_INTERPOLATION,
  31. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  32. hints.put(RenderingHints.KEY_RENDERING,
  33. RenderingHints.VALUE_RENDER_QUALITY);
  34. }
  35. public void paintComponent(Graphics g) {
  36. counter++;
  37. Graphics2D g2 = (Graphics2D) g;
  38. g2.setRenderingHints(hints);
  39. super.paintComponent(g2);
  40. float width = getWidth();
  41. float height = getHeight();
  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. g2.translate(0, height - 60);
  52. drawCurve(g2,
  53. 30.0f, -15.0f, 50.0f, 15.0f,
  54. width / 2.0f - 40.0f, 1.0f,
  55. 15.0f, -25.0f,
  56. width / 2.0f, 1.0f / 2.0f,
  57. 0.0f, 25.0f,
  58. 15.0f, 6.0f, false);
  59. g2.translate(0, -height + 60);
  60. drawCurve(g2,
  61. height - 35.0f, -5.0f, height - 50.0f, 10.0f,
  62. width / 2.0f - 40.0f, 1.0f,
  63. height - 35.0f, -25.0f,
  64. width / 2.0f, 1.0f / 2.0f,
  65. height - 20.0f, 25.0f,
  66. 25.0f, 4.0f, true);
  67. }
  68. private void drawCurve(Graphics2D g2, 
  69. float y1, float y1_offset,
  70. float y2, float y2_offset,
  71. float cx1, float cx1_offset,
  72. float cy1, float cy1_offset,
  73. float cx2, float cx2_offset,
  74. float cy2, float cy2_offset,
  75. float thickness,
  76. float speed,
  77. boolean invert) {
  78. float width = getWidth();
  79. float height = getHeight();
  80. double offset = Math.sin(counter / (speed * Math.PI));
  81. float start_x = 0.0f;
  82. float start_y = y1 + (float) (offset * y1_offset);
  83. float end_x = width;
  84. float end_y = y2 + (float) (offset * y2_offset);
  85. float ctrl1_x = (float) offset * cx1_offset + cx1;
  86. float ctrl1_y = cy1 + (float) (offset * cy1_offset);
  87. float ctrl2_x = (float) (offset * cx2_offset) + cx2;
  88. float ctrl2_y = (float) (offset * cy2_offset) + cy2;
  89. CubicCurve2D curve = new CubicCurve2D.Double(start_x, start_y, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, end_x, end_y);
  90. GeneralPath path = new GeneralPath(curve);
  91. path.lineTo(width, height);
  92. path.lineTo(0, height);
  93. path.closePath();
  94. Area thickCurve = new Area((Shape) path.clone());
  95. AffineTransform translation = AffineTransform.getTranslateInstance(0, thickness);
  96. path.transform(translation);
  97. thickCurve.subtract(new Area(path));
  98. Color start = new Color(255, 255, 255, 200);
  99. Color end = new Color(255, 255, 255, 0);
  100. Rectangle bounds = thickCurve.getBounds();
  101. GradientPaint painter = new GradientPaint(0, curve.getBounds().y,
  102. invert ? end : start,
  103. 0, bounds.y + bounds.height,
  104. invert ? start : end);
  105. Paint oldPainter = g2.getPaint();
  106. g2.setPaint(painter);
  107. g2.fill(thickCurve);
  108. g2.setPaint(oldPainter);
  109. }
  110. }