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;
}
}
}

New Topic/Question
Reply



MultiQuote







|