Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Code not working

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Code not working

    Hi. I have made a code that should allowed me to move an object in a frame, using the arrow keys. But I don't know why, is not working. So I hope that you know what I did wrong.
    package joc;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    /**
     *
     * @author Paul
     */
    public class obiect extends JPanel implements ActionListener, KeyListener
    {
        Timer tm = new Timer(5,this);
        int x = 0, y = 0, velX = 0, velY = 0;
     
        public obiect()
        {
            tm.start();
            addKeyListener(this);
            setFocusable(true); // enable keylistener
            setFocusTraversalKeysEnabled(false);
        }
        @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.CYAN);
        g.fillRect(x,y,50,30);
        tm.start();
        /*
        ImageIcon i = new ImageIcon("car.png");
        i.paintIcon(this, g, 100, 100); 
          */
    }
     
        @Override
        public void actionPerformed(ActionEvent e)
    {
        if(x<0){
            velX = 0;
            x = 0;
        }
        if(x>500) {
            velX = 0;
            x = 530;
        }
        if(y<0){
            velY=0;
            y=0;
        }
        if(y>500) {
            velY=0;
            y=500;
        } 
    x = x + velX; // dreapta sau stanga
    y = y + velY; // sus sau jos
    repaint();
    }
    @Override   
    public void keyPressed(KeyEvent e) 
    {
        int c = e.getKeyCode();
     
        if(c == KeyEvent.VK_LEFT)
            {
                velX= -1; 
                velY = 0;
            }
        if(c == KeyEvent.VK_UP)
            {
                velX=0;
                velY=-1; 
            }
        if(c == KeyEvent.VK_RIGHT)
            {
                velX=1;
                velY=0;
            }
        if(c == KeyEvent.VK_DOWN)
            {
                velX=0;
                velY=1;
            }
    }
    @Override
    public void keyTyped(KeyEvent e) {}
    @Override
    public void keyReleased(KeyEvent e) 
    {
        velX=0;
        velY=0;
    }
     
    }

    The main program is:
    package joc;
    import javax.swing.JFrame;
    /**
     *
     * @author paul
     */
    public class Joc {
     
        public static void main(String[] args) {
            obiect o = new obiect();
            JFrame frame = new JFrame("Proiect Diacu Paul");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
            frame.setSize(500,500);
            frame.setVisible(true);
            frame.add(o);
        }
     
    }
    Last edited by DPaul1994; May 8th, 2014 at 11:29 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Code not working

    Does your KeyListener get called? Have to added some debug statements to test this? If yes, the problem may lie in the logic. If no, the problem may lie in the component not having focus (in which case, I would recommend using KeyBindings)

  3. The Following User Says Thank You to copeg For This Useful Post:

    DPaul1994 (May 7th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    I have to add @Override but is not working. I will try with Bindings then..
    It is working without adding @Override but the rect is not showing up..

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code not working

    I don't see where you set it visible. Is that hidden from us?

  6. #5
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    Aaaa no, this is the main code.
    package joc;
    import javax.swing.JFrame;
    /**
     *
     * @author paul
     */
    public class Joc {
     
        public static void main(String[] args) {
            obiect o = new obiect();
            JFrame frame = new JFrame("Proiect Diacu Paul");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
            frame.setSize(500,500);
            frame.setVisible(true);
            frame.add(o);
        }
     
    }

    I tried with o.setbounds(0,0,200,200) but it worked only the first time I run the program.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code not working

    The code shows the current contents of the JFrame with: setVisible(true)
    and then adds another component to the JFrame hoping that the JFrame will redo the display to show the newly added component.
    Move the call to setVisible
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    I don't think that I see what you mean..You mean like this?

     JFrame frame = new JFrame("Proiect Diacu Paul");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
            frame.setSize(500,500);
            frame.setVisible(true);
            obiect o = new obiect();
            frame.add(o);
            o.setVisible(true);

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code not working

    No, I was talking about frame.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    It also doesn't work ..

    --- Update ---

    But frame is set visible on true

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code not working

    Try doing some debugging. Add some println statements to the methods that are being called that print out some values. I find the value from getBounds() is useful to see what is happening.
    Some methods to add it in: paintComponent() and actionPerformed().

    The output will show you which methods are being called and what the size of the component is and where it is located.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    Ok I'll try to do that..Thanks Please do not close this topic cause maybe I'll be back.

  13. #12
    Junior Member
    Join Date
    May 2014
    Location
    Romania
    Posts
    9
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working

    I didn't succeed...

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code not working

    Can you post the updated version of the code and the print out from the debug println statements so we can see what the print out is telling you about the code when it executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why is my Code not working?
    By Rhiannon1488 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 20th, 2014, 01:51 PM
  2. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  3. help i cant get my code working
    By alexrox27 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 23rd, 2013, 11:27 AM
  4. Code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 23rd, 2013, 02:12 PM
  5. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM