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 5 of 5

Thread: Help with game applet

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with game applet

    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();
    	}
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Help with game applet

    The mouse not working sounds like the component needs focus. Is the component with the mouse listener capable of getting the focus?
    Have you tried clicking on the component to give it focus?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with game applet

    i have tried clicking, it doesnt do anything, and i don't think it is capable of getting the focus on the applet, how do i do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Help with game applet

    Look in the API doc for the JComponent class. There is a link in the first part: How to Use the Focus Subsystem.
    Also look at the Component class. There is a method there that will allow a component to get the focus.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with game applet

    thanks for the suggestion, it turns out, it was google chrome.

Similar Threads

  1. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  2. Replies: 0
    Last Post: October 13th, 2011, 08:42 PM
  3. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 04:11 AM
  4. Need help with java applet game.
    By vlan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 05:18 AM
  5. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM

Tags for this Thread