Head First Java 第2版
頭とからだで覚えるJavaの基本
(オライリージャパン)
Kathy Sierra (著) Bert Bates (著) 島田 秋雄(監修)
神戸 博之 (監修) 高坂 一城 (監修) 夏目 大 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Java (プログラミング言語)
- javac (コンパイラ)
- java (application launcher)
Head First Java 第2版 ―頭とからだで覚えるJavaの基本(Kathy Sierra (著)、Bert Bates (著)、島田 秋雄 (監修)、神戸 博之 (監修)、高坂 一城 (監修)、夏目 大 (翻訳)、オライリージャパン)の18章(RMIによるプログラムの提供)、自分で考えてみよう(p.644)を解いてみる。
自分で考えてみよう(p.655)
コード(BBEdit, Emacs)
DiceService.java
// とりあえずGUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DiceService {
JLabel label;
JComboBox numOfDice;
int n = 1;
int r;
public static void main (String [] args) {
DiceService gui = new DiceService();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Roll 'em!");
MyDrawPanel dice = new MyDrawPanel();
String [] choices = {"1", "2", "3", "4", "5", "6"};
numOfDice = new JComboBox(choices);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("dice values here");
button.addActionListener(new RollEmListener());
panel.add(numOfDice);
panel.add(button);
panel.add(label);
frame.getContentPane().add(BorderLayout.NORTH, panel);
frame.getContentPane().add(BorderLayout.CENTER, dice);
frame.setSize(500, 500);
frame.setVisible(true);
boolean flag = true;
int t = 1;
while (true) {
if (flag) {
n = t;
r = (int) ((Math.random() * 6 + 1));
if (6 - n == r || n == r) {
continue;
}
n = r;
flag = false;
} else {
t = n;
n = -1;
flag = true;
}
dice.repaint();
try {
Thread.sleep(500);
} catch (Exception ex) {}
}
}
public class RollEmListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String diceOutput = "";
String selection = (String) numOfDice.getSelectedItem();
int numOfDiceToRoll = Integer.parseInt(selection);
for (int i = 0; i < numOfDiceToRoll; ++i) {
int r = (int) ((Math.random() * 6) + 1);
diceOutput += (" " + r);
}
label.setText(diceOutput);
}
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.white);
g.fillRect(100, 50, 300, 300);
g.setColor(Color.black);
switch (n) {
case 1:
g.setColor(Color.red);
g.fillOval(225, 175, 50, 50);
break;
case 2:
g.fillOval(125, 275, 50, 50);
g.fillOval(325, 75, 50, 50);
break;
case 3:
g.fillOval(125, 275, 50, 50);
g.fillOval(225, 175, 50, 50);
g.fillOval(325, 75, 50, 50);
break;
case 4:
g.fillOval(125, 75, 50, 50);
g.fillOval(125, 275, 50, 50);
g.fillOval(325, 275, 50, 50);
g.fillOval(325, 75, 50, 50);
break;
case 5:
g.fillOval(125, 75, 50, 50);
g.fillOval(125, 275, 50, 50);
g.fillOval(225, 175, 50, 50);
g.fillOval(325, 275, 50, 50);
g.fillOval(325, 75, 50, 50);
break;
case 6:
g.fillOval(125, 75, 50, 50);
g.fillOval(125, 175, 50, 50);
g.fillOval(125, 275, 50, 50);
g.fillOval(325, 275, 50, 50);
g.fillOval(325, 175, 50, 50);
g.fillOval(325, 75, 50, 50);
break;
default:
break;
}
}
}
}
入出力結果(Terminal)
$ javac DiceService.java && java DiceService $
記念に。
0 コメント:
コメントを投稿