2 Replies - 8148 Views - Last Post: 03 August 2012 - 02:37 PM

#1 davidtgo2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 12

rotating planet around sun

Posted 03 August 2012 - 09:38 AM

I have the form for planets set but I can't seem to get the planet to rotate. whenever I declare the variables in the form load event I get the undeclared variable error, yet when I move them to the timer event, no errors, yet the planet wont rotate. I attached the code as well, in my text book it states to put the variables in the form load event but that causes the error. when I put them into the timer event the code runs but the planet does not rotate, can you give me some suggestions?


#pragma endregion
    private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
                 
             }
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 


             }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
               
                timer1->Enabled = true;

         }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
             timer1->Enabled = false;

         }
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
                 // static constants
                 static const int xcenter = 200;
                 static const int ycenter = 250;
                 static const int smajor = 150;
                 static const int sminor = 50;
                // instance variable
                 

                                 
                // drawing objects
                 Drawing::Graphics^ g;
                 Drawing::Brush^ yellowBrush; //Yellow for the sun
                 Drawing::Brush^ cyanBrush;   // Cyan for planet     

                 // construct drawing objects
                 g = pictureBox1->CreateGraphics();
                 yellowBrush = gcnew Drawing::SolidBrush(Color::Yellow);
                 cyanBrush = gcnew Drawing::SolidBrush(Color::Cyan);


                 
                 
             // declare the local variables
             int x,y; //x and y coordinates of the planet
             double angle_radians; //angle measured in radians
             double angle_degrees;
               
             // Refreshing the picture box
             pictureBox1->Refresh();

             // drawing the sun with rectangle as guide
             System::Drawing::Rectangle               
             sunRect(xcenter,ycenter, 16,16);
             g->FillEllipse(yellowBrush,sunRect); //draw sun

             //draw the planet
             if (angle_degrees >=360) angle_degrees = 0;
             angle_radians = angle_degrees * Math::PI / 180;
             // calculate new x and y
             x = xcenter + smajor * Math::Cos(angle_radians);
             y = ycenter - sminor * Math::Sin(angle_radians);
             // draw planet
             System::Drawing::Rectangle planetRect(x,y,10,10);
             g->FillEllipse(cyanBrush,planetRect);
             
             // increasing angle by 5 degrees
             angle_degrees += 5;


         }
};
}



This post has been edited by GunnerInc: 03 August 2012 - 09:41 AM
Reason for edit:: Fixed code tags, removed email


Is This A Good Question/Topic? 0
  • +

Replies To: rotating planet around sun

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3423
  • View blog
  • Posts: 10,544
  • Joined: 05-May 12

Re: rotating planet around sun

Posted 03 August 2012 - 10:41 AM

Looks like C++/CLI code to me...

What variables are you trying to declare? What errors are you getting exactly? Which lines are the errors referring to?
Was This Post Helpful? 0
  • +
  • -

#3 davidtgo2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 12

Re: rotating planet around sun

Posted 03 August 2012 - 02:37 PM

Hi Skydiver, when I put this code into the form 1 load event I get the following errors, until I put them in the timer event, and this is c++ through visual C++ 2008.

[code]
#pragma endregion
private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {

}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
// static constants
static const int xcenter = 200;
static const int ycenter = 250;
static const int smajor = 150;
static const int sminor = 50;
// instance variable



// drawing objects
Drawing::Graphics^ g;
Drawing::Brush^ yellowBrush; //Yellow for the sun
Drawing::Brush^ cyanBrush; // Cyan for planet

// construct drawing objects
g = pictureBox1->CreateGraphics();
yellowBrush = gcnew Drawing::SolidBrush(Color::Yellow);
cyanBrush = gcnew Drawing::SolidBrush(Color::Cyan);

[code]

errors
Build started: Project: Planets, Configuration: Debug Win32 ------
Compiling...
Planets.cpp
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(175) : error C2065: 'xcenter' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(175) : error C2065: 'ycenter' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(176) : error C2065: 'g' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(176) : error C2227: left of '->FillEllipse' must point to class/struct/union/generic type
type is ''unknown-type''
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(176) : error C2065: 'yellowBrush' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(182) : error C2065: 'xcenter' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(182) : error C2065: 'smajor' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(183) : error C2065: 'ycenter' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(183) : error C2065: 'sminor' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(186) : error C2065: 'g' : undeclared identifier
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(186) : error C2227: left of '->FillEllipse' must point to class/struct/union/generic type
type is ''unknown-type''
c:\documents and settings\administrator\desktop\planets\planets\Form1.h(186) : error C2065: 'cyanBrush' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\Planets\Planets\Debug\BuildLog.htm"
Planets - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1