GDI+ Example: Continually Moving Lines |
|
Introduction
This GDI+ example illustrates two perpendicular lines that are drawn on a form. To make it interesting (instead of two simple lines), the intersection of the lines keep moving, and consequently, the lines.
Practical Learning: Creating the Application
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ContinuallyMovingLines { public partial class Exercise : Form { static int x; static int y; static bool IsMovingDown; static bool IsMovingRight; public Exercise() { InitializeComponent(); } private void Exercise_Load(object sender, EventArgs e) { x = 0; y = 0; IsMovingRight = false; IsMovingDown = false; BackColor = Color.Black; ClientSize = new System.Drawing.Size(745, 532); } } }
private void Exercise_Paint(object sender, PaintEventArgs e) { // If the status of the origin indicates that it is moving right, // then increase the horizontal axis if (IsMovingRight == true) x++; else // If it's moving left, then decrease the horizontal movement x--; // If the status of the origin indicates that it is moving down, // then increase the vertical axis if (IsMovingDown == true) y++; else // Otherwise, decrease it y--; // Collision: if the axis hits the right side of the screen, // then set the horizontal moving status to "Right", which will be used // by the above code if ((x + 40) > ClientSize.Width) IsMovingRight = false; if (x < 0) IsMovingRight = true; ; if ((y + 40) > ClientSize.Height) IsMovingDown = false; if (y < 0) IsMovingDown = true; // Draw the new axis e.Graphics.DrawLine(Pens.Aqua, x + 20, 0, x + 20, ClientSize.Height); e.Graphics.DrawLine(Pens.Aqua, 0, y + 20, ClientSize.Width, y + 20); }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ContinuallyMovingLines
{
public partial class Exercise : Form
{
static int x;
static int y;
static bool IsMovingDown;
static bool IsMovingRight;
public Exercise()
{
InitializeComponent();
}
private void Exercise_Load(object sender, EventArgs e)
{
x = 0;
y = 0;
IsMovingRight = false;
IsMovingDown = false;
BackColor = Color.Black;
ClientSize = new System.Drawing.Size(745, 532);
}
private void Exercise_Paint(object sender, PaintEventArgs e)
{
// If the status of the origin indicates that it is moving right,
// then increase the horizontal axis
if (IsMovingRight == true)
x++;
else // If it's moving left, then decrease the horizontal movement
x--;
// If the status of the origin indicates that it is moving down,
// then increase the vertical axis
if (IsMovingDown == true)
y++;
else // Otherwise, decrease it
y--;
// Collision: if the axis hits the right side of the screen,
// then set the horizontal moving status to "Right", which will be used
// by the above code
if ((x + 40) > ClientSize.Width)
IsMovingRight = false;
if (x < 0)
IsMovingRight = true; ;
if ((y + 40) > ClientSize.Height)
IsMovingDown = false;
if (y < 0)
IsMovingDown = true;
// Draw the new axis
e.Graphics.DrawLine(Pens.Aqua, x + 20, 0, x + 20, ClientSize.Height);
e.Graphics.DrawLine(Pens.Aqua, 0, y + 20, ClientSize.Width, y + 20);
}
private void tmrMoveLines_Tick(object sender, EventArgs e)
{
Invalidate();
}
}
}
|
||
Home | Copyright © 2010-2020, FunctionX | |
|