我的代码无效!
我用/ **来评论我的问题。
当我关闭钟摆形式时,我有一个OutOfMemory异常,因为我传递一个IntPtr句柄,它变得很大。
我每次都有摆动摆动和松动速度的问题。他们在我的理论中是一个问题,还是我做了一些愚蠢的事情。
下面是我的钟摆类的代码:
using System.Drawing; //new
using System.Windows.Forms; //new
class Pendulum
{
int length = 50;
double angle = Math.PI /2;
double aAcc = -9.81;
double aVel = 0;
double gravity = 0.1;
double mass = 0.2;
Timer timer;
public Pendulum(int frmWidth, int frmHeight, IntPtr handle)
{
timer = new Timer() { Interval = 30 };
timer.Tick += delegate(object sender, EventArgs e)
{
int originX = frmWidth / 2;
int originY = 0;
int bobX; // = frmWidth / 2;
int bobY; // = (int)length;
//to be relative to origin we go:
bobX = originX + (int)(Math.Sin(angle) * length);
bobY = originY + (int)(Math.Cos(angle) * length);
aAcc = -9.81 / length * Math.Sin(angle);
aVel += aAcc * gravity * mass;
angle += aVel * gravity; //angle += aVel;
//aVel = aVel -0.09; /** SPEED UP DRAMATICALLY! SPINS WEIRDLY! **/
DrawPendulum(originX,originY,bobX,bobY,frmWidth,frmHeight, handle);
};
timer.Start();
}
public void DrawPendulum(int originX, int originY, int bobX, int bobY, int frmWidth, int frmHeight, IntPtr handle)
{
using(Bitmap dblBuffer = new Bitmap(frmWidth, frmHeight))
using(Graphics g = Graphics.FromImage(dblBuffer))
using(Graphics f = Graphics.FromHwnd(handle))
{
g.DrawLine(Pens.Black, originX, originY, bobX, bobY);
g.FillEllipse(Brushes.Blue, bobX - 8, bobY, 20, 20); //-8 for tidyness!
f.Clear(Color.White);
f.DrawImage(dblBuffer, new Point(0, 0));
}
}
}
public partial class frmPendulum : Form
{
Pendulum p;
public frmPendulum()
{
frmLogin frm = new frmLogin();
frm.Close();
int frmWidth = this.Width;
int frmHeight = this.Height;
IntPtr Handle = this.Handle;
p = new Pendulum(frmWidth, frmHeight, Handle);
InitializeComponent();
}
private void frmPendulum_Load(object sender, EventArgs e)
{
}
public void TimerDispose()
{
timer.Dispose();
}
}
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e)
{
frmPendulum f = new frmPendulum();
f.Show();
//this.Hide();
}
}
编辑:尝试处理我已经使Pendulum p全局的计时器,以便它可以用来运行我在下面的钟摆类中放入的新方法:
public void TimerDispose()
{
timer.Dispose();
}
这也意味着使计时器全局化。 但是仍然会发生OutOfMemory异常。所有代码都是上面的当前状态。
答案 0 :(得分:0)
多么糟糕......
处理frmPendulum()的Paint()事件并将其e.Graphics
传递给DrawPendulum()。
frmPendulum应该有自己的Timer,只需刷新自己。
在Pendulum中,直接绘制传递的Graphics。摆脱Bitmap和Handle代码。 变量应该都在类级别,因此可以在DrawPendulum()中访问它们。 Pendulum不应该在Tick()事件中绘制自己。
所以看起来应该更像这样:
public partial class frmPendulum : Form
{
private Timer timer;
private Pendulum p = null;
public frmPendulum()
{
InitializeComponent();
this.Shown += new EventHandler(frmPendulum_Shown);
this.Paint += new PaintEventHandler(frmPendulum_Paint);
}
void frmPendulum_Shown(object sender, EventArgs e)
{
p = new Pendulum(this.ClientRectangle.Width, this.ClientRectangle.Height);
timer = new Timer() { Interval = 100 };
timer.Tick += delegate(object s2, EventArgs e2)
{
this.Refresh();
};
timer.Start();
}
void frmPendulum_Paint(object sender, PaintEventArgs e)
{
if (p != null)
{
p.DrawPendulum(e.Graphics);
}
}
}
public class Pendulum
{
int length = 50;
double angle = Math.PI / 2;
double aAcc = -9.81;
double aVel = 0;
double gravity = 0.1;
double mass = 0.2;
int originX = 0;
int originY = 0;
int bobX; // = frmWidth / 2;
int bobY; // = (int)length;
Timer timer;
public Pendulum(int frmWidth, int frmHeight)
{
timer = new Timer() { Interval = 50 };
timer.Tick += delegate(object sender, EventArgs e)
{
originX = frmWidth / 2;
originY = 0;
//to be relative to origin we go:
bobX = originX + (int)(Math.Sin(angle) * length);
bobY = originY + (int)(Math.Cos(angle) * length);
aAcc = -9.81 / length * Math.Sin(angle);
aVel += aAcc * gravity * mass;
angle += aVel * gravity;
};
timer.Start();
}
public void DrawPendulum(Graphics G)
{
G.DrawLine(Pens.Black, originX, originY, bobX, bobY);
G.FillEllipse(Brushes.Blue, bobX - 8, bobY, 20, 20); //-8 for tidyness!
}
}