public class sSView extends JFrame
{
//Initialize variables
private JMenuBar mnuBar;
private JDialog dlgHighscore, dlgHelp, dlgAbout;
private JPanel pnlMain, pnlRight, pnlHighscore, pnlHelp, pnlAbout;
private JButton cmdPlay = new JButton("Start!");
public Container pnlLeft;
public JLabel lblLives, lblSpeed, lblJoints, lblBeers, lblPoints;
public ImageIcon imgJoint, imgBeer;
private JButton bUp,bDown,bLeft,bRight;
private BufferedImage imgHighscore;
private ImageIcon imgHelp, imgAbout;
private Icon icnHighscore;
private String[] Highscorelist;
/**
* This is the default constructor of sSView
**/
public sSView()
{
super();
Initialize();
imgJoint = new ImageIcon(daSnake.class.getClassLoader().getResource("stonedSnake/sSImages/imgJoint.gif"));
imgBeer = new ImageIcon(daSnake.class.getClassLoader().getResource("stonedSnake/sSImages/imgBeer.gif"));
}
/**
* This method Initializes JFrame
* @return void
**/
private void Initialize()
{
//Init Frame
this.setSize(550, 456);
this.setLocation(200, 0);
this.setTitle("stonedSnake ~Alpha~ - Can you really doing the snake stoned enough?!?");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();
System.out.println("Konstruktor fand KeyEvent: "+code);
if (!(code >= 0x25/*VK_LEFT*/ && code <= 0x28/*VK_DOWN*/) && (code < KeyEvent.VK_NUMPAD1 || code == KeyEvent.VK_NUMPAD5 || code > KeyEvent.VK_NUMPAD9))
{
return;
}
if (code == KeyEvent.VK_NUMPAD8 || code == KeyEvent.VK_UP)
{
if (sSControl.Snake.Movin != sSControl .Snake.MOVEUP && sSControl.Snake.Movin != sSControl .Snake.MOVEDOWN)
sSControl.Snake.Movin = sSControl.Snake.MOVEUP;
}
else if (code == KeyEvent.VK_NUMPAD2 || code == KeyEvent.VK_NUMPAD5 || code == KeyEvent.VK_DOWN)
{
if (sSControl.Snake.Movin != sSControl .Snake.MOVEDOWN && sSControl.Snake.Movin != sSControl .Snake.MOVEUP)
sSControl.Snake.Movin = sSControl.Snake.MOVEDOWN;
}
else if (code == KeyEvent.VK_NUMPAD4 || code == KeyEvent.VK_LEFT)
{
if (sSControl.Snake.Movin != sSControl .Snake.MOVELEFT && sSControl.Snake.Movin != sSControl .Snake.MOVERIGHT)
sSControl.Snake.Movin = sSControl.Snake.MOVELEFT;
}
else if (code == KeyEvent.VK_NUMPAD6 || code == KeyEvent.VK_RIGHT)
{
if (sSControl.Snake.Movin != sSControl .Snake.MOVERIGHT && sSControl.Snake.Movin != sSControl .Snake.MOVELEFT)
sSControl.Snake.Movin = sSControl.Snake.MOVERIGHT;
}
}
});
//init Container pnlLeft
pnlLeft = new JComponent()
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.drawRect(0,0,400,400);
try
{
if (null != sSControl.Snake.stonedSnake)
{
Enumeration s = sSControl.Snake.stonedSnake.elements();
while (s.hasMoreElements())
{
ColorRectangle cr = (ColorRectangle)s.nextElement();
g.setColor(cr.color);
g.fillRect(cr.x, cr.y, cr.width, cr.height);
}
}
if (sSControl.Snake.avJoint == true)
imgJoint.paintIcon(sSControl.viewSnake.pnlLeft, g, sSControl.Snake.jointX, sSControl.Snake.jointY);
if (sSControl.Snake.avBeer == true)
imgBeer.paintIcon(sSControl.viewSnake.pnlLeft, g, sSControl.Snake.beerX, sSControl.Snake.beerY);
}
catch (NullPointerException ex)
{
//John Wayne interested...
}
}
};
pnlLeft.setLayout(null);
pnlLeft.setSize(400,455);
//Init Menu
mnuBar = new JMenuBar();
mnuBar.add(getMenuGame());
mnuBar.add(getMenuHelp());
//Init Panels
pnlMain = new JPanel();
pnlMain.setLayout(new BorderLayout());
pnlMain.setSize(550,456);
pnlRight = new JPanel();
pnlRight.setSize(150,456);
pnlRight.setLayout(new GridLayout(20,2));
pnlRight.add(new JLabel(""));
cmdPlay.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
onClickcmdPlay();
}
}
);
pnlRight.add(cmdPlay);
pnlRight.add(new JLabel("Lives: "));
pnlRight.add(lblLives = new JLabel(Integer.toString(sSControl.modelSnake.getLives())));
pnlRight.add(new JLabel("Speed: "));
pnlRight.add(lblSpeed = new JLabel(Integer.toString(sSControl.modelSnake.getSpeed())));
pnlRight.add(new JLabel("Joints: "));
pnlRight.add(lblJoints = new JLabel(Integer.toString(sSControl.modelSnake.getJoints())));
pnlRight.add(new JLabel("Beers: "));
pnlRight.add(lblBeers = new JLabel(Integer.toString(sSControl.modelSnake.getBeers())));
pnlRight.add(new JLabel("Points: "));
pnlRight.add(lblPoints = new JLabel(Integer.toString(sSControl.modelSnake.getPoints())));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
pnlRight.add(new JLabel(""));
bUp = new JButton("Up");
bDown = new JButton("Down");
bLeft = new JButton("Left");
bRight = new JButton("Right");
bUp.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (sSControl.Snake.Movin != daSnake.MOVEUP && sSControl.Snake.Movin != sSControl.Snake.MOVEDOWN)
sSControl.Snake.Movin = daSnake.MOVEUP;
}
}
);
bDown.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (sSControl.Snake.Movin != daSnake.MOVEDOWN && sSControl.Snake.Movin != sSControl.Snake.MOVEUP)
sSControl.Snake.Movin = daSnake.MOVEDOWN;
}
}
);
bLeft.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (sSControl.Snake.Movin != daSnake.MOVELEFT && sSControl.Snake.Movin != sSControl.Snake.MOVERIGHT)
sSControl.Snake.Movin = daSnake.MOVELEFT;
}
}
);
bRight.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (sSControl.Snake.Movin != daSnake.MOVERIGHT && sSControl.Snake.Movin != sSControl.Snake.MOVELEFT)
sSControl.Snake.Movin = daSnake.MOVERIGHT;
}
}
);
pnlRight.add(bUp);
pnlRight.add(new JLabel(""));
pnlRight.add(bLeft);
pnlRight.add(bRight);
pnlRight.add(bDown);
pnlRight.add(new JLabel(""));
pnlMain.add(pnlLeft,BorderLayout.CENTER);
pnlMain.add(pnlRight,BorderLayout.EAST);
//Add components into Frame
this.setJMenuBar(mnuBar);
this.getContentPane().add(pnlMain);
//Set the Frame visible
this.setVisible(true);
}
/**
* This method starts/stops playing
* @param void
* @return void
**/
protected void onClickcmdPlay()
{
if (cmdPlay.getText().equals("Start!"))
{
sSControl.startGame();
cmdPlay.setText("Stop!");
}
else if (cmdPlay.getText().equals("Stop!"))
{
sSControl.stopGame();
cmdPlay.setText("Start!");
}
}
/**
* This method updates the right panel
* @param void
* @return void
**/
public void updateRightPanel()
{
lblLives.setText(Integer.toString(sSControl.modelSnake.getLives()));
lblSpeed.setText(Integer.toString(sSControl.modelSnake.getSpeed()));
lblJoints.setText(Integer.toString(sSControl.modelSnake.getJoints()));
lblBeers.setText(Integer.toString(sSControl.modelSnake.getBeers()));
lblPoints.setText(Integer.toString(sSControl.modelSnake.getPoints()));
}
/**
* This method gets the Menu Game
* @param void
* @return JMenu
**/
private JMenu getMenuGame()
{
//Initialize components
JMenu mnuGame;
JMenuItem mnuItemGame;
mnuGame = new JMenu("Game");
// MenuItem New
mnuItemGame = new JMenuItem("New Game!");
setCtrlAccelerator(mnuItemGame, 'N');
mnuItemGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
sSControl.newGame();
}
}
);
mnuGame.add(mnuItemGame);
// MenuItem Highscores
mnuItemGame = new JMenuItem("HighScores");
setCtrlAccelerator(mnuItemGame, 'S');
mnuItemGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
showHighscores();
}
}
);
mnuGame.add(mnuItemGame);
// MenuItem Exit
mnuItemGame = new JMenuItem("Exit");
setCtrlAccelerator(mnuItemGame, 'E');
mnuItemGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
);
mnuGame.add(mnuItemGame);
//returns the Menu
return mnuGame;
}
/**
* This method gets the Menu Help
* @return JMenu
**/
private JMenu getMenuHelp()
{
//Initialize components
JMenu mnuHelp;
JMenuItem mnuItemHelp;
mnuHelp = new JMenu("?");
//MenuItem Help
mnuItemHelp = new JMenuItem("Help");
setCtrlAccelerator(mnuItemHelp, 'H');
mnuItemHelp.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
showHelp();
}
}
);
mnuHelp.add(mnuItemHelp);
//MenuItem About
mnuItemHelp = new JMenuItem("About");
setCtrlAccelerator(mnuItemHelp, 'A');
mnuItemHelp.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
showAbout();
}
}
);
mnuHelp.add(mnuItemHelp);
//returns the Menu
return mnuHelp;
}
/**
* This method shows the stonedSnake Highscore list
* @param void
* @return void
**/
public void showHighscores()
{
//Init components
final String strBroker = new String(" ");
Highscorelist = new String[11];
dlgHighscore = new JDialog(this,"stonedSnake - Highscores", true);
dlgHighscore.setLocation(250, 0);
dlgHighscore.setSize(237,450);
dlgHighscore.setResizable(false);
JLabel lblHighscore = new JLabel();
JPanel pnlHighscore = new JPanel(new BorderLayout());
JPanel pnlHighscoreTop = new JPanel(new BorderLayout());
JPanel pnlHighscoreCenter1 = new JPanel(new GridLayout(1,1));
JPanel pnlHighscoreCenter2 = new JPanel(new GridLayout(7,1));
JPanel pnlHighscoreBottom = new JPanel(new BorderLayout());
JButton cmdHighscoreClose = new JButton("Close!");
cmdHighscoreClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
dlgHighscore.setVisible(false);
}
}
);
Highscorelist = sSControl.getHighscorelist();
try
{
imgHighscore = ImageIO.read(sSView.class.getClassLoader().getResource("stonedSnake/sSImages/imgHighscore.jpg"));
final String[] strUsername = new String[4];
final String[] strPoints = new String[4];
final int xHolder;
final int yHolder;
xHolder = 0;
yHolder = 13;
for (int i=1; i<=3; i++)
{
int ioAt = Highscorelist[i].indexOf("@");
strUsername[i] = Highscorelist[i].substring(0, ioAt);
strPoints[i] = Highscorelist[i].substring(ioAt+1, Highscorelist[i].length());
}
icnHighscore = new Icon(){
public int getIconHeight() {
return 250;
}
public int getIconWidth() {
return 250;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.BLACK);
g.drawImage(imgHighscore, 0, 0, null);
g.drawString("1. "+strUsername[1], 74, 224);
g.drawString("2. "+strUsername[2], 15, 187);
g.drawString("3. "+strUsername[3], 165, 192);
g.drawString(strPoints[1]+" Pts", 74+xHolder, 224+yHolder);
g.drawString(strPoints[2]+" Pts", 15+xHolder, 187+yHolder);
g.drawString(strPoints[3]+" Pts", 165+xHolder, 192+yHolder);
}
};
}
catch (IOException e) {
System.out.println(e);
}
//add components
pnlHighscoreCenter1.add(new JLabel(icnHighscore));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 4. "+Highscorelist[4]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 5. "+Highscorelist[5]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 6. "+Highscorelist[6]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 7. "+Highscorelist[7]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 8. "+Highscorelist[8]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+" 9. "+Highscorelist[9]+" Pts"));
pnlHighscoreCenter2.add(new JLabel(strBroker+"10. "+Highscorelist[10]+" Pts"));
pnlHighscoreTop.add(pnlHighscoreCenter1,BorderLayout.NORTH);
pnlHighscoreTop.add(pnlHighscoreCenter2,BorderLayout.CENTER);
pnlHighscoreBottom.add(cmdHighscoreClose);
pnlHighscore.add(pnlHighscoreTop,BorderLayout.CENTER);
pnlHighscore.add(pnlHighscoreBottom,BorderLayout.SOUTH);
dlgHighscore.add(pnlHighscore);
//sets the DialogAbout visible!
dlgHighscore.setVisible(true);
}
/**
* This method shows the Help dialog.
* @param void
* @return void
*/
public void showHelp()
{
//Init components
dlgHelp = new JDialog(this,"stonedSnake - Help", true);
dlgHelp.setLocation(250, 0);
dlgHelp.setSize(450,239);
dlgHelp.setResizable(false);
JLabel lblHelp = new JLabel();
JPanel pnlHelp = new JPanel(new BorderLayout());
JPanel pnlHelpTop = new JPanel(new BorderLayout());
JPanel pnlHelpBottom = new JPanel(new BorderLayout());
JButton cmdHelpClose = new JButton("Close!");
cmdHelpClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
dlgHelp.setVisible(false);
}
}
);
imgHelp = new ImageIcon(sSView.class.getClassLoader().getResource("stonedSnake/sSImages/imgHelp.gif"));
lblHelp.setText("<html>This is the stonedSnake help!
" +
"This game knows the following keys:
" +
"Arrow Left -> Snake turns to the left
" +
"Arrow Right -> Snake turns to the right
" +
"Ctrl+N -> New game
" +
"Ctrl+S -> Show Highscores
" +
"Ctrl+E -> Exit game
" +
"Ctrl+H -> stonedSnake - Help
" +
"Ctrl+A -> stonedSnake - About</html>");
//add components
pnlHelpTop.add(new JLabel(imgHelp),BorderLayout.WEST);
pnlHelpTop.add(lblHelp,BorderLayout.CENTER);
pnlHelpBottom.add(cmdHelpClose);
pnlHelp.add(pnlHelpTop,BorderLayout.CENTER);
pnlHelp.add(pnlHelpBottom,BorderLayout.SOUTH);
dlgHelp.add(pnlHelp);
//sets the DialogAbout visible!
dlgHelp.setVisible(true);
}
/**
* This method shows the About dialog.
* @param void
* @return void
*/
public void showAbout()
{
//Init components
dlgAbout = new JDialog(this,"stonedSnake - About", true);
dlgAbout.setLocation(250, 0);
dlgAbout.setSize(300,180);
dlgAbout.setResizable(false);
JLabel lblAbout = new JLabel();
JPanel pnlAbout = new JPanel(new BorderLayout());
JPanel pnlAboutTop = new JPanel(new BorderLayout());
JPanel pnlAboutBottom = new JPanel(new BorderLayout());
JButton cmdAboutClose = new JButton("Close!");
cmdAboutClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
dlgAbout.setVisible(false);
}
}
);
imgAbout = new ImageIcon(sSView.class.getClassLoader().getResource("stonedSnake/sSImages/imgAbout.gif"));
lblAbout.setText("<html>This game is written by dR.LoL,
a stoned dude from a little hicksville!
(s)Right to Smoke 2006</html>");
//add components
pnlAboutTop.add(new JLabel(imgAbout),BorderLayout.WEST);
pnlAboutTop.add(lblAbout,BorderLayout.CENTER);
pnlAboutBottom.add(cmdAboutClose);
pnlAbout.add(pnlAboutTop,BorderLayout.CENTER);
pnlAbout.add(pnlAboutBottom,BorderLayout.SOUTH);
dlgAbout.add(pnlAbout);
//sets the DialogAbout visible!
dlgAbout.setVisible(true);
}
/**
* This method creates the CTRL-Accelerator.
* @param JMenuItem, char
* @return void
*/
private void setCtrlAccelerator(JMenuItem myMenuItem, char chrKey)
{
KeyStroke ks = KeyStroke.getKeyStroke(chrKey, Event.CTRL_MASK);
myMenuItem.setAccelerator(ks);
}
}