Java Appletの解説

<20.お絵かきソフト(その6)>

テキストフィールドに文字を入力して、それを画面上に表示する機能を追加します。

1.文字入力

19.「お絵かきソフト例題2」を改善したものです。ボタン(bmt7に文字入力)、テキストフィールド、ラベルが増えています。
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

public class tpaint8 extends Applet{

Button bmt1,bmt2,bmt3,bmt4,bmt5,bmt6,bmt7;
Label pmeth,pstr;
TextField tf1;
String sp;

public void init(){

p1.setLayout(new GridLayout(6,2,0,0));
p1.add((bmt7=new Button("文字入力")));
p1.add(tf1=new TextField("",6));
p1.add(new Label("入力文字"));
p1.add(pstr=new Label("   "));

bmt7.addActionListener(new MyListener());
tf1.addActionListener(new StListener());

public void mousePressed(MouseEvent e){

if (chi==7){offg.drawString(sp,px1,py1);repaint();}

class MyListener implements ActionListener{

else if(e.getSource()==bmt7){plydisp(tadi);tadi=0;taka=0;chi=7; pmeth.setText("文字入力");sp="";pstr.setText(sp);tf1.setText("");tf1.requestFocus();}

class StListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
sp=t.getText();
pstr.setText(sp);
repaint();} }

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例1.ここをクリックして下さい。
2.<解説>
1)初期設定
1.Button bmt7;
文字入力のボタン設定。
2.Label pstr;
入力された文字を表示するラベル。
3.TextField tf1;String sp;
文字入力するテキストフィールドと入力された文字を格納する変数。
2)ボタン、ラベルの配置
1.p1.setLayout(new GridLayout(6,2,0,0));
6行、2列の配置。
2.p1.add((bmt7=new Button("文字入力")));
文字入力ボタン
3.p1.add(tf1=new TextField("",6));
6文字のテキストフィールド
4.p1.add(new Label("入力文字"));
p1.add(pstr=new Label("   "));
ラベルpstrに入力された文字を表示する。
3)リスナー宣言
bmt7.addActionListener(new MyListener());
tf1.addActionListener(new StListener());
4)マウスボタンが押された時の処理
if (chi==7){offg.drawString(sp,px1,py1);repaint();}
入力された文字(sp格納)をマウスの座標(px1,py1)に表示する。
5)方法の文字入力ボタンが押された時の処理
else if(e.getSource()==bmt7){plydisp(tadi);tadi=0;taka=0;chi=7; pmeth.setText("文字入力");sp="";pstr.setText(sp);tf1.setText("");tf1.requestFocus();}
sp="";pstr.setText(sp);入力文字を表示するラベルをクリアーする。
tf1.setText("");テキストフィールドの文字をクリアーする。
tf1.requestFocus();ボタンが押されればカーソルがくるようにする。
6)テキストフィールド入力処理
class StListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TextField t=(TextField)e.getSource();
sp=t.getText();
pstr.setText(sp);
repaint();} }

2.文字の大きさの選択

1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

public class tpaint9 extends Applet{

Choice moji;
String mojft="12";
int mojsz;
public void init(){

Panel p4=new Panel();
p4.setBackground(Color.lightGray);
p4.add(new Label("文字の大きさ"));
moji=new Choice();
moji.addItem("12");
moji.addItem("14");
moji.addItem("16");
moji.addItem("18");
moji.addItem("20");
p4.add(moji);

moji.addItemListener(new MojiListener());

public void mousePressed(MouseEvent e){

if (chi==7){mojsz=Integer.parseInt(mojft);
offg.setFont(new Font("TimesRoman",Font.BOLD,mojsz));
offg.drawString(sp,px1,py1);repaint();}

class MojiListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Choice c=(Choice)e.getSource();
mojft=c.getSelectedItem();
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例2.ここをクリックして下さい。
2.<解説>
1)初期設定
1.Choice moji;
文字の大きさを選ぶための選択ボックス
2.String mojft="12";
選ばれた文字を入れる変数。初期値12
3.int mojsz;
選ばれた文字を整数に置き換えて代入する変数。これで文字の大きさを決める。
2)選択ボックスの内容
p4のパネルに置きます。上の場所です。
1.moji=new Choice();
選択ボックスの宣言
2.moji.addItem("12");moji.addItem("14");moji.addItem("16");
moji.addItem("18");moji.addItem("20");
内容>12から20までの数字がある。
3.p4.add(moji);
3)リスナー宣言
moji.addItemListener(new MojiListener());
4)マウスボタンが押された時
1.if (chi==7){mojsz=Integer.parseInt(mojft);
選ばれた数字の文字(mojitf)を数字に変換してmojiszに代入
2.offg.setFont(new Font("TimesRoman",Font.BOLD,mojsz));
mojszの大きさの文字で表示する。
5)選択ボックスの処理
class MojiListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Choice c=(Choice)e.getSource();
mojft=c.getSelectedItem();
選択ボックスから選ばれた文字をmojftに代入。

3.移動表示

入力された文字をイメージ上に表示して、ドラッグで好きな場所に移動させる方法。
例1を元にして、改良してます。
1.<Javaファイル>
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

public class tpaint10 extends Applet{

Image testImage;
Graphics testg;
public void init(){

testg=getGraphics();
testImage=createImage(clo.width,clo.height);
testg=testImage.getGraphics();

public void mousePressed(MouseEvent e){

if (chi==7){testg.clearRect(0,0,getSize().width-1,getSize().height-1);
testg.drawString(sp,px1,py1);tadi=3;xcc1=px1-10;ycc1=py1-15;
cutfil=new CropImageFilter(px1-10,py1-15,60,20);
cutimg=createImage(new FilteredImageSource(testImage.getSource(),cutfil));
repaint();}
public void mouseReleased(MouseEvent e){

else if(chi==7){if(tadi==3){tadi=0;xcc1=0;ycc1=0;offg.drawString(sp,e.getX(),e.getY());repaint();}}

public void mouseDragged(MouseEvent e){

else if(chi==7){if (tadi==3){w1=(px2-oldx);h1=(py2-oldy);repaint();}}

−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
例3.ここをクリックして下さい。
2.<解説>
1)初期設定
Image testImage;Graphics testg;
Image testImage;入力した文字を貼り付けるイメージ Graphics testg;イメージを表示する画面
2)文字表示のためのオフスクリーン
testg=getGraphics();グラフィック・オブジェクトを得る。
testImage=createImage(clo.width,clo.height);オフスクリーン描画イメージを作成。
testg=testImage.getGraphics();上記の方法で作ったイメージのグラフィックオブジェクトを得る。
3)マウスボタンが押された時の処理
if (chi==7){testg.clearRect(0,0,getSize().width-1,getSize().height-1);
testg.drawString(sp,px1,py1);tadi=3;xcc1=px1-10;ycc1=py1-15;
cutfil=new CropImageFilter(px1-10,py1-15,60,20);
cutimg=createImage(new FilteredImageSource(testImage.getSource(),cutfil));
repaint();}
1.testg.clearRect(0,0,getSize().width-1,getSize().height-1);
文字を表示するtestg画面をクリアーする。
2.testg.drawString(sp,px1,py1); testg画面に文字を表示する。
3.tadi=3;イメージ移動の状態にする。
4.xcc1=px1-10;ycc1=py1-15;移動するイメージと文字のずれを修正する。
5.cutfil=new CropImageFilter(px1-10,py1-15,60,20);文字の表示してる画面を切り取る。
cutimg=createImage(new FilteredImageSource(testImage.getSource(),cutfil));
切り取った画像をcutimgに入れる。
4)マウスボタンが放された時の処理
else if(chi==7){if(tadi==3){tadi=0;xcc1=0;ycc1=0;offg.drawString(sp,e.getX(),e.getY());repaint();}}
tadi=0;xcc1=0;ycc1=0;各条件を初期状態にする。
offg.drawString(sp,e.getX(),e.getY());offg上の移動先の最終的な場所(マウスボタンの放された所)に、 spの文字列を表示する。
5)マウスドラッグ中の処理
else if(chi==7){if (tadi==3){w1=(px2-oldx);h1=(py2-oldy);repaint();}}
tadi==3は、コピーした画像の移動を意味しますが、同じ方法で文字イメージを移動させますので コピーした画像の移動と同じ処理します。



トップページ

趣味のトップページ

娯楽室の各部屋
Aplletの解説へ戻る

Scriptの解説 Scriptのゲーム Appletのゲーム Linuxの部屋 物置部屋