// Source File Name: Block1.java
import java.awt.Container;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
class Block1
{
Block1()
{
}
public static void main(String args[])
{
JFrame jframe = new JFrame();
jframe.setTitle("Tennis");
jframe.setSize(640, 480);
jframe.setDefaultCloseOperation(3);
MyPanel mypanel = new MyPanel();
jframe.getContentPane().add(mypanel);
jframe.setVisible(true);
}
}
class MyPanel extends JPanel
implements Runnable, MouseMotionListener
{
Ball ball;
Racket racket;
boolean lose;
int speed = 10; // NOT USE, NOW
int block[] = new int[30]; // ブロックがあるかどうかの配列
int x[] = new int[30]; // ブロックのx座標
int y[] = new int[30]; // ブロックのy座標
int blockWidth = 8; // ブロックの幅
int blockHeight = 6; // ブロックの高さ
int margin = 5; // 端まで行かないよう余裕を取る
public MyPanel()
{
int i ,j;
int k; // ブロック番号
int yy; // 段毎のy座標
/* ブロックの位置(x、y座標)の設定 */
k = 0;
for( i = 0 ; i < 3 ; i++ ) {
yy = i * (blockHeight+3) + margin*3;
for( j = 0 ; j < 10 ; j++ ) {
x[k] = j * (blockWidth+4) + margin +2;
y[k] = yy;
block[k] = 1; // ブロックがある
k = k + 1;
}
}
setBackground(Color.white);
addMouseMotionListener(this);
ball = new Ball(100, 100, 5, 3, 0, 0, 610, 430, 20);
racket = new Racket(300, 400, 100, 10);
lose = false;
Thread thread = new Thread(this);
thread.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ball.forward();
if(ball.getY() + ball.getDiameter() >= racket.getY() &&
ball.getX() + ball.getDiameter() / 2 >= racket.getX() && ball.getX()
+ ball.getDiameter() / 2 <= racket.getX() + racket.getWidth())
ball.bounce();
// if(ball.getY() > racket.getY() + 10) {
// g.setColor(Color.black);
// g.drawString("You lose", 290, 220);
// lose = true;
// }
/* ブロックに当たったときの処理 */
// for( int i = 0 ; i < 30 ; i++ ) {
// if ( block[i] == 1 ) {
// if ( ... ) { // ブロックに当たったら反転
// ...... // Y 方向に反転
// ...... = 0; // ブロックを消す
// if ( 10 <= i && i < 20 ) {
// speed = 8;
// } else {
// speed = 10;
// }
// }
// }
// }
/* ブロックを描く */
for( int i = 0 ; i < 30 ; i++ ) {
if ( i < 10 ) {
/* 1段目は青 */
g.setColor(Color.blue);
}
if ( 10 <= i && i < 20 ) {
/* 2段目は赤 */
g.setColor(Color.red);
}
if ( 20 <= i ) {
/* 3段目はシアン */
g.setColor(Color.cyan);
}
if ( block[i] == 1 ) {
/* ブロックがあれば、ブロックを描く */
g.fillRect(x[i], y[i], blockWidth, blockHeight);
}
}
// DRAW BALL & RACKET
g.setColor(Color.red);
g.fillOval(ball.getX(), ball.getY(), ball.getDiameter(),
ball.getDiameter());
g.setColor(Color.gray);
g.fillRect(racket.getX(), racket.getY(), racket.getWidth(),
racket.getHeight());
}
public void mouseDragged(MouseEvent mouseevent)
{
}
public synchronized void mouseMoved(MouseEvent mouseevent)
{
racket.setX(mouseevent.getX());
}
public void run()
{
do
{
do
{
repaint();
try
{
Thread.sleep(10L);
//Thread.sleep(speed);
}
catch(Exception exception) { }
} while(!lose);
try
{
Thread.sleep(5000L);
}
catch(Exception exception1) { }
System.exit(0);
} while(true);
}
}
class Ball
{
private int x;
private int y;
private int vx;
private int vy;
private int left;
private int right;
private int top;
private int bottom;
private int diameter;
public Ball(int i, int j, int k, int l, int i1, int j1, int k1,
int l1, int i2)
{
x = i;
y = j;
vx = k;
vy = l;
right = k1;
left = i1;
top = j1;
bottom = l1;
diameter = i2;
}
public void forward()
{
x = x + vx;
y = y + vy;
if(x < left || x > right)
vx = -vx;
if(y < top || y > bottom)
vy = -vy;
}
public void bounce()
{
vy = -vy;
forward();
}
public int getX()
{
return x;
}
public void setX(int i)
{
x = i;
}
public int getY()
{
return y;
}
public void setY(int i)
{
y = i;
}
public int getDiameter()
{
return diameter;
}
}
class Racket
{
int x;
int y;
int width;
int height;
public Racket(int i, int j, int k, int l)
{
x = i;
y = j;
width = k;
height = l;
}
public void setX(int i)
{
x = i;
}
public int getX()
{
return x;
}
public void setY(int i)
{
y = i;
}
public int getY()
{
return y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
}