Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-18-2003, 04:49 PM   #1 (permalink)
Zenogear11
Registered User
 
Zenogear11's Avatar
 
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
Zenogear11 is on a distinguished road
Send a message via ICQ to Zenogear11 Send a message via AIM to Zenogear11
Lightbulb My first post, I need help

My first post...Hi! I am writing a calculator (only programming for 6 weeks now) and I need some help. I know all my code is in main, and thats a bad thing, but ill organize it later. Code:
Code:
#include <iostream.h>
#include <CMUgraphics.h>



int main()
{
	//****************
	// Variables    **
	int sizebutx = 35;
	int counter2 = 0;
	int counter = 0;
	int sizebuty = 35;
	int spacebutx = 17;
	int spacebuty = 34;
	int calcx1 = 20;
	int calcy1 = 20;
	int calcx2 = 300;
	int calcy2 = 500;
	int buttonx1 = (calcx1+spacebutx);
	int buttony1 = (calcy2-spacebuty);
	int buttonx2 = (calcx1+spacebutx+sizebutx);
	int buttony2 = (calcy2-spacebuty-sizebuty);
	//*******************
	window testWindow(650,500,0,0);
	testWindow.SetPen(RED);
	testWindow.SetBrush(BLUE);
	testWindow.DrawRectangle(calcx1,calcy1,calcx2,calcy2,FILLED);
	
	testWindow.SetPen(GREEN);
	testWindow.SetBrush(RED);
	do
	{
		//Draws Columns
		counter2++;

	do
	{
		//Draws Rows
		
	testWindow.DrawRectangle(buttonx1,buttony1,buttonx2,buttony2,FILLED);
	buttonx1 = buttonx1+sizebutx+spacebutx;
	buttonx2 = buttonx2+sizebutx+spacebutx;
	
		counter++;
	}
	while (counter < 5);
		buttony1 = buttony1-sizebuty-spacebuty;
		buttony2 = buttony2-spacebuty-sizebuty;
		buttonx1 = (calcx1+spacebutx);
		buttonx2 = (calcx1+spacebutx+sizebutx);
		counter = 0;
	}
	while (counter2 < 5);
	testWindow.SetFont(20,BOLD,SCRIPT);
	testWindow.SetPen(YELLOW);
	testWindow.DrawString(50,165,"1");
	testWindow.DrawString(100,165,"2");
	testWindow.DrawString(150,165,"3");
	

	return 0;
}
CMU graphics is what im using to draw the calculator and the guide for it can be found
Here

I need to know how to make my calculator buttons clickable, and display the number they represent in a graphics box (after I draw all of them).

Thank you for your help in advance.
Zenogear11 is offline   Reply With Quote
Old 03-18-2003, 05:07 PM   #2 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
I took a quick look at the instructions. What you will have to do is look into the mouse functions and get the coordinates for the mouse and check to see which button the mouse is in on your own. Then draw text in your text area.

Since this is a word Doc can I assume you are on a windows platform? Its a shame they don't use a standard set of libraries for drawing this stuff. I remember doing this in school and we were using macs to do our programing and at the time I had a windows machine. So I was forced to do all my programs that required graphics at school labs instead of my home PC. Ok enough of my rant
palin is offline   Reply With Quote
Old 03-19-2003, 08:07 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
What you are asking is to study the CMU libraries. The help file doesn't seem to provide all the info. But if the doc is complete then the CMU lib doesn't support "buttons". Only shapes.

Give the link to the CMU libs. I'll take a quick peek at it.

But the GetMouseCoord() method is clear. Compare if GetMouseCoord(x,y) falls within your button specs.

Next step is to fool with GetButtonState() and GetMouseClick().
Like I said, I don't know the details of the CMU lib.

Just provide the link so I can take a peek or try to solve it your self with this info.

- Val -
Valmont is offline   Reply With Quote
Old 03-19-2003, 08:25 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Next time name your variables more logically (recognisable) so "strangers" understand strange code faster. It really helps , and is considered an essential part of good programming .

- Val -
Valmont is offline   Reply With Quote
Old 03-19-2003, 11:28 AM   #5 (permalink)
Zenogear11
Registered User
 
Zenogear11's Avatar
 
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
Zenogear11 is on a distinguished road
Send a message via ICQ to Zenogear11 Send a message via AIM to Zenogear11
Val, I don't have the Lib for CMU graphics, all I know about it is in that link, also, sorry about the variables, Ill fix em up next time.
Zenogear11 is offline   Reply With Quote
Old 03-19-2003, 12:06 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Then at least tell me wich version of CarnegieMellonGraphics you are using.

I've downloaded a version but it doesn't contain the SetPen() method in the CarnegieMellonGraphics.h header file.

If I only had the correct version I'd do this in 5 minutes...
Valmont is offline   Reply With Quote
Old 03-19-2003, 01:22 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Finally, got it all installed and working.

The first thing you should do is changing your code such that the program exits on pressing the esc button.
It exits now on a keypress... we don't want that.

Then you can use :

int ix, iy;
testWindow.GetMouseClick(ix,iy);


Next thing is to compare if ix and iy is within your red lil boxes (:th: got it working:th: ).
If it is, implement code that belongs to that particular red box.

It is really that simple. But next time better learn the basics of classes. How are you going to implement code for each of the boxes? One big long line of code?
Just a hint .

- Val -
Valmont is offline   Reply With Quote
Old 03-19-2003, 04:50 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road

Code:
#include <iostream.h>
#include <CMUgraphics.h>


keytype ktInput;
char chKeyData;
int ix,iy;

int main()
{
	//****************
	// Variables    **
	int sizebutx = 35;
	int counter2 = 0;
	int counter = 0;
	int sizebuty = 35;
	int spacebutx = 17;
	int spacebuty = 34;
	
	int buttonx1 = (20+spacebutx);
	int buttony1 = (500-spacebuty);
	int buttonx2 = (20+spacebutx+sizebutx);
	int buttony2 = (500-spacebuty-sizebuty);
	//int ix,iy;
	bool bQuit=false;
	
	//*******************
	window testWindow(650,520,0,0);//550 was 500
	testWindow.SetPen(RED);
	testWindow.SetBrush(BLUE);
	testWindow.DrawRectangle(20,20,300,500,FILLED);
	
	testWindow.SetPen(GREEN);
	testWindow.SetBrush(RED);
	do
	{
		
		//Draws Columns
		counter2++;
		do
		{
			//Draws Rows
			testWindow. DrawRectangle(buttonx1,buttony1,buttonx2,buttony2,FILLED);
			buttonx1 = buttonx1+sizebutx+spacebutx;
			buttonx2 = buttonx2+sizebutx+spacebutx;
			counter++;
		}
		while (counter < 5);
		
		buttony1 = buttony1-sizebuty-spacebuty;
		buttony2 = buttony2-spacebuty-sizebuty;
		buttonx1 = (20+spacebutx);
		buttonx2 = (20+spacebutx+sizebutx);
		counter = 0;
	}
	while (counter2 < 5);

	// Flush out the input queues before beginning
    testWindow.FlushMouseQueue();
    testWindow.FlushKeyQueue();

//BEGIN loop to enable esc to enable click-to-end option.

	do
	{
		//ktInput = testWindow.GetKeyPress(chKeyData) 
		if(testWindow.GetKeyPress(chKeyData) == ESCAPE) 
		{
//ESC pressed so click to end available again!!
			bQuit=true;
		}
//You can use this only once, then quit program and re-run.
		testWindow.WaitMouseClick(ix,iy);
		testWindow.DrawInteger(100,100,ix);
		testWindow.DrawInteger(100,120,iy);
	}
	while (bQuit==false);	
//END loop to enable esc to enable click-to-end option.
	
	return 0;
}
Code:
 
I changed your proggie a bit to make it a lil more readable (removed a few "one time used) vars and replaced the vars when used with actual numbers).

Stupid UBB messes up the tabs but I am too tired to correct it.

Press esc THEN click twice slowly on the window to end.

Here is the main.cpp file I made. Download it and test it:
Click here to download


The rest is up to you. My line of expertise is Qt not CMU .

- Val -
Valmont is offline   Reply With Quote
Old 03-19-2003, 06:28 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Wink This prgrammer is a crazy addict!

CHECK THIS OUT!
Click on screen and watch the windows title bar!!
U will love this demo I think . Also a bit of comment added.

Cut and paste this code to use instead of the original one.
Code:
#include <iostream.h>
#include <CMUgraphics.h>
#include <string.h>


//These variables are added by Valmont.
char chKeyData;
int ix,iy;
bool bQuit=false;

int main()
{
	//****************
	// Variables    **
	int sizebutx = 35;
	int counter2 = 0;
	int counter = 0;
	int sizebuty = 35;
	int spacebutx = 17;
	int spacebuty = 34;
	
	int buttonx1 = (20+spacebutx);
	int buttony1 = (500-spacebuty);
	int buttonx2 = (20+spacebutx+sizebutx);
	int buttony2 = (500-spacebuty-sizebuty);
		
	//*******************
	window testWindow(650,520,0,0);//550 was 500
	testWindow.SetPen(RED);
	testWindow.SetBrush(BLUE);
	testWindow.DrawRectangle(20,20,300,500,FILLED);
	
	testWindow.SetPen(GREEN);
	testWindow.SetBrush(RED);
	do
	{
		
		//Draws Columns
		counter2++;
		do
		{
			//Draws Rows
			testWindow. DrawRectangle(buttonx1,buttony1,buttonx2,buttony2,FILLED);
			buttonx1 = buttonx1+sizebutx+spacebutx;
			buttonx2 = buttonx2+sizebutx+spacebutx;
			counter++;
		}
		while (counter < 5);
		
		buttony1 = buttony1-sizebuty-spacebuty;
		buttony2 = buttony2-spacebuty-sizebuty;
		buttonx1 = (20+spacebutx);
		buttonx2 = (20+spacebutx+sizebutx);
		counter = 0;
	}
	while (counter2 < 5);

	//By Valmont:
	//We start here with the ESC option and mouseclick demo. YOU MUST PRESS
	//ESC THEN 2 TIMES LEFT CLICK TO END PROGRAM ENTIRELY!!
	
	// Flush out the input queues before beginning
    testWindow.FlushMouseQueue();
    testWindow.FlushKeyQueue();
	//Few vars to demonstrate co-ordinates in window title.
	char chNum[5]; //Make a char for upcomimg int-string conversion.
	string szX,szY,szComb;//Use strings to make a pretty output.And ChangeTitle() method demands a string.

	//Start loop for esc input until esc is pressed. bool value bQuit will be true then.
	do
	{
		if(testWindow.GetKeyPress(chKeyData) == ESCAPE) //Is the esc key pressed?
		{
			bQuit=true; //yes, the esc key is pressed.
			break;//Get out of loop then. So click twice to end program entirely.
		}
		testWindow.WaitMouseClick(ix,iy);
		
		itoa(ix,chNum,10); //conversion from int to char. Find more about itoa on the net if u like.
		szX=chNum;//auto conversion from char to string. Must do since ChangeTitle needs a string, NOT a char*!
		itoa(iy,chNum,10);
		szY=chNum;

		szComb="Coordinates are: ("+szX+","+szY+")";//Coordinates are: (szX,szY)
		testWindow.ChangeTitle(szComb); //Update the current windows title area.
				
	}
	while (bQuit==false);	

//End by Valmont.
	
	return 0;
}
Valmont is offline   Reply With Quote
Old 03-19-2003, 06:33 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Thumbs down Bloody tab-less-cut-and-paste-UBB

download updated cpp file here:
click me hard
Valmont is offline   Reply With Quote
Old 03-19-2003, 07:46 PM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Lightbulb WAAAAA lololololol

And finally the answer to your original question wich was: How to do stuff when a button is pressed with a mouseclick.

Re-download the main.cpp file at given previous link and jolly run it!!!

Also lots of comments added to help understand and move on. The code is also completely reviewed to correct a few ugly comments.
When running the program, click on the upper left square a few times and enjoy .

Press esc key and click twice with mouse to end program.

For the sake of completeness, I post the final code below.
Hehe, I had fun fun fun with CMU though totally useless for me :p .
Watch out for the comments. Tabs are messed up. Too much work on organizing it. Downloadable main.cpp file is obviously correct.
-----------------
-----------------

Code:
#include <iostream.h>
#include <CMUgraphics.h>
#include <string.h>


//These variables are added by Valmont. This is by no means correct programming but it will do fine for now.
char chKeyData;
int ix,iy;//This is a bad naming example... What does ix and iy?
bool bQuit=false;//...but this one is nice. You have a clear hint of what it means and wich type it is (b=boolean).
bool bOriginalColorState =true;//This one will show its purpose once you see when it is used.Naming is good but placement is conceptual bad.

int main()
{
	//****************
	// Variables    **
	int sizebutx = 35;//Idea: Call it "intButtonSizeX".
	int counter2 = 0;//Idea: "intColCounter"
	int counter = 0;//...and so forth.
	int sizebuty = 35;
	int spacebutx = 17;
	int spacebuty = 34;
	
	int buttonx1 = (20+spacebutx);
	int buttony1 = (500-spacebuty);
	int buttonx2 = (20+spacebutx+sizebutx);
	int buttony2 = (500-spacebuty-sizebuty);
		
	//*******************
	window testWindow(650,520,0,0);//520 was 500, looks more friendly like this.
	testWindow.SetPen(RED);
	testWindow.SetBrush(BLUE);
	testWindow.DrawRectangle(20,20,300,500,FILLED);
	
	testWindow.SetPen(GREEN);
	testWindow.SetBrush(RED);

	//Start drawing the buttons on testWindow.
	do
	{
		
		//Draws Columns
		counter2++;
		do
		{
			//Draws Rows
			testWindow. DrawRectangle(buttonx1,buttony1,buttonx2,buttony2,FILLED);
			buttonx1 = buttonx1+sizebutx+spacebutx;
			buttonx2 = buttonx2+sizebutx+spacebutx;
			counter++;
		}
		while (counter < 5);
		
		buttony1 = buttony1-sizebuty-spacebuty;
		buttony2 = buttony2-spacebuty-sizebuty;
		buttonx1 = (20+spacebutx);
		buttonx2 = (20+spacebutx+sizebutx);
		counter = 0;
	}
	while (counter2 < 5);

	//By Valmont:
	//We start here with the ESC option and mouseclick demo. YOU MUST PRESS
	//ESC THEN 2 TIMES LEFT CLICK TO END PROGRAM ENTIRELY!!
	
	// Flush out the input queues before beginning
    testWindow.FlushMouseQueue();
    testWindow.FlushKeyQueue();
	//Few vars to demonstrate co-ordinates in window title.
	char chNum[5]; //Make a char for upcomimg int-string conversion. This is also a demonstration of where to declare a variable like this one.
	string szX,szY,szComb;//Use strings to make a pretty output.And ChangeTitle() method demands a string.
	
	//Start and run main events loop until esc key is pressed. bool value bQuit will be true then. It is now false.
	do
	{
		if(testWindow.GetKeyPress(chKeyData) == ESCAPE) //Is the esc key pressed?
		{
			bQuit=true; //yes, the esc key is pressed.
			break;//Get out of loop then. So click twice to end program entirely.
		}
		
		testWindow.WaitMouseClick(ix,iy);//Right mouse button or left one... doesn't matter, but program will not move on until a mouseclick OR the ESC key is pressed.
		
		itoa(ix,chNum,10); //conversion from int to char. Find more about itoa on the net if u like.
		szX=chNum;//auto conversion from char to string. Must do since ChangeTitle needs a string, NOT a char*!
		itoa(iy,chNum,10);
		szY=chNum;

		szComb="Coordinates are: ("+szX+","+szY+")";//Coordinates are: (szX,szY)
		testWindow.ChangeTitle(szComb); //Update the current windows title area.

		//AND FINALLY THE SOLUTION TO YOUR ORIGINAL PROBLEM!!!
		//Keep clicking on first square.

		//If mouseclick was on first square (upper left) then do fun stuff.
		if(ix>36 && ix<73 && iy>154 && iy<191)//The first square I did for you. You need to do the rest of the squares.
		{
			if (bOriginalColorState)
			{
				testWindow.SetPen(RED);
				testWindow.SetBrush(GREEN);
				testWindow.DrawRectangle(37,155,72,190);
				bOriginalColorState = false;
			}
			else
			{
				testWindow.SetPen(GREEN);
				testWindow.SetBrush(RED);
				testWindow.DrawRectangle(37,155,72,190);
				bOriginalColorState = true;
			}
		}
	}
	while (!bQuit); //And finally we exit the ESC loop because the ESC key was pressed.	

//End by Valmont.
return 0;
}
Valmont is offline   Reply With Quote
Old 03-20-2003, 02:17 AM   #12 (permalink)
Zenogear11
Registered User
 
Zenogear11's Avatar
 
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
Zenogear11 is on a distinguished road
Send a message via ICQ to Zenogear11 Send a message via AIM to Zenogear11
Val, you are my BEST friend. I got to sleep last night, trying to understand everyones posts, and now this morning, I GET ALL THE ANWSERS. I thank you VERY very much for all your help, and I hope to be able to help other people in the future. Again, you are AMAZING. :rock: :rock:
Zenogear11 is offline   Reply With Quote
Old 03-20-2003, 02:24 AM   #13 (permalink)
Zenogear11
Registered User
 
Zenogear11's Avatar
 
Join Date: Mar 2003
Location: Pittsburgh
Posts: 45
Zenogear11 is on a distinguished road
Send a message via ICQ to Zenogear11 Send a message via AIM to Zenogear11
Oooh.......I think im really helpless here. I copy all your code from the CPP, and instantly paste it indo my window (like a moocher). Of course I get 8 errors like so.
----------------------------------------------------------------
Compiling...
Calculator.cpp
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(70) : error C2065: 'string' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(70) : error C2146: syntax error : missing ';' before identifier 'szX'
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(70) : error C2065: 'szX' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(70) : error C2065: 'szY' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(70) : error C2065: 'szComb' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(84) : error C2440: '=' : cannot convert from 'char [5]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(86) : error C2440: '=' : cannot convert from 'char [5]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\VC98\Calculator\Calculator.cpp(88) : error C2110: cannot add two pointers
Error executing cl.exe.
--------------------------------------------------------

I added STRING.h to my file, no help.
Zenogear11 is offline   Reply With Quote
Old 03-20-2003, 10:43 AM   #14 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
Don't cut and paste, download the latest main.cpp file from my url I gave you in the previous posts.

It is fully revised and fully commented.
Also realise that I used the CMUgraphics under Visual C++ 6.

I need to do something right now but I'll send a screenie and then I will post knowledge on how to install CMU properly because there are a few pitfalls.

Just start a new win32 console project (no files created!) and copy my main.cpp file in your project folder.

Ah, let me zip my folder and post it. Hang on. Gotta do stuff first.

- Val -
Valmont is offline   Reply With Quote
Old 03-20-2003, 11:01 AM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,546
Valmont is on a distinguished road
:)

Check the new thread on CMU in this forum!!!
click here for step by step tutorial
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending POST multiple post requests in one php script. sde PHP 2 08-09-2003 05:10 PM
post indicators abc123 Feedback 7 08-14-2002 08:25 AM
first off topic post revolution Lounge 3 05-09-2002 05:19 PM


All times are GMT -8. The time now is 04:31 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting