import Font(“Arial”, 1, 120)); snakey.setColor(Color.white); snakey.drawString(“Good Game”, 8, 149);

import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.RenderingHints;import java.awt.Robot;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Vector;import javax.swing.ActionMap;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.KeyStroke;class data{ int sidea; int sidez; int diameter; int linez; int linea; int counter; int multi; data(){  diameter = 9; }}public class Game extends JFrame { snake gameboard = null; JMenuBar scrolldown = null; JMenu options = null; JMenuItem play = null; JMenuItem replay = null; boolean game1 = true; Game(){ scrolldown = new JMenuBar(); options = new JMenu(“Click Here for Options”);   play   = new JMenuItem(“Play”); replay = new JMenuItem(“Replay”); replay.setMnemonic(KeyEvent.VK_R); replay.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK)); replay.setMnemonic(KeyEvent.VK_I); play.setMnemonic(KeyEvent.VK_P); play.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK)); options.add(play); options.add(replay); scrolldown.add(options); this.setJMenuBar(scrolldown); gameboard = new snake(); this.setMinimumSize(new Dimension(750, 600)); gameboard.dim = this.getSize(); this.add(gameboard); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  play.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent initiategame){play();}    });  replay.addActionListener(new ActionListener(){      public void actionPerformed(ActionEvent initiategame2){replay();}    });  this.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){gameboard.keyPress(e);}});       this.setVisible(true);  this.pack(); }  public void play(){  if(game1){   gameboard.t.start();   game1 = false;   play.setEnabled(false);  } }  public void replay(){  int score = 0;  gameboard.vector.clear();  data snakebody = new data3;  for(int i =0; i < 3; i++){   snakebodyi = new data();   snakebodyi.sidez = 200;   snakebodyi.sidea = 150-score;   snakebodyi.linea = 150-score;   snakebodyi.linez = 18;   gameboard.vector.add(snakebodyi);   score = score+1;  }  gameboard.end = false; }}//a panel where all the drawings take place and it is embedded into JFrameclass snake extends JPanel implements Runnable{ Vector vector = null;   //vector which will hold all the beads of snake body data snakebody = null; int centx = 0, centy = 0; int inc = 10, pos = 0; char dir = ‘D’; Dimension dim = null; Thread t = null; boolean b = true, end = false; snake(){  vector = new Vector();  snakebody = new data3;  for(int i =0; i < 3; i++){   snakebodyi = new data();   snakebodyi.sidez = 200;   snakebodyi.sidea = 150-pos;   snakebodyi.linea = 150-pos;   snakebodyi.linez = 200;   vector.add(snakebodyi);   score = score+1;  }  t = new Thread(this);  this.setLayout(null);  this.setBackground(Color.black);  this.setDoubleBuffered(true);  dim = this.getSize();  this.addKeyListener(new KeyAdapter(){public void keyTyped(KeyEvent e){keyPress(e);}});  this.setVisible(true); } int time = 70; boolean gamepause = true;  void keyPress(KeyEvent e){  if(e.getKeyCode() == KeyEvent.VK_DOWN){   if(dir != 'U')    dir = 'D';  }  else if(e.getKeyCode() == KeyEvent.VK_UP){   if(dir != 'D')    dir = 'U';  }  else if(e.getKeyCode() == KeyEvent.VK_LEFT){   if(dir != 'R')    dir = 'L';  }  else if(e.getKeyCode() == KeyEvent.VK_RIGHT){   if(dir != 'L')    dir = 'R';  }  else if(e.getKeyCode() == KeyEvent.VK_ESCAPE)   System.exit(0);  else if(e.getKeyCode() == KeyEvent.VK_SPACE){   if(gamepause){    t.suspend();    gamepause = false;   }   else{    t.resume();    gamepause = true;   }  }  else if(e.getKeyCode() == KeyEvent.VK_S){   time--;  }  else if(e.getKeyCode() == KeyEvent.VK_D){   time++;  }   }   //generating random position of fruit int fruitx = 20+(int)(Math.random()*56)*10; int fruity = 20+(int)(Math.random()*52)*10; int score = 0; public void paintComponent(Graphics draw){  super.paintComponent(draw);  Graphics2D snakey = (Graphics2D)draw;  score = vector.size()-3;  snakey.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  int red = 30+(int)(Math.random()*233);  snakey.setColor(Color.white);  snakey.setFont(new Font("Arial", 1, 20));  snakey.drawString("your score: "+score,650, 230);  snakey.fillRect(0, 0, dim.width-200, dim.height);  snakey.setColor(Color.gray);  snakey.fillRect(10, 10, dim.width-220, dim.height-65);  snakey.setColor(new Color(red));//, green, blue));  snakey.fillOval(fruitx, fruity, 10, 10);  snakey.setColor(Color.red);  if(end){   //done   snakey.setFont(new Font("Arial", 1, 120));   snakey.setColor(Color.white);   snakey.drawString("Good Game", 8, 149);   snakey.drawString("Select Replay", 8, 349);  }  else{     //review   snakey.fillOval(vector.get(0).sidez, vector.get(0).sidea, vector.get(0).diameter, vector.get(0).diameter);   for(int i=1; i < vector.size(); i++){    snakey.fillOval(vector.get(i).sidez, vector.get(i).sidea, vector.get(i).diameter, vector.get(i).diameter);    vector.get(i).linez = vector.get(i).sidez;    vector.get(i).linea = vector.get(i).sidea;    vector.get(i).sidez = vector.get(i-1).linez;    vector.get(i).sidea = vector.get(i-1).linea;   }  }   }   //important function which will see collision of snake with wall or itself void checkOutOfBounds(int x, int y){  if(x < 10 || x > dim.width-220 || y < 10 || y > dim.height-61){   end = true;   try{Thread.sleep(1000);}catch(Exception e){}   repaint();  }  else if(x==fruitx && y==fruity){   data snakebody = new data();   snakebody.sidez = vector.get(vector.size()-1).linez;   snakebody.sidea = vector.get(vector.size()-1).linea;   vector.add(snakebody);   fruitx = 20+(int)(Math.random()*56)*10;   fruity = 20+(int)(Math.random()*52)*10;     }  for(int i=1; i < vector.size(); i++){   if(x == vector.get(i).sidez && y == vector.get(i).sidea){    end = true;    try{Thread.sleep(1000);}catch(Exception e){}    repaint();   }  } }  public void run(){    while (true){   switch(dir){    case 'L':     vector.get(0).sidez = (vector.get(0).sidez-inc);     vector.get(0).linez = vector.get(0).sidez;     checkOutOfBounds(vector.get(0).sidez, vector.get(0).sidea);     repaint();     break;    case 'R':          vector.get(0).sidez = (vector.get(0).sidez+inc);     vector.get(0).linez = vector.get(0).sidez;     checkOutOfBounds(vector.get(0).sidez, vector.get(0).sidea);     repaint();     break;    case 'U':          vector.get(0).sidea = (vector.get(0).sidea-inc);     vector.get(0).linea = vector.get(0).sidea;     checkOutOfBounds(vector.get(0).sidez, vector.get(0).sidea);     repaint();     break;    case 'D':          vector.get(0).sidea = (vector.get(0).sidea+inc);     vector.get(0).linea = vector.get(0).sidea;     checkOutOfBounds(vector.get(0).sidez, vector.get(0).sidea);     repaint();     break;   }   try{    Thread.sleep(time);   }   catch(Exception e){}  } }}