このチュートリアルはJava Applet初心者ためのものです。プログラミングする前に、NotepadようなテキストエディタをOpenしてください。例とするソースコードは次になります。それらソースコードをNotepadに貼り付けて、DrawingLines.javaという名前をつけて保存します。そして、ソースをコンパイルするためのJDKをSunのウェブサイトからDownloadしてください。コマンドラインに[Javac DrawingLines.java]でコンパイルできます。DrawingLines.classファイルが生成すれば、コンパイルが成功です。ブラウザに表示するためのHTMLファイル中に次のソースを入れてください。

  1. <applet width=300 height=300 code="DrawingLines.class"> </applet>

ソースコード:

  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. // The applet's class name must be identical to the filename.
  5. public class DrawingLines extends Applet {
  6.  
  7.    // Declare two variables of type "int" (integer).
  8.    int width, height;
  9.  
  10.    // This gets executed when the applet starts.
  11.    public void init() {
  12.  
  13.       // Store the height and width of the applet for future reference.
  14.       width = getSize().width;
  15.       height = getSize().height;
  16.  
  17.       // Make the default background color black.
  18.       setBackground( Color.black );
  19.    }
  20.  
  21.    // This gets executed whenever the applet is asked to redraw itself.
  22.    public void paint( Graphics g ) {
  23.  
  24.       // Set the current drawing color to green.
  25.       g.setColor( Color.green );
  26.  
  27.       // Draw ten lines using a loop.
  28.       // We declare a temporary variable, i, of type "int".
  29.       // Note that "++i" is simply shorthand for "i=i+1"
  30.       for ( int i = 0; i < 10; ++i ) {
  31.  
  32.          // The "drawLine" routine requires 4 numbers:
  33.          // the x and y coordinates of the starting point,
  34.          // and the x and y coordinates of the ending point,
  35.          // in that order.  Note that the cartesian plane,
  36.          // in this case, is upside down (as it often is
  37.          // in 2D graphics programming): the origin is at the
  38.          // upper left corner, the x-axis increases to the right,
  39.          // and the y-axis increases downward.
  40.          g.drawLine( width, height, i * width / 10, 0 );
  41.       }
  42.    }
  43. }
メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Sunday, 11th July 2010 by admin

Tags:
Posted in Java | Comments (0) | 1,016 views

Leave a Reply