import java.awt.*; public class Paddle { // instance variables - replace the example below with your own private int x, y, length; public static final int MAX = 600; /** * Constructor for objects of class Paddle */ public Paddle(int x, int y, int length) { this.x = x; this.y = y; this.length = length; } public void moveLeft(int n){ x -= n; if (x <0 ) x=0; } public void moveRight(int n){ x += n; if (x > MAX ) x=MAX; } public void draw(Graphics g){ g.setColor(Color.BLACK); g.fillRect(x, y, length, 20); } } import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; public class PaddleTester extends JPanel implements KeyListener { public static int WIDTH=800; public static int HEIGHT=600; private Font titleFont, regularFont; private int keyCode; private char c; private Paddle paddle; public PaddleTester() { //initialize variables here... titleFont = new Font("Roman", Font.BOLD, 18); regularFont = new Font("Helvetica", Font.PLAIN, 12); keyCode=0; c='-'; paddle = new Paddle(20,300,100); } public static void main(String[] args) { PaddleTester app= new PaddleTester(); JFrame window = new JFrame("Type a Key to See Its Code"); window.setSize(WIDTH, HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(app); window.addKeyListener(app); //window.pack(); window.setVisible(true); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(),getHeight()); g.setColor(Color.BLUE); g.setFont(titleFont); g.drawString("Type a Key to See Its Code", 20, 20); g.setColor(Color.BLACK); g.setFont(regularFont); g.drawString("Version 1.0", 20, 40); g.drawString("the key char "+c+" has code "+keyCode, 20, 60); g.setFont(new Font("Arial", Font.PLAIN, 48)); g.setColor(Color.red); g.drawString("\""+c+"\"",80, 120); paddle.draw(g); } // update is a workaround to cure Windows screen flicker problem public void update(Graphics g){ paint(g); } // These 3 methods need to be declares to implement the KeyListener Interface @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { keyCode=e.getKeyCode(); c=e.getKeyChar(); if (keyCode == 37) paddle.moveLeft(3); if (keyCode == 39) paddle.moveRight(3); repaint(); } @Override public void keyReleased(KeyEvent e) {} }