【仕様】
import java.awt.*;
import java.awt.event.*;
public class Homework6 extends Frame implements ActionListener {
MyCanvas can;
// 生成
public Homework6(){
super("Homework6");
setSize(500,500);
setLocation(100,50);
can=new MyCanvas();
add(can);
can.addMouseListener(can);
Panel p=new Panel();
add(p,BorderLayout.NORTH);
Button bt;
p.add(bt=new Button("Undo"));
bt.addActionListener(can);
Choice ch = new Choice();
ch.add("○");
ch.add("□");
ch.add("△");
p.add(ch);
ch.addItemListener(can);
MenuBar mb=new MenuBar();
setMenuBar(mb);
Menu m=new Menu("ファイル");
m.add("-");
m.add("終了");
m.addActionListener(this);
m.addActionListener(can);
mb.add(m);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){ exit(); }
});
}
// 終了
public void exit(){
System.exit(0);
}
// アクションリスナー
public void actionPerformed(ActionEvent ev){
String cmd=ev.getActionCommand();
if(cmd.equals("終了")) exit();
}
// メイン処理
public static void main(String[] args){
(new Homework6()).setVisible(true);
}
}
・Windowを出す基本プログラム
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
class MyCanvas extends Canvas implements MouseListener,ItemListener,ActionListener,Runnable {
private Thread runner=null;
private ArrayList<Plane> plane;
private String Select_Cmd = "○"; //デフォルトの図形
private int Point_x, Point_y; //座標
// 生成
public MyCanvas(){
plane=new ArrayList<Plane>();
}
// 描画
public void paint(Graphics g){
for(Plane c:plane)
c.draw(g);
}
// 実行
public void run(){
while(runner!=null){
int w=getWidth(),h=getHeight();
for(Plane c:plane)
c.move(w,h);
repaint();
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
// ○を追加する
public void addCircle(){
int x=Point_x;
int y=Point_y;
int r=(int)(Math.random()*100)+120;
int g=(int)(Math.random()*100)+120;
int b=(int)(Math.random()*100)+120;
plane.add(new Circle(new Color(r,g,b),x,y));
repaint();
}
// □を追加する
public void addSquare(){
int x=Point_x;
int y=Point_y;
int r=(int)(Math.random()*100)+120;
int g=(int)(Math.random()*100)+120;
int b=(int)(Math.random()*100)+120;
plane.add(new Square(new Color(r,g,b),x,y));
repaint();
}
// △を追加する
public void addTriangle(){
int x=Point_x;
int y=Point_y;
int r=(int)(Math.random()*100)+120;
int g=(int)(Math.random()*100)+120;
int b=(int)(Math.random()*100)+120;
plane.add(new Triangle(new Color(r,g,b),x,y));
repaint();
}
// 図形を削除する
public void remove(){
int n=plane.size();
if(n==0) {
runner=null;
return;
}
plane.remove(n-1);
repaint();
}
//マウスリスナー
public void mouseClicked(MouseEvent ev){}
//マウス押下時の処理
public void mousePressed(MouseEvent ev){
if(runner==null){
runner=new Thread(this);
runner.start();
}
// マウスを押下した座標を取得
Point_x=ev.getX();
Point_y=ev.getY();
if(Select_Cmd.equals("○")) addCircle();
if(Select_Cmd.equals("□")) addSquare();
if(Select_Cmd.equals("△")) addTriangle();
}
public void mouseReleased(MouseEvent ev){}
public void mouseEntered(MouseEvent ev){}
public void mouseExited(MouseEvent ev){}
// アクションリスナー
public void actionPerformed(ActionEvent ev){
String cmd=ev.getActionCommand();
if(cmd.equals("Undo")) remove();
}
// アイテムリスナー
public void itemStateChanged(ItemEvent ev) {
Select_Cmd=(String)ev.getItem();
}
}
・ 図形クラス
import java.awt.*;
public class Plane {
protected Color color; // 図形の色
protected int px,py; // 図形の位置
// 生成
public Plane(Color c,int x,int y){
color=c;
px=x;
py=y;
}
// 描画
public void draw(Graphics g){}
// 移動
public void move(int w,int h){}
}
・ 図形(○)クラス
import java.awt.*;
public class Circle extends Plane {
private int dx;
// 生成
public Circle(Color c,int x,int y){
super(c,x,y);
dx=10;
}
// 描画
public void draw(Graphics g){
g.setColor(color);
g.drawOval(px-10,py-10,20,20);
}
// 移動
public void move(int w,int h){
if(py<0 || h<py) py=0;
py+=dx;
}
}
・ 図形(□)クラス
import java.awt.*;
public class Square extends Plane {
private int dx;
// 生成
public Square(Color c,int x,int y){
super(c,x,y);
dx=10;
}
// 描画
public void draw(Graphics g){
g.setColor(color);
g.drawRect(px-10,py-10,20,20);
}
// 移動
public void move(int w,int h){
if(py<0 || h<py) py=0;
py+=dx;
}
}
・ 図形(△)クラス
import java.awt.*;
public class Triangle extends Plane {
private int dx;
// 生成
public Triangle(Color c,int x,int y){
super(c,x,y);
dx=10;
}
// 描画
public void draw(Graphics g){
int xPoint[] = {px, px+10, px+25};
int yPoint[] = {py, py-20, py};
g.setColor(color);
g.drawPolygon(xPoint,yPoint,3);
}
// 移動
public void move(int w,int h){
if(py<0 || h<py) py=0;
py+=dx;
}
}