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

Thread: Problem in Swing Game

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in Swing Game

    The problem I am having is that I have a character peter griffin that runs into a burger, and when he does, he and the burger are supposed to reset to a random position. The game runs, other things move, but the problem is that most of the time he goes and touches the burger and the console opens up and gives errors
    The code for the reset thing is on lines 64-75.
    Here is my code;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Random;
    import java.util.Timer;
     
    import javax.imageio.ImageIO;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
    public class BallPanel extends JPanel {
        public int hamx = 300;
        public int hamy = 300;
        public int peterx;
    	public int petery;
    	public int score = 0;
        Random rand = new Random();
    	Random rand2 = new Random();
    	protected int ballX = 250;
    	public int count = 0;
    	protected int ballY = 250;
    	private int ballDiameter = 50;
    	private BufferedImage image1;
    	private BufferedImage image2;
    	private BufferedImage image3;
    	public BufferedImage image4;
    	private double ballXSpeed = 5;
    	private double ballYSpeed = 10;
    	public int life = 3;
    	public int time = 0;
    	Random r;
    	Random r2;
     
     
     
    	public void ResetGame(){
    		count = 0;
    		hamx = r.nextInt(600);
    		hamy = r2.nextInt(600);
    		peterx = rand.nextInt(600);
    		petery = rand2.nextInt(600);
    		score = 0;
    		life = 3;
    		repaint();
    	}
    	public int getLives() {
    	    return life;
    	}
     
    	public void setLives(int lives) {
    	    this.life = life;
    	}
     
     
     
    	public void resetIt(){
    		if(peterx <= ballX + 45 && petery <= ballY + 45 && peterx >= ballX - 45 && petery >= ballY - 45)
    		{
    			setLives(3);
    			getLives();
    			life--;
    			r = new Random();
    			r2 = new Random();
    			peterx=r.nextInt(600);
    			petery=r2.nextInt(600);
    			repaint();
    		     }
    		if(peterx <= hamx + 45 && petery <= hamy + 45 && peterx >= hamx - 45 && petery >= hamy - 45)
    		{
    			score++;
    			hamx = r.nextInt(600);
    			hamy = r2.nextInt(600);
    			repaint();
    		  }
    		if(life<=0){
    			Object[] possibleValues = { "Close", "Restart"};
    			Object selectedValue = JOptionPane.showInputDialog(null,
    			"Your score was:"+score, "You Lose!",
    			JOptionPane.INFORMATION_MESSAGE, null,
    			possibleValues, possibleValues[0]);
    			if (selectedValue == possibleValues[0]){
    				System.exit(1);
    			}
    			if (selectedValue == possibleValues[1]){
                  ResetGame();
    			}
    		  }
    		}
     
     
            KeyListener kw = new KeyListener() {
     
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
    		@Override
    		public void keyPressed(KeyEvent e) {
    			int k = e.getKeyCode();
    			if (k == KeyEvent.VK_UP) {
    				petery = petery - 10;
    				resetIt();
    				repaint();
    			}
    			if (k == KeyEvent.VK_DOWN) {
    				petery = petery + 10;
    				resetIt();
    				repaint();
    			}
    			if (k == KeyEvent.VK_LEFT) {
    				peterx = peterx - 10;
    				resetIt();
    				repaint();
    			}
    			if (k == KeyEvent.VK_RIGHT) {
    				peterx = peterx + 10;
    				resetIt();
    				repaint();
    			}
    		}
     
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
     
    		}
    	};
     
    	public BallPanel() {
    		setBackground(Color.BLACK);
    		try {
    			image1 = ImageIO.read(new File(
    					"/Users/MW/Downloads/petergriffin2.png"));
    			image2 = ImageIO.read(new File(
    					"/Users/MW/Downloads/cheeseburger2.png"));
    			image3 = ImageIO.read(new File(
    					"/Users/MW/Downloads/SpikeBall.png"));
    			image4 = ImageIO.read(new File(
    					"/Users/MW/Downloads/petericon.png"));
    		} catch (Exception e) {
    			System.out.println("could not load img");
    		}
     
    	}
     
    	// call this method from a Timer to move the ball!
    	public void step() {
     
    		// move the ball on the X axis
    		ballX += ballXSpeed;
    		// if the ball goes off the edge, bounce it by reversing its speed
    		if (ballX < 0 || ballX > getWidth() - ballDiameter) {
    			ballXSpeed *= -1;
    		}
     
    		// move the ball on the Y axis
    		ballY += ballYSpeed;
    		// if the ball goes off the edge, bounce it by reversing its speed
    		if (ballY < 0 || ballY > getHeight() - ballDiameter) {
    			ballYSpeed *= -1;
    		}
     
    		// tell this JPanel to repaint itself since the ball has moved
    		repaint();
    	}
     
    	// paint the ball
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.white);
    		g.drawString("Score:"+score,30,20);
    		g.drawString("lives:"+Integer.toString(life),600,20);
    		g.drawImage(image1, peterx, petery, null);
    		g.drawImage(image2,hamx, hamy, null);
    		g.drawImage(image3,ballX,ballY,null);
    		g.drawString(Integer.toString(time/30),20,40);
    		g.setColor(Color.BLUE);
    		g.setColor(Color.white);
    		repaint();
     
    	}
     
     
     
    }

    If you solve this, I will declare you java master

    PS, if you could link to tutorials on double buffering to stop laggy movement you would be GrandJavaMaster

    Here is a pic of the issue:Screen Shot 2014-01-23 at 10.56.43 PM.jpg


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem in Swing Game

    Never mind guys, I fixed it
    Guess I'm the javamaster

Similar Threads

  1. I am having trouble writing code that solves this problem involving check sums.
    By uncoordinated in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2013, 08:30 PM
  2. Replies: 4
    Last Post: November 14th, 2012, 06:38 PM
  3. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 08:59 AM