アットウィキロゴ

ppp345いい

import java.io.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.net.URL;
import javax.sound.sampled.*;

class pro extends Frame implements Runnable{

Frame f;
Thread th;
int ballx,bally,dx,dy;

public static void main(String[] args){
Frame f=new pro();
f.setSize(500,500);
f.setBackground(Color.yellow);
f.show();
}

pro() {
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 ) {
int n;
g.setColor(Color.red);
g.drawRect(ballx,bally,20,20);
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;

}

public void update(Graphics g) {
paint(g);
}

public void run() {
int i;
for (i=1;i<500;i++){

if (ballx>490){

try {URL url = this.getClass().getClassLoader().getResource("sound.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();}
catch (UnsupportedAudioFileException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
catch (LineUnavailableException e) {e.printStackTrace();}

}

if (ballx<10){

try {URL url = this.getClass().getClassLoader().getResource("sound.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();}
catch (UnsupportedAudioFileException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
catch (LineUnavailableException e) {e.printStackTrace();}

}

repaint();
try{th.sleep(100);} catch(InterruptedException e) { }
}
}


}
最終更新:2010年10月03日 10:19