[PR] この広告は3ヶ月以上更新がないため表示されています。
ホームページを更新後24時間以内に表示されなくなります。

  1. Home
  2. Java
  3. WaveRing ( Source Code )

WaveRing ( Source Code )

Last-Modified: 2008-01-03 01:52:36

ちょっとした説明

この"WaveRing"は以下の3つのクラスからできています。

WaveRing
波紋をあらわすクラスです。
WaveRingPanel
メインのパネルです。イベントリスナーが内部クラスとして含まれています。
WaveApplet
アプレットです。

jarファイルをダウンロード

WaveRing

  1. public class WaveRing implements Runnable {  
  2.     /**フィールド*/  
  3.     private int xPoint, yPoint, radius;  
  4.     //波紋のX座標、Y座標、直径  
  5.     WaveRingPanel panel;  
  6.     static int GrowRate = 10;//直径の増加  
  7.     static int Wait = 30;//描画間隔  
  8.     /**コンストラクタ*/  
  9.     /** 
  10.      * 指定された場所に指定された半径のWaveRingを生成 
  11.      * @param x int X座標 
  12.      * @param y int Y座標 
  13.      * @param r int 直径 
  14.      */  
  15.     public WaveRing(int x, int y, int r) {  
  16.         xPoint = x;  
  17.         yPoint = y;  
  18.         radius = r;  
  19.     }  
  20.     /** 
  21.      * 指定された場所に半径1のWaveRingを生成 
  22.      * @param x int X座標 
  23.      * @param y int Y座標 
  24.      */  
  25.     public WaveRing(int x, int y) {  
  26.         this(x, y, 1);  
  27.     }  
  28.     /**(100,100)に半径1のWaveRingを生成*/  
  29.     public WaveRing() {  
  30.         this(100100);  
  31.     }  
  32.     /**メソッド*/  
  33.     /** 
  34.      * panelをセット 
  35.      * @param wrp WaveRingPanel 
  36.      */  
  37.     void setPanel(WaveRingPanel wrp) {  
  38.         this.panel = wrp;  
  39.     }  
  40.     /**パネルがセットされているかチェック*/  
  41.     void pcheck() {  
  42.         if (this.panel == null) {  
  43.             System.out.println(  
  44. "You need to add a WaveRing to a Panel befor you use it!!");  
  45.             System.exit(1);  
  46.         }  
  47.     }  
  48.     /** 
  49.      * 波紋を見せる 
  50.      * @param wait int 
  51.      */  
  52.     public void showRing(int wait) {  
  53.         panel.paintImmediately(00, panel.getWidth(), panel.getHeight());  
  54.           //即再描画  
  55.         try {  
  56.             Thread.sleep(wait);//ちょっと休む  
  57.         } catch (InterruptedException e) {  
  58.         }  
  59.     }  
  60.     /**波紋を作る*/  
  61.     public void makeRing() {  
  62.         while (radius / 2 <= maxDistance()) {  
  63.             showRing(Wait);//波紋を見せて  
  64.             grow();//波紋の直径を大きくする  
  65.         }  
  66.         showRing(Wait);  
  67.     }  
  68.     public void run() {  
  69.         makeRing();  
  70.         panel.remove(this);  
  71.     }  
  72.     /**直径を増やす*/  
  73.     public void grow() {  
  74.         radius = radius + GrowRate;  
  75.     }  
  76.     public void grow(int rate) {  
  77.         radius = radius + rate;  
  78.     }  
  79.     /**直径を返す*/  
  80.     public int getR() {  
  81.         return radius;  
  82.     }  
  83.     /**x座標を返す*/  
  84.     public int getX() {  
  85.         return xPoint;  
  86.     }  
  87.     /**y座標を返す*/  
  88.     public int getY() {  
  89.         return yPoint;  
  90.     }  
  91.     /**壁との最大距離を返す*/  
  92.     public int maxDistance() {  
  93.         int xx, yy;  
  94.         pcheck();  
  95.         if (getX() > panel.getWidth() / 2) {  
  96.             xx = getX();  
  97.         } else {  
  98.             xx = panel.getWidth() - getX();  
  99.         }  
  100.         if (getY() > panel.getHeight() / 2) {  
  101.             yy = getY();  
  102.         } else {  
  103.             yy = panel.getHeight() - getY();  
  104.         }  
  105.         return (int) (Math.sqrt(xx * xx + yy * yy));  
  106.     }  
  107. }  

上に戻る

WaveRingPanel

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.MouseAdapter;  
  4. import java.awt.event.MouseEvent;  
  5. import java.util.Vector;  
  6. public class WaveRingPanel extends JPanel {  
  7.     /**フィールド*/  
  8.     static final Color BackGroundColor = new Color(20150255);  
  9.     //パネルの背景色  
  10.     Vector rings = new Vector(10);//WaveRing をしまうVector  
  11.     /**コンストラクタ*/  
  12.     /** 
  13.      * 幅width,高さheightのWaveRingPanelを作成 
  14.      * @param width int 幅 
  15.      * @param height int 高さ 
  16.      */  
  17.     public WaveRingPanel(int width,int height) {  
  18.         setBackground(BackGroundColor); //背景の色を設定  
  19.         setPreferredSize(new Dimension(width, height));  
  20.          // パネルの推奨サイズを設定  
  21.         addMouseListener(new WaveRingEvent(this));  
  22.          //イベントリスナーを設定  
  23.     }  
  24.     /**幅300,高さ300のWaveRingPanelを作成*/  
  25. public WaveRingPanel(){  
  26.         this(300,300);  
  27.     }  
  28.     /**メソッド*/  
  29.     protected void paintComponent(Graphics g) {//描く内容  
  30.         super.paintComponent(g);  
  31.         g.setColor(Color.BLACK);  
  32.         for (int i=0;i<rings.size();i++){  
  33.         //ringsにしまわれているWaveringを1個ずつ取り出して  
  34.             WaveRing ring = (WaveRing)rings.elementAt(i);  
  35.             g.drawOval(ring.getX() - ring.getR() / 2,//円を描く  
  36.                        ring.getY() - ring.getR() / 2,   
  37.                        ring.getR(), ring.getR());  
  38.         }  
  39.     }  
  40.     /** 
  41.      * WaveRing をこのフレームの上に追加する。 
  42.      * @param wr WaveRing 追加するWaveRing 
  43.      */  
  44.     public void add(WaveRing wr){  
  45.         rings.addElement(wr);  
  46.         wr.setPanel(this);  
  47.     }  
  48.     /** 
  49.      * WaveRing をこのフレームから取り除く。 
  50.      * @param wr WaveRing 取り除くWaveRing 
  51.      */  
  52.     public void remove(WaveRing wr){  
  53.         rings.removeElement(wr);  
  54.         paintImmediately(0,0,this.getWidth(),this.getHeight());  
  55.     }  
  56. /**メイン*/  
  57. public static void main(String args[]){  
  58.     WaveRingPanel wrp= new WaveRingPanel();  
  59.     JFrame frame=new JFrame("WaveRing");  
  60.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  61.         //ウィンドウを閉じたときプログラムを終了  
  62.         Container content = frame.getContentPane();  
  63.         content.add(wrp);  
  64.         frame.pack();  
  65.         frame.setVisible(true);//frameを可視化  
  66. }  
  67. /**イベント処理用クラス*/  
  68. class WaveRingEvent extends MouseAdapter {  
  69.     WaveRingPanel wavePanel;  
  70.     public WaveRingEvent(WaveRingPanel wp) {  
  71.         wavePanel = wp;  
  72.     }  
  73.     public void mouseClicked(MouseEvent e) {  
  74.     //マウスがクリックされたら  
  75.         WaveRing r = new WaveRing(e.getX(),e.getY());  
  76.          //WaveRingを作って  
  77.         wavePanel.add(r);//パネルに加え  
  78.         Thread t = new Thread(r);  
  79.         t.start();//円を描かせる  
  80.     }  
  81. }  
  82. }  

上に戻る

Wave

  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. public class WaveApplet extends JApplet {  
  4.     WaveRingPanel wrp;  
  5.     public void init(){  
  6.         wrp = new WaveRingPanel();  
  7.         Container c = getContentPane();  
  8.         c.add(wrp);  
  9.     }  
  10. }  

上に戻る | Appletに戻る | Javaに戻る | Homeに戻る

Copyright (c) Toru Mano. Last-Modified: 2008-01-03 01:52:36