// // Pon.java // import java.awt.*; import java.awt.event.*; import java.applet.*; public class Pon extends Applet implements Runnable { Pad[] pad=new Pad[2]; Ball ball; Player player; Computer computer; Image offscreen; Graphics og; public void init() { setBackground(Color.white); pad[0]=new Pad(getSize().width/20,getSize().height*3/8,getSize().width/20,getSize().height/4); pad[1]=new Pad(getSize().width*18/20,getSize().height*3/8,getSize().width/20,getSize().height/4); ball=new Ball(getSize().width/2,getSize().height/2,getSize().width/20,(Math.random()>0?1:-1)*getSize().width/50,0); player=new Player(this,pad[0],ball); computer=new Computer(this,pad[1],ball); new Thread(this).start(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (offscreen==null) { offscreen=createImage(getSize().width,getSize().height); og=offscreen.getGraphics(); } og.clearRect(0,0,getSize().width,getSize().height); for (int i=0;i<2;i++) og.fillRect(pad[i].x,pad[i].y,pad[i].width,pad[i].height); og.fillOval(ball.x-ball.radius/2,ball.y-ball.radius/2,ball.radius,ball.radius); g.drawImage(offscreen,0,0,this); } void move(Pad pad,int y) { if (pad.y!=y) { pad.y=y; if (pad.y<0) pad.y=0; else if (pad.y+pad.height>getSize().height) pad.y=getSize().height-pad.height; repaint(); } } public void run() { while (true) { ball.x+=ball.dx; ball.y+=ball.dy; if (pad[0].contains(ball)) { ball.dx=Math.abs(ball.dx); ball.dy+=(ball.y-pad[0].y-pad[0].height/2)/10; } else if (pad[1].contains(ball)) { ball.dx=-Math.abs(ball.dx); ball.dy+=(ball.y-pad[1].y-pad[1].height/2)/10; } else if (ball.x<0) { ball.x=pad[1].x+pad[1].width/2; ball.y=pad[1].y+pad[1].height/2; ball.dx=-Math.abs(ball.dx); ball.dy/=2; } else if (ball.x>getSize().width) { ball.x=pad[0].x+pad[0].width/2; ball.y=pad[0].y+pad[0].height/2; ball.dx=Math.abs(ball.dx); ball.dy/=2; } if (ball.y<0) ball.dy=Math.abs(ball.dy); else if (ball.y>getSize().height) ball.dy=-Math.abs(ball.dy); repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { } } } } class Pad extends Rectangle { Pad(int x,int y,int width,int height) { super(x,y,width,height); } } class Ball extends Point { int radius; int dx,dy; Ball(int x,int y,int radius,int dx,int dy) { super(x,y); this.radius=radius; this.dx=dx; this.dy=dy; } } class Player implements MouseMotionListener { Pon mediator; Pad pad; Ball ball; Player(Pon pon,Pad pad,Ball ball) { mediator=pon; this.pad=pad; this.ball=ball; mediator.addMouseMotionListener(this); } public void mouseDragged(MouseEvent evt) { mediator.move(pad,evt.getY()-pad.height/2); } public void mouseMoved(MouseEvent evt) { mediator.move(pad,evt.getY()-pad.height/2); } } class Computer implements Runnable { Pon mediator; Pad pad; Ball ball; Computer(Pon pon,Pad pad,Ball ball) { mediator=pon; this.pad=pad; this.ball=ball; new Thread(this).start(); } public void run() { while (true) { mediator.move(pad,pad.y+Math.max(-pad.height/7,Math.min(ball.y-pad.height/2-pad.y,pad.height/7))); try { Thread.sleep(150); } catch (InterruptedException e) { } } } }