Results 1 to 7 of 7
Thread: if (...) ... won't set color
- 04-11-2010, 02:34 PM #1
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
- 04-11-2010, 02:42 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
-
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?
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.Java Code:for (int i = 100 ; i <= 1 ; i-- ){ //.... }
Much luck.
- 04-11-2010, 03:28 PM #4
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:
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.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
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.
-joeLast edited by livingdog; 04-11-2010 at 03:32 PM.
-
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.
- 04-11-2010, 03:45 PM #6
Other routes you could have suggested, sans the insult (that I am too stupid to be a Java programmer) is:
- try finding help at a local university, they may have free tutoring
- 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.
- 04-11-2010, 03:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
RGB from Color Name
By winklerd in forum Advanced JavaReplies: 5Last Post: 03-26-2010, 05:16 PM -
[COLOR="Navy"]execute .bat file in mysql [/COLOR]
By msankar.ravi in forum NetworkingReplies: 0Last Post: 02-24-2010, 04:27 AM -
color texts
By layst in forum New To JavaReplies: 1Last Post: 10-22-2008, 02:47 PM -
Color objects
By CyberFrog in forum New To JavaReplies: 4Last Post: 04-01-2008, 12:41 AM -
A bit of color!
By tim in forum Java 2DReplies: 8Last Post: 02-11-2008, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks