Results 1 to 5 of 5
- 01-16-2011, 12:17 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
"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); } }
-
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).
- 01-16-2011, 12:33 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
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
-
- 01-16-2011, 12:53 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks