Make your own free website on Tripod.com

This Java code is converted into HTML using JAVA_Colorizator v1.0 (c) 2001 by Alexander Yanuar Koentjara (lexzeus@hotmail.com)
/* Created by : Alexander Yanuar KOENTJARA - LexZEUS E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com This program is a good start for java newbie who want to learn Java (Applet, Graphics, and Thread). Any comment or question, just contact me via my e-mail ... This code can be destributed freely without notice, but must maintain the original author. If you modify some code and redestribute the code, you can include your name and the date when you modified it. Created date : 19-Apr-2001 - by Author Date By Modified #1 : Modified #2 : Modified #3 : */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; public class pinball extends Applet implements MouseMotionListener, KeyListener, Runnable { private MyBall Allball[]; private MyBoard brd; private MyBricks brick; private Image myBuff; private Thread thisThread; private int maxball, missed, timer=0, timer2=0; private Color backC = Color.lightGray; private Color arrColor[] = { new Color(0,0,255), new Color(0,255,255), new Color(255,255,255), new Color(180,120,60), new Color(255,255,0), new Color(0,255,0), new Color(0,0,0), new Color(255,0,255), new Color(60,120,180) }; private boolean isPaused=false; public static int hgPanel,wdPanel,hgBrick, wdBrick,ballSize,score,borderX,borderY; public void init() { score = 0; missed = 0; // total ball displayed on screen maxball=1; // the pannel for pinball hgPanel=300; wdPanel=300; // distance between the applet and the panel borderX=20; borderY=20; // dimension of the whole bricks (there are 8 x 8 = 64 bricks) hgBrick=120; wdBrick=300; // ballSize ballSize=15; // create Image buffer myBuff = createImage(wdPanel,hgPanel+70); // create bouncher board brd = new MyBoard((int) (wdPanel-40)/2, hgPanel-10,40,Color.red, wdPanel); // create bricks (8x8) brick = new MyBricks(wdBrick,hgBrick); // create 9 balls Allball = new MyBall[9]; int a,b,c; for (int i=0;i<9;i++) { a=(int) (Math.random()*(wdBrick-20-ballSize))+10; b=(int) (Math.random()*20)+hgBrick+20; c=(int) (Math.random()*3)+1; Allball[i]=new MyBall(a,b,0,c,ballSize,ballSize); Allball[i].setColor(arrColor[i]); } // timer thread thisThread = new Thread(this); thisThread.start(); // user's interface addMouseMotionListener(this); addKeyListener(this); } public String getAppletInfo() { String t = " ************************************* \n" + " \n" + " LexZEUS PinBall Ver 1.0 \n" + " \n" + " ------------------------------------- \n" + " \n" + " (C) by Alexander Yanuar Koentjara \n" + " (lexzeus@hotmail.com) \n" + " \n" + " ************************************* \n"; return t; } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_R) // "R" is pressed { int a,b,c; for (int i=0;i<9;i++) { a=(int) (Math.random()*(wdBrick-20-ballSize))+10; b=(int) (Math.random()*20)+hgBrick+20; c=(int) (Math.random()*3)+1; Allball[i].px=a; Allball[i].py=b; Allball[i].dx=0; Allball[i].dy=c; } score=0; missed=0; brick = new MyBricks(wdBrick,hgBrick); } if (e.getKeyCode()==KeyEvent.VK_P) // "P" is pressed isPaused = !isPaused; // "1" - "9" is pressed : if (e.getKeyCode()==KeyEvent.VK_1) maxball=1; if (e.getKeyCode()==KeyEvent.VK_2) maxball=2; if (e.getKeyCode()==KeyEvent.VK_3) maxball=3; if (e.getKeyCode()==KeyEvent.VK_4) maxball=4; if (e.getKeyCode()==KeyEvent.VK_5) maxball=5; if (e.getKeyCode()==KeyEvent.VK_6) maxball=6; if (e.getKeyCode()==KeyEvent.VK_7) maxball=7; if (e.getKeyCode()==KeyEvent.VK_8) maxball=8; if (e.getKeyCode()==KeyEvent.VK_9) maxball=9; } // to change background color (when ball hit the brick) public void setBackColor(Color col) { timer=10; backC = col; } public void mouseDragged(MouseEvent me) { } // bouncher board is moved by mouse pointer public void mouseMoved(MouseEvent me) { brd.setX(me.getX()-borderX-20); repaint(); } // thread timer running ... public void run() { MyBall ball; int bl; while(true) // repeat forever { if (!isPaused) for (int i=0;i<maxball;i++) { ball = Allball[i]; bl = ball.moveBall(brick); if (bl>0) // if ball move horizontally/vertically/diagonally { /* 1 = horizontally 2 = vertically 3 = both */ ball.bounchBrick(brick,bl,this); if (bl==1 || bl==3) // move horizontally { if (ball.px>wdPanel-2-ballSize || ball.px<2) ball.bounchV(); } if (bl==2 || bl==3) // move vertically { brd.isBounch(ball); if (ball.py<2) ball.bounchH(); if (ball.py>hgPanel-2-ballSize) // if ball fall out from // border, restart the ball { ball.restart(); missed++; } } } } repaint(); try { thisThread.sleep(5); } catch (Exception ex) {} } } public void start() { setBackground(Color.white); } public void destroy() { } public void update(Graphics scGR) { paint(scGR); } public void paint(Graphics scGR) { Graphics Buff = myBuff.getGraphics(); // clear stage Buff.setColor(Color.white); Buff.fillRect(0,0,wdPanel,hgPanel+70); // paint the panel if (--timer<0) timer=0; if (timer==0) backC=Color.lightGray; Buff.setColor(backC); Buff.fill3DRect(1,1,wdPanel-2,hgPanel-2,true); // draw all brick.drawBricks(Buff); for (int i=0;i<maxball;i++) { Allball[i].drawBall(Buff); } brd.drawBoard(Buff); // draw border Buff.setColor(Color.black); Buff.drawRect(0,0,wdPanel-1,hgPanel-1); // print score and missed ball Buff.setColor(Color.red); Buff.drawString("Score : "+score, 5, 130 ); Buff.drawString("Missed : "+missed, 5, 145 ); // write info Buff.setColor(Color.black); Buff.drawString("LexZEUS Pinball - Move your mouse to bounch them.", 1, hgPanel+12 ); Buff.drawString("(c) By Alexander Yanuar K. 19/Apr/2001", 1, hgPanel+27); Buff.drawString("Press 1-9 to set the balls, 'R' to restart, 'P' to pause.", 1, hgPanel+50); // if paused ... if (isPaused) { timer2++; if (timer2 % 2 == 0) Buff.setColor(Color.white); else Buff.setColor(Color.black); Buff.drawString("PAUSED !!!", 5, 160 ); } // refresh to applet scGR.drawImage(myBuff,borderX,borderY,this); } } class MyBall { public int px, py, dx, dy, wd, hg, timer, tx=0, ty=0; public Color col = Color.red; int arr[] = new int[2]; public MyBall(int x,int y,int xx,int yy) { px=x; py=y; dx=xx; dy=yy; wd=5; hg=5; } public MyBall(int x,int y,int xx,int yy, int w, int h) { px=x; py=y; dx=xx; dy=yy; wd=w; hg=h; } public void setColor(Color c) { col = c; } public void bounchV() { dx=-dx; tx=0; ty=0; } public void bounchH() { dy=-dy; tx=0; ty=0; } public static int abs(int x) { if (x>=0) return x; else return -x; } public static int intFlag(int x) { if (x==0) return 0; if (x<0) return -1; else return 1; } public int[] bounchBrick(MyBricks brick, int bl, pinball pb) { arr[0]=-1; arr[1]=-1; int wid = (int) brick.wd / 8; int hig = (int) brick.hg / 8; int bx1,bx2,by1,by2,rx=0,ry=0; boolean bln=false; if (bl==2 || bl==3) // move vertical { outerloop2: for (int y=0; y<8; y++) for (int x=0; x<8; x++) if ( brick.brick[y][x] > 0 ) { bx1=x*wid+1; bx2=bx1+wid-2; if (dy>=0) by1=y*hig+1; else by1=y*hig+1+hig-2; if ( ( (py+hg-1==by1 && dy>=0) || (py==by1 && dy<0) ) && MyBoard.inside(px,bx1-wd-1,bx2)) { bln=true; rx=x; ry=y; dy=-dy; break outerloop2; } } } if (bl==1 || bl==3) // move horizontal { outerloop1: for (int y=0; y<8; y++) for (int x=0; x<8; x++) if ( brick.brick[y][x] > 0 ) { by1=y*hig+1; by2=by1+hig-2; if (dx>=0) bx1=x*wid+1; else bx1=x*wid+1+wid-2; if ( ( ( px+wd-1==bx1 && dx>=0) || ( px==bx1 && dx<0) ) && MyBoard.inside(py,by1-hg-1,by2)) { dx=-dx; bln=true; rx=x; ry=y; break outerloop1; } } } if (bln) { pb.score+=brick.brick[ry][rx]*10; brick.brick[ry][rx]=0; arr[0]=rx; arr[1]=ry; tx=1000; ty=1000; pb.setBackColor(Color.gray); } return arr; } public int moveBall(MyBricks brc) { int bl=0; tx++; ty++; if (tx>=abs(dx)) { tx=0; px+=intFlag(dx); bl=1; } if (ty>=abs(dy)) { ty=0; py+=intFlag(dy); bl+=2; } return bl; } public void drawBall(Graphics Buff) { if (timer % 2 == 0) Buff.setColor(col); else Buff.setColor(Color.red); timer--; if (timer<0) timer=0; Buff.fillOval(px,py,wd,hg); } public void restart() { py=pinball.hgBrick+10; px=(int) (Math.random()*(pinball.wdBrick-20-pinball.ballSize))+10; timer=100; dx=0; dy=2; } } class MyBoard { public int px, py, wd, size; public Color col = Color.red; public MyBoard(int x,int y,int w, Color c,int s) { px=x; py=y; wd=w; col=c; size=s; } public void setColor(Color c) { col = c; } public void setX(int x) { px = x; if (px<0) px=0; if (px>size-wd) px=size-wd; } public static boolean inside(int a, int b, int c) { if (b<=a && a<=c) return true; else return false; } public boolean isBounch(MyBall MB) // check if ball bounch to bouncher board { if (inside((int) MB.px + MB.wd/2,px,px+wd) && inside((int) MB.py + MB.hg/2,py,py+8)) { MB.dy=(int) -((Math.random()*3)+1); MB.py=MB.py-10; if (MB.dx>0) MB.dx=(int) (Math.random()*3)+1; else MB.dx=(int) -((Math.random()*3)+1); return true; } else return false; } public void drawBoard(Graphics Buff) { Buff.setColor(col); Buff.fill3DRect(px,py,wd,5,true); } } class MyBricks { Color col[] = new Color[8]; int wd, hg; public int brick[][] = { { 5 , 6, 7, 7, 7, 7, 6, 5 }, { 4 , 5, 6, 6, 6, 6, 5, 4 }, { 3 , 4, 5, 5, 5, 5, 4, 3 }, { 2 , 3, 4, 5, 5, 4, 3, 2 }, { 2 , 2, 3, 4, 4, 3, 2, 2 }, { 1 , 2, 3, 3, 3, 3, 2, 1 }, { 1 , 2, 2, 3, 3, 2, 2, 1 }, { 1 , 1, 1, 1, 1, 1, 1, 1 } }; public MyBricks(int w, int h) { col[1] = new Color(128,74,255); col[2] = new Color(233,233,233); col[3] = new Color(42,128,235); col[4] = new Color(42,255,190); col[5] = new Color(23,32,255); col[6] = new Color(244,78,100); col[7] = new Color(255,55,51); wd = w; hg = h; } public void drawBricks(Graphics Buff) // draw all bricks { int wid = (int) wd / 8; int hig = (int) hg / 8; for (int y=0; y<8; y++) for (int x=0; x<8; x++) if (brick[y][x]>0) // if brick[y][x] is still exist ... { Buff.setColor(col[brick[y][x]]); Buff.fill3DRect(x*wid+1,y*hig+1,wid-2,hig-2,true); } } }