public void run() { getWidth(); getHeight(); double x = getWidth() / 2; double y = getHeight() / 2; GRect a = new GRect(x - 1.5 * SIZE, y - 1.5 * SIZE, SIZE, 3 * SIZE); a.setColor(Color.RED); a.setFilled(true); add(a); GRect b = new GRect(x - SIZE / 2, y - 1.5 * SIZE, SIZE, 3 * SIZE); b.setColor(Color.YELLOW); b.setFilled(true); add(b); GRect c = new GRect(x + SIZE / 2, y - 1.5 * SIZE, SIZE, 3 * SIZE); c.setColor(TURQUOISE); c.setFilled(true); add(c); GLabel d = new GLabel("Flag of Peru", x + SIZE, y + 1.9 * SIZE); d.setColor(Color.BLACK); d.setFont("Broadway-12"); add(d); }
private void AddLabel() { GLabel label = new GLabel("Flag of Mali"); label.setFont("Verdana-18"); double x = getWidth() - label.getWidth(); double y = getHeight() - label.getDescent(); add(label, x, y); }
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; }
/** Draws the inscription in the lower-right corner */ private void drawInscription() { GLabel label = new GLabel("Flag of Luxembourg"); label.setFont("Courier New"); label.setColor(Color.BLACK); double x = getWidth() - label.getWidth(); double y = getHeight() - label.getDescent(); add(label, x, y); }
public void showMessage(String message) { pausePlay(); gameMessage.setLabel(message); gameMessage.setLocation( WIDTH / 2 - gameMessage.getWidth() / 2, HEIGHT / 2 - gameMessage.getHeight() / 2); add(gameMessage); }
@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); } }
private void paint_label() { /* Write an inscription in the lower right corner of the window * From the window width and subtract the width of the label we get the start * point of the X inscription. (similar to height) */ GLabel label = new GLabel("Flag of " + COUNTRY, FLAG_WIDTH, getHeight()); label.setFont(new Font("Tamoha", Font.BOLD, 18)); label.setLocation((getWidth() - label.getWidth()), (getHeight() - label.getHeight() / 2)); add(label); }
public void startApp() { waitForClick(); getVelocity(); while (true) { moveBall(); // delay time, reduced as the player is about to end the game to increases it's difficult pause(delay + reakCounter / 25); if (reakCounter == 0) { GLabel win = new GLabel("YOU WIN!!"); add(win, (getWidth() - win.getWidth()) / 2, HEIGHT / 2); } } }
public void setup() { setSize(WIDTH, HEIGHT); getGCanvas().addMouseMotionListener(this); getGCanvas().addMouseListener(this); lifeDisplay = new GLabel(""); lifeDisplay.setFont("*-bold-14"); gameMessage = new GLabel(""); gameMessage.setFont("*-bold-32"); showMessage("CLICK TO BEGIN"); }
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; }
public void restartGame() { getGCanvas().removeAll(); lives = NTURNS; bricksRemaining = NBRICK_ROWS * NBRICKS_PER_ROW; Color[] colors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN}; int xOffset = BRICK_SEP / 2; int yOffset = BRICK_Y_OFFSET; for (int row = 0; row < NBRICK_ROWS; row++) { Color color = colors[(int) ((double) colors.length / (double) NBRICK_ROWS * row)]; for (int col = 0; col < NBRICKS_PER_ROW; col++) { BreakoutBrick rect = new BreakoutBrick( col * BRICK_WIDTH + col * BRICK_SEP + xOffset, row * BRICK_HEIGHT + row * BRICK_SEP + yOffset, BRICK_WIDTH, BRICK_HEIGHT); rect.setColor(color); rect.setFilled(true); add(rect); } } paddle = new BreakoutPaddle( WIDTH / 2 - PADDLE_WIDTH / 2, HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET, PADDLE_WIDTH, PADDLE_HEIGHT); add(paddle); ball = new BreakoutBall(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS, BALL_RADIUS); add(ball); lifeDisplay.setLabel(String.format("Lives remaining: %d", lives)); lifeDisplay.setLocation(WIDTH - lifeDisplay.getWidth() - 10, lifeDisplay.getHeight() + 10); add(lifeDisplay); resumePlay(); }
/** 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); }
@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(); }
/** The method accepts a string and displays the green text in the center of the screen */ private GLabel createMessage(String message) { GLabel text = new GLabel(message); text.setFont("Arial Black-20"); text.setColor(Color.GREEN); double x = (WIDTH - text.getWidth()) / 2; double y = (HEIGHT - text.getDescent()) / 2; text.setLocation(x, y); add(text); return text; }
/** Displays the amount of points on the screen in text form */ private GLabel createScore(String score) { GLabel text = new GLabel(score); text.setFont("Arial Black-12"); text.setColor(Color.black); double x = (WIDTH - text.getWidth()) / 2; double y = HEIGHT - text.getDescent(); text.setLocation(x, y); add(text); return text; }
@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); } }
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); }
/* Draw graph line with the name and rating */ private void drawEntry(NameSurferEntry entry, int entryNumber) { /* Draws graph line */ for (int i = 0; i < NDECADES - 1; i++) { int position1 = entry.getRank(i); int position2 = entry.getRank(i + 1); double x1 = i * (getWidth() / NDECADES); double x2 = (i + 1) * (getWidth() / NDECADES); double y1 = 0; double y2 = 0; if (position1 != 0 && position2 != 0) { y1 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position1 / MAX_RANK; y2 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position2 / MAX_RANK; } else if (position1 == 0 && position2 == 0) { y1 = getHeight() - GRAPH_MARGIN_SIZE; y2 = y1; } else if (position1 == 0) { y1 = getHeight() - GRAPH_MARGIN_SIZE; y2 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position2 / MAX_RANK; } else { y1 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position1 / MAX_RANK; y2 = getHeight() - GRAPH_MARGIN_SIZE; } GLine line = new GLine(x1, y1, x2, y2); /* Set line color */ if (entryNumber % 4 == 0) { line.setColor(Color.BLUE); } else if (entryNumber % 4 == 1) { line.setColor(Color.RED); } else if (entryNumber % 4 == 2) { line.setColor(Color.MAGENTA); } else if (entryNumber % 4 == 3) { line.setColor(Color.BLACK); } add(line); } /* Add label with the name and ranking */ for (int i = 0; i < NDECADES; i++) { String name = entry.getName(); int position = entry.getRank(i); String positionString = Integer.toString(position); String label = name + " " + positionString; double x = i * (getWidth() / NDECADES) + 10; double x1 = x - NDECADES; double y; int R = 5; if (position != 0) { y = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position / MAX_RANK; } else { /* Add "*" if name was not used and remove marker point */ label = name + " *"; y = getHeight() - GRAPH_MARGIN_SIZE - 10; R = 0; } /* Add marker point */ GOval marker = new GOval(x1, y - 2, R, R); /* Got "y-2" by scientific research =) */ marker.setFilled(true); GLabel nameLabel = new GLabel(label, x, y); nameLabel.setFont(new Font("Times Roman", Font.BOLD, 12)); /* Set label color */ if (entryNumber % 4 == 0) { nameLabel.setColor(Color.BLUE); marker.setColor(Color.BLUE); } else if (entryNumber % 4 == 1) { nameLabel.setColor(Color.RED); marker.setColor(Color.RED); } else if (entryNumber % 4 == 2) { nameLabel.setColor(Color.MAGENTA); marker.setColor(Color.MAGENTA); } else if (entryNumber % 4 == 3) { nameLabel.setColor(Color.BLACK); marker.setColor(Color.BLACK); } add(nameLabel); add(marker); } }
private GLabel showLifeCount(int life) { GLabel lifeCount = new GLabel("Life Count: " + life); lifeCount.setFont("Times-15"); lifeCount.setLocation(10, lifeCount.getAscent() + 10); return lifeCount; }
public void play() { GCanvas canvas = getGCanvas(); ball.move(); if (ball.getX() < 0 || ball.getX() + ball.getDiameter() > WIDTH) { ball.bounceX(); } if (ball.getY() < 0) { ball.bounceY(); } if (ball.getY() + ball.getDiameter() > HEIGHT) { lives--; lifeDisplay.setLabel(String.format("Lives remaining: %d", lives)); if (lives <= 0) { showMessage("DEFEAT!"); } else { ball.setLocation(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS); } return; } for (int i = 0; i < 4; i++) { GPoint point = new GPoint( ball.getX() + (i & 1) * ball.getDiameter(), ball.getY() + ((i & 2) / 2) * ball.getDiameter()); GObject o = canvas.getElementAt(point); if (o == null) { continue; } if (o instanceof BreakoutBrick) { canvas.remove(o); bricksRemaining--; if (bricksRemaining <= 0) { showMessage("VICTORY!"); } } else if (o instanceof BreakoutPaddle) { paddleBounces++; if (paddleBounces > NPADDLE_BOUNCES) { paddleBounces = 0; ball.accelerate(1.0); } } if (point.getX() < o.getX() + o.getWidth() || point.getX() < o.getX()) { ball.bounceX(); } if (point.getY() < o.getY() + o.getHeight() || point.getY() < o.getY()) { ball.bounceY(); } return; } }
/** 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); }
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); }
private void moveBall() { ball.move(vx, vy); /* * top side reflection */ if (ball.getY() - ball.getHeight() / 2 < 0) { vy = -vy; } /* * sides reflect */ else if (ball.getX() > WIDTH - ball.getHeight() || ball.getX() - ball.getHeight() / 2 < 0) { vx = -vx; } /* * ending game if ball goes beyond the paddle, with 3 tries * and restarting it b4 the 3 tries */ else if (ball.getY() > HEIGHT) { endtrying++; if (endtrying >= 3) { removeAll(); GLabel end = new GLabel("YOU LOST!!"); add(end, (getWidth() - end.getWidth()) / 2, getHeight() / 2); } // restart part else { removeAll(); run(); } } // getting what's on sides of the ball, and starting to handle it GObject row = collectSides(); if (row == baddle) { /* * to test Y coordinates for ball and paddle * println( baddle.getY()+ "|" + ball.getY()); * turned out to be that there's 19 pixel between ball.y and paddle.y; fixing that with if */ if (ball.getY() > baddle.getY() - BALL_RADIUS) { vy = -vy; bounceClip.play(); /* * glue issue fix, move the ball above the paddle, so that it doesn't reverse again */ if (row == baddle) { ball.move(0, -PADDLE_HEIGHT); } } } /* here removing object which is brick, then subtracting the counter, also checking if * bricks ended so it display final message "YOY WIN" */ else if (row != null) { remove(row); reakCounter--; vy = -vy; } }
/*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); }
public void run() { addKeyListeners(); File boardFile = new File("largerboard.txt"); try { Scanner sc = new Scanner(boardFile); int rowNum = 0; while (sc.hasNextLine()) { String s = sc.nextLine(); char[] nums = s.toCharArray(); for (int i = 0; i < nums.length; i++) { char2D[i][rowNum] = nums[i]; System.out.print(char2D[i][rowNum] + " "); } System.out.println(); rowNum++; } } catch (IOException e) { System.out.println("File not found."); } GRect box; GOval o; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { // int rand = (int) (3*Math.random()); // twoD[j][i] = rand; // System.out.print(twoD[j][i] + " "); box = new GRect(boxSize * j, boxSize * i, boxSize, boxSize); add(box); if (char2D[j][i] == '0') { box.setFilled(true); box.setColor(Color.BLACK); o = new GOval(boxSize * j + 10, boxSize * i + 10, 5, 5); o.setFilled(true); o.setColor(Color.YELLOW); add(o); food.add(o); } else if (char2D[j][i] == '1') { box.setFilled(true); box.setColor(Color.BLUE); } } // System.out.println(); } // end of nested for loops player = new GOval(playerX + 5, playerY + 5, 15, 15); player.setFilled(true); player.setColor(Color.RED); add(player); addGhosts(); while (true) { // ghost movement // 0: up, 1: down, 2: right, 3: left for (int i = 0; i < ghostList.size(); i++) { ghost = (Ghost) ghostList.get(i); if (timer % 500 == 0) { if (playerX < ghost.getGhostX()) { ghost.move(3); } else if (playerY > ghost.getGhostY()) { ghost.move(1); } else if (playerX > ghost.getGhostX()) { ghost.move(2); } else if (playerY < ghost.getGhostY()) { ghost.move(0); } // int rand =(int) (4*Math.random()); // between 0-3 // if(rand == 2 && isCollidable(ghost.getGhostX()+1, ghost.getGhostY())){ // ghost.move(rand); // } // else if(rand == 3 && isCollidable(ghost.getGhostX()-1, ghost.getGhostY())){ // ghost.move(rand); // } // else if(rand == 1 && isCollidable(ghost.getGhostX(), ghost.getGhostY()+1)){ // ghost.move(rand); // } // else if(rand == 0 && isCollidable(ghost.getGhostX(), ghost.getGhostY()-1)){ // ghost.move(rand); // } // System.out.println(ghost.getGhostX() +" , "+ ghost.getGhostY() ); } } if (collision(ghost, player)) { remove(player); hasLost = true; break; } if (food.size() == 0) { break; } for (int i = 0; i < food.size(); i++) { GOval foodPiece = (GOval) food.get(i); if (collision(player, foodPiece)) { remove(foodPiece); food.remove(i); } } pause(10); timer = timer + 10; } GLabel wonLost; if (hasLost) { wonLost = new GLabel("YOU LOSE", 220, 250); } else { wonLost = new GLabel("YOU WON AND ARE AWESOME", 220, 250); } wonLost.setColor(Color.WHITE); add(wonLost); }