Results 1 to 7 of 7
  1. #1
    livingdog's Avatar
    livingdog is offline Member
    Join Date
    Jan 2010
    Location
    nyc
    Posts
    12
    Rep Power
    0

    Question if (...) ... won't set color

    This is only an exercise for me. I want to become familiar with color manipulation. I am trying to draw a circle of 'o's one half red and the other half blue. If I leave out the if() statement then the code works - but only one color is possible.

    Thanks in advance or any help you may give me.

    -joe

    Java Code:
    // ColorPlay : playing with colors
    //
    // DATE      : 04-10-10
    //
    // TIME      : 08:11 PM EST USA
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ColorPlay extends JFrame{
    
      public static final double PI = 3.141592653589793;
    
      public ColorPlay(){
        super ( "COLOR PLAY" );
        setSize( 500, 500 );
        setVisible(true);
      }//ColorPlay()
    
      public void paint ( Graphics g ){
        double x = 0., y = 0.;
        double t = PI/50.;
        for (int i = 100 ; i <= 1 ; i-- ){
          t = PI/(double)i;
          x = 100.*Math.cos(t) + 200.;
          y = 100.*Math.sin(t) + 200.;
          if ( i >= 50 )
          {
            g.setColor( Color.red );
          }//if_i
          else
          {
            g.setColor( Color.blue );
          }//else_i
          g.drawString( "O", (int)x, (int)y );
          g.drawString( "+",    200,    200 );
        }//for_i
      }//paint
    
      public static void main( String args[] ){
        ColorPlay app = new ColorPlay();
        app.addWindowListener
          ( new WindowAdapter() {
              public void WindowClosing( WindowEvent e ) {
                System.exit( 0 );
              }//Windowclosing
            }//WindowAdapter
          );
      }//main
    
    }//ColorPlay

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by livingdog View Post
    Java Code:
        for (int i = 100 ; i <= 1 ; i-- ){
    Does this make sense to you? Not to me.

    kind regards,

    Jos

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) You'll want to read the Sun tutorials on how to draw with Swing. You don't want to draw directly in the JFrame, but rather in a JPanel held by the JFrame. Also, you'll want to draw in the JPanel's paintComponent method, not the paint method. And you'll likely want to call super.paintComponent(g) as the first method of your paintComponent method. The tutorials will tell you more.

    2) Logically, will this for loop ever get processed?
    Java Code:
    for (int i = 100 ; i <= 1 ; i-- ){
      //....
    }
    3) You might want to check your math as what you have written will not draw a circle. It will draw some points on a circle (after the for loop has been fixed), but all the red points will be clustered tight at the right most portion of the circle.

    Much luck.

  4. #4
    livingdog's Avatar
    livingdog is offline Member
    Join Date
    Jan 2010
    Location
    nyc
    Posts
    12
    Rep Power
    0

    Default

    The for loop increases PI from 0 (PI/100.) to 3.14 (PI/1.) so the circle is drawn with increasing theta (clockwise).

    I have been working on another program "ColorPlay2.java" and I just got it to work. Here it is:

    Java Code:
    // ColorPlay2 : playing with colors, v.2
    //
    // DATE       : 04-11-10
    //
    // TIME       : 09:34 AM EST USA
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ColorPlay2 extends JFrame{
    
      public static final double PI = 3.141592653589793;
    
      public ColorPlay2(){
        super ( "COLOR PLAY 2" );
        setSize( 500, 500 );
        setVisible(true);
      }//ColorPlay2()
    
      public void paint ( Graphics g ){
        //set color using float dynamically - within the loop
        double x=0., y=0., t;
        float  fx  , fy  ;
          for (int i = 100 ; i >= 1 ; i-- ){
          t = PI/(double)i;
          x = 100.*Math.cos(t) + 200.;
          y = 100.*Math.sin(t) + 200.;
          fx = (float)Math.cos(t);
          fy = (float)Math.sin(t);
          g.setColor( new Color( 0.0f, fx, fy ) );
          g.drawString( "o", (int)x, (int)y );
          //
          x = -100.*Math.cos(t) + 200.;
          y = 100.*Math.sin(t) + 200.;
          fx = (float)Math.cos(t);
          fy = (float)Math.sin(t);
          g.setColor( new Color( fx, 0.0f, fy ) );
          g.drawString( "+", (int)x, (int)y );
          //
          x = 100.*Math.cos(t) + 200.;
          y = -100.*Math.sin(t) + 200.;
          fx = (float)Math.cos(t);
          fy = (float)Math.sin(t);
          g.setColor( new Color( fx, fy, 0.0f ) );
          g.drawString( "x", (int)x, (int)y );
          //
          x = -100.*Math.cos(t) + 200.;
          y = -100.*Math.sin(t) + 200.;
          fx = (float)Math.cos(t);
          fy = (float)Math.sin(t);
          g.setColor( new Color( fx, fy, fx ) );
          g.drawString( "*", (int)x, (int)y );
        }//for_i
      }//paint
    
      public static void main( String args[] ){
        ColorPlay2 app = new ColorPlay2();
        app.addWindowListener
          ( new WindowAdapter() {
              public void WindowClosing( WindowEvent e ) {
                System.exit( 0 );
              }//Windowclosing
            }//WindowAdapter
          );//addWindowAdapter
      }//main
    
    }//ColorPlay2
    Part of my problem - probably the largest part - is that I still think sequentially. In the above for loop I originally had 4 loops - one for each quadrant of the xy-axes. But the code would only draw the first loop. Then I commented out the first loop and saw only the second loop drawn. That's when I realized that Java will do all four operations in one loop. I compiled and it worked.

    As to the more correct code suggested, I am using the 3rd edition of "Java: How to Program" by Deitel and Deitel, which was last printed in 1997. I can't afford a new text, and the sun tutorials are as helpful as the unix docs - only helpful as a reference - not as a teaching tool. E.g. I was using "show();" but that has been deprecated. I corrected this by doing a google search not by reading that it is deprecated (in fact I read that the sun docs only mention this and do not give a suggested fix). So my apologies for using 13 year old (yikes!) code.


    Thanks again for the helpful suggestions.

    -joe
    Last edited by livingdog; 04-11-2010 at 03:32 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by livingdog View Post
    As to the more correct code suggested, I am using the 3rd edition of "Java: How to Program" by Deitel and Deitel, which was last printed in 1997. I can't afford a new text, and the sun tutorials are as helpful as the unix docs - only helpful as a reference - not as a teaching tool. E.g. I was using "show();" but that has been deprecated. I corrected this by doing a google search not by reading that it is deprecated (in fact I read that the sun docs only mention this and do not give a suggested fix). So my apologies for using 13 year old (yikes!) code.
    My suggestion: get used to using the Sun tutorials. Like any skill, it requires practice to get better. So keep studying those tutorials and they should get easier to understand and more helpful for you. This really is a necessary skill if you want to pursue Java programming.
    Last edited by Fubarable; 04-11-2010 at 03:36 PM.

  6. #6
    livingdog's Avatar
    livingdog is offline Member
    Join Date
    Jan 2010
    Location
    nyc
    Posts
    12
    Rep Power
    0

    Exclamation

    Other routes you could have suggested, sans the insult (that I am too stupid to be a Java programmer) is:

    1. try finding help at a local university, they may have free tutoring
    2. find a user friendly forum


    Option (2) may be impossible. The Sun forums encourages ppl to be nothing but down right insulting. Here you only implied that I am stupid and therefore unwelcome here - much nicer approach.

    PS: I won't return. Please remove me from your members list.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by livingdog View Post
    Other routes you could have suggested, sans the insult (that I am too stupid to be a Java programmer) is:

    1. try finding help at a local university, they may have free tutoring
    2. find a user friendly forum


    Option (2) may be impossible. The Sun forums encourages ppl to be nothing but down right insulting. Here you only implied that I am stupid and therefore unwelcome here - much nicer approach.

    PS: I won't return. Please remove me from your members list.
    I don't see an insult, nor an insinuation of an insult anywhere in his reply; you are too itchy.

    kind regards,

    Jos

Similar Threads

  1. RGB from Color Name
    By winklerd in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2010, 05:16 PM
  2. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 04:27 AM
  3. color texts
    By layst in forum New To Java
    Replies: 1
    Last Post: 10-22-2008, 02:47 PM
  4. Color objects
    By CyberFrog in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 12:41 AM
  5. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-11-2008, 11:57 PM

Posting Permissions

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