2016-09-29 4 views
-1

저는 Visual Studio 2013에서 C#을 배우면서 문제가 발생했습니다.
나는 화면을 가로 질러 움직이는 점들 (지금은 20 개)을 그리고있다. 새로 고침 사이 (매 ms)마다 그래픽을 지우라고합니다. 그러나 이것은 내가 지워지기를 마치면 그림을 그리게됩니다. 이것의 최종 결과는 점들이 화면에서 번쩍이는 것처럼 보입니다. 저는 자바 프로그래머입니다. 자바 에서처럼 그래픽에 접근했습니다. 잘못인가? 문제를 해결하기 위해 무엇을 할 수 있습니까?C# 그래픽이 느림

필자의 틱 (tick) 방법은 약 9ms가 걸린다는 사실 때문에 오류가 발생한다고 생각합니다.

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; 
     } 
    } 
} 
+0

'새로 고침 (매회) 사이에, 나는 그래픽'을 어디에서 호출해야합니까? –

+1

1) 끊임없이'this.CreateGraphics()'를 호출하고 절대로 처리하지 않습니다. 2) 99.9 %의 시간에'this.CreateGraphics()'를하면 안되며'OnPaint' 이벤트를 오버라이드하고 거기에서 드로잉을해야합니다. –

+0

죄송합니다. 모든 것을 직사각형으로 덮어서 고정 된 것으로 바꾸 었습니다. 지금 다시 변경하십시오. –

답변

2

아니, 일반적으로는 C#으로 그리는 올바른 방법이 아니다 :

여기 내 코드입니다.

개체를 제공하고 그 그림을 그리는 OnPaint 이벤트를 재정의해야합니다. 당신의 타이머 틱 내부, 당신은 Invalidate 지역의 전부 또는 일부

또한 얻을 더 나은 성능/양식을 말해서 적은 깜박임이 DoubleBuffered

public Form1() 
{ 
    InitializeComponent(); 
    this.DoubleBuffered = true 
} 

사용하여 코드를 재현 할 수

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 
} 

를 다시 그려야 할 수 있습니다 위의 변경 사항으로 인해 큰 깜박임이 발생하지 않습니다.

+0

고마워요! DoubleBuffered가 트릭을했습니다. –

관련 문제