Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Why is this class getting errors?

    This is a really weird error, because I am getting errors on declaring a double.
    The errors are on line 26, 34, 84
    It says error on ";" { expected after this token

    Here is the class
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class BallPanel extends JPanel {
        public int hamx;
        public int hamy;
        public int peterx;
    	public int petery;
        Random rand = new Random();
    	Random rand2 = new Random();
    	protected int ballX = 250;
    	public int count;
    	protected int ballY = 250;
    	private int ballDiameter = 50;
    	private BufferedImage image1;
    	private BufferedImage image2;
    	private double ballXSpeed = 5;
    	private double ballYSpeed = 10;
    	hamx = rand.nextInt(500);
    	hamy = rand2.nextInt(500);
    	public int life = 3;
    	Random r;
    	Random r2;
    	
    	
    	public void resetIt(){
    		if(peterx <= ballX + 45 && petery <= ballY + 45 && peterx >= ballX - 45 && petery >= ballY - 45)
    		{
    			life--;		
    			r = new Random();
    			r2 = new Random();
    			peterx=r.nextInt(500);
    			petery=r2.nextInt(500);
    			repaint();
    		     }
    		}
    	
            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"));
    		} 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.drawImage(image1, peterx, petery, null);
    		g.drawImage(image2,hamx, hamy, null);
    		g.setColor(Color.BLUE);
    		g.fillOval(ballX, ballY, ballDiameter, ballDiameter);
    		g.setColor(Color.white);
    		g.setColor(Color.white);
    		g.drawString("lives:"+Integer.toString(life),400,50);
    		repaint();
    
    	}
    
    }

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Why is this class getting errors?

    Check your hamx and hamy variables. You cannot initialize variables separately from the declaration, unless your do it inside of a method or function.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Why is this class getting errors?

    Looks like you're missing the closing brace of your resetIt() method.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Compile .java to .class, with all errors kept
    By KamikadZee in forum New To Java
    Replies: 10
    Last Post: 10-24-2011, 03:32 PM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 08:05 AM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Posting Permissions

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