2014年3月29日土曜日

開発環境

Head First Java 第2版 ―頭とからだで覚えるJavaの基本(Kathy Sierra (著)、Bert Bates (著)、島田 秋雄 (監修)、神戸 博之 (監修)、高坂 一城 (監修)、夏目 大 (翻訳)、オライリージャパン)の12章(GUIの基礎)、自分で考えてみよう(p.385)を解いてみる。

自分で考えてみよう(p.385)

コード(BBEdit, Emacs)

TestAnimation2.java

import javax.swing.*;
import java.awt.*;

public class TestAnimation2 {
    JFrame frame;
    int x = 50;
    int y = 50;

    public static void main(String [] args) {
        TestAnimation2 gui = new TestAnimation2();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);

        frame.setSize(500, 500);
        frame.setVisible(true);

        try {
            Thread.sleep(10000);
        } catch (Exception ex) {}

        // 1
        while (x < 400) {
            x = x + 1;
            y = y + 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }

        try {
            Thread.sleep(1000);
        } catch (Exception ex) {}

        // 2
        x = 50;
        y = 100;
        drawPanel.repaint();
        while (x < 400) {
            x = x + 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }
        
        try {
            Thread.sleep(1000);
        } catch (Exception ex) {}

        // 3
        x = 400;
        drawPanel.repaint();
        while (x > 50) {
            x = x - 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }

        try {
            Thread.sleep(1000);
        } catch (Exception ex) {}

        // 1
        x = 400;
        y = 50;
        drawPanel.repaint();
        while (x > 50) {
            x = x - 1;
            y = y + 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }

        try {
            Thread.sleep(1000);
        } catch (Exception ex) {}
        // 2
        x = 50;
        y = 400;
        drawPanel.repaint();
        while (x < 400) {
            x = x + 1;
            y = y - 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }

        try {
            Thread.sleep(1000);
        } catch (Exception ex) {}

        // 3
        x = 400;
        y = 400;
        drawPanel.repaint();
        while (x > 50) {
            x = x - 1;
            y = y - 1;
            drawPanel.repaint();

            try {
                Thread.sleep(10);
            } catch (Exception ex) {}
        }
    }
    
    class MyDrawPanel extends JPanel {
        public void paintComponent(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            
            g.setColor(Color.red);        
            g.fillOval(x, y, 50, 50);
        }
    }
}

入出力結果(Terminal)

$ javac TestAnimation2.java && java TestAnimation2
$

記念に。

0 コメント:

コメントを投稿