Exemplo n.º 1
0
 private GLabel prompt(String endGame) {
   GLabel prompt = new GLabel(endGame);
   prompt.setFont("Times-Bold-50");
   double x = (WIDTH - prompt.getWidth()) / 2;
   double y = HEIGHT * 4.0 / 5.0;
   prompt.setLocation(x, y);
   return prompt;
 }
Exemplo n.º 2
0
 @Override
 public void keyPressed(KeyEvent event) {
   char key = event.getKeyChar();
   if (key == 'k') {
     player.move(0, playerspeed);
   } else if (key == 'i') {
     player.move(0, -playerspeed);
   }
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
  /** Initialize the game */
  public void run() {

    // Get ready
    getReady();
    vx = 0;
    vy = 0;
    num_brick_left = NUM_BRICK_ROW * NUM_BRICK_COL;

    // Add label at the bottom
    mylabel = new GLabel("Developed by Teng Zhong");
    add(mylabel, (getWidth() - mylabel.getWidth()) / 2, (getHeight() - 300));

    // Start game playing
    while (!gameover()) {
      moveBall();
      checkRebounce();
      pause(DELAY);
    }

    // When game is over
    remove(ball);
    GLabel lab = new GLabel("Game Over!");
    lab.setFont("SansSerif-28");
    lab.setColor(Color.RED);
    add(lab, (getWidth() - lab.getWidth()) / 2, (getHeight() - lab.getHeight()) / 2);
  }
Exemplo n.º 5
0
  @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();
  }
Exemplo n.º 6
0
  @Override
  public void run() {
    double reset = getWidth() - letters[0].getWidth();
    while (true) {

      for (int i = 0; i < letters.length; i++) {

        if (letters[i].getBounds().intersects(player.getBounds())) {
          if (letters[i].getLabel().equals("A")) score--; // A for avoid lose a point
          else score++; // G for get earn 1 point
          scoreLabel.setText("Score: " + score);
          // move the letter immediately
          letters[i].setLocation(reset, letters[i].getY());
        } else {
          letters[i].move(-speed, 0);
          // wrap around if past the left edge
          if (letters[i].getX() < 0) letters[i].setLocation(reset, letters[i].getY());
        }
      }
      pause(gameSpeed);
    }
  }
Exemplo n.º 7
0
 public void mouseMoved(MouseEvent e) {
   if (label != null) remove(label);
   label = new GLabel("X: " + e.getX() + " Y: " + e.getY());
   add(label, (getWidth() - label.getWidth()) / 2, (getHeight() - label.getHeight()) / 2);
 }
  public void run() {
    int x_center = getWidth() / 2;
    int y_center = getHeight() / 2;

    GRect top_rect =
        new GRect(
            x_center - (RECT_WIDTH / 2), y_center - (2 * RECT_HEIGHT), RECT_WIDTH, RECT_HEIGHT);
    add(top_rect);

    GRect bottom_left_rect =
        new GRect(
            (x_center - (RECT_WIDTH / 2)) - (RECT_WIDTH + 15), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_left_rect);

    GRect bottom_middle_rect =
        new GRect(x_center - (RECT_WIDTH / 2), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_middle_rect);

    GRect bottom_right_rect =
        new GRect(
            (x_center - (RECT_WIDTH / 2)) + (RECT_WIDTH + 15), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_right_rect);

    GLine left_line =
        new GLine(x_center, y_center - RECT_HEIGHT, x_center - (RECT_WIDTH + 15), y_center);
    add(left_line);

    GLine right_line =
        new GLine(x_center, y_center - RECT_HEIGHT, x_center + (RECT_WIDTH + 15), y_center);
    add(right_line);

    GLine middle_line = new GLine(x_center, y_center - RECT_HEIGHT, x_center, y_center);
    add(middle_line);

    GLabel top_label =
        new GLabel(
            "Program", top_rect.getX() + (RECT_WIDTH / 2), top_rect.getY() + (RECT_HEIGHT / 2));
    top_label.move(-top_label.getWidth() / 2, top_label.getAscent() / 2);
    add(top_label);

    GLabel bottom_left_label =
        new GLabel(
            "GraphicsProgram",
            bottom_left_rect.getX() + (RECT_WIDTH / 2),
            bottom_left_rect.getY() + (RECT_HEIGHT / 2));
    bottom_left_label.move(-bottom_left_label.getWidth() / 2, bottom_left_label.getAscent() / 2);
    add(bottom_left_label);

    GLabel bottom_middle_label =
        new GLabel(
            "ConsoleProgram",
            bottom_middle_rect.getX() + (RECT_WIDTH / 2),
            bottom_middle_rect.getY() + (RECT_HEIGHT / 2));
    bottom_middle_label.move(
        -bottom_middle_label.getWidth() / 2, bottom_middle_label.getAscent() / 2);
    add(bottom_middle_label);

    GLabel bottom_right_label =
        new GLabel(
            "DialogProgram",
            bottom_right_rect.getX() + (RECT_WIDTH / 2),
            bottom_right_rect.getY() + (RECT_HEIGHT / 2));
    bottom_right_label.move(-bottom_right_label.getWidth() / 2, bottom_right_label.getAscent() / 2);
    add(bottom_right_label);
  }
Exemplo n.º 9
0
 private GLabel showLifeCount(int life) {
   GLabel lifeCount = new GLabel("Life Count: " + life);
   lifeCount.setFont("Times-15");
   lifeCount.setLocation(10, lifeCount.getAscent() + 10);
   return lifeCount;
 }
Exemplo n.º 10
0
 /** positions a label in a particular spot */
 private void position(GLabel label_positioned) {
   GLabel box = label_positioned;
   box.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
   box.setColor(FONTCOLOR);
   add(box);
 }
Exemplo n.º 11
0
  /*Place the labels on top */
  private void placeLabels() {
    /*Place label On Rent */
    GLabel onRentLabel = new GLabel("ON RENT");
    onRentLabel.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    onRentLabel.setColor(FONTCOLOR);
    add(onRentLabel, RENTX + RENTWIDTH / 2 - onRentLabel.getWidth() / 2, RENTY - EQUIPHEIGHT);

    /*Place label Available */
    GLabel availForRentLabel = new GLabel("AVAILABLE FOR RENT");
    availForRentLabel.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    availForRentLabel.setColor(FONTCOLOR);
    add(
        availForRentLabel,
        AVAILX + AVAILWIDTH / 2 - availForRentLabel.getWidth() / 2,
        AVAILY - EQUIPHEIGHT);

    /*Place label Shop */
    GLabel shopLabel = new GLabel("SHOP");
    shopLabel.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    shopLabel.setColor(FONTCOLOR);
    add(shopLabel, SHOPX + SHOPWIDTH / 2 - shopLabel.getWidth() / 2, SHOPY - EQUIPHEIGHT * 14);

    /*Place the lost sales label */
    lostSalesLabel1 = new GLabel("Lost Sales HR = " + lostSales.get(0));
    lostSalesLabel2 = new GLabel("Lost Sales MR = " + lostSales.get(1));
    lostSalesLabel3 = new GLabel("Lost Sales LR = " + lostSales.get(2));
    lostSalesLabel1.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    lostSalesLabel2.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    lostSalesLabel3.setFont(new Font("Serif", Font.BOLD, BIGFONTSIZE));
    lostSalesLabel1.setColor(FONTCOLOR);
    lostSalesLabel2.setColor(FONTCOLOR);
    lostSalesLabel3.setColor(FONTCOLOR);
    add(lostSalesLabel1, START_X + 4 * EQUIPWIDTH, 4 * EQUIPHEIGHT);
    add(
        lostSalesLabel2,
        START_X + 4 * EQUIPWIDTH,
        4 * EQUIPHEIGHT + lostSalesLabel1.getHeight() * 1.5);
    add(
        lostSalesLabel3,
        START_X + 4 * EQUIPWIDTH,
        4 * EQUIPHEIGHT + lostSalesLabel2.getHeight() * 3);

    /*Place the days elapsed label */
    daysElapsed = 0;
    daysElapsedLabel = new GLabel("DAY" + daysElapsed);
    daysElapsedLabel.setFont(new Font("Serif", Font.BOLD, 24));
    daysElapsedLabel.setColor(Color.red.darker());
    add(daysElapsedLabel, APPLICATION_WIDTH / 2, START_Y / 2);

    /*Place the sales Label */
    sales = 0;
    salesLabel = new GLabel("SALES: $" + sales);
    salesLabel.setFont(new Font("Serif", Font.BOLD, 24));
    salesLabel.setColor(Color.GREEN.darker());
    add(salesLabel, APPLICATION_WIDTH / 2, daysElapsedLabel.getY() + daysElapsedLabel.getY());

    /*Place the capitalInvested Label */
    capitalInvested = 0;
    capitalLabel = new GLabel("Capital Invested: $" + capitalInvested);
    capitalLabel.setFont(new Font("Serif", Font.BOLD, 18));
    capitalLabel.setColor(Color.RED.darker());
    add(
        capitalLabel,
        lostSalesLabel1.getX(),
        lostSalesLabel3.getY() + lostSalesLabel3.getHeight() * 2);
  }