Results 1 to 5 of 5
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default "actionloop" is never used locally

    why is the square not moving? it doesn't move when a user pressed an arrow key; as it should. plus im getting a warning saying "actionloop" (my method?) is never used locally.. do i have to call it? i thought would run automaticly.


    Java Code:
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class DrawingStuff extends JApplet {
       public void init() {
          try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   createGUI();
                }
             });
          } catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
          }
       }
    
       private void createGUI() {
          getContentPane().add(new DrawingStuffPanel());
          setSize(600, 600);
       }
    }
    
    @SuppressWarnings("serial")
    class DrawingStuffPanel extends JPanel {
       protected static final int DELTA = 4;
       private int x = 100;
       private int y = 100;
       public DrawingStuffPanel() {
          // if using a key listener, then the component, here the JPanel must have focus
          setFocusable(true);
          requestFocusInWindow();
       }
       
    
       boolean leftKey = false;
       boolean rightKey = false;
       boolean upKey = false;
       boolean downKey = false;
       boolean fireKey = false;
       int speed = 1;
       public void keyPressed(KeyEvent e)
       {
         switch(e.getKeyCode())
         {
            case KeyEvent.VK_LEFT: leftKey = true;
            case KeyEvent.VK_RIGHT: rightKey = true;
            case KeyEvent.VK_UP: upKey = true;
            case KeyEvent.VK_DOWN: downKey = true;
            case KeyEvent.VK_SPACE: fireKey = true;
         }
       }
       public void keyReleased(KeyEvent e)
       {
         switch(e.getKeyCode())
         {
            case KeyEvent.VK_LEFT: leftKey = false;
            case KeyEvent.VK_RIGHT: rightKey = false;
            case KeyEvent.VK_UP: upKey = false;
            case KeyEvent.VK_DOWN: downKey = false;
         }
       }
    
       private void actionLoop()
       {
         if(leftKey && downKey){
        	 x = x - speed;
        	 y = y + speed;
         }
         else if(leftKey && upKey){
        	y = y - speed;
        	x = x - speed;
         }
         else if(leftKey){
        	 x = x - speed;
         }
         
         else if(rightKey && downKey){
        	 x = x + speed;
        	 y = y + speed;
         }
          
         else if(rightKey && upKey){
        	 x = x + speed;
        	 y = y - speed;
         }
          
         else if(rightKey){
        	 x = x + speed;
         }
          
         else if(downKey){
        	 y = y + speed;
         }
           
         else if(upKey){
        	 y = y - speed;
         }
       } 
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.green);
          g2.fill3DRect(x, y, 55, 55, true);
       }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where do you call it? If you don't call it, how will it run? Again, consider using a Swing Timer (as was suggested before).

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default

    i didn't know i had to call it.. i got the idea from the paint method because i never have to call that. i would use a swing timer but thats a little to advanced for me at the moment

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by natdizzle View Post
    i didn't know i had to call it.. i got the idea from the paint method because i never have to call that. i would use a swing timer but thats a little to advanced for me at the moment
    paint and paintComponent are special methods that are called whenever the operating system feels the app needs to be repainted (or a "dirty" region of the screen), or it is usually called if you call repaint() on the component.

  5. #5
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •