Java Reference
In-Depth Information
public class BreakoutPanel extends JPanel
{
private javax . swing .Timer timer ;
private Ball ball ;
private Paddle paddle ;
public BreakoutPanel () {
ball = new Ball (Color .RED, this );
paddle = new Paddle(Color .BLUE, this );
timer = new javax . swing .Timer(10 , new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
ball .move() ;
repaint () ;
}
}
);
timer. start() ;
addKeyListener( new KeyAdapter ()
{
public void keyPressed (KeyEvent e ) {
if ( e . getKeyCode ()==KeyEvent .VK LEFT) {
paddle .moveLeft() ;
if ( e . getKeyCode ()==KeyEvent .VK RIGHT)
{
paddle .moveRight() ;
repaint () ;
}
} );
addMouseMotionListener( new MouseMotionAdapter ()
{
boolean firstTime = true ;
int oldX ;
public void mouseMoved(MouseEvent e )
{
if (firstTime) {
oldX = e . getX() ;
firstTime = false ;
paddle .move(e .getX()
oldX) ;
oldX = e . getX() ;
repaint () ;
}
}
);
setFocusable( true );
}
public void paintComponent(Graphics g)
{
super . paintComponent(g) ;
Graphics2D g2 = (Graphics2D) g ;
ball .draw(g2);
paddle .draw(g2) ;
}
}
 
Search WWH ::




Custom Search