| 静岡理工科大学 | 総合情報学部 (by 菅沼) | 菅沼ホーム | 目次 | 演習解答例 | 付録 | 索引 |
java Test 10 20
和は 30
/****************************/
/* Window の生成 */
/* coded by Y.Suganuma */
/****************************/
import java.awt.*; // Frame も AWT の一種であるため必要
public class Test {
public static void main (String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Win win = new Win("整数の和", a, b);
}
}
/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends Frame {
/******************/
/* コンストラクタ */
/******************/
Win (String name, int a, int b)
{
// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(300, 200);
// ウィンドウを表示
setVisible(true);
// 出力
System.out.println("和は = " + (a + b));
}
}
/****************************/
/* Window の生成と消滅 */
/* coded by Y.Suganuma */
/****************************/
import java.awt.*; // Frame も AWT の一種であるため必要
import java.awt.event.*; // イベント処理を行う場合に必要
public class Test {
public static void main (String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Win win = new Win("整数の和", a, b);
}
}
/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends Frame implements WindowListener {
/******************/
/* コンストラクタ */
/******************/
Win (String name, int a, int b)
{
// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(300, 200);
// ウィンドウを表示
setVisible(true);
// イベントの登録
addWindowListener(this);
// 出力
System.out.println("和は = " + (a + b));
}
/************/
/* 終了処理 */
/************/
public void windowClosing(WindowEvent e) {
System.exit(0);
}
/********************************/
/* イベントリスナの他のメソッド */
/********************************/
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
}
/****************************/
/* Window の生成と消滅 */
/* coded by Y.Suganuma */
/****************************/
import java.awt.*; // Frame も AWT の一種であるため必要
import java.awt.event.*; // イベント処理を行う場合に必要
public class Test {
public static void main (String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Win win = new Win("整数の和", a, b);
}
}
/*******************/
/* クラスWinの定義 */
/*******************/
class Win extends Frame {
/******************/
/* コンストラクタ */
/******************/
Win (String name, int a, int b)
{
// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
super(name);
// Windowの大きさ
setSize(300, 200);
// ウィンドウを表示
setVisible(true);
// イベントの登録
addWindowListener(new WinEnd());
// 出力
System.out.println("和は = " + (a + b));
}
/************/
/* 終了処理 */
/************/
class WinEnd extends WindowAdapter
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
Main-Class: Test
jar cvfm window.jar manifest.txt *.class
jar [オプション] JARファイル [マニフェストファイル] [圧縮するファイル ・・・]
java -jar window.jar
<object type="application/x-java-applet" width="600" height="400"> <param name="code" value="Ball.class"> <param name="codebase" value="applet/" > </object>
jar cvf Ball.jar *.class
<object type="application/x-java-applet" width="600" height="400"> <param name="codebase" value="applet/" > <param name="archive" value="Ball.jar" > <param name="code" value="Ball" > </object>
import java.io.*;
import java.applet.*;
public class Test extends Applet {
public void init()
{
int a, b;
a = Integer.parseInt(getParameter("x"));
b = Integer.parseInt(getParameter("y"));
System.out.println("和は " + (a+b));
}
}
01 import java.awt.*;
02 import java.applet.*;
03
04 public class Test_1 extends Applet {
05 int wa;
06 // 初期設定(計算)
07 public void init()
08 {
09 int a, b;
10 a = Integer.parseInt(getParameter("x"));
11 b = Integer.parseInt(getParameter("y"));
12 wa = a + b;
13 setBackground(Color.white);
14 }
15 // 描画
16 public void paint (Graphics g)
17 {
18 Font f = new Font("TimesRoman", Font.BOLD, 30); // 30ポイント
19 g.setFont(f); // フォントの設定
20 g.drawString("和は " + wa + " です", 10, 30); // 文字列と開始位置(左下)
21 g.setColor(new Color(0, 0, 255)); // 色の設定
22 g.fillRect(40, 40, 50, 50); // 塗りつぶした正方形(左上,幅,高さ)
23 g.setColor(Color.red); // 色の設定
24 g.drawOval(115, 40, 50, 50); // 円(左上,幅,高さ)
25 }
26 }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class Test_2 extends Applet implements MouseMotionListener {
ArrayList <Point> v = new ArrayList <Point> ();
// 初期設定(計算)
public void init()
{
setBackground(Color.white);
addMouseMotionListener(this);
}
// マウスイベントの処理
public void mouseDragged(MouseEvent e)
{
v.add(new Point(e.getX(), e.getY()));
repaint();
}
public void mouseMoved(MouseEvent e)
{
int sw = 0;
if (v.size() > 0) {
Point p = (Point)v.get(v.size()-1);
if (p.x >= 0)
v.add(new Point(-1, -1));
}
}
// 描画
public void paint (Graphics g)
{
int i1;
Point p1, p2;
if (v.size() > 0) {
p1 = (Point)v.get(0);
for (i1 = 1; i1 < v.size(); i1++) {
p2 = (Point)v.get(i1);
if (p1.x >= 0 && p2.x >= 0)
g.drawLine(p1.x, p1.y, p2.x, p2.y);
p1 = p2;
}
}
}
}
001 import java.awt.*;
002 import java.awt.event.*;
003 import java.applet.*;
004 import java.util.*;
005
006 public class Key extends Applet implements Runnable
007 {
008 boolean state = true, fire = false, game = true;
009 int xt, yt, vx, vy, x, y, r = 25, sp = 5, target = 0;
010 Dimension d;
011 Thread th;
012 Random rn;
013
014 public void init() {
015 // 背景色
016 setBackground(new Color(238, 255, 238));
017 // 初期設定
018 d = getSize();
019 x = d.width / 2 - 10;
020 y = d.height - 20;
021 rn = new Random();
022 // キーリスナの付加
023 addKeyListener(new Key_e());
024 // スレッドの生成
025 th = new Thread(this);
026 th.start();
027 }
028
029 public Insets getInsets()
030 {
031 return new Insets(0, 0, 0, 0);
032 }
033
034 public void stop()
035 {
036 state = false;
037 }
038
039 public void run()
040 {
041 double ang, x1, y1;
042
043 while (state) {
044 try {
045 th.sleep(33);
046 }
047 catch (InterruptedException e) {}
048 // ターゲットの移動と消滅
049 if (target == 1) {
050 // ゲームオーバ
051 x1 = xt + r - (x + 10);
052 y1 = yt + r - (y + 10);
053 if (Math.sqrt(x1*x1+y1*y1) < r+10)
054 game = false;
055 // 命中
056 else if (fire && xt <= x+10 && xt > x+10-2*r)
057 target = 2;
058 // 命中しない
059 else {
060 xt += vx;
061 yt += vy;
062 if (xt < -2*r || xt > d.width || yt > d.height)
063 target = 0;
064 }
065 }
066 // ターゲットの生成
067 else if (target == 0){
068 target = 1;
069 xt = (int)((d.width - 2 * r) * rn.nextDouble());
070 yt = -r;
071 ang = 0.5 * Math.PI * rn.nextDouble() + 0.25 * Math.PI;
072 vx = (int)(sp * Math.cos(ang));
073 vy = (int)(sp * Math.sin(ang));
074 }
075 // 再描画
076 repaint();
077 }
078 }
079
080 public void paint (Graphics g)
081 {
082 // ゲーム中
083 if (game) {
084 g.fill3DRect(x, y, 20, 20, true);
085 // ターゲットの表示
086 if (target > 0) {
087 if (target == 1)
088 g.setColor(Color.green);
089 else {
090 g.setColor(Color.pink);
091 target = 0;
092 }
093 g.fillOval(xt, yt, 2*r, 2*r);
094 }
095 // レーザ砲の発射
096 if (fire){
097 g.setColor(Color.red);
098 g.drawLine(x+9, 0, x+9, d.height-10);
099 g.drawLine(x+10, 0, x+10, d.height-10);
100 g.drawLine(x+11, 0, x+11, d.height-10);
101 fire = false;
102 }
103 }
104 // ゲームオーバ
105 else {
106 Font f = new Font("TimesRoman", Font.BOLD, 50);
107 g.setFont(f);
108 g.drawString("Game Over", d.width/2-130, d.height/2);
109 }
110 }
111
112 public boolean isFocusable() { return true; }
113
114 class Key_e extends KeyAdapter {
115 public void keyPressed(KeyEvent e)
116 {
117 if (e.getKeyCode() == 37) // 左矢印
118 x -= 20;
119 else if (e.getKeyCode() == 39) // 右矢印
120 x += 20;
121 if (e.isShiftDown()) // Shift キー
122 fire = true;
123 }
124 }
125 }
01 import java.awt.*;
02 import java.awt.event.*;
03 import java.applet.*;
04
05 public class Test extends Applet implements ActionListener {
06
07 Button bt;
08 TextField tx1, tx2, tx3;
09
10 /************/
11 /* 初期設定 */
12 /************/
13 public void init()
14 {
15 // レイアウトの変更(フローレイアウト)
16 setLayout(new FlowLayout(FlowLayout.CENTER));
17 // フォントと背景色の設定
18 Font f = new Font("MS 明朝", Font.PLAIN, 20);
19 setFont(f);
20 setBackground(Color.cyan);
21 // テキストフィールドとボタンの追加
22 // テキストフィールド
23 tx1 = new TextField(10);
24 tx1.setBackground(Color.white);
25 add(tx1);
26 // ラベル
27 Label lb = new Label("+");
28 lb.setBackground(Color.cyan);
29 add(lb);
30 // テキストフィールド
31 tx2 = new TextField(10);
32 tx2.setBackground(Color.white);
33 add(tx2);
34 // ボタン
35 bt = new Button("=");
36 bt.setBackground(Color.pink);
37 bt.addActionListener(this); // リスナー
38 add(bt);
39 // テキストフィールド
40 tx3 = new TextField(10);
41 tx3.setBackground(Color.white);
42 add(tx3);
43 }
44
45 /******************************/
46 /* 上,左,下,右の余白の設定 */
47 /******************************/
48 public Insets getInsets()
49 {
50 return new Insets(10, 10, 10, 10);
51 }
52
53 /******************************/
54 /* ボタンが押されたときの処理 */
55 /******************************/
56 public void actionPerformed(ActionEvent e)
57 {
58 int a, b;
59 if (e.getSource() == bt) {
60 a = Integer.parseInt(tx1.getText());
61 b = Integer.parseInt(tx2.getText());
62 tx3.setText(Integer.toString(a+b));
63 }
64 }
65 }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Ani_1 extends Applet implements Runnable, ActionListener
{
boolean state;
Button b_start, b_stop;
D_Panel dp;
Thread th;
public void init() {
// レイアウト,背景色,フォント
setLayout(new BorderLayout(5, 10));
setBackground(new Color(225, 255, 225));
setFont(new Font("TimesRoman", Font.BOLD, 20));
// 上のパネル(ボタンの設定)
Panel pn1 = new Panel();
pn1.setLayout(new FlowLayout(FlowLayout.CENTER));
add(pn1, BorderLayout.NORTH);
b_start = new Button("開始");
b_start.addActionListener(this);
pn1.add(b_start);
b_stop = new Button("停止");
b_stop.addActionListener(this);
pn1.add(b_stop);
// 中央のパネル(描画領域)
dp = new D_Panel();
add(dp, BorderLayout.CENTER);
// スレッドの生成
th = new Thread(this);
}
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
public void stop() // 他ページへ移動の際,一時的にスレッドを停止
{
state = false;
}
public void run()
{
while (state) {
dp.count++;
if (dp.count > 10)
dp.count = 0;
dp.repaint(); // 再描画
try {
th.sleep(100); // 100 ms 毎の描画
}
catch (InterruptedException e) {}
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b_start) { // 開始
state = true;
th.start();
}
else { // 停止
state = false;
th = new Thread(this);
dp.count = 0;
dp.repaint();
}
}
}
class D_Panel extends Panel
{
int count;
D_Panel()
{
setBackground(Color.white);
count = 0;
}
public void paint (Graphics g) // 描画
{
int i1, r;
r = 10;
for (i1 = 0; i1 < count; i1++) {
g.drawOval(0, 0, 2*r, 2*r);
r = (int)(1.5 * r);
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Ani_2 extends Applet implements Runnable
{
private boolean state = true;
private double g = 9.8;
private double v0 = 0;
private double v = 0;
private double t = 0;
private double h0, x, y;
private int sw = 1;
private Dimension d;
private Thread th;
public void init() {
// 背景色
setBackground(new Color(238, 255, 238));
// 初期設定
d = getSize();
h0 = d.height + 40;
x = -40;
y = -40;
addMouseListener(new Mouse());
// スレッドの生成
th = new Thread(this);
th.start();
}
public Insets getInsets()
{
return new Insets(0, 0, 0, 0);
}
public void stop() // 他ページへ移動の際,一時的にスレッドを停止
{
state = false;
}
public void run()
{
while (state) {
try {
th.sleep(33);
}
catch (InterruptedException e) {}
if (x < d.width + 80 && sw > 0) {
x += 1.5;
t += 0.1;
v = -g * t + v0;
y = d.height - (-0.5 * g * t * t + v0 * t + h0);
if (y >= d.height - 80 && v < 0) {
y = d.height - 80;
v0 = -0.8 * v;
h0 = 80;
t = 0;
}
System.out.println("position " + x + " " + y);
repaint();
}
}
}
public void paint (Graphics g)
{
g.setColor(Color.green);
g.fillOval((int)x, (int)y, 80, 80);
}
class Mouse extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
int mx, my;
double x1, y1, r;
mx = e.getX();
my = e.getY();
x1 = x + 40 - mx;
y1 = y + 40 - my;
r = Math.sqrt(x1 * x1 + y1 * y1);
if (r < 40) {
if (sw > 0)
sw = 0;
else
sw = 1;
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Ani_3 extends Applet implements Runnable
{
private boolean state = true;
private double g = 9.8;
private double v0 = 0;
private double v = 0;
private double t = 0;
private double h0, x, y;
private int sw = 1;
private Dimension d;
private Thread th;
private Image im;
public void init() {
// 背景色
setBackground(new Color(238, 255, 238));
// 初期設定
d = getSize();
h0 = d.height + 40;
x = -40;
y = -40;
im = getImage(getCodeBase(), "ball.gif");
addMouseListener(new Mouse());
// スレッドの生成
th = new Thread(this);
th.start();
}
public Insets getInsets()
{
return new Insets(0, 0, 0, 0);
}
public void stop()
{
state = false;
}
public void run()
{
while (state) {
try {
th.sleep(33);
}
catch (InterruptedException e) {}
if (x < d.width + 80 && sw > 0) {
x += 1.5;
t += 0.1;
v = -g * t + v0;
y = d.height - (-0.5 * g * t * t + v0 * t + h0);
if (y >= d.height - 80 && v < 0) {
y = d.height - 80;
v0 = -0.8 * v;
h0 = 80;
t = 0;
}
System.out.println("position " + x + " " + y);
repaint();
}
}
}
public void paint (Graphics g)
{
g.drawImage(im, (int)x, (int)y, this);
}
class Mouse extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
int mx, my;
double x1, y1, r;
mx = e.getX();
my = e.getY();
x1 = x + 40 - mx;
y1 = y + 40 - my;
r = Math.sqrt(x1 * x1 + y1 * y1);
if (r < 40) {
if (sw > 0)
sw = 0;
else
sw = 1;
}
}
}
}
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.util.Random;
public class Ani_4 extends Applet implements Runnable
{
private int max = 20; // 花火の数
private int m_pr = 7; // 打ち上げ間隔の最大値
private int m_cl = 10; // 花火の色の最大値
private int f_l = 300; // 花火の直径
private int count = 0, next = 0, height, width, size, f_size, k[], x[], y[], pixels[][], color[], cl[];
private boolean state = true;
private Thread th;
private MemoryImageSource mis[];
private Image im[];
private Random rn;
public void init() {
int i1;
// 背景色
setBackground(new Color(0, 0, 0));
// 初期設定
width = getSize().width;
height = getSize().height;
size = width * height;
f_size = f_l * f_l;
pixels = new int [max][f_size];
k = new int [max];
x = new int [max];
y = new int [max];
cl = new int [max];
mis = new MemoryImageSource [max];
im = new Image [max];
for (i1 = 0; i1 < max; i1++)
k[i1] = -1;
rn = new Random();
color = new int [m_cl];
color[0] = 0xffff0000;
color[1] = 0xff00ff00;
color[2] = 0xff0000ff;
color[3] = 0xffffff00;
color[4] = 0xffff00ff;
color[5] = 0xff00ffff;
color[6] = 0xffeeffee;
color[7] = 0xffffaaaa;
color[8] = 0xffaaffaa;
color[9] = 0xffaaaaff;
// スレッドの生成
th = new Thread(this);
th.start();
}
public Insets getInsets()
{
return new Insets(0, 0, 0, 0);
}
public void stop()
{
state = false;
}
public void run()
{
double ang, s;
int i0, i1, i2, i3, kx, ky, kxy, sw;
while (state) {
try {
th.sleep(200);
}
catch (InterruptedException e) {}
sw = 0;
for (i0 = 0; i0 < max; i0++) {
if (k[i0] < 0) {
if (count >= next && sw == 0) {
sw = 1;
count = 0;
cl[i0] = (int)(m_cl * rn.nextDouble());
if (cl[i0] >= m_cl)
cl[i0] = m_cl - 1;
for (i1 = 0; i1 < f_size; i1++)
pixels[i0][i1] = 0x00000000;
x[i0] = (int)(width * rn.nextDouble()) - f_l / 2;
y[i0] = (int)(height * rn.nextDouble()) - f_l / 2;
k[i0] = 0;
mis[i0] = new MemoryImageSource(f_l, f_l, pixels[i0], 0, f_l);
mis[i0].setAnimated(true);
im[i0] = createImage(mis[i0]);
next = (int)(m_pr * rn.nextDouble());
if (next <= 0)
next = 1;
}
}
else {
k[i0]++;
if (k[i0] > m_pr)
k[i0] = -1;
else {
s = Math.PI / 6;
ang = 0;
for (i1 = 0; i1 < 12; i1++) {
kx = f_l / 2 + (int)(20 * k[i0] * Math.cos(ang));
ky = f_l / 2 + (int)(20 * k[i0] * Math.sin(ang));
for (i2 = kx-5; i2 < kx+5; i2++) {
for (i3 = ky-5; i3 < ky+5; i3++) {
kxy = f_l * i2 + i3;
if (kxy >= 0 && kxy < f_size)
pixels[i0][kxy] = color[cl[i0]];
}
}
pixels[i0][f_l*(kx-1)+ky-6] = color[cl[i0]];
pixels[i0][f_l*kx+ky-6] = color[cl[i0]];
pixels[i0][f_l*(kx+1)+ky-6] = color[cl[i0]];
pixels[i0][f_l*(kx-1)+ky+5] = color[cl[i0]];
pixels[i0][f_l*kx+ky+5] = color[cl[i0]];
pixels[i0][f_l*(kx+1)+ky+5] = color[cl[i0]];
pixels[i0][f_l*(kx-6)+ky-1] = color[cl[i0]];
pixels[i0][f_l*(kx-6)+ky] = color[cl[i0]];
pixels[i0][f_l*(kx-6)+ky+1] = color[cl[i0]];
pixels[i0][f_l*(kx+5)+ky-1] = color[cl[i0]];
pixels[i0][f_l*(kx+5)+ky] = color[cl[i0]];
pixels[i0][f_l*(kx+5)+ky+1] = color[cl[i0]];
ang += s;
}
im[i0] = createImage(mis[i0]);
}
}
}
count++;
repaint();
}
}
public void paint (Graphics g)
{
int i0;
for (i0 = 0; i0 < max; i0++) {
if (k[i0] >= 0)
g.drawImage(im[i0], x[i0], y[i0], this);
}
}
}
import java.awt.*;
import java.applet.*;
import java.util.Random;
public class Ani_5 extends Applet implements Runnable {
int w, h, x, y;
boolean win_state = true;
Image hana;
Thread th = null;
public void init()
{
// イメージの読み込み
hana = getImage(getCodeBase(), "hana.gif");
// スクリーンのサイズ
w = getSize().width;
h = getSize().height;
}
public void start()
{
th = new Thread(this);
th.start();
}
public void run()
{
// ランダム変数の初期化
Random rand = new Random();
// 点の生成と描画
while (win_state) {
try {
th.sleep(10);
}
catch (InterruptedException e) {}
x = (int)(rand.nextDouble() * w);
y = (int)(rand.nextDouble() * h);
repaint();
}
}
public void paint (Graphics g)
{
g.setColor(new Color(0, 255, 255));
g.drawImage(hana, 0, 0, this);
Font f = new Font("TimesRoman", Font.BOLD, 20);
g.setFont(f);
g.drawString("背景", 140, 50);
g.drawString("朝顔", 140, 80);
g.fillOval(x, y, 10, 10);
}
}
import java.awt.*;
import java.applet.*;
import java.util.Random;
public class Ani_6 extends Applet implements Runnable {
int w, h;
boolean win_state = true;
Image hana, Buf;
Graphics g_b;
Thread th = null;
public void init()
{
// イメージの読み込み
hana = getImage(getCodeBase(), "hana.gif");
MediaTracker trk = new MediaTracker(this);
trk.addImage(hana, 0);
try {
trk.waitForID(0);
}
catch (InterruptedException e) {}
// スクリーンバッファの生成
w = getSize().width;
h = getSize().height;
Buf = createImage(w, h); //バッファ生成
g_b = Buf.getGraphics(); //グラフィックコンテキスト取得
}
public void start()
{
th = new Thread(this);
th.start();
}
public void run()
{
int x, y;
// ランダム変数の初期化と色の設定
Random rand = new Random();
g_b.setColor(new Color(0, 255, 255));
// 背景画像の生成
Image Back = createImage(w, h);
Graphics g = Back.getGraphics();
g.drawImage(hana, 0, 0, this);
Font f = new Font("TimesRoman", Font.BOLD, 20);
g.setFont(f);
g.drawString("背景", 140, 50);
g.drawString("朝顔", 140, 80);
g.dispose(); //グラフィックコンテキスト破棄
while (win_state) {
try {
th.sleep(10);
}
catch (InterruptedException e) {}
g_b.drawImage(Back, 0, 0, this); //背景画像をバッファに描画
x = (int)(rand.nextDouble() * w);
y = (int)(rand.nextDouble() * h);
g_b.fillOval(x, y, 10, 10); // 円をバッファに描画
repaint();
}
}
public void paint (Graphics g)
{
g.drawImage(Buf, 0, 0, this); //バッファを画面に描画
}
public void update(Graphics g) //オーバーライドして最低限のことだけをする
{
paint(g);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class Net_1 extends Applet implements ActionListener
{
Button bt[] = new Button [2];
URL url[] = new URL [2];
public void init() {
Font f = new Font("TimesRoman", Font.BOLD, 20);
setFont(f);
// ボタンの設定
bt[0] = new Button(getParameter("URL1_name"));
bt[0].addActionListener(this);
add(bt[0]);
url[0] = URL_List(getParameter("URL1"));
bt[1] = new Button(getParameter("URL2_name"));
bt[1].addActionListener(this);
add(bt[1]);
url[1] = URL_List(getParameter("URL2"));
}
// URL の設定
public URL URL_List(String name)
{
URL ul = null;
try {
ul = new URL(name);
}
catch (MalformedURLException e)
{
System.out.println("Bad URL: " + name);
}
return ul;
}
// ボタンがクリックされたときの処理
public void actionPerformed(ActionEvent e)
{
URL link = null;
for (int i1 = 0; i1 < bt.length && link == null; i1++) {
if (e.getSource() == bt[i1])
link = url[i1];
}
if (link != null) {
System.out.println(link + " に接続中\n");
getAppletContext().showDocument(link); // リンク
}
}
}
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class Net_2 extends Applet implements Runnable
{
URL url;
Thread th;
TextArea ta;
boolean state = true;
public void init() {
// 読み込むファイル名
String name = getParameter("FILE");
// 読み込むファイルの設定
try {
url = new URL(getCodeBase(), name);
}
catch (MalformedURLException e)
{
System.out.println("Bad URL: " + name);
}
// テキストエリアの追加
Font f = new Font("TimesRoman", Font.BOLD, 20);
setFont(f);
ta = new TextArea(15, 50);
add(ta);
}
// 周囲の余白
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
// スレッドのスタート
public void start()
{
if (th == null) {
th = new Thread(this);
th.start();
}
}
// スレッドの停止
public void stop()
{
state = false;
}
public void run()
{
InputStream stm = null;
// URLConnectionクラスを使用する場合
// URLConnection c_inURL = null;
BufferedReader in = null;
String line;
StringBuffer buf = new StringBuffer();
while (state) {
try {
Thread.currentThread().sleep(100); // 100msスリープ
}
catch (InterruptedException e) {};
try {
// URL への接続
stm = this.url.openStream();
// URLConnectionクラスを使用する場合(上の1行は削除)
// c_inURL = url.openConnection();
// c_inURL.setDoInput(true);
// c_inURL.setDoOutput(false);
// c_inURL.setUseCaches(false);
// c_inURL.connect();
// stm = c_inURL.getInputStream();
// ファイルから 1 行ずつ読み込み
in = new BufferedReader(new InputStreamReader(stm));
while ((line = in.readLine()) != null)
buf.append(line + "\n");
// テキストエリアに出力
ta.setText(buf.toString());
state = false;
in.close();
}
catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class Net_3 extends Applet implements Runnable, ActionListener
{
URL url;
Thread th;
TextArea ta = new TextArea(15, 50);
boolean state = true;
boolean submit_sw = false;
Button soshin;
public void init() {
// CGIの設定
String name = "test.cgi"; // PHPの場合: String name = "test.php";
try {
url = new URL(getCodeBase(), name);
}
catch (MalformedURLException e)
{
System.out.println("Bad URL: " + name);
}
// テキストエリアの追加
Font f = new Font("TimesRoman", Font.BOLD, 20);
setFont(f);
add(ta);
// ボタンの追加
soshin = new Button("送信");
add(soshin);
soshin.addActionListener(this);
}
// 周囲の余白
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
// スレッドのスタート
public void start()
{
if (th == null) {
th = new Thread(this);
th.start();
}
}
// スレッドの停止
public void stop()
{
state = false;
}
// ボタンが押されたときの処理
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == soshin)
submit_sw = true;
}
public void run()
{
URLConnection c_URL;
OutputStream o_stm = null;
PrintStream out = null;
InputStream i_stm = null;
BufferedReader in = null;
String line;
StringBuffer buf = new StringBuffer();
while (state) {
try {
Thread.currentThread().sleep(100); // 100msスリープ
}
catch (InterruptedException e) {};
if (submit_sw) {
try {
line = ta.getText();
// データが入力されたことの確認
if (line.equals("")) {
ta.setText("データを入れて下さい\n");
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {};
ta.setText("");
}
else {
// CGIへ接続
c_URL = url.openConnection();
c_URL.setDoInput(true);
c_URL.setDoOutput(true);
c_URL.setUseCaches(false);
c_URL.connect();
// データをCGIへ送信
o_stm = c_URL.getOutputStream();
out = new PrintStream(o_stm);
// line = "trans=" + line; PHP の場合はこの行が必要
out.print(line);
out.close();
// CGIからのメッセージを受け取る
i_stm = c_URL.getInputStream();
in = new BufferedReader(new InputStreamReader(i_stm));
while ((line = in.readLine()) != null)
buf.append(line + "\n");
// メッセージをテキストエリアに出力
ta.setText(buf.toString());
in.close();
state = false;
}
submit_sw = false;
}
catch (UnknownServiceException e) {
ta.setText("Unknown Service: " + e.getMessage());
}
catch (IOException e) {
ta.setText("IO Error: " + e.getMessage());
}
}
}
}
}
#!/bin/sh echo Content-type: text/plain # HTTP header echo # HTTP header umask 000 cat > data umask 022 echo End of Submit
#!/usr/local/bin/perl
#
#
# 出力データファイルを指定
#
$datafile = "data";
#
# 標準入力データ読込
#
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#
# ファイルのオープン
#
if (!open(OUT, ">$datafile")) {
print "Content-type: text/plain\n\n"; # オープンに失敗した場合
print "Writing Error\n"; # 書込失敗メッセージ送出
exit(0);
}
#
# データファイル書込
#
flock(OUT, 2); # ファイルをロック
print OUT "$buffer\n"; # データを書き出す
print OUT "\n"; # データを書き出す
flock(OUT, 8);
close(OUT); # ファイルをクローズ
print "Content-type: text/plain\n\n"; # HTTPヘッダの送信
# (これから送る情報の種類)
print "データの送信を終了しました\n"; # 書込完了メッセージ送出
exit(0);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
FILE *out;
out = fopen("data", "w");
while ((c = getchar()) != EOF)
fputc(c, out);
fclose(out);
printf("Content-type: text/plain\n\n"); // HTTPヘッダの送信
printf("データの送信を終了しました\n");
return 0;
}
<?php
// 送信されてきたデータを受け取る(「\n」で分離)
$k = 0;
$x = strtok($_POST['trans'], "\n");
while ($x) {
$p[$k] = $x;
$x = strtok("\n");
$k++;
}
// ファイルへ出力
$out = fopen("data", "wb");
for ($i1 = 0; $i1 < $k; $i1++)
fwrite($out, $p[$i1]);
fclose($out);
// メッセージの送信
printf("データの送信を終了しました\n");
?>
#!/usr/local/bin/php

<object type="application/x-java-applet" width="600" height="400"> <param name="code" value="Ball.class"> <param name="data" value="1,2,・・・" > </object>
/************************/
/* 2次方程式を解く */
/************************/
public void solve()
{
double a = dt.a, b = dt.b, c = dt.c, D, x, x1, x2;
String str;
/*
一次方程式 or 解なし
*/
if (Math.abs(a) <= 1.0e-10) {
if (Math.abs(b) <= 1.0e-10)
text.insert("解を求めることができません!\n", 0);
else {
x = -c / b;
str = "x = " + Double.toString(x) + "\n";
text.insert(str, 0);
}
}
/*
二次方程式
*/
else {
D = b * b - 4.0 * a * c;
// 実根
if (D >= 0.0) {
D = Math.sqrt(D);
x1 = 0.5 * (-b - D) / a;
x2 = 0.5 * (-b + D) / a;
str = "x = " + Double.toString(x1) + ", " + Double.toString(x2) + "\n";
text.insert(str, 0);
}
// 虚根
else {
D = Math.sqrt(-D);
x1 = -0.5 * b / a;
x2 = 0.5 * D / a;
str = "x = " + Double.toString(x1) + " ± i " + Double.toString(x2) + "\n";
text.insert(str, 0);
}
}
}




| 静岡理工科大学 | 総合情報学部 (by 菅沼) | 菅沼ホーム | 目次 | 演習解答例 | 付録 | 索引 |