class Card31 extends JPanel implements KeyListener, MouseListener {
public Card31() {
addKeyListener(this);
addMouseListener(this);
x1 = 700;
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT)
x1 += 10;
else if (key == KeyEvent.VK_LEFT)
x1 -= 10;
repaint();
}
public void mouseEntered(MouseEvent e) {
requestFocus();
}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
court2 = Toolkit.getDefaultToolkit().getImage("court2.jpg");
bhoop = Toolkit.getDefaultToolkit().getImage("bhoop2.png");
balla = Toolkit.getDefaultToolkit().getImage("balla.png");
ballc = Toolkit.getDefaultToolkit().getImage("ballc.png");
balld = Toolkit.getDefaultToolkit().getImage("balld.png");
balle = Toolkit.getDefaultToolkit().getImage("balle.png");
ballf = Toolkit.getDefaultToolkit().getImage("ballf.png");
balli = Toolkit.getDefaultToolkit().getImage("balli.png");
balll = Toolkit.getDefaultToolkit().getImage("balll.png");
balln = Toolkit.getDefaultToolkit().getImage("balln.png");
ballo = Toolkit.getDefaultToolkit().getImage("ballo.png");
ballr = Toolkit.getDefaultToolkit().getImage("ballr.png");
balls = Toolkit.getDefaultToolkit().getImage("balls.png");
ballt = Toolkit.getDefaultToolkit().getImage("ballt.png");
ballu = Toolkit.getDefaultToolkit().getImage("ballu.png");
bally = Toolkit.getDefaultToolkit().getImage("bally.png");
g.drawImage(court2, 0, 0, 1600, 1000, this);
g.drawImage(ballo, 130, 100, 110, 110, this);
g.drawImage(ballc, 280, 100, 110, 110, this);
g.drawImage(balln, 430, 100, 110, 110, this);
g.drawImage(balld, 580, 100, 110, 110, this);
g.drawImage(balle, 730, 100, 110, 110, this);
g.drawImage(ballc, 880, 100, 110, 110, this);
g.drawImage(balle, 1030, 100, 110, 110, this);
g.drawImage(ballu, 1180, 100, 110, 110, this);
g.drawImage(bally, 1330, 100, 110, 110, this);
g.drawRect(30, 30, 80, 20);
Font plainfont = new Font("Serif", Font.BOLD, 20);
g.setFont(plainfont);
g.setColor(Color.BLACK);
g.drawString("Score: " + score1, 30, 30);
g.fillRect(700, 800, 200, 20);
g.drawImage(bhoop, x1, 500, 150, 150, this);
g.drawString(msg1, 200, 200);
System.out.println("x1" + x1);
}
}
Above I attached part of the code, a nested class. X1 is a global variable and is initialized in the constructor. The KeyPressed, which is used for moving the Iamge bhoop, is not working. I have checked all the code to make it right but the Image is not moving no matter what. Please help me solve this. I would really appreciate your help<3.