public GLabel makeLetterLabel(int numDivs) { boolean get = randgen.nextBoolean(); String ga; if (get) ga = "G"; else ga = "A"; GLabel getOrAvoid = new GLabel(ga); getOrAvoid.setFont(new Font("Cambria", Font.BOLD, 24)); double locx = randgen.nextDouble(INDENT, getWidth() - getOrAvoid.getWidth()); int whichdiv = randgen.nextInt(1, numDivs); double locy = whichdiv * getHeight() / numDivs; getOrAvoid.setLocation(locx, locy); return getOrAvoid; }
@Override public void init() { randgen = new RandomGenerator(); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setTitle("Get or Avoid!"); setBackground(Color.gray); score = 0; scoreLabel = new JLabel("Score: " + score); scoreLabel.setFont(new Font("Cambria", Font.BOLD, 18)); add(scoreLabel, NORTH); letters = new GLabel[10]; for (int i = 0; i < 10; i++) { letters[i] = makeLetterLabel(screenDivisions); // 5 initial divisions add(letters[i]); } player = new GLabel("P", INDENT, 3 * getHeight() / screenDivisions); player.setFont(new Font("Cambria", Font.BOLD, 24)); player.setColor(Color.blue); add(player); addKeyListeners(); }