现在的位置: 首页 > 综合 > 正文

跳动的球

2013年06月29日 ⁄ 综合 ⁄ 共 3308字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testgrid
{
    
public partial class Bounce : Form
    
{
        
const int iTimerInterval=25;
        
const int iBallSize=16;
        
const int iMoveSize = 4;
        Bitmap bitmap;
        
int xCenter, yCenter;
        
int cxRadius, cyRadius, cxMove, cyMove, cxTotal, cyTotal;
        
public Bounce()
        
{
            InitializeComponent();
            ResizeRedraw 
= true;
            Timer timer 
= new Timer();
            timer.Interval 
= iTimerInterval;
            timer.Tick 
+= new EventHandler(timer_Tick);
            timer.Start();
            OnResize(EventArgs.Empty);
        }

        
protected override void OnResize(EventArgs e)
        
{
            
//base.OnResize(e);
            Graphics g = CreateGraphics();
            g.Clear(BackColor);
            
float fRadius = Math.Min(ClientSize.Width / g.DpiX, ClientSize.Height / g.DpiY)/iBallSize;
            cxRadius 
= (int)(fRadius * g.DpiX);
            cyRadius 
= (int)(fRadius * g.DpiY);
            g.Dispose();
            cxMove 
= Math.Max(1, cxRadius / iMoveSize);
            cyMove 
= Math.Max(1, cyRadius / iMoveSize);
            cxTotal 
= 2 * (cxRadius + cxMove);
            cyTotal 
= 2 * (cyRadius + cyMove);
            bitmap 
= new Bitmap(cxTotal,cyTotal);
            g 
= Graphics.FromImage(bitmap);
            g.Clear(BackColor);
            DrawBall(g, 
new Rectangle(cxMove, cyMove, 2 * cxRadius, 2 * cyRadius));
            g.Dispose();
            xCenter 
= ClientSize.Width / 2;
            yCenter 
= ClientSize.Height / 2;
        }

        
protected virtual void DrawBall(Graphics g, Rectangle rect)
        
{
            g.FillEllipse(Brushes.Red, rect);
        }

        
void timer_Tick(object sender, EventArgs e)
        
{
            
//throw new Exception("The method or operation is not implemented.");
            Graphics g = CreateGraphics();
            g.DrawImage(bitmap, xCenter 
- cxTotal / 2, yCenter - cyTotal / 2, cxTotal, cyTotal);
            g.Dispose();
            xCenter 
+= cxMove;
            yCenter 
+= cyMove;
            
if ((xCenter + cxRadius >= ClientSize.Width) || (xCenter - cxRadius <= 0))
                cxMove 
= -cxMove;
            
if ((yCenter + cyRadius >= ClientSize.Height) || (yCenter - cyRadius <= 0))
                cyMove 
= -cyMove;
        }

    }

}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace testgrid
{
    
public partial class BouncingGradientBrushBall : Bounce
    
{
        
public BouncingGradientBrushBall()
        
{
            InitializeComponent();
        }

        
protected override void DrawBall(Graphics g, Rectangle rect)
        
{
            GraphicsPath path 
= new GraphicsPath();
            path.AddEllipse(rect);
            PathGradientBrush pgbrush 
= new PathGradientBrush(path);
            pgbrush.CenterPoint 
= new PointF((rect.Left + rect.Right) / 3, (rect.Top + rect.Bottom) / 3);
            pgbrush.CenterColor 
= Color.White;
            pgbrush.SurroundColors 
= new Color[] { Color.Red };
            g.FillRectangle(pgbrush, rect);
        }

    }

}

 

 

抱歉!评论已关闭.