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

    Post Restart/Reset Button

    Hi guys,

    I am a beginner Java programmer and I have made a very simple applet with the wonderful help of Moderator edit: link removed and I was wondering if you guys knew of anyway of making a button that restarted the game without having to exit and reload the game manually. Here is my main class:

    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.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;
    
    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;
    
    	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, "http://www.java-forums.org/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);
    			}
    
    			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);
    		if (gameOver) {
    			g.setColor(Color.WHITE);
    			g.drawString("HAHA YOU LOST! What a plum.", 180, 300);
    			
    		}
    	}
    
    	@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
    
    	}
    
    }

    Any suggestions or code alterations would be so gratefully received also any other comments on the code would be nice as well.

    Thanks

    PlumApps
    Last edited by PlumApps; 05-03-2012 at 09:58 PM. Reason: Adding [code]

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Restart/Reset Button

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Restart/Reset Button

    Soz.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Restart/Reset Button

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Clear/Reset Button Problem
    By Ryan10 in forum New To Java
    Replies: 10
    Last Post: 04-12-2011, 03:04 PM
  2. Replies: 5
    Last Post: 09-26-2010, 03:44 PM
  3. Level Restart Button
    By Curtiz in forum AWT / Swing
    Replies: 4
    Last Post: 04-08-2010, 09:04 PM
  4. Reset form button and duplicated data
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 04:40 AM
  5. <html:reset> button in struts
    By Satheeshhere in forum Web Frameworks
    Replies: 1
    Last Post: 07-15-2008, 03:14 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
  •