Package com.beancore.ui;
Import Java.awt.BorderLayout;
Import Java.awt.Container;
Import java.awt.Dimension;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Date;
Import java.util.List;
Import javax.sound.sampled.LineUnavailableException;
Import javax.sound.sampled.UnsupportedAudioFileException;
Import Javax.swing.Box;
Import Javax.swing.BoxLayout;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Com.beancore.config.Config;
Import com.beancore.config.ImageConstants;
Import Com.beancore.entity.Score;
Import Com.beancore.util.FileUtil;
Import Com.beancore.util.ImageLoader;
Import Com.beancore.util.Images;
Import Com.beancore.util.SoundPlayer;
public class MainFrame extends JFrame implements ActionListener {
Private static final long serialversionuid = 1L;
Private Imageloader Imgloader;
Private Gameloadingpanel Gameloadingpanel;
Private Gameplayingpanel Gameplayingpanel;
Private Popupmenupanel Popupmenupanel;
Private Top10scorepanel Popupscorepanel;
Private Helpdialog Helpdialog;
Private SoundPlayer Achievementsoundplayer;
Private list<score> scorelist;
Public MainFrame () throws IOException, Lineunavailableexception, unsupportedaudiofileexception {
try {
This.scorelist = Fileutil.readscore (config.score_file);
catch (Exception e) {
This.scorelist = new arraylist<score> ();
}
This.loadimage ();
This.initsoundplayer ();
This.initcomponents ();
This.setbackgroundimage ();
}
/**
* Load the image part from the whole image, Shoot_background.png shoot.png
* are both extracted from the Weixin apk file
* */
private void LoadImage () throws IOException {
This.imgloader = new Imageloader (config.shoot_background_img);
IMAGES.GAME_LOADING_IMG1 = This.imgLoader.getImage (imageconstants.game_loading_plane_1_pos_x,
Imageconstants.game_loading_plane_1_pos_y, Imageconstants.game_loading_plane_1_width,
Imageconstants.game_loading_plane_1_height);
Images.game_loading_img2 = This.imgLoader.getImage (imageconstants.game_loading_plane_2_pos_x,
Imageconstants.game_loading_plane_2_pos_y, Imageconstants.game_loading_plane_2_width,
Imageconstants.game_loading_plane_2_height);
IMAGES.GAME_LOADING_IMG3 = This.imgLoader.getImage (imageconstants.game_loading_plane_3_pos_x,
Imageconstants.game_loading_plane_3_pos_y, Imageconstants.game_loading_plane_3_width,
Imageconstants.game_loading_plane_3_height);
images.shoot_background_img = This.imgLoader.getImage (imageconstants.game_background_img_pos_x,
Imageconstants.game_background_img_pos_y, Imageconstants.game_background_img_width,
Imageconstants.game_background_img_height);
images.game_loading_text_img = This.imgLoader.getImage (imageconstants.game_loading_text_img_pos_x,
Imageconstants.game_loading_text_img_pos_y, Imageconstants.game_loading_text_img_width,
Imageconstants.game_loading_text_img_height);
This.imgloader = new Imageloader (config.shoot_img);
images.yellow_bullet_img = This.imgLoader.getImage (imageconstants.yellow_bullet_pos_x,
Imageconstants.yellow_bullet_pos_y, Imageconstants.yellow_bullet_width,
Imageconstants.yellow_bullet_height);
images.blue_bullet_img = This.imgLoader.getImage (imageconstants.blue_bullet_pos_x,
Imageconstants.blue_bullet_pos_y, Imageconstants.blue_bullet_width, imageconstants.blue_bullet_height);
images.my_plane_img = This.imgLoader.getImage (imageconstants.my_plane_pos_x, Imageconstants.my_plane_pos_y,
Imageconstants.my_plane_width, Imageconstants.my_plane_height);
images.my_plane_flying_img = This.imgLoader.getImage (imageconstants.my_plane_flying_pos_x,
Imageconstants.my_plane_flying_pos_y, Imageconstants.my_plane_flying_width,
Imageconstants.my_plane_flying_height);
images.small_plane_img = This.imgLoader.getImage (imageconstants.small_plane_pos_x,
Imageconstants.small_plane_pos_y, Imageconstants.small_plane_width, imageconstants.small_plane_height);
images.big_plane_img = This.imgLoader.getImage (imageconstants.big_plane_pos_x, Imageconstants.big_plane_pos_y,
Imageconstants.big_plane_width, Imageconstants.big_plane_height);
images.boss_plane_img = This.imgLoader.getImage (imageconstants.boss_plane_pos_x,
Imageconstants.boss_plane_pos_y, Imageconstants.boss_plane_width, imageconstants.boss_plane_height);
images.bomb_img = This.imgLoader.getImage (imageconstants.bomb_pos_x, Imageconstants.bomb_pos_y,
Imageconstants.bomb_width, Imageconstants.bomb_height);
images.caught_bomb_img = This.imgLoader.getImage (imageconstants.caught_bomb_pos_x,
Imageconstants.caught_bomb_pos_y, Imageconstants.caught_bomb_width, imageconstants.caught_bomb_height);
images.double_laser_img = This.imgLoader.getImage (imageconstants.double_laser_pos_x,
Imageconstants.double_laser_pos_y, Imageconstants.double_laser_width,
Imageconstants.double_laser_height);
images.small_plane_fighting_img = This.imgLoader.getImage (imageconstants.small_plane_fighting_pos_x,
Imageconstants.small_plane_fighting_pos_y, Imageconstants.small_plane_fighting_width,
Imageconstants.small_plane_fighting_height);
images.small_plane_killed_img = This.imgLoader.getImage (imageconstants.small_plane_killed_pos_x,
Imageconstants.small_plane_killed_pos_y, Imageconstants.small_plane_killed_width,
Imageconstants.small_plane_killed_height);
images.small_plane_ashed_img = This.imgLoader.getImage (imageconstants.small_plane_ashed_pos_x,
Imageconstants.small_plane_ashed_pos_y, Imageconstants.small_plane_ashed_width,
Imageconstants.small_plane_ashed_height);
images.big_plane_fighting_img = This.imgLoader.getImage (imageconstants.big_plane_fighting_pos_x,
Imageconstants.big_plane_fighting_pos_y, Imageconstants.big_plane_fighting_width,
Imageconstants.big_plane_fighting_height);
images.big_plane_hitted_img = This.imgLoader.getImage (imageconstants.big_plane_hitted_pos_x,
Imageconstants.big_plane_hitted_pos_y, Imageconstants.big_plane_hitted_width,
Imageconstants.big_plane_hitted_height);
images.big_plane_baddly_wounded_img = This.imgLoader.getImage (imageconstants.big_plane_baddly_wounded_pos_x,
Imageconstants.big_plane_baddly_wounded_pos_y, Imageconstants.big_plane_baddly_wounded_width,
Imageconstants.big_plane_baddly_wounded_height);
images.big_plane_killed_img = This.imgLoader.getImage (imageconstants.big_plane_killed_pos_x,
Imageconstants.big_plane_killed_pos_y, Imageconstants.big_plane_killed_width,
Imageconstants.big_plane_killed_height);
images.big_plane_ashed_img = This.imgLoader.getImage (imageconstants.big_plane_ashed_pos_x,
Imageconstants.big_plane_ashed_pos_y, Imageconstants.big_plane_ashed_width,
Imageconstants.big_plane_ashed_height);
images.boss_plane_fighting_img = This.imgLoader.getImage (imageconstants.boss_plane_fighting_pos_x,
Imageconstants.boss_plane_fighting_pos_y, Imageconstants.boss_plane_fighting_width,
Imageconstants.boss_plane_fighting_height);
images.boss_plane_hitted_img = This.imgLoader.getImage (imageconstants.boss_plane_hitted_pos_x,
Imageconstants.boss_plane_hitted_pos_y, Imageconstants.boss_plane_hitted_width,
Imageconstants.boss_plane_hitted_height);
images.boss_plane_baddly_wounded_img = This.imgLoader.getImage (imageconstants.boss_plane_baddly_wounded_pos_x,
Imageconstants.boss_plane_baddly_wounded_pos_y, Imageconstants.boss_plane_baddly_wounded_width,
Imageconstants.boss_plane_baddly_wounded_height);
images.boss_plane_killed_img = This.imgLoader.getImage (imageconstants.boss_plane_killed_pos_x,
Imageconstants.boss_plane_killed_pos_y, Imageconstants.boss_plane_killed_width,
Imageconstants.boss_plane_killed_height);
images.boss_plane_ashed_img = This.imgLoader.getImage (imageconstants.boss_plane_ashed_pos_x,
Imageconstants.boss_plane_ashed_pos_y, Imageconstants.boss_plane_ashed_width,
Imageconstants.boss_plane_ashed_height);
images.score_img = This.imgLoader.getImage (imageconstants.score_img_pos_x, Imageconstants.score_img_pos_y,
Imageconstants.score_img_width, Imageconstants.score_img_height);
This.imgloader = new Imageloader (config.font_img);
images.x_mark_img = This.imgLoader.getImage (imageconstants.x_mark_pos_x, Imageconstants.x_mark_pos_y,
Imageconstants.x_mark_width, Imageconstants.x_mark_height);
images.number_0_img = This.imgLoader.getImage (imageconstants.number_0_pos_x, Imageconstants.number_0_pos_y,
Imageconstants.number_0_width, Imageconstants.number_0_height);
images.number_1_img = This.imgLoader.getImage (imageconstants.number_1_pos_x, Imageconstants.number_1_pos_y,
Imageconstants.number_1_width, Imageconstants.number_1_height);
images.number_2_img = This.imgLoader.getImage (imageconstants.number_2_pos_x, Imageconstants.number_2_pos_y,
Imageconstants.number_2_width, Imageconstants.number_2_height);
images.number_3_img = This.imgLoader.getImage (imageconstants.number_3_pos_x, Imageconstants.number_3_pos_y,
Imageconstants.number_3_width, Imageconstants.number_3_height);
images.number_4_img = This.imgLoader.getImage (imageconstants.number_4_pos_x, Imageconstants.number_4_pos_y,
Imageconstants.number_4_width, Imageconstants.number_4_height);
images.number_5_img = This.imgLoader.getImage (imageconstants.number_5_pos_x, Imageconstants.number_5_pos_y,
Imageconstants.number_5_width, Imageconstants.number_5_height);
images.number_6_img = This.imgLoader.getImage (imageconstants.number_6_pos_x, Imageconstants.number_6_pos_y,
Imageconstants.number_6_width, Imageconstants.number_6_height);
images.number_7_img = This.imgLoader.getImage (imageconstants.number_7_pos_x, Imageconstants.number_7_pos_y,
Imageconstants.number_7_width, Imageconstants.number_7_height);
images.number_8_img = This.imgLoader.getImage (imageconstants.number_8_pos_x, Imageconstants.number_8_pos_y,
Imageconstants.number_8_width, Imageconstants.number_8_height);
images.number_9_img = This.imgLoader.getImage (imageconstants.number_9_pos_x, Imageconstants.number_9_pos_y,
Imageconstants.number_9_width, Imageconstants.number_9_height);
}
private void Initcomponents () {
This.settitle ("Shoot plane-www.java1234.com");
This.seticonimage (New ImageIcon (config.logo_img). GetImage ());
This.setsize (Config.main_frame_width, config.main_frame_height);
Dimension d = Toolkit.getdefaulttoolkit (). Getscreensize ();
This.setbounds (D.width-config.main_frame_width)/2, (D.height-config.main_frame_height)/2,
Config.main_frame_width, Config.main_frame_height);
This.setresizable (FALSE);
This.setvisible (TRUE);
This.setdefaultcloseoperation (Jframe.exit_on_close);
}
private void Initsoundplayer () throws Lineunavailableexception, Unsupportedaudiofileexception, IOException {
Achievementsoundplayer = new SoundPlayer (Config.achievement_audio);
}
private void SetBackgroundImage () {
imageicon Bgimgicon = new ImageIcon (images.shoot_ BACKGROUND_IMG);
jlabel Bglabel = new JLabel (Bgimgicon);
this.getlayeredpane (). Add (Bglabel, New Integer (Integer.min_value));
bglabel.setbounds (0, 0, bgimgicon.geticonwidth (), Bgimgicon.geticonheight ());
((JPanel) This.getcontentpane ()). Setopaque (false);
}
private void Popupmenupanel () {
Container C = This.getcontentpane ();
C.removeall ();
This.repaint ();
if (This.popupmenupanel = = null) {
This.popupmenupanel = new Popupmenupanel (this);
}
BoxLayout boxlayout = new BoxLayout (c, Boxlayout.y_axis);
C.setlayout (BoxLayout);
C.add (Box.createverticalglue ());
C.add (This.popupmenupanel);
C.add (Box.createverticalglue ());
This.validate ();
}
public void Loadgame () throws Lineunavailableexception, Unsupportedaudiofileexception, IOException {
Container C = This.getcontentpane ();
C.removeall ();
This.repaint ();
if (This.gameloadingpanel = = null) {
This.gameloadingpanel = new Gameloadingpanel ();
}
BoxLayout boxlayout = new BoxLayout (c, Boxlayout.y_axis);
C.setlayout (BoxLayout);
C.add (Box.createverticalglue ());
C.add (This.gameloadingpanel);
C.add (Box.createverticalglue ());
This.gameLoadingPanel.loadingGame ();
This.startgame ();
}
private void Startgame () throws Lineunavailableexception, Unsupportedaudiofileexception, IOException {
Container C = This.getcontentpane ();
C.removeall ();
This.repaint ();
BorderLayout borderlayout = new BorderLayout ();
C.setlayout (BorderLayout);
This.gameplayingpanel = new Gameplayingpanel ();
C.add (This.gameplayingpanel, Borderlayout.center);
This.gamePlayingPanel.startGame ();
Long starttime = System.currenttimemillis ();
while (This.gamePlayingPanel.getMyPlane (). IsAlive ()) {
try {
Thread.Sleep (Config.game_panel_repaint_interval);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
Long endtime = System.currenttimemillis ();
Add to score List
This.addscore (This.gamePlayingPanel.getScore (), endtime-starttime);
int option = Joptionpane.showconfirmdialog (This, "Game over, Score:" + this.gamePlayingPanel.getScore ()
+ ", Start Again", "Game over", joptionpane.yes_no_option);
Switch (option) {
Case Joptionpane.yes_option:
Loadgame ();
Break
Case Joptionpane.no_option:
Stopgame ();
Break
}
}
private void Addscore (int score, long lastmilliseconds) throws IOException {
score s = new Score (New Date (System.currenttimemillis ()), Score, lastmilliseconds);
int size = This.scoreList.size ();
if (This.scoreList.contains (s)) {
return;
&NBSP}
if (Size < Config.max_score_count) {
this.scoreList.add (s);
&NBSP} else {
Score lastscore = This.scoreList.get (size-1);
if (S.compareto (Lastscore) > 0) {
this.scorelist.remove (lastscore);
this.scorelist.add (s);
}
}
collections.sort (this.scorelist);
collections.reverse (this.scorelist);
fileutil.writescore (scorelist, config.score_file);
}
public void Stopgame () {
Popupmenupanel ();
}
@Override
public void actionperformed (ActionEvent e) {
String actioncmd = E.getactioncommand ();
if (Actioncmd.equals (Popupmenupanel.start_game_button)) {
Startgameaction ();
else if (actioncmd.equals (Popupmenupanel.top_10_scores_button)) {
This.achievementSoundPlayer.play ();
Popupscorepanel (this.scorelist);
else if (actioncmd.equals (Popupmenupanel.exit_game_button)) {
Exitgameaction ();
else if (actioncmd.equals (Popupmenupanel.help_button)) {
Helpaction ();
else if (actioncmd.equals (Top10scorepanel.ok_button)) {
This.popupmenupanel ();
}
}
private void Popupscorepanel (list<score> sortedscorelist) {
Container C = This.getcontentpane ();
C.removeall ();
This.repaint ();
if (This.popupscorepanel = = null) {
This.popupscorepanel = new Top10scorepanel (this);
}
This.popupScorePanel.loadScore (sortedscorelist);
BoxLayout boxlayout = new BoxLayout (c, Boxlayout.y_axis);
C.setlayout (BoxLayout);
C.add (Box.createverticalglue ());
C.add (This.popupscorepanel);
C.add (Box.createverticalglue ());
This.validate ();
}
private void Startgameaction () {
New Thread (New Startgameactionclass ()). Start ();
}
Class Startgameactionclass implements Runnable {
@Override
public void Run () {
try {
Loadgame ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
private void Exitgameaction () {
System.exit (0);
}
private void Helpaction () {
if (This.helpdialog = = null) {
This.helpdialog = new Helpdialog ();
}
This.helpDialog.setVisible (TRUE);
}
}