
Since 2003.05.21
前回のプログラムで、星印は点滅していますが、「輝く」という感じはしません。
今回はこの点を改良します。
この為に星のイメージを十字形と×印に分解し、順番に表示させます。
2つにファイルを用意します。前回のStar30.pngと同じように、背景を透過色にしてあります。右クリックして「名前をつけて画像を保存」で「images」フォルダに保存します。
![]() |
![]() |
Plus30.png |
Cross30.png |
switch文
変更するのは、追加した2つのイメージの取得と、Paintメソッドで背景だけの画像を含めて4画像を順次表示させることの、2箇所です。
コードは今までと同じように、このページの一番下に表示します。
10行目で、4状態を区分できるように、maxstageを1から3に増やしています。
15,16行目、Imageを2追加。
57-62行目、追加した2つのイメージの取得。
以上については新しいことはありません。
Paintメソッドで背景だけの画像を含めて4画像を順次表示させる33-46行目では、switch文を使っています。
ここは、if else文を使って次のように書くこともできます。
g.drawImage(backImage, 0, 0, this);
if (stage == 0) { }
else if (stage == 1) {
g.drawImage(crossImage, 0, 0, this);
}
else if (stage == 2) {
g.drawImage(plusImage, 0, 0, this);
}
else {
g.drawImage(starImage, 0, 0, this);
}
今回のように、整数型の値によって処理を変える場合は、if else文よりはswitch文を使ったほうが、プログラムがすっきりします。
switch文の構文は次のとおりです。
switch (条件式) {
case 定数a:
条件式が定数aのとき実行する文の並び
break;
case 定数b:
条件式が定数bのとき実行する文の並び
break;
default:
条件式がいずれにも該当しないとき実行する文の並び
break;
}
条件式は、整数型(int,short,char,byte)を返すものでなければなりません。整数型であっても、longは使えません。また、boolean,float,doubleも使えません。
case節はいくつ並べてもかまいません。
default節は、実行する文がなければ省略できます。
java/net/sys5jp/basic/Star4.java
1 package net.sys5jp.basic;
2
3 import java.applet.*;
4 import java.awt.*;
5
6 public class Star4 extends Applet implements Runnable {
7 boolean runnable = true;
8 boolean running = false;
9 Thread timer;
10 int maxstage = 3;
11 int stage = 0;
12 long sleep = 50;
13 Color backColor = new Color(0, 0, 0);
14 Image starImage;
15 Image plusImage;
16 Image crossImage;
17 Image backImage;
18 Dimension dim;
19
20 private Color getColorParameter(String param) {
21 String value = getParameter(param);
22 try { return new Color(Integer.parseInt(value, 16)); }
23 catch(Exception e) { return null; }
24 }
25
26 private int getIntParameter(String param) {
27 String value = getParameter(param);
28 try { return Integer.parseInt(value, 10); }
29 catch(Exception e) { return 0; }
30 }
31
32 public void paint(Graphics g) {
33 g.drawImage(backImage, 0, 0, this);
34 switch (stage) {
35 case 0:
36 break;
37 case 1:
38 g.drawImage(crossImage, 0, 0, this);
39 break;
40 case 2:
41 g.drawImage(plusImage, 0, 0, this);
42 break;
43 default:
44 g.drawImage(starImage, 0, 0, this);
45 break;
46 }
47 }
48
49 public void init() {
50 Color color;
51 if((color = getColorParameter("backcolor")) != null) {
52 backColor = color;
53 }
54 try { starImage
55 = getImage(getDocumentBase(), getParameter("starimage")); }
56 catch(Exception e) { runnable = false; }
57 try { plusImage
58 = getImage(getDocumentBase(), getParameter("plusimage")); }
59 catch(Exception e) { runnable = false; }
60 try { crossImage
61 = getImage(getDocumentBase(), getParameter("crossimage")); }
62 catch(Exception e) { runnable = false; }
63 int cc;
64 if((cc = getIntParameter("speed")) > 0) {
65 sleep = 1000 / cc;
66 }
67 setBackground(backColor);
68 dim = getSize();
69 backImage = createImage(dim.width, dim.height);
70 Graphics bg = backImage.getGraphics();
71 bg.setColor(backColor);
72 bg.fillRect(0, 0, dim.width, dim.height);
73 }
74
75 public void run() {
76 while(running) {
77 repaint();
78 if (stage >= maxstage) { stage = 0; }
79 else { stage++; }
80 try { Thread.sleep(sleep); }
81 catch (InterruptedException e) {}
82 }
83 timer = null;
84 }
85
86 public void start() {
87 if (runnable) {
88 running = true;
89 if (timer == null) {
90 timer = new Thread(this);
91 timer.start();
92 }
93 }
94 }
95
96 public void stop() { running = false; }
97
98 }
java/html/Star4.html
<html>
<head>
<style>
body {background-color:rgb(0, 0, 0);
color:rgb(255, 255, 255)}
table {text-align:center;
margin:10px 10px}
td {padding:5px 10px}
</style>
</head>
<body>
<table>
<tr><td>
<applet code="net/sys5jp/basic/Star3.class"
codebase="../"
width=30 height=30>
<param name="starimage" value="../images/Star30.png">
</applet>
</td><td>
<applet code="net/sys5jp/basic/Star4.class"
codebase="../"
width=30 height=30>
<param name="starimage" value="../images/Star30.png">
<param name="plusimage" value="../images/Plus30.png">
<param name="crossimage" value="../images/Cross30.png">
</applet>
</td></tr>
<tr><td>
Star3.class
</td><td>
Star4.class
</td></tr>
</table>
</body>
</html>
| トップヘ | 目次ヘ | 前ヘ | 次ヘ |
Copyright (C) 2003 System Five Japan (Mori). All Rights Reserved
Java およびそのほか Java を含む標章は、米国 Sun Microsystems, Inc.の商標です