アプリ版:「スタンプのみでお礼する」機能のリリースについて

今晩は、「Graphicsの取得」についての質問です、宜しくお願い
致します。

アナログ時計の秒針だけを表示させるプログラムです。

アプレットで書いていたものをアプリケーションに書き換えた
ところ「WorkImage.getGraphics( ) ;」の箇所で下のようなエラ
-が発生しました。

どうも「getGraphics( )」が「NULL値をとっている」という意味
のようですが、何故「NULL値」を取るのかが全く分かりません。

以前のアプレットでは、全く問題なく動作していたものが、
何故アプリケーションに変更しただけでこのようなエラ-になる
のかが、全く理解出来ません。

どこをどのように変更すればよいのでしょうか。
詳しい方、どうか宜しくご教示お願い致します。

/*-----
Exception in thread "main" java.lang.NullPointerException
at AnalogClock.<init>(AnalogClock.java:29)
at JframeTest.<init>(JframeTest.java:11)
at JframeTest.main(JframeTest.java:18)
-----*/
//---------------------------------------------------

public class JframeTest extends JFrame
{
public JframeTest()
{
AnalogClock ac = new AnalogClock( ) ;
Container cnt = this.getContentPane() ;
this.add( ac ) ;
}

public static void main( String[] args )
{
JframeTest jt = new JframeTest() ;
// jt.setSize( 700 , 700 ) ;
jt.setBackground( Color.red ) ;
jt.setVisible( true ) ;
}
}
//-----------------------------------------
public class AnalogClock extends JPanel implements Runnable
{
Thread thread = null ;
Image WorkImage ;
Graphics WorkGraphics ;
int Center_X, Center_Y ;
int Radius ;
int Ap_Width ;
int Ap_Height ;

public AnalogClock( )
{
Ap_Width = 700 ;
Ap_Height = 700 ;
WorkImage = createImage( 700 , 700 ) ;
WorkGraphics = WorkImage.getGraphics( ) ;
Center_X = Ap_Width / 2 ;
Center_Y = Ap_Height / 2 ;
Radius = (int)( Ap_Height / 2 * 0.8 ) ;
}

public void start( )
{
thread = new Thread( this ) ;
thread.start( ) ;
}

public void paintComponents( Graphics g )
{
paintComponents( g ) ;
g.drawImage( WorkImage , 0 , 0 , this ) ;
}

public void run( )
{
while ( thread != null )
{
DispTime( ) ;
repaint( ) ;
try
{
thread.sleep( 100 ) ;
}
catch( InterruptedException e ) { }
}
}

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

void DispTime( )
{
WorkGraphics.setColor( Color.white ) ;
WorkGraphics.fillRect( 0 , 0 , Ap_Width , Ap_Height ) ;

for ( int kakudo = 0 ; kakudo < 360 ; kakudo += 6 )
{
double RD = kakudo * Math.PI / 180 ;
int x1 = Center_X + (int)( Math.sin( RD ) * Radius ) ;
int y1 = Center_Y - (int)( Math.cos( RD ) * Radius ) ;
int radius2 ;
if ( kakudo % 30 == 0 )
{
radius2 = Radius - 8 ;
}
else
{
radius2 = Radius - 5 ;
}
int x2 = Center_X + (int)( Math.sin( RD ) * radius2 ) ;
int y2 = Center_Y - (int)( Math.cos( RD ) * radius2 ) ;

WorkGraphics.setColor( Color.red ) ;
WorkGraphics.drawLine( x1 , y1 , x2 , y2 ) ;
}

Calendar date = Calendar.getInstance( TimeZone.getTimeZone( "JST" ) ) ;
int second = date.get( Calendar.SECOND ) ;
double RD = second * 6 * Math.PI / 180 ;
int sx = Center_X + (int)( Math.sin( RD ) * Radius * 0.9 ) ;
int sy = Center_Y - (int)( Math.cos( RD ) * Radius * 0.9 ) ;

WorkGraphics.setColor( Color.red ) ;
WorkGraphics.drawLine( Center_X , Center_Y , sx , sy ) ;
}
public void stop( )
{
thread = null;
}
}
//-----------------------------------------------------------------------

A 回答 (1件)

    • good
    • 0
この回答へのお礼

Tacosanさん回答有難う御座いました、何んとかうまく正常に動作させることが出来ました。ところで以前からずっと描画処理についての解決出来ない疑問があるのですが再度宜しくお願い致します。

1.Swingで書いたプログラムで、「update(Graphicsg){}」内のメソッドは、「paintComponents(g);」と書くのが正解でしょうか。それとも「paintg);」と書くのが正しいのでしょうか。

2.「publicvoidpaint/*Components*/(Graphicsg)」のメソッド名は「publicvoidpaintComponents/(Graphicsg)」が正解なのでしょうか。それとも「public void paint(Graphicsg)」が正しいのでしょうか。実はメソッド名を「public void paintComponents/(Graphicsg)」と書いていた時には、正しく動作しませんでした。パネル自体は表示されるのですが、パネルには何も表示されませんでした。メソッド名を「publicvoidpaint(Graphicsg)」に変更すると正しく画像が表示されました。

3.この「publicvoidupdate(Graphics g)」はどのような作業をしているのでしょうか。メソッド内の「paintComponents(g);」をコメントアウトしても正常に動作しているように見えました。更に、「publicvoidupdate(Graphicsg){paintComponents(g);}」全てをコメントアウトしても正常に動作しているように見えました。「repaint⇒update」により、更新される以前の画像を、一旦「update」により全てパネル上からクリアし」、その後パネル上に新しく更新した画像paintメソッドによりを描くとばかり思っていたのですが、時計の針は更新された後の秒針しか表示されませんでした。publicvoidupdate(Graphics g){paintComponents(g);}publicvoidpaint/*Components*/(Graphicsg){super.paintComponents(g);g.drawImage(WorkImage,0,0,this);}
(文字数がギリです)

お礼日時:2016/10/10 20:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!