12 Replies - 207 Views - Last Post: 25 November 2011 - 03:36 PM Rate Topic: -----

#1 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

adding random GLabels

Posted 25 November 2011 - 01:29 PM

Hello again,

Still making my way through "Art and Science of Java" and bummed about the lack of solutions on the web to compare mine against. Here is the latest exercise. Description is in the notes at the top of the code. It works as intended with no errors. But the code seems clunky - very repetitive. Is there a better way to do this?

Thanks!!!
Mickey

/**
 *  File: ColorLabels.java
 *  ----------------------
 *  Write a GraphicsProgram that creates GLabels for each of the color names RED, ORANGE, YELLOW, GREEN, BLUE, CYAN, MAGENTA, and then puts those labels up on the screen in a random position.  The color of each label, howevere, should be randomly chosen from  the other colors in this list, so that the GLabel for GREEN is allowed to be any color except green. 
 */

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class ColorLabels extends GraphicsProgram {

	private static final long serialVersionUID = 1L;
	
	// Initializing the instance variables
	private RandomGenerator rgen = RandomGenerator.getInstance();
	int colorNumber = 1;
	double x = 0;
	double y = 0;	
	Color color = Color.black;
	
	
	public void run() {
		
		//Setting the labels one at a time.  :-\  There's got to be a better way.
		
		GLabel red = new GLabel("RED");
		colorNumber = notThisColor(1);
		color = chooseColor(colorNumber);
		red.setColor(color);
		red.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - red.getWidth());
		y = rgen.nextDouble(0, getHeight() - red.getHeight());
		add(red,x,y);
		
		GLabel orange = new GLabel("ORANGE");
		colorNumber = notThisColor(2);
		color = chooseColor(colorNumber);
		orange.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - orange.getWidth());
		y = rgen.nextDouble(0, getHeight() - orange.getHeight());
		add(orange,x,y);
		
		GLabel yellow = new GLabel("YELLOW");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		yellow.setFont("Serif-bold-32");
		yellow.setColor(color);
		x = rgen.nextDouble(0, getWidth() - yellow.getWidth());
		y = rgen.nextDouble(0, getHeight() - yellow.getHeight());
		add(yellow,x,y);
		
		GLabel green = new GLabel("GREEN");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		green.setColor(color);
		green.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - green.getWidth());
		y = rgen.nextDouble(0, getHeight() - green.getHeight());
		add(green,x,y);
		
		GLabel magenta = new GLabel("MAGENTA");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		magenta.setColor(color);
		magenta.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - magenta.getWidth());
		y = rgen.nextDouble(0, getHeight() - magenta.getHeight());
		add(magenta,x,y);
		
		GLabel blue = new GLabel("BLUE");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		blue.setColor(color);
		blue.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - blue.getWidth());
		y = rgen.nextDouble(0, getHeight() - blue.getHeight());
		add(blue,x,y);
		
		GLabel cyan = new GLabel("CYAN");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		cyan.setColor(color);
		cyan.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - cyan.getWidth());
		y = rgen.nextDouble(0, getHeight() - cyan.getHeight());
		add(cyan,x,y);
		
	}
	

	// Check to make sure that the text color is not the same as the label color.
	private int notThisColor(int num){
		colorNumber = rgen.nextInt(1,7);
		while (colorNumber == num) {
			colorNumber = rgen.nextInt(1,7);
		}
		return colorNumber;
	}
	
	// The list of the colors for the randomgenerator to choose from.
	private Color chooseColor(int colorNumber) {
		switch (colorNumber) {
			case 1: return Color.RED;
			case 2: return Color.ORANGE;
			case 3: return Color.YELLOW;
			case 4: return Color.GREEN;
			case 5: return Color.BLUE;
			case 6: return Color.CYAN;
			case 7: return Color.MAGENTA;
			default: return Color.BLACK;
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: adding random GLabels

#2 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 01:34 PM

Oops. Just noticed a couple problems. I don't see a way to edit my post so I'm reposting my code.

Thanks,
Mickey

/**
 *  File: ColorLabels.java
 *  ----------------------
 *  Write a GraphicsProgram that creates GLabels for each of the color names RED, ORANGE, YELLOW, GREEN, BLUE, CYAN, MAGENTA, and then puts those labels up on the screen in a random position.  The color of each label, howevere, should be randomly chosen from  the other colors in this list, so that the GLabel for GREEN is allowed to be any color except green. 
 */

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class ColorLabels extends GraphicsProgram {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Initializing the instance variables
	private RandomGenerator rgen = RandomGenerator.getInstance();
	int colorNumber = 1;
	double x = 0;
	double y = 0;	
	Color color = Color.black;
	
	
	public void run() {
		
		//Setting the labels one at a time.  :-\  There's got to be a better way.
		
		GLabel red = new GLabel("RED");
		colorNumber = notThisColor(1);
		color = chooseColor(colorNumber);
		red.setColor(color);
		red.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - red.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - red.getHeight());
		add(red,x,y);
		
		GLabel orange = new GLabel("ORANGE");
		colorNumber = notThisColor(2);
		color = chooseColor(colorNumber);
		orange.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - orange.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - orange.getHeight());
		add(orange,x,y);
		
		GLabel yellow = new GLabel("YELLOW");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		yellow.setFont("Serif-bold-32");
		yellow.setColor(color);
		x = rgen.nextDouble(0, getWidth() - yellow.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - yellow.getHeight());
		add(yellow,x,y);
		
		GLabel green = new GLabel("GREEN");
		colorNumber = notThisColor(4);
		color = chooseColor(colorNumber);
		green.setColor(color);
		green.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - green.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - green.getHeight());
		add(green,x,y);
		
		GLabel magenta = new GLabel("MAGENTA");
		colorNumber = notThisColor(7);
		color = chooseColor(colorNumber);
		magenta.setColor(color);
		magenta.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - magenta.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - magenta.getHeight());
		add(magenta,x,y);
		
		GLabel blue = new GLabel("BLUE");
		colorNumber = notThisColor(5);
		color = chooseColor(colorNumber);
		blue.setColor(color);
		blue.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - blue.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - blue.getHeight());
		add(blue,x,y);
		
		GLabel cyan = new GLabel("CYAN");
		colorNumber = notThisColor(6);
		color = chooseColor(colorNumber);
		cyan.setColor(color);
		cyan.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - cyan.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - cyan.getHeight());
		add(cyan,x,y);
		
	}
	

	// Check to make sure that the text color is not the same as the label color.
	private int notThisColor(int num){
		colorNumber = rgen.nextInt(1,7);
		while (colorNumber == num) {
			colorNumber = rgen.nextInt(1,7);
		}
		return colorNumber;
	}
	
	// The list of the colors for the randomgenerator to choose from.
	private Color chooseColor(int colorNumber) {
		switch (colorNumber) {
			case 1: return Color.RED;
			case 2: return Color.ORANGE;
			case 3: return Color.YELLOW;
			case 4: return Color.GREEN;
			case 5: return Color.BLUE;
			case 6: return Color.CYAN;
			case 7: return Color.MAGENTA;
			default: return Color.BLACK;
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,819
  • Joined: 10-September 10

Re: adding random GLabels

Posted 25 November 2011 - 01:35 PM

Two thoughts that I haven't taken too far:

You could:
- use an array of GLabels in a loop that applies the repetitive actions to the GLabel at each index.

- write a method that builds and returns a GLabel (also an array member?) according to the parameters it was passed.
Was This Post Helpful? 1
  • +
  • -

#4 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 01:36 PM

Grrr. Hopefully 3rd time's a charm.

Mickey

/**
 *  File: ColorLabels.java
 *  ----------------------
 *  Write a GraphicsProgram that creates GLabels for each of the color names RED, ORANGE, YELLOW, GREEN, BLUE, CYAN, MAGENTA, and then puts those labels up on the screen in a random position.  The color of each label, howevere, should be randomly chosen from  the other colors in this list, so that the GLabel for GREEN is allowed to be any color except green. 
 */

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class ColorLabels extends GraphicsProgram {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Initializing the instance variables
	private RandomGenerator rgen = RandomGenerator.getInstance();
	int colorNumber = 1;
	double x = 0;
	double y = 0;	
	Color color = Color.black;
	
	
	public void run() {
		
		//Setting the labels one at a time.  :-\  There's got to be a better way.
		
		GLabel red = new GLabel("RED");
		colorNumber = notThisColor(1);
		color = chooseColor(colorNumber);
		red.setColor(color);
		red.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - red.getWidth());
		y = rgen.nextDouble(red.getHeight(), getHeight() - red.getHeight());
		add(red,x,y);
		
		GLabel orange = new GLabel("ORANGE");
		colorNumber = notThisColor(2);
		color = chooseColor(colorNumber);
		orange.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - orange.getWidth());
		y = rgen.nextDouble(orange.getHeight(), getHeight() - orange.getHeight());
		add(orange,x,y);
		
		GLabel yellow = new GLabel("YELLOW");
		colorNumber = notThisColor(3);
		color = chooseColor(colorNumber);
		yellow.setFont("Serif-bold-32");
		yellow.setColor(color);
		x = rgen.nextDouble(0, getWidth() - yellow.getWidth());
		y = rgen.nextDouble(yellow.getHeight(), getHeight() - yellow.getHeight());
		add(yellow,x,y);
		
		GLabel green = new GLabel("GREEN");
		colorNumber = notThisColor(4);
		color = chooseColor(colorNumber);
		green.setColor(color);
		green.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - green.getWidth());
		y = rgen.nextDouble(green.getHeight(), getHeight() - green.getHeight());
		add(green,x,y);
		
		GLabel magenta = new GLabel("MAGENTA");
		colorNumber = notThisColor(7);
		color = chooseColor(colorNumber);
		magenta.setColor(color);
		magenta.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - magenta.getWidth());
		y = rgen.nextDouble(magenta.getHeight(), getHeight() - magenta.getHeight());
		add(magenta,x,y);
		
		GLabel blue = new GLabel("BLUE");
		colorNumber = notThisColor(5);
		color = chooseColor(colorNumber);
		blue.setColor(color);
		blue.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - blue.getWidth());
		y = rgen.nextDouble(blue.getHeight(), getHeight() - blue.getHeight());
		add(blue,x,y);
		
		GLabel cyan = new GLabel("CYAN");
		colorNumber = notThisColor(6);
		color = chooseColor(colorNumber);
		cyan.setColor(color);
		cyan.setFont("Serif-bold-32");
		x = rgen.nextDouble(0, getWidth() - cyan.getWidth());
		y = rgen.nextDouble(cyan.getHeight(), getHeight() - cyan.getHeight());
		add(cyan,x,y);
		
	}
	

	// Check to make sure that the text color is not the same as the label color.
	private int notThisColor(int num){
		colorNumber = rgen.nextInt(1,7);
		while (colorNumber == num) {
			colorNumber = rgen.nextInt(1,7);
		}
		return colorNumber;
	}
	
	// The list of the colors for the randomgenerator to choose from.
	private Color chooseColor(int colorNumber) {
		switch (colorNumber) {
			case 1: return Color.RED;
			case 2: return Color.ORANGE;
			case 3: return Color.YELLOW;
			case 4: return Color.GREEN;
			case 5: return Color.BLUE;
			case 6: return Color.CYAN;
			case 7: return Color.MAGENTA;
			default: return Color.BLACK;
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9028
  • View blog
  • Posts: 33,488
  • Joined: 27-December 08

Re: adding random GLabels

Posted 25 November 2011 - 01:37 PM

Your program begs for arrays. You could use a GLabel[] and Color[] to simplify your code.
GLabel[] labels = new GLabel[7];
Color[] colors = {Color.RED, Color.ORANGE, ...Populate with the remaining colors... };

for(int i = 0; i < labels.length; i++){
     labels[i] = new GLabel("Text");
     labels[i].setColor(colors[i]);
     //finish initializing labels[i]
}


Was This Post Helpful? 0
  • +
  • -

#6 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 01:38 PM

View PostGregBrannon, on 25 November 2011 - 01:35 PM, said:

Two thoughts that I haven't taken too far:

You could:
- use an array of GLabels in a loop that applies the repetitive actions to the GLabel at each index.

- write a method that builds and returns a GLabel (also an array member?) according to the parameters it was passed.


Thanks Greg,

Hmmm...arrays are still two chapters in the future. A method that builds and returns a GLabel may be doable. Hmmm...what about a class that extends the GLabel? Off to play.

Thanks!
Mickey
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9028
  • View blog
  • Posts: 33,488
  • Joined: 27-December 08

Re: adding random GLabels

Posted 25 November 2011 - 01:40 PM

Quote

Hmmm...what about a class that extends the GLabel?

Inheritance really doesn't make sense here. You wouldn't really be modifying or adding much to the existing GLabel class.
Was This Post Helpful? 0
  • +
  • -

#8 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 01:42 PM

View Postmacosxnerd101, on 25 November 2011 - 01:40 PM, said:

Quote

Hmmm...what about a class that extends the GLabel?

Inheritance really doesn't make sense here. You wouldn't really be modifying or adding much to the existing GLabel class.




Gotcha. A new method, it is.

Thanks,
Mickey
Was This Post Helpful? 0
  • +
  • -

#9 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 02:32 PM

Ok, first roadblock. How do I pass a variable as an argument to assign the name of the GLabel?

I have:

private GLabel makeGlabel(String labelText, int colorNumber) {
	GLabel labelText= new GLabel(labeltext);
}



I want to be able to say:

makeGlabel(red, 1)

and end up with a GLabel named red with the text of the GLabel of "red".

Eclipse isn't liking it. :-(

Thanks,
Mickey
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,819
  • Joined: 10-September 10

Re: adding random GLabels

Posted 25 November 2011 - 02:48 PM

I'm not experienced with the ACM graphics library. I found a GLabel API online, but I don't know if it's the same as you're using.

Referring to the available GLabel constructors from that API and to do what you'd want, you'd do something like:

private GLabel makeGLabel( String labelName, Color color )
{
  GLabel newGLabel = new GLabel( labelName );
  newGLabel.setColor( color );
  // make any other label mods here as well, passing
  // the parameters to the method as needed
  return newGlabel;
}

When you say you get errors, provide details. Cut and paste compiler errors exactly as they appear, or if it is a syntax or other error from Eclipse, reproduce it as best you can.
Was This Post Helpful? 0
  • +
  • -

#11 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 02:55 PM

That's the one I'm using.

Not sure I'm following - aren't all the GLabels made from this method going to be named newGLabel? Doesn't each GLabel need a distinct name so they aren't just overwriting each other?

Thanks,
Mickey
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,819
  • Joined: 10-September 10

Re: adding random GLabels

Posted 25 November 2011 - 03:05 PM

Naming the label returned by the method is accomplished by the calling statement:

GLabel label1 = makeGLabel( "LabelText", Color );

Until you learn arrays, naming them uniquely can only be done with successive calls of the makeGLabel method, assigning the GLabel returned with each call to a new name:

label1 = . . . ;
label2 = . . . ;
label3 = . . . ;
. . .

Swing provides more methods for telling the labels apart, but I don't know if they'd help you here. Arrays aren't that challenging. Unless forbidden, peak ahead a few chapters, but I think you're doing self study.
Was This Post Helpful? 0
  • +
  • -

#13 micnap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-November 11

Re: adding random GLabels

Posted 25 November 2011 - 03:36 PM

Aha. I follow. Gonna skip the arrays for now. If the book continues the way it's been going, it'll probably shoot me back to this exercise to change it over to using arrays in the array chapter anyway. Here's what I have now and it seems to still be working.

Thanks,
Mickey



/**
 *  File: ColorLabels.java
 *  ----------------------
 *  Write a GraphicsProgram that creates GLabels for each of the color names RED, ORANGE, YELLOW, GREEN, BLUE, CYAN, MAGENTA, and then puts those labels up on the screen in a random position.  The color of each label, howevere, should be randomly chosen from  the other colors in this list, so that the GLabel for GREEN is allowed to be any color except green. 
 */

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class ColorLabels extends GraphicsProgram {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Initializing the instance variables
	private RandomGenerator rgen = RandomGenerator.getInstance();
	int colorNumber = 1;
	double x = 0;
	double y = 0;	
	Color color = Color.black;
	
	
	public void run() {
		

		GLabel red = makeGlabel("red", 1);
		add(red);
		
		GLabel orange = makeGlabel("orange", 1);
		add(orange);
		
		GLabel yellow = makeGlabel("yellow", 1);
		add(yellow);
		
		GLabel green = makeGlabel("green", 1);
		add(green);
		
		GLabel blue = makeGlabel("blue", 1);
		add(blue);
		
		GLabel cyan = makeGlabel("cyan", 1);
		add(cyan);
		
		GLabel magenta = makeGlabel("magenta", 1);
		add(magenta);
		
		
	}
	
	private GLabel makeGlabel(String labelText, int colorNumber) {
		
		GLabel glabel = new GLabel(labelText);
		int colorNum = notThisColor(colorNumber);
		color = chooseColor(colorNum);
		glabel.setColor(color);
		glabel.setFont("Serif-bold-22");
		x = rgen.nextDouble(0, getWidth() - glabel.getWidth());
		y = rgen.nextDouble(glabel.getHeight(), getHeight() - glabel.getHeight());
		glabel.setLocation(x,y);
		return glabel;
	}
	
	
	// Check to make sure that the text color is not the same as the label color.
	private int notThisColor(int num){
		colorNumber = rgen.nextInt(1,7);
		while (colorNumber == num) {
			colorNumber = rgen.nextInt(1,7);
		}
		return colorNumber;
	}
	
	// The list of the colors for the randomgenerator to choose from.
	private Color chooseColor(int colorNumber) {
		switch (colorNumber) {
			case 1: return Color.RED;
			case 2: return Color.ORANGE;
			case 3: return Color.YELLOW;
			case 4: return Color.GREEN;
			case 5: return Color.BLUE;
			case 6: return Color.CYAN;
			case 7: return Color.MAGENTA;
			default: return Color.BLACK;
		}
	}
}







Shoot, forgot to change the colorNumber for each call.


/**
 *  File: ColorLabels.java
 *  ----------------------
 *  Write a GraphicsProgram that creates GLabels for each of the color names RED, ORANGE, YELLOW, GREEN, BLUE, CYAN, MAGENTA, and then puts those labels up on the screen in a random position.  The color of each label, howevere, should be randomly chosen from  the other colors in this list, so that the GLabel for GREEN is allowed to be any color except green. 
 */

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class ColorLabels extends GraphicsProgram {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Initializing the instance variables
	private RandomGenerator rgen = RandomGenerator.getInstance();
	int colorNumber = 1;
	double x = 0;
	double y = 0;	
	Color color = Color.black;
	
	
	public void run() {
		
		
		GLabel red = makeGlabel("red", 1);
		add(red);
		
		GLabel orange = makeGlabel("orange", 2);
		add(orange);
		
		GLabel yellow = makeGlabel("yellow", 3);
		add(yellow);
		
		GLabel green = makeGlabel("green", 4);
		add(green);
		
		GLabel blue = makeGlabel("blue", 5);
		add(blue);
		
		GLabel cyan = makeGlabel("cyan", 6);
		add(cyan);
		
		GLabel magenta = makeGlabel("magenta", 7);
		add(magenta);
		
		
	}
	
	private GLabel makeGlabel(String labelText, int colorNumber) {
		
		GLabel glabel = new GLabel(labelText);
		int colorNum = notThisColor(colorNumber);
		color = chooseColor(colorNum);
		glabel.setColor(color);
		glabel.setFont("Serif-bold-22");
		x = rgen.nextDouble(0, getWidth() - glabel.getWidth());
		y = rgen.nextDouble(glabel.getHeight(), getHeight() - glabel.getHeight());
		glabel.setLocation(x,y);
		return glabel;
	}
	
	
	// Check to make sure that the text color is not the same as the label color.
	private int notThisColor(int num){
		colorNumber = rgen.nextInt(1,7);
		while (colorNumber == num) {
			colorNumber = rgen.nextInt(1,7);
		}
		return colorNumber;
	}
	
	// The list of the colors for the randomgenerator to choose from.
	private Color chooseColor(int colorNumber) {
		switch (colorNumber) {
			case 1: return Color.RED;
			case 2: return Color.ORANGE;
			case 3: return Color.YELLOW;
			case 4: return Color.GREEN;
			case 5: return Color.BLUE;
			case 6: return Color.CYAN;
			case 7: return Color.MAGENTA;
			default: return Color.BLACK;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1