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

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1

    Problem with oval graphic moving with keyboard.


    I'm learning Java graphics and how to move graphics on screen. My problem is that I can't get the red oval to move up, down, left or left 100 pixels when I press the up, down, left, right buttons on they keyboard. I can't figure out what I'm missing. I wrote the code for the key listener by following a tutorial on YouTube.

    The YouTube video I was looking at.

    Code:
    package javaapplication38;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class panel extends JPanel{
        
        public int redX, redY;
    
        public panel()
        {
            addKeyListener(new AL());
    
            setBackground(Color.BLACK);
            setPreferredSize(new Dimension(500,500)); 
            
            redX = 100;
            redY = 200;
        }
        
            public class AL extends KeyAdapter
        {
            public void KeyPressed(KeyEvent e)
            {
                int keyCode = e.getKeyCode();
                
                if(keyCode == e.VK_LEFT)
                    {
                        redX-= 100;
                    }
                if(keyCode == e.VK_RIGHT)
                    {
                        redX+= 100;
                    }
                if(keyCode == e.VK_UP)
                    {
                        redY-= 100;
                    }
                if(keyCode == e.VK_DOWN)
                    {
                        redY+= 100;
                    }
            }
        }
    
        public void paint(Graphics g)
        { 
            super.paint(g);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 0, 100, 100);
            g.drawRect(100, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 0, 100, 100);
            g.drawRect(300, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 0, 100, 100);
            
            g.drawRect(0, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 100, 100, 100);
            g.drawRect(200, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 100, 100, 100);
            g.drawRect(400, 100, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 200, 100, 100);
            g.drawRect(100, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 200, 100, 100);
            g.drawRect(300, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 200, 100, 100);
    
            g.drawRect(0, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 300, 100, 100);
            g.drawRect(200, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 300, 100, 100);
            g.drawRect(400, 300, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 400, 100, 100);
            g.drawRect(100, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 400, 100, 100);
            g.drawRect(300, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 400, 100, 100);
            
            g.setColor(Color.red);
            g.fillOval(redX, redY, 100, 100);
            
            g.setColor(Color.blue);
            g.fillOval(300, 200, 100, 100);
            
            repaint();
        }    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Have you tried debugging it by adding some println statements to see what methods are executing and what the values of the variables are as the code executes?
    The printout will help you see what the code is doing.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    I didn't before, but now I did, I didn't get any print out on the actionListener part.

    Below is my printout and code

    Code:
    run:
    Panel works.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    Graphics work.
    BUILD SUCCESSFUL (total time: 2 seconds)

    Code:
    package javaapplication38;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class panel extends JPanel{
        
        public int redX, redY;
    
        public panel()
        {
            addKeyListener(new AL());
    
            setBackground(Color.BLACK);
            setPreferredSize(new Dimension(500,500)); 
            
            redX = 100;
            redY = 200;
            
            System.out.println("Panel works.");
        }
        
            public class AL extends KeyAdapter
        {
                         
            public void KeyPressed(KeyEvent e)
            {
                System.out.println("Action Listener works.");
                
                int keyCode = e.getKeyCode();
                
                if(keyCode == e.VK_LEFT)
                    {
                        redX-= 100;
                    }
                if(keyCode == e.VK_RIGHT)
                    {
                        redX+= 100;
                    }
                if(keyCode == e.VK_UP)
                    {
                        redY-= 100;
                    }
                if(keyCode == e.VK_DOWN)
                    {
                        redY+= 100;
                    }
            }
        }
    
        public void paintComponent(Graphics g)
        { 
            super.paintComponent(g);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 0, 100, 100);
            g.drawRect(100, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 0, 100, 100);
            g.drawRect(300, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 0, 100, 100);
            
            g.drawRect(0, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 100, 100, 100);
            g.drawRect(200, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 100, 100, 100);
            g.drawRect(400, 100, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 200, 100, 100);
            g.drawRect(100, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 200, 100, 100);
            g.drawRect(300, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 200, 100, 100);
    
            g.drawRect(0, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 300, 100, 100);
            g.drawRect(200, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 300, 100, 100);
            g.drawRect(400, 300, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 400, 100, 100);
            g.drawRect(100, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 400, 100, 100);
            g.drawRect(300, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 400, 100, 100);
            
            g.setColor(Color.red);
            g.fillOval(redX, redY, 100, 100);
            
            g.setColor(Color.blue);
            g.fillOval(300, 200, 100, 100);
            
            repaint();
            
            System.out.println("Graphics work.");
        }    
    }
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    You println just tell that it is painting.
    what about the values of redX and redY? are those updated/changed? as expected?
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can your Panel class receive the focus? There are methods that will enable a component to receive focus.
    If the panel can receive focus, does it have the focus? There is a method to set the focus.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by NormR
    Can your Panel class receive the focus? There are methods that will enable a component to receive focus.
    If the panel can receive focus, does it have the focus? There is a method to set the focus.
    It's working now, I added setFocusable(true);

    Code:
    package javaapplication38;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    public class panel extends JPanel{
        
        public int redX, redY, blueX, blueY;
    
        public panel()
        {
            addKeyListener(new AL());
    
            setBackground(Color.BLACK);
            setPreferredSize(new Dimension(500,500)); 
            
            redX = 100;
            redY = 200;
            
            blueX = 300;
            blueY = 200;
            
            setFocusable(true);
            
            System.out.println("Panel works.");
        }
        
            public class AL extends KeyAdapter
        {                     
            public void keyPressed(KeyEvent e)
            {
                System.out.println("Action Listener works.");
                
                int keyCode = e.getKeyCode();
                
                if(keyCode == e.VK_LEFT)
                    {
                        redX-= 100;
                        System.out.println("Left key listening");
                    }
                if(keyCode == e.VK_RIGHT)
                    {
                        redX+= 100;
                    }
                if(keyCode == e.VK_UP)
                    {
                        redY-= 100;
                    }
                if(keyCode == e.VK_DOWN)
                    {
                        redY+= 100;
                    }
            }
        }
         
        public void paintComponent(Graphics g)
        { 
            super.paintComponent(g);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 0, 100, 100);
            g.drawRect(100, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 0, 100, 100);
            g.drawRect(300, 0, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 0, 100, 100);
            
            g.drawRect(0, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 100, 100, 100);
            g.drawRect(200, 100, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 100, 100, 100);
            g.drawRect(400, 100, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 200, 100, 100);
            g.drawRect(100, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 200, 100, 100);
            g.drawRect(300, 200, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 200, 100, 100);
    
            g.drawRect(0, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(100, 300, 100, 100);
            g.drawRect(200, 300, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(300, 300, 100, 100);
            g.drawRect(400, 300, 100, 100);
            g.setColor(Color.yellow);
            
            g.setColor(Color.yellow);
            g.fillRect(0, 400, 100, 100);
            g.drawRect(100, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(200, 400, 100, 100);
            g.drawRect(300, 400, 100, 100);
            g.setColor(Color.yellow);
            g.fillRect(400, 400, 100, 100);
            
            g.setColor(Color.red);
            g.fillOval(redX, redY, 100, 100);
            
            g.setColor(Color.blue);
            g.fillOval(blueX, blueY, 100, 100);
            
            Font xyFont = new Font("Helvetica", Font.BOLD, 12);
            
            g.setColor(Color.red);
            g.setFont(xyFont);
            g.drawString("redX: " + redX, 10, 50);
            g.drawString("redY: " + redY, 10, 70);
            
            g.setColor(Color.blue);
            g.drawString("blueX: " + blueX, 410, 50);
            g.drawString("blueY: " + blueY, 410, 70);
            
            repaint();
            
            System.out.print("redX is: " + redX);
            System.out.println(" , redY is: " + redY);
    
        }    
    }

IMN logo majestic logo threadwatch logo seochat tools logo