您的位置:首页 > 编程语言 > Java开发

java满天星系列之(四)动态星星结合…

2016-01-21 10:21 429 查看
个人官方网站 :点击进入

原文地址http://blog.csdn.net/monitor1394/article/details/6222209

package org.gui;
import java.awt.Color;  
import java.awt.GradientPaint;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.RadialGradientPaint;  
import java.awt.Rectangle;  
import java.awt.RenderingHints;  
import java.awt.Shape;  
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;  
import javax.swing.JComponent;  
import javax.swing.JFrame;  
import javax.swing.SwingUtilities;  
import javax.swing.Timer;  
import javax.swing.UIManager;  
 
public class StarShine extends JComponent{
 
    private List stars=new
LinkedList();  
    private static Random
random=new Random();  
    private static Color[][]
colors={  
     
  {Color.WHITE, Color.BLACK},
 
     
  {Color.WHITE, Color.BLUE},
 
     
  {Color.ORANGE, Color.PINK},
 
     
  {Color.ORANGE, Color.green}
 
    };
 
    private String
menInfo="";  
    public StarShine(){
 
     
  setBackground(Color.WHITE);
 
     
  //每秒输出内存信息  
     
  new Timer(500, new ActionListener() {
 
     
      public
void actionPerformed(ActionEvent evt) {  
     
     
    //随机多边形
 
     
     
    int centerX
=random.nextInt(getWidth());  
     
     
    int centerY
=random.nextInt(getHeight());  
     
     
    double innerSize = 1 + (25 *
Math.random());  
     
     
    double outerSize = innerSize
+ 10 + (15 * Math.random());  
     
     
    int numPoints = (int)(8 *
Math.random() + 5);  
     
     
   
stars.add(getStar(centerX,centerY,innerSize,outerSize,numPoints));
 
     
     
    //内存信息
 
     
     
    long
tm=Runtime.getRuntime().totalMemory();  
     
     
    long
mm=Runtime.getRuntime().maxMemory();  
     
     
    long
fm=Runtime.getRuntime().freeMemory();  
     
     
    long um=tm-fm;
 
     
     
    menInfo=String.format("%d /
%d MB  %d",
um/(1024*1024),mm/(1024*1024),stars.size());
 
     
     
    repaint();
 
     
      }
 
     
  }).start();  
    }
 
    @Override
 
    protected void
paintComponent(Graphics g) {  
     
  Graphics2D g2d = (Graphics2D)g;
 
     
  //天空背景  
     
  GradientPaint background = new GradientPaint(0f,
0f, Color.GRAY.darker(),  
     
     
    0f, (float)getHeight(),
Color.GRAY.brighter());  
     
  g2d.setPaint(background);
 
     
  g2d.fillRect(0, 0, getWidth(), 4*getHeight()/5);
 
     
  //地面背景  
     
  background = new GradientPaint(0f,
(float)4*getHeight()/5,  
     
     
    Color.BLACK,
 
     
     
    0f, (float)getHeight(),
Color.GRAY.darker());  
     
  g2d.setPaint(background);
 
     
  g2d.fillRect(0, 4*getHeight()/5, getWidth(),
getHeight()/5);  
     
  //开启抗锯齿  
     
 
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
     
     
   
RenderingHints.VALUE_ANTIALIAS_ON);  
     
  //画所有的星星  
     
  for (Shape star : stars) {
 
     
      Rectangle
rect = star.getBounds();  
     
      Point2D
center = new Point2D.Float(  
     
     
     
  rect.x + (float)rect.width / 2.0f,
 
     
     
     
  rect.y + (float)rect.height / 2.0f);
 
     
      float
radius = (float)rect.width / 2.0f;  
     
      float[]
dist = {0.1f, 0.9f};  
     
     
//圆形辐射颜色渐变模式  
     
     
RadialGradientPaint paint = new RadialGradientPaint(center, radius,
 
     
     
     
  dist, colors[random.nextInt(colors.length)]);
 
     
     
g2d.setPaint(paint);  
     
     
g2d.fill(star);  
     
  }  
     
  g2d.drawString(menInfo,10, 10);
 
    }
 
     
    private static Shape
getStar(double x, double y,  
     
      double
innerRadius, double outerRadius,int pointsCount) {
 
     
  GeneralPath path = new GeneralPath();
 
     
  double outerAngleIncrement = 2 * Math.PI /
pointsCount;  
     
  double outerAngle = 0.0;
 
     
  double innerAngle = outerAngleIncrement / 2.0;
 
     
  x += outerRadius;  
     
  y += outerRadius;  
     
  float x1 = (float) (Math.cos(outerAngle) *
outerRadius + x);  
     
  float y1 = (float) (Math.sin(outerAngle) *
outerRadius + y);  
     
  float x2 = (float) (Math.cos(innerAngle) *
innerRadius + x);  
     
  float y2 = (float) (Math.sin(innerAngle) *
innerRadius + y);  
     
  path.moveTo(x1, y1);  
     
  path.lineTo(x2, y2);  
     
  outerAngle += outerAngleIncrement;
 
     
  innerAngle += outerAngleIncrement;
 
     
  for (int i = 1; i < pointsCount; i++) {
 
     
      x1 =
(float) (Math.cos(outerAngle) * outerRadius + x);
 
     
      y1 =
(float) (Math.sin(outerAngle) * outerRadius + y);
 
     
     
path.lineTo(x1, y1);  
     
      x2 =
(float) (Math.cos(innerAngle) * innerRadius + x);
 
     
      y2 =
(float) (Math.sin(innerAngle) * innerRadius + y);
 
     
     
path.lineTo(x2, y2);  
     
      outerAngle
+= outerAngleIncrement;  
     
      innerAngle
+= outerAngleIncrement;  
     
  }  
     
  path.closePath();  
     
  return path;  
    }
 
     
    private static void
createAndShowGUI() {  
     
  final JFrame f = new JFrame("Star Shine");
 
     
 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
     
  f.setSize(800, 500);  
     
  f.add(new StarShine());  
     
  f.setVisible(true);  
     
  f.setLocationRelativeTo(f.getOwner());
 
    }
 
    public static void
main(String args[]) {  
     
  SwingUtilities.invokeLater(new Runnable() {
 
     
      public
void run() {  
     
     
    try {
 
     
     
     
 
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 
     
     
    } catch (Exception ex) {
 
     
     
    }  
     
     
    createAndShowGUI();
 
     
      }
 
     
  });  
    }
 
}  

看效果:



好了
   满天星效果简单实例实现完毕

有问题发我邮箱
  1360461332@qq.com

我的更多文章:

java满天星系列之(四)动态星星结合内存效果 泽0715 新浪博客

(2016-01-12 11:23:41)
java满天星系列之(三)JFrame实现满天星一闪一闪动态效果  泽0715 新浪博客

(2016-01-11 09:46:20)
java满天星系列之(二)JFrame实现满天星效果  泽0715 新浪博客

(2016-01-10 17:45:59)
java满天星系列之(一)Frame实现满天星效果  泽0715 新浪博客

(2016-01-07 11:11:06)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: