1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pongnew;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author User
 */
public class Racket implements KeyListener{
    private int x,y,width,height;
    private int formHeight;
    private int defaultX, defaultY;
    boolean auto;
    Graphics g;
    Color pColor;

    public Racket(int x, int y, int width, int height, int formHeight, boolean auto, Graphics g, Color panelColor) {
        this.x = x;
        this.y = y;
        defaultX = x;
        defaultY = y;
        this.width = width;
        this.height = height;
        this.formHeight = formHeight;
        this.auto = auto;
        this.g = g;
        pColor = panelColor;
    }

    public boolean isAuto() {
        return auto;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

public void moveUp(int speed){
        if(y-speed>=0){
        this.y -= speed;
        drawMe();
        }
    }
    public void moveDown(int speed){
        if(y+speed <= formHeight){
        this.y += speed;
        drawMe();
        }
    }
    public void drawMe(){
        clear();
        g.setColor(Color.green);
        g.fillRect(x, y, width, height);
    }
    public void drawMeDefault(){

    }

    public void clear(){
        g.setColor(pColor);
        g.fillRect(x, y, width,height);
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        if(!isAuto()){
            if(e.getKeyCode() == KeyEvent.VK_UP)
                moveUp(5);
            else if(e.getKeyCode() == KeyEvent.VK_DOWN)
                moveDown(5);
        }
    }

    public void keyReleased(KeyEvent e) {
        
    }
}