c#图形速度慢
本文关键字:速度慢 图形 | 更新日期: 2023-09-27 18:09:42
我一直在Visual Studio 2013中学习c#,并且遇到了一个问题。
我画了一堆点(现在是20个)在屏幕上移动。在刷新之间(每毫秒一次),我称之为清理图像。然而,这会导致我在清除后绘制的点被擦除。这样做的最终结果是这些点似乎在屏幕上闪烁。我是一名Java程序员,我用Java的方式处理这些图形。这有错吗?我能做些什么来解决我的问题?
我认为这个错误是由于我的tick方法需要大约9毫秒的时间来运行。
下面是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace AstroidBlast {
public partial class Form1 : Form {
Random r = new Random();
System.Windows.Forms.Timer gameTimer = new System.Windows.Forms.Timer();
Particle[] particles = new Particle[20];
int tick = 0;
public Form1() {
InitializeComponent();
gameTimer.Interval = 1;
gameTimer.Tick += new EventHandler(gameTimer_Tick);
gameTimer.Start();
this.Width = 800;
this.Height = 600;
for (int i = 0; i < particles.Length; i++) {
particles[i] = new Particle(new Velocity(r.Next(0, 360) * (Math.PI / 180), r.NextDouble() *.75 + 0.25), 100, 100, r);
}
}
private void Form1_Load(object sender, EventArgs e) {}
private void gameTimer_Tick(object sender, EventArgs e) {
Graphics g = this.CreateGraphics();
Stopwatch s = new Stopwatch();
s.Start();
g.Clear(Color.White);
for (int i = 0; i < particles.Length; i++)
particles[i].draw(g, Math.Sqrt(tick++));
s.Stop();
Debug.Print(s.ElapsedMilliseconds + "");
}
}
class Particle {
static SolidBrush brush = new SolidBrush(Color.FromArgb(40, 40, 40));
Random r;
Velocity velocity;
double x, y;
public Particle(Velocity v, double x, double y, Random r){
velocity = v;
this.x = x;
this.y = y;
this.r = r;
}
public void draw(Graphics g, double t) {
g.FillEllipse(brush, (int)(velocity.speed * t * Math.Cos(velocity.angle) + x), (int)(velocity.speed * t * Math.Sin(velocity.angle) + y), 8, 8);
}
}
class Velocity {
public double angle, speed;
public Velocity(double angle, double speed) {
this.angle = angle;
this.speed = speed;
}
}
}
不,在c#中这通常不是正确的绘图方式。
你应该重写OnPaint
事件,它为你提供了一个Graphics
对象并对其进行绘图。在你的计时器滴答,你可以Invalidate
全部或部分区域要重新绘制
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// draw here
}
private void gameTimer_Tick(object sender, EventArgs e)
{
this.Invalidate(); // optionally, provide the area to invalidate for better performance
}
你也可以得到更好的性能/更少的闪烁通过告诉你的窗体使用DoubleBuffered
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true
}
用上述更改重新生成代码不会导致明显的闪烁。