マウスクリックで別のスレッドを動かしたい
うまく動きません、どなたかご教示下さい(文字数が不足です)
=====
public class ImgMove3 extends Applet implements Runnable , MouseListener
{
private int width , height ;
private Thread th1 /*, th2*/ ;
private boolean bLoopEnd = false ;
private int x1 = 30 , y1 = 100 ;
// private int x2 = 80 , y2 = 150 ;
Image img1 , img2 ;
AudioClip sound ;
private int vx1 = 3 , vy1 = 2 ;
// private int vx2 = 1 , vy2 = 1 ;
Dimension d ;
public void init()
{
d = getSize() ;
width = d.width ;
height = d.height ;
this.addMouseListener(this) ;
this.addMouseListener(new MyImage3(this)) ;
img1 = getImage( getDocumentBase() , "ambulance.jpg" ) ;
img2 = getImage( getDocumentBase() , "firecar.jpg" ) ;
sound = getAudioClip( getDocumentBase() , "UFO.wav" ) ;
}
public void mouseEntered( MouseEvent e)
{
sound.play() ;
}
public void mouseClicked(MouseEvent e) { }
public void start()
{
if(th1 == null)
{
th1 = new Thread(this);
th1.start();
}
}
public void stop()
{
if( th1 != null ) { th1 = null ; }
}
public void run()
{
while(bLoopEnd == false)
{
move() ;
repaint() ;
try{ Thread.sleep( 10 ) ; }
catch (InterruptedException e ) {}
}
}
private void move()
{
if( x1 >= width - 130 ) { vx1 =- vx1 ; }
if( y1 >= height - 110 ) { vy1 =- vy1 ; }
if( x1 < 0 ) { vx1 =- vx1 ; }
if( y1 < 0 ) { vy1 =- vy1 ; }
}
public void update( Graphics g1 )
{
paint( g1 ) ;
}
public void paint( Graphics g1 )
{
g1.drawImage( img1 , x1 , y1 , this ) ;
}
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e){ }
}
//=======================================================================
class MyImage3 extends Applet implements Runnable , MouseListener
{
private int width , height ;
private Thread th2 ;
private boolean bLoopEnd = false ;
private int x2 = 80 , y2 = 150 ;
Image img2 ;
AudioClip sound ;
private int vx2 = 1 , vy2 = 1 ;
Dimension d ;
Image firecar ;
ImgMove3 imv3 ;
public MyImage3(ImgMove3 app)
{
imv3 = app ;
imv3.img2 = getImage( getDocumentBase() , "firecar.jpg" ) ;
}
public void mouseEntered( MouseEvent e)
{
imv3.sound.play() ;
}
public void mouseClicked(MouseEvent e)
{
Graphics g1 =getGraphics();
g1.drawImage( img2 , x2 , y2 , this ) ;
imv3.repaint();
}
public void start()
{
if(th2 == null)
{
th2 = new Thread(this);
th2.start();
}
}
public void stop()
{
if( th2 != null ) { th2 = null ; }
}
public void run()
{
while( th2 != null && bLoopEnd == false)
{
move() ;
imv3.repaint() ;
try{ Thread.sleep( 10 ) ; }
catch (InterruptedException e ) {}
}
}
private void move()
{
if( x2 >= width - 130 ) { vx2 =- vx2 ; }
if( y2 >= height - 110 ) { vy2 =- vy2 ; }
if( x2 < 0 ) { vx2 =- vx2 ; }
if( y2 < 0 ) { vy2 =- vy2 ; }
x2 += vx2 ;
y2 += vy2 ;
}
public void update( Graphics g1 )
{
imv3.paint( g1 ) ;
}
public void paint( Graphics g1 )
{
g1.drawImage( img2 , x2 , y2 , this ) ;
}
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
}
お礼
早速実行させていただいたところ 殆ど、ちらつかなくなりました。 迅速なご回答ありがとうございました。