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

public class WaveRing implements Runnable {
    /**フィールド*/
    private int xPoint, yPoint, radius;
    //波紋のX座標、Y座標、直径
    WaveRingPanel panel;
    static int GrowRate = 10;//直径の増加
    static int Wait = 30;//描画間隔
    /**コンストラクタ*/
    /**
     * 指定された場所に指定された半径のWaveRingを生成
     * @param x int X座標
     * @param y int Y座標
     * @param r int 直径
     */
    public WaveRing(int x, int y, int r) {
        xPoint = x;
        yPoint = y;
        radius = r;
    }
    /**
     * 指定された場所に半径1のWaveRingを生成
     * @param x int X座標
     * @param y int Y座標
     */
    public WaveRing(int x, int y) {
        this(x, y, 1);
    }
    /**(100,100)に半径1のWaveRingを生成*/
    public WaveRing() {
        this(100, 100);
    }
    /**メソッド*/
    /**
     * panelをセット
     * @param wrp WaveRingPanel
     */
    void setPanel(WaveRingPanel wrp) {
        this.panel = wrp;
    }
    /**パネルがセットされているかチェック*/
    void pcheck() {
        if (this.panel == null) {
            System.out.println(
"You need to add a WaveRing to a Panel befor you use it!!");
            System.exit(1);
        }
    }
    /**
     * 波紋を見せる
     * @param wait int
     */
    public void showRing(int wait) {
        panel.paintImmediately(0, 0, panel.getWidth(), panel.getHeight());
          //即再描画
        try {
            Thread.sleep(wait);//ちょっと休む
        } catch (InterruptedException e) {
        }
    }
    /**波紋を作る*/
    public void makeRing() {
        while (radius / 2 <= maxDistance()) {
            showRing(Wait);//波紋を見せて
            grow();//波紋の直径を大きくする
        }
        showRing(Wait);
    }
    public void run() {
        makeRing();
        panel.remove(this);
    }
    /**直径を増やす*/
    public void grow() {
        radius = radius + GrowRate;
    }
    public void grow(int rate) {
        radius = radius + rate;
    }
    /**直径を返す*/
    public int getR() {
        return radius;
    }
    /**x座標を返す*/
    public int getX() {
        return xPoint;
    }
    /**y座標を返す*/
    public int getY() {
        return yPoint;
    }
    /**壁との最大距離を返す*/
    public int maxDistance() {
        int xx, yy;
        pcheck();
        if (getX() > panel.getWidth() / 2) {
            xx = getX();
        } else {
            xx = panel.getWidth() - getX();
        }
        if (getY() > panel.getHeight() / 2) {
            yy = getY();
        } else {
            yy = panel.getHeight() - getY();
        }
        return (int) (Math.sqrt(xx * xx + yy * yy));
    }
}

上に戻る

WaveRingPanel

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
public class WaveRingPanel extends JPanel {
    /**フィールド*/
    static final Color BackGroundColor = new Color(20, 150, 255);
    //パネルの背景色
    Vector rings = new Vector(10);//WaveRing をしまうVector
    /**コンストラクタ*/
    /**
     * 幅width,高さheightのWaveRingPanelを作成
     * @param width int 幅
     * @param height int 高さ
     */
    public WaveRingPanel(int width,int height) {
        setBackground(BackGroundColor); //背景の色を設定
        setPreferredSize(new Dimension(width, height));
         // パネルの推奨サイズを設定
        addMouseListener(new WaveRingEvent(this));
         //イベントリスナーを設定
    }
    /**幅300,高さ300のWaveRingPanelを作成*/
public WaveRingPanel(){
        this(300,300);
    }
    /**メソッド*/
    protected void paintComponent(Graphics g) {//描く内容
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        for (int i=0;i<rings.size();i++){
        //ringsにしまわれているWaveringを1個ずつ取り出して
            WaveRing ring = (WaveRing)rings.elementAt(i);
            g.drawOval(ring.getX() - ring.getR() / 2,//円を描く
                       ring.getY() - ring.getR() / 2, 
                       ring.getR(), ring.getR());
        }
    }
    /**
     * WaveRing をこのフレームの上に追加する。
     * @param wr WaveRing 追加するWaveRing
     */
    public void add(WaveRing wr){
        rings.addElement(wr);
        wr.setPanel(this);
    }
    /**
     * WaveRing をこのフレームから取り除く。
     * @param wr WaveRing 取り除くWaveRing
     */
    public void remove(WaveRing wr){
        rings.removeElement(wr);
        paintImmediately(0,0,this.getWidth(),this.getHeight());
    }
/**メイン*/
public static void main(String args[]){
    WaveRingPanel wrp= new WaveRingPanel();
    JFrame frame=new JFrame("WaveRing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //ウィンドウを閉じたときプログラムを終了
        Container content = frame.getContentPane();
        content.add(wrp);
        frame.pack();
        frame.setVisible(true);//frameを可視化
}
/**イベント処理用クラス*/
class WaveRingEvent extends MouseAdapter {
    WaveRingPanel wavePanel;
    public WaveRingEvent(WaveRingPanel wp) {
        wavePanel = wp;
    }
    public void mouseClicked(MouseEvent e) {
    //マウスがクリックされたら
        WaveRing r = new WaveRing(e.getX(),e.getY());
         //WaveRingを作って
        wavePanel.add(r);//パネルに加え
        Thread t = new Thread(r);
        t.start();//円を描かせる
    }
}
}

上に戻る

Wave

import java.awt.*;
import javax.swing.*;
public class WaveApplet extends JApplet {
    WaveRingPanel wrp;
    public void init(){
        wrp = new WaveRingPanel();
        Container c = getContentPane();
        c.add(wrp);
    }
}

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

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