import java.io.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.net.URL;
import javax.sound.sampled.*;
class game0228 extends Frame implements Runnable{
Frame f;
Thread th;
int ballx,bally,dx,dy;
public static void main(String[] args){
Frame f=new game0228();
f.setSize(700,700);
f.setBackground(Color.yellow);
f.show();
}
game0228(){
addWindowListener(
new stopwin());
ballx=
100;
bally=300;
dx=10;
dy=7;
th=new Thread(this);
th.start();
}
class stopwin extends WindowAdapter{
public void windowClosing(WindowEvent we){System.exit(0);}
}
public void paint( Graphics g ) {
g.setColor(Color.red);
g.drawRect(ballx,bally,20,20);
}
public void update(Graphics g) {
paint(g);
}
public void run() {
int i;
for (i=1;i<500;i++){
ballx=ballx+dx;
bally=bally+dy;
if (ballx>500)dx=-dx;
if (ballx<0)dx=-dx;
if (bally>500)dy=-dy;
if (bally<0)dy=-dy;
if (ballx>490){
}
if (ballx<10){
music();
}
if (bally>490){
music();
}
if (bally<10){
music();
}
repaint();
try{th.sleep(100);} catch(InterruptedException e) { }
}
}
void music(){
try {URL url = this.getClass().getClassLoader().getResource("sound.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
try{Thread.sleep(100);} catch(InterruptedException e) { }
clip.stop();
}
catch (UnsupportedAudioFileException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
catch (LineUnavailableException e) {e.printStackTrace();}
}
}
最終更新:2011年02月17日 00:44