/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bouncingball;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author project
*/
public class BouncingBall extends JPanel{
static final int BOX_WIDTH=300;
static final int BOX_HEIGHT=400;
Ball ball=new Ball(this);
Bat bat=new Bat(this);
BouncingBall(){
this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
bat.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
bat.keyReleased(e);
}
});setFocusable(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
g2d.setColor(Color.white);
g2d.drawString("Developed By Aravind Sankaran", BOX_WIDTH/5, BOX_HEIGHT/getHeight()+15);
ball.paint(g2d);
bat.paint(g2d);
}
void move(){
ball.move();
bat.move();
}
public static void main(String[] args) {
// TODO code application logic here
BouncingBall bBall=new BouncingBall();
JFrame frame=new JFrame("Ball Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(BOX_WIDTH, BOX_HEIGHT);
frame.add(bBall);
frame.setResizable(false);
frame.setVisible(true);
while(true){
bBall.move();
bBall.repaint();
try {
Thread.sleep(8);
} catch (InterruptedException ex) {
Logger.getLogger(BouncingBall.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class Ball{
int x=0;
int y=0;
int xa=1;
int ya=1;
private BouncingBall bBall;
public Ball(BouncingBall bBall){
this.bBall=bBall;
}
void move(){
if(x+xa<0){
xa=1;
}
if(x+xa>bBall.getWidth()-30){
xa=-1;
}
if(y+ya<0){
ya=1;
}
if(y+ya>bBall.getHeight()-40){
ya=-1;
}
x=x+xa;
y=y+ya;
}
public void paint(Graphics g){
//super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.red);
g2d.fillOval(x, y, 30, 30);
}
}
class Bat{
int x=0;
int xa=0;
private BouncingBall bBall;
public Bat(BouncingBall bBall){
this.bBall=bBall;
}
void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white);
g2d.fillRect(x, bBall.getHeight()-12, 70, 20);
}
void move(){
if(x+xa>0 && x+xa<bBall.getWidth()-60){
x=x+xa;
}
}
void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT){
xa=-1;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
xa=1;
}
}
public void keyReleased(KeyEvent e) {
xa=0;
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bouncingball;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author project
*/
public class BouncingBall extends JPanel{
static final int BOX_WIDTH=300;
static final int BOX_HEIGHT=400;
Ball ball=new Ball(this);
Bat bat=new Bat(this);
BouncingBall(){
this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
bat.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
bat.keyReleased(e);
}
});setFocusable(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
g2d.setColor(Color.white);
g2d.drawString("Developed By Aravind Sankaran", BOX_WIDTH/5, BOX_HEIGHT/getHeight()+15);
ball.paint(g2d);
bat.paint(g2d);
}
void move(){
ball.move();
bat.move();
}
public static void main(String[] args) {
// TODO code application logic here
BouncingBall bBall=new BouncingBall();
JFrame frame=new JFrame("Ball Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(BOX_WIDTH, BOX_HEIGHT);
frame.add(bBall);
frame.setResizable(false);
frame.setVisible(true);
while(true){
bBall.move();
bBall.repaint();
try {
Thread.sleep(8);
} catch (InterruptedException ex) {
Logger.getLogger(BouncingBall.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class Ball{
int x=0;
int y=0;
int xa=1;
int ya=1;
private BouncingBall bBall;
public Ball(BouncingBall bBall){
this.bBall=bBall;
}
void move(){
if(x+xa<0){
xa=1;
}
if(x+xa>bBall.getWidth()-30){
xa=-1;
}
if(y+ya<0){
ya=1;
}
if(y+ya>bBall.getHeight()-40){
ya=-1;
}
x=x+xa;
y=y+ya;
}
public void paint(Graphics g){
//super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.red);
g2d.fillOval(x, y, 30, 30);
}
}
class Bat{
int x=0;
int xa=0;
private BouncingBall bBall;
public Bat(BouncingBall bBall){
this.bBall=bBall;
}
void paint(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white);
g2d.fillRect(x, bBall.getHeight()-12, 70, 20);
}
void move(){
if(x+xa>0 && x+xa<bBall.getWidth()-60){
x=x+xa;
}
}
void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT){
xa=-1;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
xa=1;
}
}
public void keyReleased(KeyEvent e) {
xa=0;
}
}