DxBall uygulaması
Kucuk bir calisma bir bolumluk
import java.awt.*;
import java.awt.event.*;
public class DXBall extends Frame implements KeyListener {
Button b;
Panel p;
static int x,y,w,h;
static DXBall d;
DXBall(){
this.setTitle("JAVABALL");
this.setSize(400,400);
x=200;y=370; w=100; h=15;
this.setLayout(null);
b=new Button("\t\t\t\t\t\t\t");
b.setEnabled(false);
b.setBounds(x,y,w,h);
this.add(b);
new Stone(this);
this.addKeyListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
this.setVisible(true);
new Top(d,this.getGraphics());
}
public void keyTyped(KeyEvent ke){
}
public void keyPressed(KeyEvent ke){
if (KeyEvent.VK_LEFT==ke.getKeyCode()){
x=x-4;
b.setBounds(x,y,w,h);
}
if (KeyEvent.VK_RIGHT==ke.getKeyCode()){
x=x+4;
b.setBounds(x,y,w,h);
}
}
public void keyReleased(KeyEvent ke){
}
public static void main(String[] args){
d=new DXBall();
}
}
class Top extends Thread {
static DXBall b;
static Graphics g;
static int x,y,w,h,dx,dy;
Stone s;
Thread t;
int oldy;
boolean durdur=false;
Top(DXBall a,Graphics g){
this.b=a;
this.g=g;
x=250;y=370;w=10;h=10;
dx=6;
dy=3;
this.start();
draw();
}
public void draw(){
g.setColor(Color.black);
g.fillOval(x,y,w,h);
}
public void run(){
try {
while(true){
move();
Thread.sleep(1);
vur(x,y);
this.sleep(2);
}
}
catch (InterruptedException ex) {
System.out.println ("Problem var!!");
}
}
public void move(){
try {
g.fillOval(x,y,w,h);
Thread.sleep(15);
g.clearRect(0,0,(int)Toolkit.getDefaultToolkit().g etScreenSize().getWidth(),(int)Toolkit.getDefaultT oolkit().getScreenSize().getHeight());
if (x<10){
x=10; dx=-dx;
}
if (x+dx>400){
x=390; dx=-dx;
}
if (y<10){
y=10; dy=-dy;}
if ((y+dy)>(b.y-15)){
if (((x-b.x)<90) && (x-b.x>0)) {
y=b.y-20; dy=-dy;
}else this.destroy();
}
x=x+dx;
y=y+dy;
}
catch (Exception ex) {}
}
public void vur(int x,int y){
for (int i = 0; i<5; i++) {
for (int j = 0; j<5; j++) {
if (s.stone[i][j].getBounds().contains(x,y) && s.stone[i][j].isVisible()){
s.stone[i][j].setVisible(false);
dx=-dx;
dy=-dy;
}
}
}
}
}
class Stone{
static Button[][] stone;
static int x,y,w,h;
static DXBall a;
static int siray[]=new int[5];
Stone(DXBall d){
this.a=d;
stone=new Button[5][5];
x=0;
y=40;
w=80;
h=20;
for(int j=0;j<5;j++)
for(int i=0;i<5;i++){
stone[j][i]=new Button("\t\t\t\t");
stone[j][i].setBackground(Color.blue);
stone[j][i].setEnabled(false);
stone[j][i].setBounds(x+80*i,y+20*j,w,h);
siray[i]=y+20*j;
d.add(stone[j][i]);
}
y=stone[4][1].getY();
}
}
__________________
Kucuk bir calisma bir bolumluk
import java.awt.*;
import java.awt.event.*;
public class DXBall extends Frame implements KeyListener {
Button b;
Panel p;
static int x,y,w,h;
static DXBall d;
DXBall(){
this.setTitle("JAVABALL");
this.setSize(400,400);
x=200;y=370; w=100; h=15;
this.setLayout(null);
b=new Button("\t\t\t\t\t\t\t");
b.setEnabled(false);
b.setBounds(x,y,w,h);
this.add(b);
new Stone(this);
this.addKeyListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
this.setVisible(true);
new Top(d,this.getGraphics());
}
public void keyTyped(KeyEvent ke){
}
public void keyPressed(KeyEvent ke){
if (KeyEvent.VK_LEFT==ke.getKeyCode()){
x=x-4;
b.setBounds(x,y,w,h);
}
if (KeyEvent.VK_RIGHT==ke.getKeyCode()){
x=x+4;
b.setBounds(x,y,w,h);
}
}
public void keyReleased(KeyEvent ke){
}
public static void main(String[] args){
d=new DXBall();
}
}
class Top extends Thread {
static DXBall b;
static Graphics g;
static int x,y,w,h,dx,dy;
Stone s;
Thread t;
int oldy;
boolean durdur=false;
Top(DXBall a,Graphics g){
this.b=a;
this.g=g;
x=250;y=370;w=10;h=10;
dx=6;
dy=3;
this.start();
draw();
}
public void draw(){
g.setColor(Color.black);
g.fillOval(x,y,w,h);
}
public void run(){
try {
while(true){
move();
Thread.sleep(1);
vur(x,y);
this.sleep(2);
}
}
catch (InterruptedException ex) {
System.out.println ("Problem var!!");
}
}
public void move(){
try {
g.fillOval(x,y,w,h);
Thread.sleep(15);
g.clearRect(0,0,(int)Toolkit.getDefaultToolkit().g etScreenSize().getWidth(),(int)Toolkit.getDefaultT oolkit().getScreenSize().getHeight());
if (x<10){
x=10; dx=-dx;
}
if (x+dx>400){
x=390; dx=-dx;
}
if (y<10){
y=10; dy=-dy;}
if ((y+dy)>(b.y-15)){
if (((x-b.x)<90) && (x-b.x>0)) {
y=b.y-20; dy=-dy;
}else this.destroy();
}
x=x+dx;
y=y+dy;
}
catch (Exception ex) {}
}
public void vur(int x,int y){
for (int i = 0; i<5; i++) {
for (int j = 0; j<5; j++) {
if (s.stone[i][j].getBounds().contains(x,y) && s.stone[i][j].isVisible()){
s.stone[i][j].setVisible(false);
dx=-dx;
dy=-dy;
}
}
}
}
}
class Stone{
static Button[][] stone;
static int x,y,w,h;
static DXBall a;
static int siray[]=new int[5];
Stone(DXBall d){
this.a=d;
stone=new Button[5][5];
x=0;
y=40;
w=80;
h=20;
for(int j=0;j<5;j++)
for(int i=0;i<5;i++){
stone[j][i]=new Button("\t\t\t\t");
stone[j][i].setBackground(Color.blue);
stone[j][i].setEnabled(false);
stone[j][i].setBounds(x+80*i,y+20*j,w,h);
siray[i]=y+20*j;
d.add(stone[j][i]);
}
y=stone[4][1].getY();
}
}
__________________