13 July 2014

KeyListener example in java

KeyListener Example


/*
*its the first class
*save is as Car.java
/*
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class Car {

static int CARWIDTH = 40;
static int CARLENGTH = 70;
int x = Game.WIDTH/2 - CARWIDTH/2;
int y = Game.HEIGHT - 105;
int dx = 0;
int dy = 0;
private Game game;

public Car(Game game){
this.game = game;
}

public void paint(Graphics2D g2d){
g2d.setColor(Color.blue);
g2d.fillRect(x, y, CARWIDTH, CARLENGTH);
}

public void move(){

if(x+dx > 0 && x+dx < Game.WIDTH-56) x = x+dx;
if(y+dy > 0 && y+dy < Game.HEIGHT-105) y = y+dy;

}

public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT) dx = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT) dx = 1;
if(e.getKeyCode() == KeyEvent.VK_UP) dy = -1;
if(e.getKeyCode() == KeyEvent.VK_DOWN) dy = 1;
}

public void keyReleased(){
dx = 0;
dy = 0;
}

}
//////////////EnD of First Class////////////////

/*
*its the SECOND class
*save is as Game.java
/*


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel{

static int WIDTH = 500;
static int HEIGHT = 700;
Car car = new Car(this);


public Game(){
addKeyListener(new KeyListener() {

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
car.keyReleased();

}

@Override
public void keyPressed(KeyEvent e) {
car.keyPressed(e);

}
});
setFocusable(true);
}

private void move(){
car.move();
}

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
car.paint(g2d);
}



public static void main(String args[]) throws InterruptedException{
Game game = new Game();
JFrame frame = new JFrame("Car Game");
frame.add(game);
frame.getContentPane().setBackground(Color.white);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


while(true){
game.move();
game.repaint();
Thread.sleep(1);
}
}


}
////////////////////EnD of Second Class////////////////////////////

How to paddle in brick breaker

Simple Paddle in Brick Breaker




Code for simple paddle in java brick breaker game

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import test1.BallBreaker;



public class Bat extends JPanel{


private int x = 140;

private int y = 240;

int width = 30;

int height = 10;
public Bat(){
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
moveIt(evt);
}
});
setFocusable(true);
}

public void paint(Graphics g)
{
/*
*
* As we are extending JPanel,
* we need to paint background with some color first
* Note: if we extend Canvas class than we need not
* paint back ground. we can directly draw peddle
*/
g.setColor(Color.LIGHT_GRAY);//paint background with light gray
g.fillRect(0, 0, 350, 450);//screen dimensions
g.setColor(Color.blue);//change color value from gray to blue
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);//fill blue color to paddle

/*
* you can uncomment below two lines to see styles of peddle
*/
// g.fill3DRect(x, y+40, width, height, false);

// g.fill3DRect(x, y+80, width, height, true);
}
protected void moveIt(KeyEvent evt) {////finds which key is pressed eighter left of right
if (x < 3) {
x = 3;
} else if (x > 290) {
x = 290;
}

switch (evt.getKeyCode()) {
case KeyEvent.VK_LEFT:
x -= 5;
break;
case KeyEvent.VK_RIGHT:
x += 5;
break;
}

repaint();
}

//public int getX() {
// return x;
//}
//
//public void setX(int x) {
// this.x = x;
//}
//
//public int getY() {
// return y;
//}
//
//public void setY(int y) {
// this.y = y;
//}

public static void main(String[] args) {
Bat bat = new Bat();
JFrame jf = new JFrame("game");
jf.add(bat);
jf.setSize(350,450);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}


}

Contact Form

Name

Email *

Message *

Smooth Graphics Java

// Arrange Bricks in Game //java brick breaker game code without flickering // added game levels 1,2,3 in the game //java paddle ball ga...