0% found this document useful (0 votes)
8 views

Main Frame

Uploaded by

anhnt.22it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Main Frame

Uploaded by

anhnt.22it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package main;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import connect.SQLStatments;
import read.ReadFile;

public class MainFrame extends JFrame implements KeyListener, ActionListener{


private Ball ball;
private JButton level_1, level_2, level_3;
private JButton newGameButton, exitButton, returnButton;
private JLabel highScoreLabel;
private Menu menu;
private Level level;
private Draw draw;

private String userName;


private int highScore;
public int getHighScore() {
return highScore;
}

private int width = 600;


private int height = 640;
private int sign = 0;
public MainFrame(String userName, int highScore) {
this.userName = userName;
this.highScore = highScore;
setUp();
}

private void setUp() {


this.setTitle("Flappy Ball");
this.setFocusable(true);
this.setSize(width, height);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setComps();
this.addKeyListener(this);
this.setVisible(true);
}

private void setComps() {


ball = new Ball(40, 300, 50, Color.RED);

newGameButton = new JButton("New Game");


highScoreLabel = new JLabel("High Score: " + highScore);
exitButton = new JButton("Exit");
newGameButton.addActionListener(this);
exitButton.addActionListener(this);

level_1 = new JButton("Level 1");


level_1.addActionListener(this);
level_2 = new JButton("Level 2");
level_2.addActionListener(this);
level_3 = new JButton("Level 3");
level_3.addActionListener(this);
returnButton = new JButton("Return");
returnButton.addActionListener(this);

menu = new Menu(newGameButton, exitButton, highScoreLabel);


level = new Level(level_1, level_2, level_3, returnButton);
this.add(menu);
}

@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == 38 || e.getKeyCode() == 87) {
draw.jump();
}
}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 38 || e.getKeyCode() == 87) {
draw.jump();
}
if(e.getKeyCode() == 69) {
if(sign == 1) {
resetBall();
updateScore();
draw = new Draw(ball, ReadFile.getRectanglesLevel1(),
width, height);
draw.setScore(0);
this.setContentPane(draw);
this.invalidate();
this.validate();
}
if(sign == 2) {
resetBall();
updateScore();
draw = new Draw(ball, ReadFile.getRectanglesLevel2(),
width, height);
draw.setScore(0);
this.setContentPane(draw);
this.invalidate();
this.validate();
}
if(sign == 3) {
resetBall();
updateScore();
draw = new Draw(ball, ReadFile.getRectanglesLevel3(),
width, height);
draw.setScore(0);
this.setContentPane(draw);
this.invalidate();
this.validate();
}
}

if(e.getKeyCode() == 77) {
this.setContentPane(menu);
this.invalidate();
this.validate();
}
}

@Override
public void keyReleased(KeyEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newGameButton) {
this.setContentPane(level);
this.invalidate();
this.validate();
}

if(e.getSource() == returnButton) {
this.setContentPane(menu);
this.invalidate();
this.validate();
}
if(e.getSource() == exitButton) {
System.exit(0);
}

if(e.getSource() == level_1) {
sign = 1;
draw = new Draw(ball, ReadFile.getRectanglesLevel1(), width,
height);
this.setContentPane(draw);
this.invalidate();
this.validate();
}

if(e.getSource() == level_2) {
sign = 2;
draw = new Draw(ball, ReadFile.getRectanglesLevel2(), width,
height);
this.setContentPane(draw);
this.invalidate();
this.validate();
}

if(e.getSource() == level_3) {
sign = 3;
draw = new Draw(ball, ReadFile.getRectanglesLevel3(), width,
height);
this.setContentPane(draw);
this.invalidate();
this.validate();
}

private void resetBall() {


ball.setBallPosition(new Point(40, 300));
ball.setBallSize(50);
}

private void updateScore() {


int newHighScore = draw.getHighScore();
if(checkScore(highScore, newHighScore)) {
SQLStatments.updateUserHighScore(userName, newHighScore);
}
}

private boolean checkScore(int oldScore, int newScore) {


boolean isGreater = false;

if(newScore > oldScore) {


isGreater = true;
}

return isGreater;
}
}

You might also like