import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JLabel; import java.net.*; import java.io.*; /* */ public class TankApplet2 extends JApplet implements ActionListener{ final static int MAX_Y = 15; final static int MAX_X = 15; final static int FRAME_HEIGHT = 450; final static int FRAME_WEIGHT = 450; final static int LAST_STAGE = 7; JLabel stageName; JButton nextStageButton; TankSimulator tank; Grid grid; int stageNumber,score,xp; public TankApplet2(){ JRootPane pane = getRootPane(); //@assume pane != null pane.putClientProperty("defaultSystemEventQueueCheck", Boolean.TRUE); } public void init(){ try{ try{ stageNumber = Integer.decode(getParameter("stageNumber")).intValue(); }catch(RuntimeException re){ stageNumber = 1; } try{ score = Integer.decode(getParameter("score")).intValue(); xp = Integer.decode(getParameter("xp")).intValue(); }catch(RuntimeException re){ score = 0; xp = 0; } if(stageNumber<=LAST_STAGE){ URL url = new URL(getCodeBase(), "stages/stage"+stageNumber+".txt"); URLConnection uc = url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream())); grid = new Grid(MAX_X,MAX_Y); tank = new TankSimulator (FRAME_HEIGHT,FRAME_WEIGHT,grid,br); tank.status.setStatus(score,xp); tank.setBackground(new Color(235,235,235)); stageName = new JLabel(tank.stageName); nextStageButton = new JButton ("Next Stage"); nextStageButton.setVerticalTextPosition (AbstractButton.CENTER); nextStageButton.setHorizontalTextPosition (AbstractButton.LEFT); nextStageButton.setMnemonic (KeyEvent.VK_I); nextStageButton.setActionCommand ("Next Stage"); nextStageButton.addActionListener(this); Container content = getContentPane(); if (content != null) { content.add(stageName, BorderLayout.NORTH); content.add(tank, BorderLayout.CENTER); content.add(nextStageButton, BorderLayout.SOUTH); }else{ System.err.println("ERROR: No content pane"); } }else{ JLabel done = new JLabel("Congratulation, you have complete the missions.\n"+ "Your score is "+score+"."); Container content = getContentPane(); content.add(done); } }catch(MalformedURLException me){ System.out.println(me.toString()); showStatus("Where is my file?"); }catch(IOException ie){ showStatus(ie.toString()); } } public void actionPerformed(ActionEvent ae){ String command = ae.getActionCommand(); if(command != null){ if(command=="Next Stage"){ if(tank.ut.numEnemies<=0){ stageNumber++; String values = "stageNumber="+stageNumber; /* values += "&item1="+tank.ut.items[0].getName(); values += "&item2="+tank.ut.items[1].getName(); values += "&item3="+tank.ut.items[2].getName(); */ values += "&score="+tank.ut.getStatus().score; values += "&xp="+tank.ut.getStatus().xp; try{ URL next = new URL("http://www.cs.virginia.edu/cs201j/problem-sets/ps5-submit/tankhunt/TankHunt.php?"+values); getAppletContext().showDocument(next,"_self"); }catch(MalformedURLException me){ System.out.println(me.toString()); } } } } } }