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) { } } } }