今晩は。
花火のようなものが開くプログラムをアプリケーションで書きました。
うまく動作しません。
色々と途中の内容を書き出し、チェックしてみると、
途中の「CreateImage」には、うまく作画しているのですが、
「paint」のパネルへの作画箇所で、うまく「CreateImage」
との「Graphics」の受け渡しがいってないような気がします。
「CreateImage」に作画した黒色の表示がパネルに作画される
だけです。
どこをどのように修正してやればよいのでしょうか。
詳しい方宜しくご教示お願い致します。
//---------------------------------------------
public class Hanabi extends JPanel implements Runnable
{
Thread thread = null ;
int AppletWidth = 1300 ; int AppletHeight = 900 ; Image WorkImage ; Graphics WorkGraphics ; int sleeptime = 30 ; int AngleRange = 50 ; int SpeedMax = 90 ; int ExplosionSpeed = 50 ; int XP , YP ; int HMAX = 30 ; int TMAX = 200 ; int LEN = 4 ; int SIZE = 1 ; int COUNT = 30 ; int X[ ][ ] = new int[ HMAX ][ TMAX ] ; int Y[ ][ ] = new int[ HMAX ][ TMAX ] ; double Angle[ ][ ] = new double[ HMAX ][ TMAX ] ; double Speed[ ][ ] = new double[ HMAX ][ TMAX ] ; double V0x[ ][ ] = new double[ HMAX ][ TMAX ] ; double V0y[ ][ ] = new double[ HMAX ][ TMAX ] ; int Process[ ] = new int[ HMAX ] ; int Time[ ] = new int[ HMAX ] ; float Hue[ ] = new float[ HMAX ] ; double Rad = Math.PI / 180 ;
// Initialize
public Hanabi()
{
this.setSize( 1300 , 900 ) ;
if ( HMAX > 30 ) { HMAX = 30 ; } for ( int i = 0 ; i < HMAX ; i ++ ) { Process[ i ] = 0 ; } XP = AppletWidth / 2 ; YP = AppletHeight ; }
// make_CreateImage
public void make_CreateImage()
{
WorkImage = createImage( AppletWidth , AppletHeight ) ;
WorkGraphics = WorkImage.getGraphics( ) ;
WorkGraphics.setColor( Color.black ) ;
WorkGraphics.fillRect( 0 , 0 , AppletWidth , AppletHeight ) ;
System.out.println(" make_CreateImage()_WorkGraphics = " + WorkGraphics ) ; System.out.println(" make_CreateImage()_WorkImage = "+ WorkImage ) ; }
// Thread Start
public void start( )
{
thread = new Thread( this ) ;
thread.start( ) ;
}
// painting
public void paint( Graphics g )
{
System.out.println( " paint_WorkImage = " + WorkImage ) ; g.drawImage( WorkImage , 0 , 0 , this ) ; }
// Thread
public void run( )
{
while( thread != null )
{
MakingHanabi( ) ;
try
{
thread.sleep( sleeptime ) ;
}
catch( InterruptedException e ){ }
repaint( ) ; } }
// Update
public void update( Graphics g )
{
paint( g ) ;
}
// Thread停止
public void stop( )
{
thread = null ;
}
// MakingHanabi
void MakingHanabi( )
{
for( int n = 0 ; n < HMAX ; n ++ )
{
if( Process[n] == 0 )
{
Angle[ n ][ 0 ] = 90 - AngleRange / 2 + (int)( Math.random( ) * AngleRange ) ;
Speed[ n ][ 0 ] = SpeedMax + (int)( Math.random( ) * 20 ) ;
X[ n ][ 0 ] = AppletWidth / 2 ; Y[ n ][ 0 ] = AppletHeight ; V0x[ n ][ 0 ] = Speed[ n ][ 0 ] * Math.cos( Angle[ n ][ 0 ] * Rad ) ; V0y[ n ][ 0 ] = Speed[ n ][ 0 ] * Math.sin( Angle[ n ][ 0 ] * Rad ) ; Time[ n ] = 0 ; Process[ n ] = 1 ; Hue[ n ] = (float)Math.random( ) ; } else if( Process[ n ] == 1 ) { double nowtime = Time[ n ] / 10.0 ; if( V0y[ n ][ 0 ] - 9.8 * nowtime >= 10.0 ) { WorkGraphics.setColor( Color.black ) ; WorkGraphics.drawLine( X[ n ][ 0 ] , Y[ n ][ 0 ] , X[ n ][ 0 ] , Y[ n ][ 0 ] ) ; Time[ n ] += 1 ; nowtime = Time[ n ] / 10.0 ; int xt = (int)( V0x[ n ][ 0 ] * nowtime ) ; int yt = (int)( V0y[ n ][ 0 ] * nowtime - 9.8 / 2 * nowtime * nowtime ) ; X[ n ][ 0 ] = XP + xt ; Y[n][ 0 ] = YP - yt ; WorkGraphics.setColor( Color.white ) ; WorkGraphics.fillRect( X[ n ][ 0 ] , Y[ n ][ 0 ] , 1 , 1 ) ; } else { WorkGraphics.setColor( Color.black ) ; WorkGraphics.drawLine( X[ n ][ 0 ] , Y[ n ][ 0 ] , X[ n ][ 0 ] , Y[ n ][ 0 ] ) ; Process[ n ] = 2 ; Time[ n ] = 0 ; for ( int i = 1 ; i < TMAX ; i ++ ) { Angle[ n ][i] = (int)( Math.random( ) * 360 ) ; Speed[ n ][i] = (int)( ExplosionSpeed * Math.random( ) ) ; X[ n ][ i ] = X[ n ][ 0 ] ; Y[ n ][ i ] = Y[ n ][ 0 ] ; V0x[ n ][ i ] = Speed[ n ][ i ] * Math.cos( Angle[ n ][ i ] * Rad ) ; V0y[ n ][ i ] = Speed[ n ][ i ] * Math.sin( Angle[ n ][ i ] * Rad ) ; } } } else if( Process[ n ] >= 2 ) { Time[ n ] += 1 ; for( int w = 1 ; w < TMAX ; w ++ ) { if( Time[ n ] >= LEN ) { double backtime = ( Time[ n ] - LEN ) / 10.0 ; WorkGraphics.setColor( Color.black ) ; WorkGraphics.fillRect ( X[ n ][ 0 ] + (int)( V0x[ n ][ w ] * backtime ) , Y[ n ][ 0 ] - (int)( V0y[ n ][ w ] * backtime - 9.8 / 2 * backtime * backtime ) , SIZE , SIZE ) ; } double nowtime = Time[ n ] / 10.0 ; int xt = (int)( V0x[ n ][ w ] * nowtime ) ; int yt = (int)( V0y[ n ][ w ] * nowtime - 9.8 / 2 * nowtime * nowtime ) ; X[ n ][ w ] = X[ n ][ 0 ] + xt ; Y[ n ][ w ] = Y[ n ][ 0 ] - yt ; if( COUNT - Process[ n ] > LEN ) { if( Process[ n ] < COUNT / 2 ) { WorkGraphics.setColor( Color.getHSBColor( Hue[ n ] , 1f ,1f ) ) ; } else { WorkGraphics.setColor( Color.getHSBColor( Hue[ n ] , 1f ,1f * ( COUNT - Process[ n ] ) / (COUNT / 2 ) ) ) ; } } else { //黒色 WorkGraphics.setColor( Color.getHSBColor( Hue[ n ] , 1f , 0 ) ) ; } WorkGraphics.fillRect( X[ n ][ w ] , Y[ n ][ w ] , SIZE , SIZE ) ; } Process[ n ] ++ ; if(Process[ n ] > COUNT ) { Process[ n ] = 0 ; } } } }
}
//---------------------------------------------------------
public class JFrameTest extends JFrame
{
Hanabi small_Hanabi ;
public JFrameTest() { this.setDefaultCloseOperation( EXIT_ON_CLOSE ) ; this.setLocation( 400 , 50 ) ; this.setLayout( null ) ; Container cnt = this.getContentPane( ) ; small_Hanabi = new Hanabi() ; cnt.add( small_Hanabi ) ; pack(); this.setSize( 1000 , 900 ) ; small_Hanabi.make_CreateImage() ; } public static void main( String[] args ) { ( new JFrameTest() ).setVisible( true ) ; }
}
回答1件
あなたの回答
tips
プレビュー