Hey,
i have made an applet game which uses multiple classes. when i run it via eclipse with applet viewer it works perfectly, but when i try to open it using a html page, it shows the graphics, but the animation or the mouse listener doesnt work.
What have i done wrong, am i forgetting anything? have i done my HTML wrong? please edit my code and help me
- Scott
HTML =
HTML Code:<html> <title>Applet</title> <head></head> <body> <applet code="Game.class" archive="Game.jar" width=500 height=500> </applet> </body> </html>
GAME.CLASS
package scottgame; import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; import java.net.URL; import java.util.concurrent.*; public class Game extends JApplet{ public static final int WIDTH = 500; public static final int HEIGHT = 500; private PaintSurface canvas; private Ball ballclass; static AudioClip bounce; URL url; @Override public void init(){ this.setSize(WIDTH, HEIGHT); canvas = new PaintSurface(); this.add(canvas, BorderLayout.CENTER); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3); executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS); try{ url = getDocumentBase(); } catch (Exception e) { // TODO: handle exception } bounce = getAudioClip(url, "music/boink1.au"); } }
PaintSurface.class
package scottgame; import java.applet.AudioClip; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.geom.Rectangle2D; import javax.swing.JComponent; public class PaintSurface extends JComponent{ int paddle_x = 0; int paddle_y = 360; int score = 0; float english = 1.0f; Ball ball; Color[] color = {Color.RED, Color.ORANGE, Color.MAGENTA, Color.ORANGE, Color.CYAN, Color.BLUE}; int colorIndex; public PaintSurface(){ addMouseMotionListener(new MouseMotionAdapter(){ public void mouseMoved(MouseEvent e){ if (e.getX() - 30 - paddle_x > 5) english = 1.5f; else if (e.getX() - 30 - paddle_x > 5) english = -1.5f; else english = 1.0f; paddle_x = e.getX() - 30; } }); ball = new Ball(20); } private Object getCodeBase() { return null; } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; Graphics2D g3 = (Graphics2D)g; Graphics2D g4 = (Graphics2D)g; Graphics2D g5 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Shape paddle = new Rectangle2D.Float(paddle_x, paddle_y, 60, 8); g2.setColor(color[colorIndex % 6]); if(ball.intersects(paddle_x, paddle_y, 60, 8) && ball.y_speed > 0){ Game.bounce.play(); ball.y_speed = -ball.y_speed; ball.x_speed = (int)(ball.x_speed * english); if(english != 1.0f) colorIndex++; score += 10; } if (ball.getY() + ball.getHeight() >= Game.HEIGHT){ ball = new Ball(20); score -= 10; colorIndex = 0; } ball.move(); g2.fill(ball); g2.setColor(Color.BLACK); g2.fill(paddle); g2.drawString("Score: " + score, 250, 20); g3.setColor(Color.GRAY); g3.fillRect(0, 400, 500, 100); if(score == -10){ //loosing screen ball = new Ball(0); g4.setColor(Color.BLACK); g4.fillRect(1, 1, 500, 500); g5.setColor(Color.YELLOW); g5.drawString( "GAME OVER PUNK!", 200, 200); g5.drawString( "score: "+ score , 225, 250); } } }
Ball.class
package scottgame; import java.applet.AudioClip; import java.awt.geom.Ellipse2D; import javax.print.DocFlavor.URL; public class Ball extends Ellipse2D.Float{ public int x_speed, y_speed; private int d; private int width = Game.WIDTH; private int height = Game.HEIGHT; public Ball(int diameter){ super((int)(Math.random() * (Game.WIDTH - 20) + 1), 0, diameter, diameter); this.d = diameter; this.x_speed = (int)(Math.random() * 5 + 2); this.y_speed = (int)(Math.random() * 5 + 2); } public void move(){ if (super.x < 0 || super.x > width - d) x_speed = -x_speed; if (super.y < 0 || super.y > height - d) y_speed = -y_speed; super.x += x_speed; super.y += y_speed; } public static AudioClip getAudioClip(URL url, String string) { return null; } }
AnimationThread.class
package scottgame; import java.awt.event.MouseEvent; import javax.swing.JApplet; public class AnimationThread implements Runnable { JApplet c; public AnimationThread(JApplet c){ this.c = c; } public void run(){ c.repaint(); } }