2017-10-06 1 views
0

Ich habe ein Programm, das Quadrate innerhalb einer Form springt, ich muss es auch so machen, dass alle 5 Sekunden die gezeichneten Quadrate Farbe ändern, ich habe einen Timer, um ein zu aktualisieren Listbox auf die Koordinaten jeder Box und ein zweiter Timer mit dem Intervall von 5000, um die Farbe des Quadrats zu ändern. Jetzt versuche ich, wie genau herauszufinden, ändere ich die Farbe der Quadrate mit dem Timer, habe ich eine Methode namens colorchange mit einer Eigenschaft rectcolorC# wie die Farbe des Rechtecks ​​alle 5 Sekunden ändern

private Color Rectcolor { get; set; } 

} 
    public Color Colorchange() 
    { 
     Rectcolor = Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256)); 
     return Rectcolor; 
    } 

in Form1 genannt habe ich das timer2 tick Ereignis mit einem foreach gehen durch die Boxen

public partial class Form1 : Form 
{ 
    Random Randcolor = Box.randcolor; 
    Random Rand = Box.rand; 
    List<Box> Boxes = new List<Box>(); 

    public Form1() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     this.SetStyle(ControlStyles.UserPaint, true); 

    } 
    private void timer2_Tick(object sender, EventArgs e) 
    { 
     foreach (Box tmp in Boxes) 
     { 
      tmp.Colorchange(); 
     } 
    } 

, wenn ich das Programm nichts geschieht mit den sich bewegenden recht~~POS=TRUNC laufen, wie mache ich colorchange tatsächlich die Farbe des Rechtecks ​​ändern?

Voll Box Klassencode

class Box 
{ 
    protected Rectangle myRectangle; 
    protected int xStep; 
    protected int yStep; 
    protected Form pForm; 
    protected Label myLocation; 
    protected Color myColor; 
    public static Random rand = new Random(); 
    public static Random randcolor = new Random(); 

    public Box(int x, int y, int s, Form pF) 
    { 
     myColor = new Color(); 
     myColor = Colorchange(); 
     myLocation = new Label(); 
     myLocation.BackColor = Color.Transparent; 
     myLocation.Left = x; 
     myLocation.Top = y; 
     myLocation.ForeColor = Colorchange(); 
     pF.Controls.Add(myLocation); 
     pForm = pF; 
     myRectangle = new Rectangle(x, y, s, s); 
     myRectangle.X = x; 
     myRectangle.Y = y; 
     xStep = rand.Next(1, 15); 
     yStep = rand.Next(1, 15); 
    } 

    public int X { get { return myRectangle.X; } } 
    public int Y { get { return myRectangle.Y; } } 
    private int Width { get { return myRectangle.Width; } } 
    private int Height { get { return myRectangle.Height; } } 
    private Color Rectcolor { get; set; } 
    public void Move() 
    { 
     myRectangle.X += xStep; 
     myRectangle.Y += yStep; 
     if (myRectangle.X <= 0) 
      xStep *= -1; 
     if (myRectangle.X + myRectangle.Width >= pForm.ClientSize.Width) 
      xStep *= -1; 
     if (myRectangle.Y <= 0) 
      yStep *= -1; 
     if (myRectangle.Y + myRectangle.Height >= pForm.ClientSize.Height) 
      yStep *= -1; 
     //if (myRectangle.X >500) 
     // xStep *= -1; 

    } 
    public Color Colorchange() 
    { 
     Rectcolor = Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256)); 
     return Rectcolor; 
    } 


    public static bool Bounce(Box One, Box Two) 
    { 
     if (One.X + One.Width < Two.X) 
      return false; 
     if (Two.X + Two.Width < One.X) 
      return false; 
     if (One.Y + One.Height < Two.Y) 
      return false; 
     if (Two.Y + Two.Height < One.Y) 
      return false; 
     return true; 
    } 
    //public void collision() 
    //{ 

    // xStep *= -1; 
    // yStep *= -1; 
    //} 
    public void Draw(Graphics g) 
    { 
     g.DrawRectangle(new Pen(Color.Blue), myRectangle); 

     myLocation.Top = myRectangle.Y -15; 
     myLocation.Left = myRectangle.X+(myRectangle.Width/4); 
     myLocation.Text = myRectangle.X.ToString() + ", " + myRectangle.Y.ToString(); 

    } 

i den Timer auf dem buttone Klick starten, werde ich Sie

public partial class Form1 : Form 
{ 
    Random Randcolor = Box.randcolor; 
    Random Rand = Box.rand; 
    List<Box> Boxes = new List<Box>(); 

    public Form1() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     this.SetStyle(ControlStyles.UserPaint, true); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // Box tmp = new Box(100, 100, 40); 
     // Boxes.Add(tmp); 
     for (int x = 1; x < 10; x++) 
     { 
      Boxes.Add(
       new Box(
        Rand.Next(100 + x * 10, this.ClientSize.Width - 100), 
       Rand.Next(100 + x * 10, this.ClientSize.Height - 100), 
       Rand.Next(100 + x * 15), this)); 

      //Boxes.Add(new Box(100 + x * 10, 100 + x * 10, x * 15, this)); 

     } 
     timer1.Enabled = true; 
     timer2.Enabled = true; 

    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     foreach (Box tmp in Boxes) 
     { 
      tmp.Draw(e.Graphics); 

     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     listBox1.Items.Clear(); 
     foreach (Box tmp in Boxes) 
     { 
      tmp.Move(); 
      listBox1.Items.Add(tmp.X + ", " + tmp.Y); 
     } 
     this.Invalidate(false); 


    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void timer2_Tick(object sender, EventArgs e) 
    { 
     foreach (Box tmp in Boxes) 
     { 
      tmp.Colorchange(); 
     } 
    } 

} 

form1 geben}

+0

Bitte geben Sie den Code für Ihre 'Box' Klasse an. – oRole

+0

wurde aktualisiert – alvintron

+0

Wo starten Sie die Timer? – JMA

Antwort

0

Ihre ColorChange() Methode nie wirklich 'gilt' die neue Farbe zum Rectangle Objekt innerhalb Ihrer Box Klasse.

Innerhalb Ihrer Box Klasse benötigen Sie ein neues Objekt Graphics. Sie könnten es als Feld festlegen oder es als Parameter für den Konstruktor übergeben, da Sie es ohnehin in der Draw()-Methode verwenden.

 private Graphics g; 

     ... 

     public Box(int x, int y, int s, Form pF) 
     { 
      g = pF.CreateGraphics(); 

      ... 
     } 

Sie werden auch Ihre ColorChange() Methode ändern müssen, so dass die Rectangle auf die gewünschte Farbe gefüllt wird:

public Color Colorchange() 
{ 
    this.Rectcolor = Color.FromArgb(randcolor.Next(256), randcolor.Next(256), randcolor.Next(256)); 

    // You need to fill the rectangle with the desired color again 
    this.g.FillRectangle(new SolidBrush(this.Rectcolor), this.myRectangle); 
    return Rectcolor; 
} 
+0

die Rechtecke ändern, aber nur für eine Sekunde, wird es immer wieder blau gezeichnet – alvintron

+0

Leider kann ich Ihr Problem nicht neu erstellen, wegen der fehlenden XAML und Code für Timer. Treiben deine Timer jede Sekunde? – oRole

+0

timer1 hat ein Intervall von 100 und timer2 hat ein Intervall von 5000 – alvintron

0
g.DrawRectangle(new Pen(Color.Blue), myRectangle); 

diese Linie zeichnet ein blaues Rechteck. Egal welche Farbe Sie auf "myColor" eingestellt haben, es wird blau dargestellt. Ändern Sie dies zu

g.DrawRectangle(new Pen(myColor), myRectangle); 

sollte Ihr Problem beheben.

Verwandte Themen