1,105,226 Community Members

CLI/C++ dot painting

Member Avatar
Mindless Z
Newbie Poster
7 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I've got an assignment due very soon that I can't for the life of me figure out.
the basic idea is to create a program that allows the user to paint with large dots.
If the R key is down the dot should be red, if the B key is down the Dot should be blue, etc. The dot (size, color, everything) is defined in a separate header file.

So far I can make it paint the dots but only of a single color and with no key pressed. how would i go about changing the color based on a key press? I've tried storing the color name in a string but i couldn't seem to get that to work

If you're not sure what the goal is, this page has my professors example as a java applet
http://cs.ua.edu/351/S2012/Assignments/P2.htm

Any suggestions would be greatly appreciated.


This is the header file containing the dot class

using namespace System;
using namespace System::Drawing;

public ref class CMyDot {
public:
	CMyDot(int x, int y) {
		X=x;
		Y=y;
	}
	
	int getX() { return X; }
	void setX(int x) { X=x; }
	int getY() { return Y; }
	void setY(int y) { X=y; }
	Color myColor = Color::White;
	
	
	double distanceTo(int x, int y) {
		return Math::Sqrt((X-x)*(X-x)+(Y-y)*(Y-y));
	}

	void draw(Graphics^ g) {
		g->FillEllipse( gcnew SolidBrush(Color::Red ), X, Y, 50, 50 ); // Draw Color
	}

private:
	int X;
	int Y;
};
Member Avatar
Mindless Z
Newbie Poster
7 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Perhaps a better way to word my question would be how do i test whether a key is down AND the mouse has been clicked?

Member Avatar
BobS0327
Posting Whiz in Training
203 posts since Feb 2008
Reputation Points: 24 [?]
Q&As Helped to Solve: 29 [?]
Skill Endorsements: 0 [?]
 
0
 

Create a KeyPressEventArgs event handler and process the KeyChar property


http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx

Member Avatar
Mindless Z
Newbie Poster
7 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm afraid I still cant seem to figure it out.
I can't figure out how to test for both the key down and mouse click together. I can get both to work separately but i'm not sure how to go about combining the two events

Member Avatar
BobS0327
Posting Whiz in Training
203 posts since Feb 2008
Reputation Points: 24 [?]
Q&As Helped to Solve: 29 [?]
Skill Endorsements: 0 [?]
 
1
 

I'm afraid I still cant seem to figure it out.
I can't figure out how to test for both the key down and mouse click together. I can get both to work separately but i'm not sure how to go about combining the two events

In my code, I've set up the following GLOBAL variables:
Pen^
Rectangle
Color
bool bMouseClick which is initialized to false

In my MouseDown event handler, I check for a depressed left mouse button. If it is depressed, I set my global boolean bMouseClick to true. I also fill in my global Rectangle with the mouse cursor X and Y coordinates and the size of the desired circle.

I have also created a KeyPress event handler. In this handler, I'm checking the KeyChar property for the appropriate depressed character AND determining that bMouseClick is true. If both these conditions exist, I create the Pen^ and ColorInput. I also invalidate the Rectangle built in the MouseDown event.

Now we have to accomodate the Rectangle invalidation by creating a Paint event handler. In this handler, you will call DrawEllipse with the global Pen^ and global Rectangle. You will also call Fillellipse with the global Color.

Finally, a MouseUp event handler is needed to change the global bMouseClick to false.

Member Avatar
Mindless Z
Newbie Poster
7 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you so much, with your suggestions i was able to get it finally.

Question Answered as of 2 Years Ago by BobS0327
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article