/****************************/
/* 様々な図形 */
/* coded by Y.Suganuma */
/****************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public static void main (String[] args)
{
Win win = new Win("図形の描画");
}
}
class Win extends JFrame {
/******************/
/* コンストラクタ */
/******************/
Win(String name)
{
// JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(300, 170);
// 背景色
setBackground(new Color(255, 255, 255));
// 描画パネル
DrawPanel pn = new DrawPanel();
getContentPane().add(pn);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
}
/******************************/
/* 上,左,下,右の余白の設定 */
/******************************/
public Insets getInsets()
{
return new Insets(50, 20, 20, 20);
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
// DrawPanel
class DrawPanel extends JPanel
{
// 描画
public void paint (Graphics g)
{
Font f = new Font("TimesRoman", Font.PLAIN, 15);
g.setFont(f);
g.drawOval(10, 25, 80, 50);
g.drawString("楕円", 35, 55);
g.drawRect(120, 25, 50, 50);
g.drawString("正方形", 123, 55);
int x[] = {225, 250, 200};
int y[] = {25, 75, 75};
int pt = x.length;
g.drawPolygon(x, y, pt);
f = new Font("TimesRoman", Font.PLAIN, 10);
g.setFont(f);
g.drawString("三角形", 210, 70);
}
}
/****************************/
/* 加減算の練習 */
/* coded by Y.Suganuma */
/****************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Test {
public static void main (String[] args)
{
Win win = new Win("整数の加算・減算");
}
}
class Win extends JFrame implements ActionListener {
JButton bt;
JTextField tx;
JTextArea ta;
JLabel lbl;
Random rn;
int a, b, op;
/******************/
/* コンストラクタ */
/******************/
Win(String name)
{
// JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(410, 210);
// フォント,背景色,乱数初期設定の設定
Container cp = getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.CENTER));
Font f = new Font("MS 明朝", Font.BOLD, 20);
cp.setFont(f);
cp.setBackground(new Color(225, 255, 225));
rn = new Random();
// 1行目
// 問題
String str = problem();
lbl = new JLabel(str);
lbl.setFont(f);
cp.add(lbl);
// テキストフィールド(答え)
tx = new JTextField(5);
tx.setFont(f);
cp.add(tx);
// ボタン
cp.add(new JLabel(" "));
bt = new JButton("チェック");
bt.setBackground(Color.pink);
bt.addActionListener(this); // リスナー
bt.setFont(f);
cp.add(bt);
// 2行目
// テキストエリア
ta = new JTextArea(3, 25);
ta.setFont(f);
cp.add(ta);
// ウィンドウを表示
setVisible(true);
// イベントアダプタ
addWindowListener(new WinEnd());
}
/******************************/
/* 上,左,下,右の余白の設定 */
/******************************/
public Insets getInsets()
{
return new Insets(50, 20, 20, 20);
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/**************/
/* 問題の設定 */
/**************/
String problem()
{
String str = "";
// a
a = -50 + (int)(rn.nextDouble() * 100);
if (a < 0)
str += "-";
str += Integer.toString(Math.abs(a));
// 演算子
if (rn.nextDouble() < 0.5) {
op = -1;
str += " - ";
}
else {
op = 1;
str += " + ";
}
// b
b = -50 + (int)(rn.nextDouble() * 100);
if (b < 0)
str += "(-";
str += Integer.toString(Math.abs(b));
if (b < 0)
str += ")";
str += " =";
return str;
}
/******************************/
/* ボタンが押されたときの処理 */
/******************************/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bt) {
int c, d;
if (tx.getText().length() <= 0)
ta.setText("答えを入力して下さい!");
else {
c = Integer.parseInt(tx.getText());
if (op > 0)
d = a + b;
else
d = a - b;
if (d == c) {
ta.setText("正解です!\n新しい問題をやって下さい");
lbl.setText(problem());
tx.setText("");
}
else
ta.setText("間違っています!\nもう一度計算して下さい");
}
}
}
}
| 菅沼ホーム | 演習解答例目次 | 本文目次 | 付録 | 索引 |