CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    1

    Question how to use rgb color?

    I want to create a program that when I click, it will display a square with rgb color(fill).
    and everytimes I click, a new square with different color will appear.
    please provide rgb code.
    >>
    or any other idea, the concept is to display different color everytimes I click the mouse. (it doesnt have to be rgb.)
    >>
    thanx!

  2. #2
    Join Date
    Jun 2005
    Posts
    33

    Thumbs up Re: how to use rgb color?

    Hi,

    U can set a color to graphics object as below:

    g2obj.setColor(Color.blue);


    You can also create your own color objects:


    1. Color ( int red, int green, int blue)

    parameter values between 0 and 255

    2. Color ( int red, int green, int blue , int alpha )

    4th parameter transparency measure

    0 - transparent

    255 - opaque



    also exist with float parameters…….

    3. Color ( float red, float green, float blue)

    4. Color ( float red, float green, float blue , float alpha )

    parameter values between 0.0 and 1.0

    After deciding upon the sequence of colors, Use MouseMotionListener and change color

  3. #3
    Join Date
    Jul 2005
    Posts
    14

    Re: how to use rgb color?

    You can get the the device context of the dialog and OnLButtonDown do as shown in the code snippet ..


    void CKeyboardcaptureDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    char lschar;
    lschar = char(nChar); //Get the Key Up character
    CClientDC dc(this); //Get the device context for this dialog

    CRect rc;
    GetWindowRect(&rc);
    ScreenToClient(&rc);
    if(lschar == 'R')
    {
    dc.FillSolidRect(rc,RGB(200,4,56));
    }
    if(lschar == 'T')
    {
    //CString strText = "Hello world";
    //dc.DrawText(strText ,rc,DT_MODIFYSTRING);
    dc.FillSolidRect(rc,RGB(0,4,244));
    }
    CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
    }

  4. #4
    Join Date
    Jul 2005
    Posts
    123

    Re: how to use rgb color?

    To generate random colors everytime. Use Math.random()*255

    Give the below code in mousePressed()


    int r=(int)(Math.random()*255);
    int g=(int)(Math.random()*255);
    int b=(int)(Math.random()*255);
    Color c=new Color(r,g,b);
    gr.setColor(c);
    gr.fillRect(50,50,100,100);

    Let me know, if you still have problem.



    -pkalp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured