Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Created simple game applet but wanting it to access a DSN on the server

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Created simple game applet but wanting it to access a DSN on the server

    Hello all

    Good to be with guys who know how to code been searching for a while. Basically, I know probably as this applet is not signed thats probably the problem (not sure how to ) but for a college assignment I have created an applet, basically a recreation of the classic space invaders and at the end of it, it should record the name, email, score, and game to an ODBC based database. Ive got it to work via a localhost server, with a few adjustment to the permissions, but cannot get it to work on the server as regards writing to the database.

    Within my project I run 2 java files as different threads, for automation purposes, and here is the code:

    SpaceInvaders.java
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class SpaceInvaders extends Applet implements KeyListener, ActionListener, MouseListener
    {
     
    	private static final long serialVersionUID = 3807112331656800048L;
    	private Connection c;
    	final static private String _driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    	final static private String _url = "jdbc:odbc:RetroGaming";
    	public int rows = 11;
    	public int cols = 8;
    	private Bots b;
    	private Image bgImage;
    	private bullets bullets;
    	public int numbullets = 5;
    	public Label bulletcount = new Label(" " + this.numbullets);
    	private Label gamestatus = new Label();
    	private TextField txtName = new TextField("Name");
      	private TextField txtEmail = new TextField("E-mail");
      	private Button btnSubmitScore = new Button("Submit");
      	public SpaceInvaders.Cell[][] board = new SpaceInvaders.Cell[this.rows][this.cols];
      	MediaTracker mt = new MediaTracker(this);
      	private AudioClip win;
      	private AudioClip lose;
     
     
     public void paint(Graphics g)
      {
        if (this.bgImage != null)
        {
          int x = 0; int y = 0;
          while (y < getHeight())
          {
            x = 0;
            while (x < getWidth())
            {
              g.drawImage(this.bgImage, x, y, this);
              x += this.bgImage.getWidth(null);
            }
            y += this.bgImage.getHeight(null);
          }
        }
      }
     public void init() 
     {
    	lose=this.getAudioClip(getCodeBase(), "gameover.au");
    	String media[] = {"bullet.JPG","blank.jpg","e1.jpg","e2.jpg","Ship.jpg","title.png"};
    	Image[] imgs = new Image[media.length];
    	for(int i=0;i<media.length;i++)
        {
    		imgs[i] = Toolkit.getDefaultToolkit().getImage(media[i]);
    	    mt.addImage(imgs[i], i);
        }
    	this.bgImage = getImage(getCodeBase(), "title.png");
        setLayout(null);
        setBackground(Color.BLACK);
        setSize(600, 750);
        int q = 100;
        int w = 15;
        for (int i = 0; i < this.rows; i++)
        {
          q += 50;
          for (int j = 0; j < this.cols; j++)
          {
            w += 50;
            this.board[i][j] = new SpaceInvaders.Cell();
            this.board[i][j].setBounds(w, q, 50, 50);
          }
          w = 15;
        }
        for (int x = 1; x < 4; x++)
        {
          for (int y = 1; y < 7; y++)
          {
            this.board[x][y].imgName = "e1.jpg";
            add(this.board[x][y]);
          }
        }
     
        int temp = this.cols / 2;
        this.board[(this.rows - 1)][temp].imgName = "Ship.jpg";
        add(this.board[(this.rows - 1)][temp]);
        add(this.bulletcount);
        add(this.gamestatus);
        add(this.txtName);
        add(this.btnSubmitScore);
        add(this.txtEmail);
        this.gamestatus.setBounds(130, 200, 300, 20);
        this.txtName.setBounds(130, 225, 150, 20);
        this.txtEmail.setBounds(130, 245, 150, 20);
        this.btnSubmitScore.setBounds(285, 245, 50, 20);
        this.gamestatus.setVisible(false);
        this.txtEmail.setVisible(false);
        this.txtName.setVisible(false);
        this.btnSubmitScore.setVisible(false);
        this.gamestatus.setForeground(Color.GREEN);
        this.bulletcount.setBounds(535, 480, 20, 40);
        this.bulletcount.setForeground(Color.GREEN);
        setFocusable(true);
        addKeyListener(this);
        requestFocus();
        this.btnSubmitScore.addActionListener(this);
        addMouseListener(this);
      }
     public void GameOver(String status)
      {
        for (int i = 0; i < this.rows; i++)
        {
          for (int j = 0; j < this.cols; j++)
          {
            this.board[i][j].setVisible(false);
          }
        }
        if (status == "Win")
        {
        	try
        	{
     
          this.gamestatus.setText("Congratulations! Please enter your name and email: ");
          this.gamestatus.setVisible(true);
          this.txtName.setVisible(true);
          this.txtEmail.setVisible(true);
          this.btnSubmitScore.setVisible(true);
          this.play(getCodeBase(), "hail.au");
        	}
        	catch(Throwable er)
        	{
        		er.printStackTrace();
     
        	}
        }
        else
        {
          this.gamestatus.setText("Unlucky! Please enter your name and email: ");
          this.gamestatus.setVisible(true);
          this.txtName.setVisible(true);
          this.txtEmail.setVisible(true);
          this.btnSubmitScore.setVisible(true);
          System.out.println("Lose");
          lose.play();
        }
      }
     public boolean lastRow()
      {
        for (int y = 0; y < this.cols; y++)
        {
          if ((this.board[(this.rows - 2)][y].imgName == "e1.jpg") || (this.board[(this.rows - 2)][y].imgName == "e2.jpg"))
          {
            return true;
          }
        }
        return false;
      }
     public void start()
      {
    	try {
    		mt.waitForAll();
    	} catch (InterruptedException e) 
    	{
    	}
        this.b = new Bots(this);
        this.b.start();
     
        for (int i = 0; i < this.rows; i++)
        {
          for (int j = 0; j < this.cols; j++)
          {
            add(this.board[i][j]);
          }
        }
      }
     
      public void keyPressed(KeyEvent ke)
      {
        int key = ke.getKeyCode();
        if (key == 37)
        {
          for (int y = 0; y < this.cols; y++)
          {
            if (this.board[(this.rows - 1)][y].imgName != "Ship.jpg")
              continue;
            if (y < 1)
            {
              continue;
            }
     
            this.board[(this.rows - 1)][y].imgName = "";
            this.board[(this.rows - 1)][(y - 1)].imgName = "Ship.jpg";
            repaint();
            this.board[(this.rows - 1)][y].repaint();
            this.board[(this.rows - 1)][(y - 1)].repaint();
          }
     
        }
     
        if (key == 39)
        {
          for (int y = this.cols - 1; y >= 0; y--)
          {
            if (this.board[(this.rows - 1)][y].imgName != "Ship.jpg")
              continue;
            if (y > this.cols - 2)
            {
              continue;
            }
     
            this.board[(this.rows - 1)][y].imgName = "";
            this.board[(this.rows - 1)][(y + 1)].imgName = "Ship.jpg";
            repaint();
            this.board[(this.rows - 1)][y].repaint();
            this.board[(this.rows - 1)][(y + 1)].repaint();
          }
     
        }
     
        if (key == 32)
        {
          if (this.numbullets != 0)
          {
            for (int y = 0; y < this.cols; y++)
            {
              if (this.board[(this.rows - 1)][y].imgName != "Ship.jpg")
              {
                continue;
              }
              play(getCodeBase(), "shoot.wav");
              this.bullets = new bullets(this);
              this.bullets.shipPos = y;
              this.bullets.start();
            }
     
            this.numbullets -= 1;
            this.bulletcount.setText(" " + this.numbullets);
          }
        }
      }
     
      public void keyReleased(KeyEvent ke)
      {
      }
     
      public void keyTyped(KeyEvent ke)
      {
      }
     
      public void actionPerformed(ActionEvent ae)
      {
    	  try {
    	  Class.forName (_driver);
    	  c = DriverManager.getConnection(_url);
    	  Statement stmt = c.createStatement();
          this.txtName.setVisible(false);
          this.txtEmail.setVisible(false);
          this.btnSubmitScore.setVisible(false);
          String temp = "INSERT INTO tblScores VALUES ('" + txtName.getText() + "', '" + txtEmail.getText() + "' , '123', 'SpaceInvaders')";      
          stmt.executeUpdate(temp);
    	  } 
    	  catch (SQLException e) {} 
    	  catch (ClassNotFoundException e) {}
          this.gamestatus.setText("Thank You for submitting your score!");
     
     
      }
      public void mouseClicked(MouseEvent arg0) {
      }
     
      public void mouseEntered(MouseEvent arg0) {
        requestFocus();
      }
     
      public void mouseExited(MouseEvent arg0)
      {
      }
     
      public void mousePressed(MouseEvent arg0)
      {
      }
     
      public void mouseReleased(MouseEvent arg0)
      {
      }
     
      public class Cell extends Canvas
      {
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 408238105995219813L;
    	private Image im;
        public String imgName = new String();
     
        public Cell() {
          setSize(45, 45);
        }
     
        public void paint(Graphics g)
        {
          g.setColor(Color.BLACK);
          g.fillRect(0, 0, 45, 45);
          this.im = SpaceInvaders.this.getImage(SpaceInvaders.this.getCodeBase(), this.imgName);
          g.drawImage(this.im, 0, 0, this);
        }
      }
    }


    bots.java

    public class Bots extends Thread
    {
      private SpaceInvaders si;
      private boolean left = false;
      private int x = 0; private int y = 0;
      private int posX1 = 0; private int posY1 = 0;
      private int posX2 = 0; private int posY2 = 0;
      private int sound = 1;
      public String status;
      private int botrow = 0;
      private boolean running = true;
     
      public Bots(SpaceInvaders si) {
        this.si = si;
      }
     
      public void move(int posY1, int posY2, int posX1, int posX2)
      {
        try
        {
          if (this.si.board[posX1][posY1].imgName == "")
          {
            this.si.GameOver("Win");
            this.running = false;
     
          }
          else if (this.si.lastRow())
          {
            this.running = false;
            this.si.GameOver("Lose"); 
          }
          Thread.sleep(1000L);
          if (this.left)
          {
            if (this.sound == 1)
            {
              this.si.play(this.si.getCodeBase(), "esound1.wav");
              this.sound = 2;
            }
            else if (this.sound == 2)
            {
              this.si.play(this.si.getCodeBase(), "esound2.wav");
              this.sound = 3;
            }
            else if (this.sound == 3)
            {
              this.si.play(this.si.getCodeBase(), "esound3.wav");
              this.sound = 4;
            }
            else if (this.sound == 4)
            {
              this.si.play(this.si.getCodeBase(), "esound4.wav");
              this.sound = 1;
            }
            if (posY1 == 0)
            {
              if (this.si.lastRow())
              {
                return;
              }
              for (int x = posX1 + this.botrow; x > posX1 - 1; x--)
              {
                for (int y = posY1; y <= posY2; y++)
                {
                  if (this.si.board[x][y].imgName == "e1.jpg")
                  {
                    this.si.board[(x + 1)][y].imgName = "e2.jpg";
                    this.si.board[(x + 1)][y].repaint();
                  }
                  else if (this.si.board[x][y].imgName == "e2.jpg")
                  {
                    this.si.board[(x + 1)][y].imgName = "e1.jpg";
                    this.si.board[(x + 1)][y].repaint();
                  }
     
                  this.si.board[x][y].imgName = "";
     
                  this.si.board[x][y].repaint();
                }
                this.left = false;
              }
            }
            else
            {
              for (int x = posX1; x < posX1 + this.botrow; x++)
              {
                for (int y = posY1; y <= posY2; y++)
                {
                  if (this.si.board[x][y].imgName == "e1.jpg")
                  {
                    this.si.board[x][(y - 1)].imgName = "e2.jpg";
                    this.si.board[x][(y - 1)].repaint();
                  }
                  else if (this.si.board[x][y].imgName == "e2.jpg")
                  {
                    this.si.board[x][(y - 1)].imgName = "e1.jpg";
                    this.si.board[x][(y - 1)].repaint();
                  }
     
                  this.si.board[x][y].imgName = "";
     
                  this.si.board[x][y].repaint();
                }
              }
            }
          }
          else
          {
            if (this.sound == 1)
            {
              this.si.play(this.si.getCodeBase(), "esound1.wav");
              this.sound = 2;
            }
            else if (this.sound == 2)
            {
              this.si.play(this.si.getCodeBase(), "esound2.wav");
              this.sound = 3;
            }
            else if (this.sound == 3)
            {
              this.si.play(this.si.getCodeBase(), "esound3.wav");
              this.sound = 4;
            }
            else if (this.sound == 4)
            {
              this.si.play(this.si.getCodeBase(), "esound4.wav");
              this.sound = 1;
            }
            if (posY2 == this.si.cols - 1)
            {
              if (this.si.lastRow())
              {
                return;
              }
              for (int x = posX1 + this.botrow; x > posX1 - 1; x--)
              {
                for (int y = posY1; y <= posY2; y++)
                {
                  if (this.si.board[x][y].imgName == "e1.jpg")
                  {
                    this.si.board[(x + 1)][y].imgName = "e2.jpg";
                    this.si.board[(x + 1)][y].repaint();
                  }
                  else if (this.si.board[x][y].imgName == "e2.jpg")
                  {
                    this.si.board[(x + 1)][y].imgName = "e1.jpg";
                    this.si.board[(x + 1)][y].repaint();
                  }
     
                  this.si.board[x][y].imgName = "";
     
                  this.si.board[x][y].repaint();
                }
              }
              this.left = true;
            }
            else
            {
              for (int x = posX1; x < posX1 + this.botrow; x++)
              {
                for (int y = posY2; y >= posY1; y--)
                {
                  if (this.si.board[x][y].imgName == "e1.jpg")
                  {
                    this.si.board[x][(y + 1)].imgName = "e2.jpg";
                  }
                  else if (this.si.board[x][y].imgName == "e2.jpg")
                  {
                    this.si.board[x][(y + 1)].imgName = "e1.jpg";
                  }
                  this.si.board[x][y].imgName = "";
     
                  this.si.board[x][(y + 1)].repaint();
                  this.si.board[x][y].repaint();
                }
              }
            }
          }
        }
        catch (InterruptedException localInterruptedException)
        {
        }
      }
     
      public void run()
      {
        while (this.running)
        {
          boolean stop = false;
     
          for (this.x = 0; this.x < this.si.rows; this.x += 1)
          {
            for (this.y = 0; this.y < this.si.cols - 1; this.y += 1)
            {
              if ((this.si.board[this.x][this.y].imgName != "e1.jpg") && (this.si.board[this.x][this.y].imgName != "e2.jpg"))
                continue;
              this.posX1 = this.x;
              this.posY1 = this.y;
              stop = true;
              break;
            }
     
            if (!stop) {
              continue;
            }
            this.x = 0;
            this.y = 0;
            stop = false;
            break;
          }
     
          for (this.x = 0; this.x < this.si.rows; this.x += 1)
          {
            for (this.y = (this.si.cols - 1); this.y > 0; this.y -= 1)
            {
              if ((this.si.board[this.x][this.y].imgName != "e1.jpg") && (this.si.board[this.x][this.y].imgName != "e2.jpg"))
                continue;
              this.posX2 = this.x;
              this.posY2 = this.y;
              stop = true;
              break;
            }
     
            if (!stop) {
              continue;
            }
            this.x = 0;
            this.y = 0;
            stop = false;
            break;
          }
     
          for (int i = this.si.rows - 1; i > 1; i--)
          {
            if ((this.si.board[i][this.posY1].imgName != "e1.jpg") && (this.si.board[i][this.posY1].imgName != "e2.jpg"))
              continue;
            this.botrow = (i - this.posX1 + 1);
            break;
          }
     
          move(this.posY1, this.posY2, this.posX1, this.posX2);
        }
      }
    }


    bullets.java
    public class bullets extends Thread
    {
      private SpaceInvaders si;
      public int shipPos = 0;
     
      public bullets(SpaceInvaders si) {
        this.si = si;
      }
     
      public void run()
      {
        for (int x = this.si.rows - 1; x >= 0; x--)
        {
          if ((this.si.board[(x - 1)][this.shipPos].imgName == "e1.jpg") || (this.si.board[(x - 1)][this.shipPos].imgName == "e2.jpg") || (x == 1))
          {
            this.si.numbullets += 1;
            this.si.bulletcount.setText(" " + this.si.numbullets);
            if ((this.si.board[(x - 1)][this.shipPos].imgName == "e1.jpg") || (this.si.board[(x - 1)][this.shipPos].imgName == "e2.jpg"))
            {
              this.si.play(this.si.getCodeBase(), "ekill.wav");
            }
            if (this.si.board[x][this.shipPos].imgName == "Ship.jpg")
            {
              this.si.board[(x - 1)][this.shipPos].imgName = "";
              this.si.board[(x - 1)][this.shipPos].repaint(); break;
            }
     
            this.si.board[x][this.shipPos].imgName = "";
            this.si.board[x][this.shipPos].repaint();
            this.si.board[(x - 1)][this.shipPos].imgName = "";
            this.si.board[(x - 1)][this.shipPos].repaint();
     
            break;
          }
     
          if (this.si.board[x][this.shipPos].imgName == "Ship.jpg")
          {
            this.si.board[(x - 1)][this.shipPos].imgName = "bullet.jpg";
            this.si.board[(x - 1)][this.shipPos].repaint();
          }
          else
          {
            this.si.board[x][this.shipPos].imgName = "";
            this.si.board[x][this.shipPos].repaint();
            this.si.board[(x - 1)][this.shipPos].imgName = "bullet.jpg";
            this.si.board[(x - 1)][this.shipPos].repaint();
          }
          try
          {
            Thread.sleep(200L);
          }
          catch (InterruptedException localInterruptedException)
          {
          }
        }
      }
    }
    Last edited by hirsty; April 10th, 2011 at 09:30 AM. Reason: Highlighting code


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    Sorry for the bump but could really do with a. Possible solution ASAP

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    BTW if you don't understand any of it please do feel free to ask

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    I'm not fully sure what your question is, but you mentioned signing the applet. If you are receiving a SecurityException (which I assume you are) then you need to do so. The following link provides step by step instructions which worked for me first time through the first time I tried
    OTN Discussion Forums : How to sign an applet (and get it to ...

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    Quote Originally Posted by copeg View Post
    I'm not fully sure what your question is, but you mentioned signing the applet. If you are receiving a SecurityException (which I assume you are) then you need to do so. The following link provides step by step instructions which worked for me first time through the first time I tried
    OTN Discussion Forums : How to sign an applet (and get it to ...

    It is being hosted on a website so I don't think that's applicable tested the odbc database and worked flawlessly with my locally hosted iis project just need some way of getting it to connect to the server odbc :/

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    Still looking on a solutions :/ Prefer not to go DSN-less as ASP pages connected to ODBC too + protection reasons

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    It is being hosted on a website
    Did you try signing the applet? Applets don't run on the server, they run on the client, and there are security issues involved. When you run the applet, are there exceptions?

  8. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Created simple game applet but wanting it to access a DSN on the server

    Woops sorry bad memory :/ will try that now

    EDIT: verified certificate, uploaded and not working.....apparently i need to contact the webmaster to grant the certificate? >.>
    Last edited by hirsty; April 21st, 2011 at 04:35 AM.

Similar Threads

  1. simple ftp server and ftp client
    By simontkk2005 in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2011, 10:29 AM
  2. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM
  3. Need help with java applet game.
    By vlan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 04:18 AM
  4. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM
  5. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM