title: Changing color with KeyListener
body: <p>I have a project that a circle goes with random x and y values and with selected colors but when the user pressed the space bar the color of the circle must be changed. My circle moves both x and y coordinate and I want to change the color of the circle when I press the space button. But it does not work when I pressed it. It goes with its original color. So how can I make this code right?</p>
  
  <pre><code>public class c {
      private int x,y,r;
      private Color co;
      private int Re,G,B;
      private Random ran;
  
      public c() {
          // TODO Auto-generated constructor stub
          ran= new Random();
          x=100;
          y=50;
          r= ran.nextInt(200)+50;
          Re=ran.nextInt(255);
          G=ran.nextInt(255);
          B=ran.nextInt(255);
          co= new Color(Re,G,B); 
      }
  
      public int getRe() {
          return Re;
      }
  
      public int getG() {
          return G;
      }
  
      public int getB() {
          return B;
      }
  
      public int getX() {
          return x;
      }
  
      public int getY() {
          return y;
      }
  
      public int getR() {
          return r;
      }
      public void setCo(int Re,int G,int B) {
          co= new Color(Re,G,B);
      }
      public Color getCo() {
          return co;
      }
  
      public Random getRan() {
          return ran;
      }
      public void setX(int x) {
          this.x=x;
      }
      public void setY(int y) {
      this.y=y;
      }
  
  }
  
  
  
  public class Circle extends JFrame implements ActionListener,KeyListener{
  
      private Timer timer;
      private int x,y,a=5,b=5;
      private Random rand;
      c circ = new c();
      public Circle() {
          setLayout(new BorderLayout());
          x=circ.getX();
          y=circ.getY();
          timer=new Timer(50,this);
          timer.start();
          addKeyListener(this);
          setSize(550,550);
          setVisible(true);
  
      }
  
      public void paint(Graphics g) {
          super.paint(g);
          g.fillOval(x,y,100,100);
          g.setColor(circ.getCo());
      }
  
  
      public static void main(String[]args) {
          new Circle();
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
  
          moveWithTimer();
          repaint();      
      }
  
      public void moveWithTimer() {
  
          x=x+b;
          y=y+a;
      if(x&lt;0) {
          b=5;
      }
  
      if(x+50&gt;500) {
          b=-5;
      }
  
      if(y&lt;0){
          a=5;
      }
  
      if(y+50&gt;500) {
          a=-5;
      }
  
  
  
      }
  
      @Override
      public void keyPressed(KeyEvent e) {
          // TODO Auto-generated method stub
         if(e.getKeyCode()==e.VK_SPACE) {
             circ.setCo(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
  
  
         }
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void keyTyped(KeyEvent e) {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>