DrawFrame
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[Lecture]]
* Javaで簡単なお絵描きプログラムを作ってみる [#b8871982]
簡単なお絵描きプログラムを作ってイベントの扱いを練習しま...
** ウィンドウを一つひらくには [#ac2c3b1d]
import java.awt.*;
public class DrawFrame extends Frame {
public DrawFrame(String _title) {
super(_title);
setSize(400,300);
setVisible(true);
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** 閉じられるウィンドウにするには [#web7d638]
- このwindowは簡単すぎて、クローズボタンを押しても閉じま...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements WindowLi...
public DrawFrame(String _title) {
super(_title);
addWindowListener(this);//自分自身を登録...
setSize(400,300);
setVisible(true);
}
//////window listenerに必要とされるメソッドを実装...
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) {System....
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** マウスのドラッグで線を描くようにするには [#web7d638]
Mouse Motion Listenerとして自分を登録すれば,マウスの移動...
また自分自身のgetGraphics()を呼び出すと,自分のウィンドウ...
Grahicsインスタンスを得ることが出来ます.
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.drawLine(lastX, lastY,e.getX(), e.getY());
lastX=e.getX(); lastY=e.getY();
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
上の例では,ドラッグの最後の位置が次回の開始点になってい...
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
lastX=e.getX(); lastY=e.getY();
}
public void mouseReleased(MouseEvent e) {}
** 太い線を描くには [#p8b5bb9b]
Graphicsクラスを拡張した(継承した)Graphics2Dクラスを使...
Graphics2DクラスはGraphicsクラスのサブクラスなので,
Graphicsクラスのインスタンスをキャストしてしまえば、Graph...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)getGraphics();
//太さ20の線を描く.線の両端は丸くする.
g2.setStroke(new BasicStroke(20.0f ,BasicStroke.CAP_RO...
g2.drawLine(lastX, lastY,e.getX(), e.getY());
lastX=e.getX(); lastY=e.getY();
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** ウィンドウに直接描かないでバッファに描く [#f4bcfbe8]
上の例では,ウィンドウの面に直接描画していました.いわば...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
Image bufferImage;
Graphics bufferGraphics;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
//バッファ用のImageとGraphicsを用意してお...
bufferImage = createImage(400,300);
bufferGraphics=bufferImage.getGraphics();
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
//バッファに描く
bufferGraphics.drawLine(lastX, lastY,e.ge...
lastX=e.getX(); lastY=e.getY();
repaint();//再描画するためpaint()を呼び出す
}
public void paint(Graphics g) {
if(null!=bufferImage) g.drawImage(bufferI...
super.paint(g); //他に描画するものがある...
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** メニューをあつかうには [#j5d87932]
教科書ではメニューアイテムのインスタンスを保持して実現し...
以下のように簡単に作ることもできます。
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements ActionLi...
MenuBar mb;
Menu mColor;
Color currentColor = Color.black; //color of the pen
public DrawFrame(String _title) {
super(_title);
mb = new MenuBar();
mb.add(mColor = new Menu("Color"));
mColor.add("Black");
mColor.add("Red");
mColor.add("Green");
mColor.add("Blue");
mColor.add("Yellow");
mColor.add("White");
mColor.addActionListener(this);//自分自身...
setMenuBar(mb);//menubarを登録する
setSize(400,300);
setVisible(true);
}
//listener methods
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Black")) currentColor=C...
else if(e.getActionCommand().equals("Red")) currentColo...
else if(e.getActionCommand().equals("Green")) currentCo...
else if(e.getActionCommand().equals("Blue")) currentCol...
else if(e.getActionCommand().equals("Yellow")) currentC...
else if(e.getActionCommand().equals("White")) currentCo...
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** JPEGファイル(写真)を表示するには [#d5077e46]
Toolkitというクラスのインスタンスを使うと,JPEGやGIFファ...
import java.awt.*;
public class DrawFrame extends Frame {
Image pictureImage;
public DrawFrame(String _title) {
super(_title);
setSize(400,300);
setVisible(true);
//画像ファイルからImageインスタンスを作る
Toolkit toolkit = Toolkit.getDefaultToolk...
pictureImage = toolkit.getImage("himawari...
//Imageインスタンスを用意して,用意できた...
toolkit.prepareImage(pictureImage,-1,-1,t...
}
public void paint(Graphics g) {
if(null!= pictureImage) g.drawImage(pictu...
super.paint(g); //他に描画するものがある...
}
public static void main(String argv[]) {
new DrawFrame("画像ファイル表示");
}
}
このプログラムの中の,
toolkit.prepareImage(pictureImage,-1,-1,th...
の行で,ファイルからイメージを読み込みイメージインスタン...
public void paint(Graphics g) {
System.out.println("painting...");
if(null!=pictureImage) g.drawImage(pictur...
super.paint(g); //他に描画するものがある...
}
とすると,以下のようになります.
siio$ java DrawFrame
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
プログラムによっては,イメージインスタンスが完全に用意さ...
public boolean imageUpdate(Image img , int flg , int x ,...
if ((flg & ALLBITS) == 0) return true;
else {
repaint();
return false;
}
}
** 写真にお絵描きをするには [#zd2f8af6]
上のようにして読み込んだイメージインスタンスを,先の描画...
public boolean imageUpdate(Image img , int flg , int x ,...
if ((flg & ALLBITS) == 0) return true;
else {
bufferGraphics.drawImage(pictureImage,0,0...
repaint();
return false;
}
}
ここでは,イメージが用意できたところで,バッファイメージ...
終了行:
[[Lecture]]
* Javaで簡単なお絵描きプログラムを作ってみる [#b8871982]
簡単なお絵描きプログラムを作ってイベントの扱いを練習しま...
** ウィンドウを一つひらくには [#ac2c3b1d]
import java.awt.*;
public class DrawFrame extends Frame {
public DrawFrame(String _title) {
super(_title);
setSize(400,300);
setVisible(true);
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** 閉じられるウィンドウにするには [#web7d638]
- このwindowは簡単すぎて、クローズボタンを押しても閉じま...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements WindowLi...
public DrawFrame(String _title) {
super(_title);
addWindowListener(this);//自分自身を登録...
setSize(400,300);
setVisible(true);
}
//////window listenerに必要とされるメソッドを実装...
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) {System....
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** マウスのドラッグで線を描くようにするには [#web7d638]
Mouse Motion Listenerとして自分を登録すれば,マウスの移動...
また自分自身のgetGraphics()を呼び出すと,自分のウィンドウ...
Grahicsインスタンスを得ることが出来ます.
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.drawLine(lastX, lastY,e.getX(), e.getY());
lastX=e.getX(); lastY=e.getY();
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
上の例では,ドラッグの最後の位置が次回の開始点になってい...
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
lastX=e.getX(); lastY=e.getY();
}
public void mouseReleased(MouseEvent e) {}
** 太い線を描くには [#p8b5bb9b]
Graphicsクラスを拡張した(継承した)Graphics2Dクラスを使...
Graphics2DクラスはGraphicsクラスのサブクラスなので,
Graphicsクラスのインスタンスをキャストしてしまえば、Graph...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)getGraphics();
//太さ20の線を描く.線の両端は丸くする.
g2.setStroke(new BasicStroke(20.0f ,BasicStroke.CAP_RO...
g2.drawLine(lastX, lastY,e.getX(), e.getY());
lastX=e.getX(); lastY=e.getY();
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** ウィンドウに直接描かないでバッファに描く [#f4bcfbe8]
上の例では,ウィンドウの面に直接描画していました.いわば...
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements MouseMot...
int lastX=0, lastY=0;
Image bufferImage;
Graphics bufferGraphics;
public DrawFrame(String _title) {
super(_title);
addMouseMotionListener(this);//自分自身を...
setSize(400,300);
setVisible(true);
//バッファ用のImageとGraphicsを用意してお...
bufferImage = createImage(400,300);
bufferGraphics=bufferImage.getGraphics();
}
//////Mouse Motion Listenerに必要とされるメソッド...
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
//バッファに描く
bufferGraphics.drawLine(lastX, lastY,e.ge...
lastX=e.getX(); lastY=e.getY();
repaint();//再描画するためpaint()を呼び出す
}
public void paint(Graphics g) {
if(null!=bufferImage) g.drawImage(bufferI...
super.paint(g); //他に描画するものがある...
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** メニューをあつかうには [#j5d87932]
教科書ではメニューアイテムのインスタンスを保持して実現し...
以下のように簡単に作ることもできます。
import java.awt.*;
import java.awt.event.*;
public class DrawFrame extends Frame implements ActionLi...
MenuBar mb;
Menu mColor;
Color currentColor = Color.black; //color of the pen
public DrawFrame(String _title) {
super(_title);
mb = new MenuBar();
mb.add(mColor = new Menu("Color"));
mColor.add("Black");
mColor.add("Red");
mColor.add("Green");
mColor.add("Blue");
mColor.add("Yellow");
mColor.add("White");
mColor.addActionListener(this);//自分自身...
setMenuBar(mb);//menubarを登録する
setSize(400,300);
setVisible(true);
}
//listener methods
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Black")) currentColor=C...
else if(e.getActionCommand().equals("Red")) currentColo...
else if(e.getActionCommand().equals("Green")) currentCo...
else if(e.getActionCommand().equals("Blue")) currentCol...
else if(e.getActionCommand().equals("Yellow")) currentC...
else if(e.getActionCommand().equals("White")) currentCo...
}
public static void main(String argv[]) {
new DrawFrame("お絵描きプログラム");
}
}
** JPEGファイル(写真)を表示するには [#d5077e46]
Toolkitというクラスのインスタンスを使うと,JPEGやGIFファ...
import java.awt.*;
public class DrawFrame extends Frame {
Image pictureImage;
public DrawFrame(String _title) {
super(_title);
setSize(400,300);
setVisible(true);
//画像ファイルからImageインスタンスを作る
Toolkit toolkit = Toolkit.getDefaultToolk...
pictureImage = toolkit.getImage("himawari...
//Imageインスタンスを用意して,用意できた...
toolkit.prepareImage(pictureImage,-1,-1,t...
}
public void paint(Graphics g) {
if(null!= pictureImage) g.drawImage(pictu...
super.paint(g); //他に描画するものがある...
}
public static void main(String argv[]) {
new DrawFrame("画像ファイル表示");
}
}
このプログラムの中の,
toolkit.prepareImage(pictureImage,-1,-1,th...
の行で,ファイルからイメージを読み込みイメージインスタン...
public void paint(Graphics g) {
System.out.println("painting...");
if(null!=pictureImage) g.drawImage(pictur...
super.paint(g); //他に描画するものがある...
}
とすると,以下のようになります.
siio$ java DrawFrame
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
painting...
プログラムによっては,イメージインスタンスが完全に用意さ...
public boolean imageUpdate(Image img , int flg , int x ,...
if ((flg & ALLBITS) == 0) return true;
else {
repaint();
return false;
}
}
** 写真にお絵描きをするには [#zd2f8af6]
上のようにして読み込んだイメージインスタンスを,先の描画...
public boolean imageUpdate(Image img , int flg , int x ,...
if ((flg & ALLBITS) == 0) return true;
else {
bufferGraphics.drawImage(pictureImage,0,0...
repaint();
return false;
}
}
ここでは,イメージが用意できたところで,バッファイメージ...
ページ名: