Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 So, I'm attempting to create a grid on the screen, and to do so, I've implemented a multidimensional array of Rectangles
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PuppetFoam




PostPosted: Wed May 21, 2014 5:11 pm   Post subject: So, I'm attempting to create a grid on the screen, and to do so, I've implemented a multidimensional array of Rectangles

So, I'm attempting to create a grid on the screen, and to do so, I've implemented a multidimensional array of Rectangles.

When the program starts, I use a for loop to increase the x and y coordinates to form the grid.
public Form1() {
InitializeComponent();

for (int x = 0; x < 12; x++) {
for (int y = 0; y < 12; y++) {
recArray[x, y] = new Rectangle(y * 50, x * 50, 100, 100);
}

Application.DoEvents();
}
}


My issue is trying to figure out when the user has clicked on a rectangle, and furthermore, which rectangle in the array that he/she has clicked on. As I will change the border to red when given the correct rectangle.

I'm using Visual Studio 2008, and here is my code so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quoridor {
public partial class Form1 : Form {
Rectangle rect = new Rectangle();

private Pen pen = Pens.Black;

Rectangle[,] recArray = new Rectangle[12, 12];

public Form1() {
InitializeComponent();

for (int x = 0; x < 12; x++) {
for (int y = 0; y < 12; y++) {
recArray[x, y] = new Rectangle(y * 50, x * 50, 100, 100);
}

Application.DoEvents();
}
}

protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);

rect.Location = new Point(2, 2);

rect.Size = new Size(100, 100);

for (int x = 0; x < 12; x++) {
for (int y = 0; y < 12; y++) {
e.Graphics.DrawRectangle(pen, recArray[x, y]);

Application.DoEvents();
}
}
}

private void Form1_Click(object sender, EventArgs e) {
Point cursor = this.PointToClient(Cursor.Position);

Refresh();
}
}
}


I'm making this into a real game, with classes and all. But keep in mind, this is my second month programming, so don't be harsh ^_^
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: