Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By brynpttrsn

Thread: When applet restarts speed rockets.

  1. #1
    PlumApps is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default When applet restarts speed rockets.

    Hey,

    I have made a applet game where you bounce a ball on some platforms moving across the screen, and I have recently made a restart feature when you hover over the words Play Again? but when this happens my FPS rocket up (possibly double) and the game becomes unplayable, any suggestions would be brilliant. Here is my main class which is where I think the problem is.

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.net.URL;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    
    public class Start extends Applet implements Runnable, KeyListener,
    		MouseMotionListener, MouseListener {
    
    	private static final long serialVersionUID = 1L;
    	private Image i;
    	private Graphics doubleG;
    	private Ball b;
    	private Platform p[] = new Platform[7];
    	Item item[] = new Item[3];
    	private int score;
    	double matrixX = 0;
    	double matrixDx = 3;
    	URL url;
    	Image matrix;
    	int levelcheck = 0;
    	boolean gameOver = false;
    	boolean mouseIn = false;
    	JButton button;
    
    	public int getScore() {
    		return score;
    	}
    
    	public void setScore(int score) {
    		this.score = score;
    	}
    
    	@Override
    	public void init() {
    		setSize(800, 600);
    		addKeyListener(this);
    		addMouseMotionListener(this);
    		try {
    			url = getDocumentBase();
    
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    
    		matrix = getImage(url, "images/Matrix-Encoding.png");
    		new Pictures(this);
    
    	}
    
    	@Override
    	public void start() {
    		b = new Ball();
    		score = 0;
    		for (int i = 0; i < p.length; i++) {
    			Random r = new Random();
    			p[i] = new Platform(i * 120, 300);
    
    		}
    
    		for (int i = 0; i < item.length; i++) {
    			Random r = new Random();
    			switch (r.nextInt(5)) {
    			case 0:
    				item[i] = new GravUp(getWidth() + 2000 * i);
    				break;
    			case 1:
    				item[i] = new GravDown(getWidth() + 2000 * i);
    				break;
    			case 2:
    				item[i] = new AgilUp(getWidth() + 2000 * i);
    				break;
    			case 3:
    				item[i] = new AgilDown(getWidth() + 2000 * i);
    				break;
    			case 4:
    				item[i] = new Score(getWidth() + 2000 * i, this);
    			}
    
    		}
    
    		Thread thread = new Thread(this);
    		thread.start();
    
    	}
    
    	@Override
    	public void run() {
    		while (true) {
    
    			gameOver = b.getGameOver();
    
    			if (levelcheck > 3000) {
    				Pictures.level++;
    				levelcheck = 0;
    			}
    			levelcheck++;
    
    			if (matrixX < getHeight() * 1) {
    				matrixX += matrixDx;
    			} else {
    				matrixX = 0;
    			}
    			if (!gameOver) {
    				score++;
    			}
    
    			Random r = new Random();
    
    			for (int i = 0; i < item.length; i++) {
    				if (item[i].isCreateNew()) {
    					item[i] = null;
    					switch (r.nextInt(5)) {
    					case 0:
    						item[i] = new GravUp(getWidth() + 10 * r.nextInt(500));
    						break;
    					case 1:
    						item[i] = new GravDown(getWidth() + 10 * r.nextInt(500));
    						break;
    					case 2:
    						item[i] = new AgilUp(getWidth() + 10 * r.nextInt(500));
    						break;
    					case 3:
    						item[i] = new AgilDown(getWidth() + 10 * r.nextInt(500));
    						break;
    					case 4:
    						item[i] = new Score(getWidth() + 10 * r.nextInt(500),
    								this);
    					}
    					item[i].setCreateNew(false);
    				}
    			}
    
    			b.update(this);
    
    			for (int i = 0; i < p.length; i++) {
    				p[i].update(this, b);
    			}
    
    			for (int i = 0; i < item.length; i++) {
    				item[i].update(this, b);
    			}
    //the fps
    			repaint();
    			try {
    				Thread.sleep(15);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		
    		
    		}
    	}
    
    	@Override
    	public void stop() {
    
    	}
    
    	@Override
    	public void destroy() {
    
    	}
    
    	@Override
    	public void update(Graphics g) {
    		if (i == null) {
    			i = createImage(this.getSize().width, this.getSize().height);
    			doubleG = i.getGraphics();
    		}
    
    		doubleG.setColor(getBackground());
    		doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
    
    		doubleG.setColor(getForeground());
    		paint(doubleG);
    
    		g.drawImage(i, 0, 0, this);
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		g.setColor(new Color(15, 77, 147));
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.drawImage(matrix, 0, (int) matrixX, this);
    		g.drawImage(matrix, 0, (int) matrixX - getHeight() + 1, this);
    		b.paint(g);
    		for (int i = 0; i < p.length; i++) {
    			p[i].paint(g);
    		}
    
    		b.paint(g);
    		for (int i = 0; i < item.length; i++) {
    			item[i].paint(g);
    		}
    		String s = Integer.toString(score);
    		Font font = new Font("Serif", Font.BOLD, 32);
    		g.setFont(font);
    		g.setColor(Color.BLACK);
    		g.drawString(s, getWidth() - 150 + 2, 50 + 2);
    		g.setColor(new Color(250, 100, 100));
    		g.drawString(s, getWidth() - 150, 50);
    //the restart button
    		if (gameOver) {
    			g.setColor(Color.WHITE);
    			g.drawString("HAHA YOU LOST! What a plum.", 180, 300);
    			if (mouseIn) {
    				g.setColor(Color.RED);
    				g.drawString("Play again?", 280, 340);
    				start();
    			} else {
    				g.setColor(Color.WHITE);
    				g.drawString("Play again?", 280, 340);
    			}
    		}
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		switch (e.getKeyCode()) {
    		case KeyEvent.VK_LEFT:
    			b.moveLeft();
    			break;
    		case KeyEvent.VK_RIGHT:
    			b.moveRight();
    			break;
    		case KeyEvent.VK_A:
    			b.moveLeft();
    			break;
    		case KeyEvent.VK_D:
    			b.moveRight();
    			break;
    		}
    	}
    
    	public void actionPerformed(ActionEvent e) {
    
    		start();
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		int key = e.VK_R;
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    
    	}
    
    	@Override
    	public void mouseDragged(MouseEvent e) {
    
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    
    		if (e.getX() > 280 && e.getX() < 460) {
    			if (e.getY() > 310 && e.getY() < 370) {
    				mouseIn = true;
    			}
    		}
    		if (e.getX() < 280 || e.getX() > 460) {
    			mouseIn = false;
    		}
    		if (e.getY() < 310 || e.getY() > 370) {
    			mouseIn = false;
    		}
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent arg0) {
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    }

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: When applet restarts speed rockets.

    At an extremely quick glance it appears that it might be this:

    Java Code:
                if (mouseIn) {
                    g.setColor(Color.RED);
                    g.drawString("Play again?", 280, 340);
                    start();
    You're calling start every time the mouse position changes inside your restart button.
    (try mouseClicked()?)

    Edit:
    Also, this looks fishy to me.
    Java Code:
    Thread thread = new Thread(this);
            thread.start();
    I don't know if this is what you want to do.. BUT if it is... disregard my blathering.
    Last edited by brynpttrsn; 05-04-2012 at 09:26 AM.
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: When applet restarts speed rockets.

    Quote Originally Posted by brynpttrsn View Post
    .... I don't know if this is what you want to do.. BUT if it is... disregard my blathering.
    I think that your "blathering" contains much wisdom. Thanks for sharing it with us!

    @plumApps: you shouldn't have any program logic within your paint method, as brynpttrsn states above.

  4. #4
    PlumApps is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: When applet restarts speed rockets.

    Thanks all for your advice it has been very useful:-)

Similar Threads

  1. Speed up program?
    By Zigster in forum New To Java
    Replies: 5
    Last Post: 04-25-2012, 02:51 PM
  2. Need help to speed up algorithm
    By XAW in forum New To Java
    Replies: 5
    Last Post: 04-07-2012, 02:01 AM
  3. Java Applet speed?
    By Masherbrum in forum New To Java
    Replies: 0
    Last Post: 04-06-2012, 03:11 AM
  4. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 05:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •