/*------------------------------------- ex14_6.java イベント処理:ボタンとマウスでお絵描き --------------------------------------*/ /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class ex14_6 extends Applet implements ActionListener, MouseListener { Button btn; // ボタン:宣言 Random rnd; Color col; int x, y, red, grn, blu; boolean flg; public void init() { // 初期化処理 x = -30; y = -30; rnd = new Random(); red = rnd.nextInt(256); grn = rnd.nextInt(256); blu = rnd.nextInt(256); flg = false; btn = new Button("Clear"); add(btn); btn.addActionListener(this); addMouseListener(this); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // 描画処理 if (flg == false) { g.setColor(col); g.fillRoundRect(x-15, y-15, rnd.nextInt(20)+11, rnd.nextInt(20)+11, 5, 5); } else { g.clearRect(0, 0, 500, 300); flg = false; } } public void actionPerformed(ActionEvent e) { // アクションイベント実行処理 if (e.getSource() == btn) { // ボタンが押されたら… flg = true; repaint(); } } public void mouseClicked(MouseEvent e) { Point pt = e.getPoint(); // マウスクリック位置取得 x = pt.x; y = pt.y; red = rnd.nextInt(256); grn = rnd.nextInt(256); blu = rnd.nextInt(256); col = new Color(red, grn, blu); repaint(); } public void mousePressed(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }