Results 1 to 6 of 6
  1. #1
    JayRee is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Keep getting error compiling

    Hi there guys, doing a project for uni and am having some problems compiling and I can't see what im doing wrong:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.sound.sampled.*;
    import java.util.Random;
    
    public class AlienForm implements MouseListener
    {
        public static void main(String [] args)
        {
            AlienForm a = new AlienForm();
        }
        
        private JFrame background;
        private Container container;
        private JButton button;
        private ImagePanel bg;
        
        public static boolean escaped;
        public static boolean paused;
        public static boolean started;
        public static boolean secondGame;
        
        public boolean shootGun;
        
        public final int YPOS;
        public final int XPOS;
        
        
        public static int score;
        public static int aliensKilled;
        public static int aliensMissed;
        public static int shotsFired;
        public static int highScore;
        public static Random randomGen= new Random();
        
        public static int alienY;
        private int moveIncrement;
        
        
        private ArrayList<MovingImage> alien;
        private MovingImage crosshair;
        
        
        
        private GameSoundAlien GameMusic;
          
        public AlienForm()
        {
            GameMusic = new GameSoundAlien("sounds//background.wav");   // Sets the background wav file 
            YPOS = 200;
            XPOS = 700;
            secondGame = false;
            score = 0;
            aliensKilled = 0;
            aliensMissed = 0;
            shotsFired = 0;
            
            load(new File("Highscore.txt"));
            GameMusic.loopClip();
            
            initiate();
        }    
        
        public void load(File file)
        {
            try
            {
                Scanner reader = new Scanner(file);
                while(reader.hasNext())
                {
                    int value = reader.nextInt();
                    if(value > score)
                        score = value;
                }
            }
            catch(IOException i )
            {
                System.out.println("Error: "+i);
            }
        }    
        
        public void save()
        {
            FileWriter out;
            try
            {
                out = new FileWriter("Best.txt");
                out.write("" + highScore);
                out.close();
            }
            catch(IOException i)
            {
                    System.out.println("Error: "+i.getMessage());
            }        
        }
        
    
        public void initiate()
        {
            if(!secondGame)   // Loads GUI and Background if first launch
            {
                background = new JFrame("Alien Shooter");
                background.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Top right "x" closes program
                background.setResizable(false); // Users can't resize window
                background.setSize(new Dimension(700,700));
                background.setVisible(true);
               
                bg = new ImagePanel("http://www.java-forums.org/images//background.png");
                background.add(bg);
                
                bg.addMouseListener(this);
            }
            secondGame = true;
            paused = false;
            escaped = false;
            started = false;
            
    
            moveIncrement = 2; 
            
            alien = new ArrayList<MovingImage>();
            crosshair = new MovingImage("http://www.java-forums.org/images//sight.png",200,200);
           
            alien.add(new MovingImage("http://www.java-forums.org/images//alien.gif", 1392, 500));
            alien.add(new MovingImage("http://www.java-forums.org/images//alien.gif", 1972, 500));
            
            drawImages();
        }
        
        public void drawImages()
        {
            long lastAlien = System.currentTimeMillis();
            while(true)
            {
                if(!paused && !escaped && started && (double)System.currentTimeMillis() - 10 > lastAlien)
                {
                    lastAlien = System.currentTimeMillis();
                    updateAlien();
                    updateRoad();
                }
                bg.updateImages(alien);
            }
        }
        
        public void updateRoad() 
        {
          if(alien.get(0).getX() > -1 * 63)
          {
            alien.set(0,new MovingImage("http://www.java-forums.org/images//alien.gif",alien.get(0).getX() - (63/5), alien.get(0).getY())); 
            alien.set(1,new MovingImage("http://www.java-forums.org/images//alien.gif",alien.get(1).getX() - (63/5), alien.get(1).getY()));
          }
          else
          {
              alien.set(0,new MovingImage("http://www.java-forums.org/images//alien.gif",alien.get(1).getX() - (63/5), alien.get(1).getY()));
              alien.set(1,new MovingImage("http://www.java-forums.org/images//alien.gif",alien.get(0).getX() + 700, 500));
          }
        }
        
              
            
        public void escaped()
        {
            GameMusic = new GameSoundAlien("Sounds//gameover.wav");
            escaped = true;
            if(score > highScore)
            {
                highScore = score;
                save();
            }
            
            GameMusic.playClip();
            initiate();
        }
        
        public void updateAlien()
        {
            if(started = true)
            {
                alien.setPosition(400, YPOS - 0.5);<<<<<<<<<<<<<<<<<<<<<<<Here
                alien.setImage("http://www.java-forums.org/images//alien.gif");
            } 
            else{
                alien.setPosition(400, YPOS = 700);<<<<<<<<<<<<<<<<<<Assuming this will be the same aswell...
                alien.setIMage("http://www.java-forums.org/images//alien.gif");
            }
        }
                
            
            
        public void mouseExited(MouseEvent e)
        {
    
            if(started)
            {
                paused = true;
            }
    
        }
        
            public void mouseEntered(MouseEvent e)
        {
    
        }
        
            public void mouseReleased(MouseEvent e)
        {
            shootGun = false;
            if(paused)
                paused = false;
        }
        
            public void mousePressed(MouseEvent e)
        {
            GameMusic = new GameSoundLobster("sounds//gunshot.wav");
            if (!started)
                started = true;
            shootGun = true;
            GameMusic.playClip();
    
        }
        
        public void mouseClicked(MouseEvent e)
        {
    
        }
    }
        
    class ImagePanel extends JPanel {
            
            private Image background;
            private ArrayList<MovingImage> alien;
            public ImagePanel(String img)
            {
                this(new ImageIcon(img).getImage());
            }
            
            public ImagePanel(Image img)
            {
                background = img;
                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                
                alien = new ArrayList<MovingImage>();
            }
            
            public void paintComponent(Graphics g)
            {
                g.drawImage(background, 0, 0, null);
                
                for(MovingImage img : alien)
                    g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
                drawStrings(g);
                
            }
            
            public void drawStrings(Graphics g)
            {
                g.setFont(new Font("Arial", Font.BOLD,20));
                g.drawString("Score: " + AlienForm.score, 30, 500);
                g.setFont(new Font("Arial", Font.BOLD,20));
                if(AlienForm.score > AlienForm.highScore)
    
                    g.drawString("Highscore: " + AlienForm.score,650,500);
                else
                    g.drawString("Highscore: " + AlienForm.highScore,650,500);
                    
                if(AlienForm.paused)
                {
                    g.setColor(Color.WHITE);
                    g.setFont(new Font("Arial", Font.BOLD,20));
                    g.drawString("PAUSED",350,350);
                    g.setFont(new Font("Arial", Font.BOLD,20));;
                    g.drawString("CLICK TO UNPAUSE",350,450);
                }
                
    
           }  
           
           public void updateImages(ArrayList<MovingImage> newAlien)
           {
               alien = newAlien;
               repaint();
            }
    }           
            
    class MovingImage
    {
        private Image image;        //The picture
        private double x;           //X position
        private double y;           //Y position
    
        //Construct a new Moving Image with image, x position, and y position given
        public MovingImage(Image img, double xPos, double yPos)
        {
            image = img;
            x = xPos;
            y = yPos;
        }
    
        //Construct a new Moving Image with image (from file path), x position, and y position given
        public MovingImage(String path, double xPos, double yPos)
        {
            this(new ImageIcon(path).getImage(), xPos, yPos);   
            //easiest way to make an image from a file path in Swing
        }
    
        //They are set methods.  I don't feel like commenting them.
        public void setPosition(double xPos, double yPos)
        {
            x = xPos;
            y = yPos;
        }
    
        public void setImage(String path)
        {
            image = new ImageIcon(path).getImage();
        }
    
        public void setY(double newY)
        {
            y = newY;
        }
    
        public void setX(double newX)
        {
            x = newX;
        }
    
        //Get methods which I'm also not commenting
        public double getX()
        {
            return x;
        }
    
        public double getY()
        {
            return y;
        }
    
        public Image getImage()
        {
            return image;
        }
    }

    Thats my code, the error I'm getting is:

    Cannot find Symbol - Method setPosition (I've highlighted in code where its saying it's wrong)

    Any helps appreciated - Cheers
    Last edited by JayRee; 03-24-2013 at 11:17 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Keep getting error compiling

    I don't believe ArrayList has a setPosition method. Did you mean set()?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Keep getting error compiling

    Did you mean to get an element contained in the ArrayList and call one of its methods?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JayRee is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Keep getting error compiling

    Hi there guyys, thanks for the replies.

    The method its meant to be finding should be the one created lower down

    Java Code:
    public void setPosition(double xPos, double yPos)
        {
            x = xPos;
            y = yPos;
    I'm new to java, and currently looking over a friends script and turning it into my own. Alot still needs to be added.

    Jay,.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Keep getting error compiling

    To call a method in a class, you need to get a reference to an instance of the class. If the ArrayList contains objects that have the method you are trying to call, you need to get an instance of the object from the ArrayList.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JayRee is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Keep getting error compiling

    Quote Originally Posted by Norm View Post
    To call a method in a class, you need to get a reference to an instance of the class. If the ArrayList contains objects that have the method you are trying to call, you need to get an instance of the object from the ArrayList.
    Ahh right okay I'll have a play about now see what I can do :)

Similar Threads

  1. Error while Compiling
    By Sunshine68 in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 04:41 AM
  2. Error Compiling Need Help
    By RebellionX in forum New To Java
    Replies: 4
    Last Post: 05-27-2012, 06:26 PM
  3. Compiling error
    By robbierut in forum Java Applets
    Replies: 4
    Last Post: 11-24-2010, 05:00 PM
  4. Error during compiling
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:42 AM
  5. Error while compiling
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2007, 11:37 AM

Posting Permissions

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