#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3

    Schoolproject with errors.


    Hey everybody,
    I'm not great at Java and I am making a game. I don't understand everything yet so please don't use very difficult terms. My problem is that I'm getting errors and I cannot figure out what to do. What i need is obviously help getting rid of the errors so ill be able to run my applet again. Any help is appreciated.

    So i'll post my code beneath this. And it will probably be 'ugly' code. But again, I am not an expert. Sorry its so long im just not sure which parts are relevant.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.image.*;
    import java.awt.Font.*;
    import java.awt.Graphics.*;
    import java.awt.Color.*;
    
    public class FCApplet extends Applet
    implements ActionListener, Runnable
    {
       // variables
      private Thread th;
      private Player player;
      private Shot [] shots;
    
        // movement
      int y = 200;
      private final int shotSpeed = 5;
      private final int playerLeftSpeed = -2;
      private final int playerRightSpeed = 2;
      private final int playerUpSpeed = -2;
      private final int playerDownSpeed = 2;
    
       // move player
      private boolean playerMoveLeft;
      private boolean playerMoveRight;
      private boolean playerMoveUp;
      private boolean playerMoveDown;
    
       // Images
      private Image dbImage;
      private Graphics dbg;
      private Image background;
      private Image mist;
      private Image mist1;
      private Image mist2;
    
       // Buttons
      Button play;
      CheckboxGroup mapkeuze;
      CheckboxGroup moeilijkheidsgraad;
    
      public void init()
      {
        background = null;
        Toolkit toolkit = Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int width = dim.width;
        int height = dim.height;
        play = new Button("Play");
          play.setFont(new Font("Arial", Font.ITALIC, 20));
    
        CheckboxGroup moeilijkheidsgraad = new CheckboxGroup();
          //setLayout(new GridLayout(3, 1));
          //moeilijkheidsgraad.setLocation(100,100,100,100);
          add(new Checkbox("Makkelijk", moeilijkheidsgraad, false));
          add(new Checkbox("Gemiddeld", moeilijkheidsgraad, false));
          add(new Checkbox("Moeilijk", moeilijkheidsgraad, false));
    
        CheckboxGroup mapkeuze = new CheckboxGroup();
          //mapkeuze.setLocation(100,100,100,100);
          add(new Checkbox("Box", moeilijkheidsgraad, false));
          add(new Checkbox("Not box", moeilijkheidsgraad, false));
    
        player = new Player(200, y);
        shots = new Shot[100];
        add(play);
        play.addActionListener(this);
      }
       
      public void loadImages()
      {
        try
        {
          background = getImage(getDocumentBase(), "background.jpg");
          mist = getImage(getDocumentBase(), "mist.jpg");
          mist1 = getImage(getDocumentBase(), "mist1.jpg");
          mist2 = getImage(getDocumentBase(), "mist2.jpg");
        }
        catch(Exception e) { }
      }
       
      public void paint(Graphics g)
      {
        Dimension a = getSize();
        int breedteapplet = a.width;
        int hoogteapplet = a.height;
        if (background == null)
        loadImages();
        g.drawImage(background, 0, 0, breedteapplet - 1, hoogteapplet - 1, this);
      }
       
      public void actionPerformed(ActionEvent evt)
      {
        new CsFrame("Spelvenster");
      }
    
      class CsFrame extends Frame
      {
        private Image mist;
    
        public CsFrame(String s)
        { super(s);
          addWindowListener(new WindowAdapter()
          {  public void windowClosing(WindowEvent e)
             {  dispose();
             }
          });
          setLayout(new BorderLayout());
          MijnCanvas cv = new MijnCanvas();
          add("Center", cv);
          Dimension sd = getToolkit().getScreenSize();
          setSize(sd.width, sd.height);
          setLocation(0, 0);
          show();
        }
      }
    
      class MijnCanvas extends Canvas
      {
        public void start ()
        {
          th = new Thread(this);
          th.start ();
        }
    
        public void stop()
        {
          th.stop();
        }
    
        public void destroy()
        {
          th.stop();
        }
    
        public void run()
        {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
          while (true)
          {
            // do operations on shots in shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] != null)
              {
                // move shot
                shots[i].moveShot(shotSpeed);
    
                // test if shot is out
                if(shots[i].getYPos() < 0)
                {
                  // remove shot from array
                  shots[i] = null;
                }
              }
            }
    
            // move player
            if(playerMoveLeft)
            {
              player.moveX(playerLeftSpeed);
            }
            else if(playerMoveRight)
            {
              player.moveX(playerRightSpeed);
            }
            else if(playerMoveUp)
            {
              y = y + playerUpSpeed;
            }
            else if(playerMoveDown)
            {
              y = y + playerDownSpeed;
            }
    
            repaint();
    
             try
            {
              Thread.sleep(10);
            }
            catch (InterruptedException ex)
            {
              // do nothing
            }
    
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
          }
        }
    
        public boolean keyDown(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = true;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = true;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = true;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = true;
          }
          else if(key == 32)
          {
            // generate new shot and add it to shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] == null)
              {
                shots[i] = player.generateShot();
                break;
              }
            }
          }
    
          return true;
        }
    
        public boolean keyUp(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = false;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = false;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = false;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = false;
          }
    
          return true;
        }
    
        public void update (Graphics g)
        {
          if (dbImage == null)
          {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics ();
          }
    
          dbg.setColor (getBackground ());
          dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
          dbg.setColor (getForeground());
          paint (dbg);
    
          g.drawImage (dbImage, 0, 0, this);
        }
    
        public void paint(Graphics g)
        {
          Dimension d = getSize();
          loadImages();
          g.drawImage(mist, 0, 0, d.width, d.height, this);
    
           // draw player
          player.drawPlayer(g);
    
           // draw shots
          for(int i=0; i<shots.length; i++)
          {
            if(shots[i] != null)
            {
              shots[i].drawShot(g);
            }
          }
        }
      }
    }
    ERRORS:

    Compileer H:\PWS\maybe2.0\FCApplet.java met Java-Compiler
    FCApplet.java:9:8: FCApplet is not abstract and does not override abstract method run() in java.lang.Runnable
    public class FCApplet extends Applet
    ^
    FCApplet.java:116:7: warning: [deprecation] show() in java.awt.Window has been deprecated
    show();
    ^
    FCApplet.java:124:12: cannot find symbol
    symbol : constructor Thread(FCApplet.MijnCanvas)
    location: class java.lang.Thread
    th = new Thread(this);
    ^
    FCApplet.java:130:9: warning: [deprecation] stop() in java.lang.Thread has been deprecated
    th.stop();
    ^
    FCApplet.java:135:9: warning: [deprecation] stop() in java.lang.Thread has been deprecated
    th.stop();
    ^
    FCApplet.java:194:20: warning: [deprecation] keyDown(java.awt.Event,int) in java.awt.Component has been deprecated
    public boolean keyDown(Event e, int key)
    ^
    FCApplet.java:228:20: warning: [deprecation] keyUp(java.awt.Event,int) in java.awt.Component has been deprecated
    public boolean keyUp(Event e, int key)
    ^
    2 errors
    5 warnings
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    When you post code, please use the code tags.
    You can find information by reading the sticky notes.

    Originally Posted by jressing
    Compileer H:\PWS\maybe2.0\FCApplet.java met Java-Compiler
    FCApplet.java:9:8: FCApplet is not abstract and does not override abstract method run() in java.lang.Runnable
    public class FCApplet extends Applet
    ^
    When you implement the Runnable class, you have to implement/create your own run() method.

    Originally Posted by jressing
    FCApplet.java:116:7: warning: [deprecation] show() in java.awt.Window has been deprecated
    show();
    ^
    Deprecated means this function has been replaced with another newer function
    If you look at the API manaual, you can see which function you should use:
    http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html

    Originally Posted by jressing
    FCApplet.java:124:12: cannot find symbol
    symbol : constructor Thread(FCApplet.MijnCanvas)
    location: class java.lang.Thread
    th = new Thread(this);
    ^
    FCApplet.MijnCanvas only extends Canvas.
    You have to make sure you parameter is of correct type.
    Thread is accepting object/class of type Runnable.

    Originally Posted by jressing
    FCApplet.java:130:9: warning: [deprecation] stop() in java.lang.Thread has been deprecated
    th.stop();
    ^
    Again, look in the API manual to see why it is deprecated:
    http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html

    Originally Posted by jressing
    FCApplet.java:135:9: warning: [deprecation] stop() in java.lang.Thread has been deprecated
    th.stop();
    ^
    Same as above.

    Originally Posted by jressing
    FCApplet.java:194:20: warning: [deprecation] keyDown(java.awt.Event,int) in java.awt.Component has been deprecated
    public boolean keyDown(Event e, int key)
    ^
    Same as above, look in the Manual:
    http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html

    Originally Posted by jressing
    FCApplet.java:228:20: warning: [deprecation] keyUp(java.awt.Event,int) in java.awt.Component has been deprecated
    public boolean keyUp(Event e, int key)
    ^
    Same as above.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    @MrFujin
    Thanks a lot for replying!
    It really helped me, turns out I only had to move 'implements Runnable' to the canvas.

    So now it runs again, but does this mean the warnings are also fixed? Or are they just not being shown?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    Also i have tried to fix the part of the code where i check if the shot is still on the screen. Because i want to be able to shoot in all directions. So i made it like this:

    Code:
    // test if shot is out
    if(shots[i].getYPos() < 0 || shots[i].getYPos() > d.height || shots[i].getXPos() < 0 || shots[i].getXPos() > d.width)
    But i get an error yet again :/

    FCApplet.java:173:109: cannot find symbol
    symbol : method getXPos()
    location: class Shot
    if(shots[i].getYPos() < 0 || shots[i].getYPos() > d.height || shots[i].getXPos() < 0 || shots[i].getXPos() > d.width)

    I get the error twice as the symbol is used twice in the line, but showing them both seemed a bit useless to me. But why is there an .getYPos symbol but not an .getXPos one? And what could I use to replace .getXPos?

    thx
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    Where did you get the Shot class from?
    If it is one of your own class, then please post the code for it.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    Here is the full code after being fixed from the earlier errors.
    The part where i check if the shot is out is in 'public void run()'
    which is on the canvas.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.image.*;
    import java.awt.Font.*;
    import java.awt.Graphics.*;
    import java.awt.Color.*;
    import javax.swing.*;
    
    public class FCApplet extends Applet
    implements ActionListener
    {
       // variables
      private Thread th;
      private Player player;
      private Shot [] shots;
    
        // movement
      int y = 200;
      private final int shotSpeed = 5;
      private final int playerLeftSpeed = -2;
      private final int playerRightSpeed = 2;
      private final int playerUpSpeed = -2;
      private final int playerDownSpeed = 2;
    
       // move player
      private boolean playerMoveLeft;
      private boolean playerMoveRight;
      private boolean playerMoveUp;
      private boolean playerMoveDown;
    
       // Images
      private Image dbImage;
      private Graphics dbg;
      private Image background;
      private Image mist;
      private Image mist1;
      private Image mist2;
    
       // Buttons
      Button play;
      private JRadioButton makkelijk;
      private JRadioButton gemiddeld;
      private JRadioButton moeilijk;
      private JRadioButton map1;
      private JRadioButton map2;
      private ButtonGroup group1;
      private ButtonGroup group2;
      
      //CheckboxGroup mapkeuze;
      //CheckboxGroup moeilijkheidsgraad;
    
    
    
      public void init()
      {
        background = null;
        Toolkit toolkit = Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int width = dim.width;
        int height = dim.height;
        play = new Button("Play");
          play.setFont(new Font("Arial", Font.ITALIC, 20));
    
        makkelijk = new JRadioButton("makkelijk", false);
        gemiddeld = new JRadioButton("gemiddeld", false);
        moeilijk = new JRadioButton("moeilijk", false);
        map1 = new JRadioButton("map1", false);
        map2 = new JRadioButton("map2", false);
        add(makkelijk);
        add(gemiddeld);
        add(moeilijk);
        add(map1);
        add(map2);
        
        group1 = new ButtonGroup();
        group1.add(makkelijk);
        group1.add(gemiddeld);
        group1.add(moeilijk);
        group2 = new ButtonGroup();
        group2.add(map1);
        group2.add(map2);
        
        player = new Player(200, y);
        shots = new Shot[100];
        add(play);
        play.addActionListener(this);
      }
       
      public void loadImages()
      {
        try
        {
          background = getImage(getDocumentBase(), "background.jpg");
          mist = getImage(getDocumentBase(), "mist.jpg");
          mist1 = getImage(getDocumentBase(), "mist1.jpg");
          mist2 = getImage(getDocumentBase(), "mist2.jpg");
        }
        catch(Exception e) { }
      }
       
      public void paint(Graphics g)
      {
        Dimension a = getSize();
        int breedteapplet = a.width;
        int hoogteapplet = a.height;
        if (background == null)
        loadImages();
        g.drawImage(background, 0, 0, breedteapplet - 1, hoogteapplet - 1, this);
      }
       
      public void actionPerformed(ActionEvent evt)
      {
        new CsFrame("Spelvenster");
      }
    
      class CsFrame extends Frame
      {
        private Image mist;
    
        public CsFrame(String s)
        { super(s);
          addWindowListener(new WindowAdapter()
          {  public void windowClosing(WindowEvent e)
             {  dispose();
             }
          });
          setLayout(new BorderLayout());
          MijnCanvas cv = new MijnCanvas();
          add("Center", cv);
          Dimension sd = getToolkit().getScreenSize();
          setSize(sd.width, sd.height);
          setLocation(0, 0);
          show();
        }
      }
    
      class MijnCanvas extends Canvas
      implements Runnable
      {
        public void start ()
        {
          th = new Thread(this);
          th.start ();
        }
    
        public void stop()
        {
          th.stop();
        }
    
        public void destroy()
        {
          th.stop();
        }
    
        public void run()
        {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
          while (true)
          {
            // do operations on shots in shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] != null)
              {
                Dimension d = getSize();
                // move shot
                shots[i].moveShot(shotSpeed);
    
                // test if shot is out
                if(shots[i].getYPos() < 0 || shots[i].getYPos() > d.height)// || shots[i].getXPos() < 0 || shots[i].getXPos() > d.width)
                {
                  // remove shot from array
                  shots[i] = null;
                }
              }
            }
    
            // move player
            if(playerMoveLeft)
            {
              player.moveX(playerLeftSpeed);
            }
            else if(playerMoveRight)
            {
              player.moveX(playerRightSpeed);
            }
            else if(playerMoveUp)
            {
              y = y + playerUpSpeed;
            }
            else if(playerMoveDown)
            {
              y = y + playerDownSpeed;
            }
    
            repaint();
    
             try
            {
              Thread.sleep(10);
            }
            catch (InterruptedException ex)
            {
              // do nothing
            }
    
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
          }
        }
    
        public boolean keyDown(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = true;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = true;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = true;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = true;
          }
          else if(key == 32)
          {
            // generate new shot and add it to shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] == null)
              {
                shots[i] = player.generateShot();
                break;
              }
            }
          }
    
          return true;
        }
    
        public boolean keyUp(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = false;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = false;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = false;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = false;
          }
    
          return true;
        }
    
        public void update (Graphics g)
        {
          if (dbImage == null)
          {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics ();
          }
    
          dbg.setColor (getForeground());
          paint (dbg);
    
          g.drawImage (dbImage, 0, 0, this);
        }
    
        public void paint(Graphics g)
        {
          Dimension d = getSize();
          loadImages();
          g.drawImage(mist, 0, 0, d.width, d.height, this);
    
           // draw player
          player.drawPlayer(g);
    
           // draw shots
          for(int i=0; i<shots.length; i++)
          {
            if(shots[i] != null)
            {
              shots[i].drawShot(g);
            }
          }
        }
      }
    }
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    You error message i referring to:
    location: class Shot
    Where do you have this class?
    did you create the class yourself?
    Do you have any documentation/information of what functions are included?

    Comments on this post

    • jressing agrees : Very helpful post.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    Ah now I see, I didnt create the class myself so I forgot about it a little.
    So easy mistakes, just need someone to show what Im doing wrong :P

    Thx!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    Thanks for all the help so far. You have tought me a lot. I just have two more issues.

    Main
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.image.*;
    import java.awt.Font.*;
    import java.awt.Graphics.*;
    import java.awt.Color.*;
    import javax.swing.*;
    
    public class FCApplet extends Applet
    implements ActionListener
    {
       // variables
      private Thread th;
      private Player player;
      private Shot [] shots;
    
        // movement
      private final int shotSpeed = 5;
      private final int playerLeftSpeed = -2;
      private final int playerRightSpeed = 2;
      private final int playerUpSpeed = -2;
      private final int playerDownSpeed = 2;
    
       // move player
      private boolean playerMoveLeft;
      private boolean playerMoveRight;
      private boolean playerMoveUp;
      private boolean playerMoveDown;
    
       // Images
      private Image dbImage;
      private Graphics dbg;
      private Image background;
      private Image mist;
      private Image mist1;
      private Image mist2;
    
       // Buttons
      Button play;
      private JRadioButton Makkelijk;
      private JRadioButton Gemiddeld;
      private JRadioButton Moeilijk;
      private JRadioButton Box;
      private JRadioButton Maze;
      private ButtonGroup group1;
      private ButtonGroup group2;
      
      //CheckboxGroup mapkeuze;
      //CheckboxGroup moeilijkheidsgraad;
    
    
      public void init()
      {
        background = null;
        Toolkit toolkit = Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int width = dim.width;
        int height = dim.height;
        play = new Button("Play");
          play.setFont(new Font("Arial", Font.ITALIC, 20));
    
        Makkelijk = new JRadioButton("Makkelijk", false);
        Gemiddeld = new JRadioButton("Gemiddeld", false);
        Moeilijk = new JRadioButton("Moeilijk", false);
        Box = new JRadioButton("Box", false);
        Maze = new JRadioButton("Maze", false);
        add(Makkelijk);
        add(Gemiddeld);
        add(Moeilijk);
        add(Box);
        add(Maze);
        
        group1 = new ButtonGroup();
        group1.add(Makkelijk);
        group1.add(Gemiddeld);
        group1.add(Moeilijk);
        group2 = new ButtonGroup();
        group2.add(Box);
        group2.add(Maze);
        
        player = new Player(200, 200);
        shots = new Shot[100];
        add(play);
        play.addActionListener(this);
      }
    
      public void loadImages()
      {
        try
        {
          background = getImage(getDocumentBase(), "background.jpg");
          mist = getImage(getDocumentBase(), "mist.jpg");
          mist1 = getImage(getDocumentBase(), "mist1.jpg");
          mist2 = getImage(getDocumentBase(), "mist2.jpg");
        }
        catch(Exception e) { }
      }
    
      public void paint(Graphics g)
      {
        Dimension a = getSize();
        int breedteapplet = a.width;
        int hoogteapplet = a.height;
        if (background == null)
        loadImages();
        g.drawImage(background, 0, 0, breedteapplet - 1, hoogteapplet - 1, this);
      }
       
      public void actionPerformed(ActionEvent evt)
      {
        new CsFrame("Spelvenster");
      }
    
      class CsFrame extends Frame
      {
        private Image mist;
    
        public CsFrame(String s)
        { super(s);
          addWindowListener(new WindowAdapter()
          {  public void windowClosing(WindowEvent e)
             {  dispose();
             }
          });
          setLayout(new BorderLayout());
          MijnCanvas cv = new MijnCanvas();
          add("Center", cv);
          Dimension sd = getToolkit().getScreenSize();
          setSize(sd.width, sd.height);
          setLocation(0, 0);
          show();
        }
      }
    
      class MijnCanvas extends Canvas
      implements Runnable
      {
        public void start ()
        {
          th = new Thread(this);
          th.start ();
        }
    
        public void stop()
        {
          th.stop();
        }
    
        public void destroy()
        {
          th.stop();
        }
    
        public void run()
        {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
          while (true)
          {
            // do operations on shots in shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] != null)
              {
                Dimension d = getSize();
                // move shot
                shots[i].moveShot(shotSpeed);
    
                // test if shot is out
                if(shots[i].getYPos() < 0 || shots[i].getYPos() > d.height || shots[i].getXPos() < 0 || shots[i].getXPos() > d.width)
                {
                  // remove shot from array
                  shots[i] = null;
                }
              }
            }
    
            // move player
            if(playerMoveLeft)
            {
              player.moveX(playerLeftSpeed);
            }
            else if(playerMoveRight)
            {
              player.moveX(playerRightSpeed);
            }
            else if(playerMoveUp)
            {
              player.moveY(playerUpSpeed);
            }
            else if(playerMoveDown)
            {
              player.moveY(playerDownSpeed);
            }
    
            repaint();
    
             try
            {
              Thread.sleep(10);
            }
            catch (InterruptedException ex)
            {
              // do nothing
            }
    
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
          }
        }
    
        public boolean keyDown(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = true;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = true;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = true;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = true;
          }
          else if(key == 32)
          {
            // generate new shot and add it to shots array
            for(int i=0; i<shots.length; i++)
            {
              if(shots[i] == null)
              {
                shots[i] = player.generateShot();
                break;
              }
            }
          }
    
          return true;
        }
    
        public boolean keyUp(Event e, int key)
        {
          if(key == Event.LEFT)
          {
            playerMoveLeft = false;
          }
          else if(key == Event.RIGHT)
          {
            playerMoveRight = false;
          }
          else if(key == Event.UP)
          {
            playerMoveUp = false;
          }
          else if(key == Event.DOWN)
          {
            playerMoveDown = false;
          }
    
          return true;
        }
    
        public void update (Graphics g)
        {
          if (dbImage == null)
          {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics ();
          }
    
          dbg.setColor (getForeground());
          paint (dbg);
    
          g.drawImage (dbImage, 0, 0, this);
        }
    
        public void paint(Graphics g)
        {
          Dimension d = getSize();
          loadImages();
          g.drawImage(mist, 0, 0, d.width, d.height, this);
    
           // draw player
          player.drawPlayer(g);
    
           // draw shots
          for(int i=0; i<shots.length; i++)
          {
            if(shots[i] != null)
            {
              shots[i].drawShot(g);
            }
          }
        }
      }
    }
       //moveY gefixed
       //getXPos gefixed
    Player
    Code:
    import java.awt.Graphics;
    import java.awt.Color;
    
    public class Player
    {
      // Variables
      private int x_pos;
      private int y_pos;
    
      public Player(int x, int y)
      {
        x_pos = x;
        y_pos = y;
      }
    
      public void moveX(int speed)
      {
        x_pos += speed;
      }
      
      public void moveY(int speed)
      {
        y_pos += speed;
      }
    
      public Shot generateShot()
      {
        Shot shot = new Shot(x_pos, y_pos);
    
        return shot;
      }
    
      public void drawPlayer(Graphics g)
      {
        g.setColor(Color.red);
        int [] x_poly = {x_pos, x_pos - 10, x_pos, x_pos + 10};
        int [] y_poly = {y_pos, y_pos + 15, y_pos + 10, y_pos + 15};
        g.fillPolygon(x_poly, y_poly, 4);
      }
    }
    Shot
    Code:
    import java.awt.Graphics;
    import java.awt.Color;
    
    public class Shot
    {
      private int x_pos;
      private int y_pos;
    
      private final int radius = 3;
    
      public Shot(int x, int y)
      {
        x_pos = x;
        y_pos = y;
      }
    
      public int getYPos()
      {
        return y_pos;
      }
      
      public int getXPos()
      {
        return x_pos;
      }
    
      public void moveShot(int speed)
      {
        y_pos += speed;
      }
    
      public void drawShot(Graphics g)
      {
        g.setColor(Color.yellow);
        g.fillOval(x_pos, y_pos, radius, radius);
      }
    }
    Issue 1:
    When I run the program, it starts a applet.
    On this applet are 5 JRadioButtons, 1 Button and a background image. The JRadioButtons are divided into two JPanels. But i cant see the Buttons until my mouse goes over them. How comes?

    Issue 2:
    The second issue is that it doesn't repaint the canvas. The code is alright i think (as it gives no errors) but it does not repaint. Is the "repaint();" placed incorrect?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    38
    Rep Power
    3
    Issue 3:
    I want to load different backgrounds depending on which mapkeuze (mapchoice) JRadioButton you selected. I made a start myself but get an error.

    Code:
    public void actionPerformed(ActionEvent e)
      {
        if (Box = true)
        {
          new CsFrame("Spelvenster");
          
        } else if (Maze = true)
        {
          new CsFrame("Spelvenster");
        } else {
    
        }
      }
    My plan is to refer to different voids from the different if parts. So every option draws a specific background.
    Code:
    FCApplet.java:112:15: cannot find symbol
    symbol  : method JRadioButton(java.lang.String,boolean)
    location: class FCApplet
        if (Box = JRadioButton("Box", true))
                         ^
    FCApplet.java:112:13: incompatible types
    found   : javax.swing.JRadioButton
    required: boolean
        if (Box = JRadioButton("Box", true))
                     ^\
    Those are errors i get, did i formulate the requirement wrong? And how should i formulate it?

IMN logo majestic logo threadwatch logo seochat tools logo