import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Painter
{
public static void main(String args[])
{
new MainWindow();
}
}
class MainWindow extends Frame
{
final Dimension default_size = new Dimension(255,255);
PainterCanvas canvas;
public MainWindow()
{
super("Paint Tool");
// ウインドウリスナの定義
WindowListener listener = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(listener);
// メニューの定義
MenuBar mb = new MenuBar();
Menu file = new Menu("File");
mb.add(file);
// ファイルを開くダイアログ
MenuItem file_open = new MenuItem("Open");
ActionListener file_open_listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog dialog = new FileDialog(MainWindow.this,"Load File",FileDialog.LOAD);
dialog.show();
String file_name = dialog.getFile();
try
{
// Polygon オブジェクトを読み込む
FileInputStream file_in = new FileInputStream(file_name);
ObjectInputStream object_in = new ObjectInputStream(file_in);
Polygon polygon = (Polygon)object_in.readObject();
canvas.setImage(polygon);
}
catch(Exception ex)
{
System.err.println(ex);
}
}
};
file_open.addActionListener(file_open_listener);
// ファイルを保存するダイアログ
MenuItem file_save = new MenuItem("Save");
ActionListener file_save_listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog dialog = new FileDialog(MainWindow.this,"Save As",FileDialog.SAVE);
dialog.show();
String file_name = dialog.getFile();
try
{
// Polygon オブジェクトを書き込む
FileOutputStream file_out = new FileOutputStream(file_name);
ObjectOutputStream object_out = new ObjectOutputStream(file_out);
object_out.writeObject(canvas.getImage());
}
catch(Exception ex)
{
System.err.println(ex);
}
}
};
file_save.addActionListener(file_save_listener);
MenuItem file_exit = new MenuItem("Exit");
ActionListener file_exit_listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
file_exit.addActionListener(file_exit_listener);
file.add(file_open);
file.add(file_save);
file.addSeparator();
file.add(file_exit);
setMenuBar(mb);
// Canvasの生成
canvas = new PainterCanvas(default_size.width,default_size.height);
canvas.setSize(default_size);
add(canvas,BorderLayout.CENTER);
// Windowの表示
pack();
setResizable(false);
setVisible(true);
}
}
class PainterCanvas extends Canvas
{
Polygon polygon; // Pointの集合
public int width,height;
int x,y;
public PainterCanvas(int width,int height)
{
super();
this.width = width;
this.height = height;
polygon = new Polygon();
// マウスダウンイベント
MouseListener mouse_listener = new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int point_x = e.getX();
int point_y = e.getY();
PaintPoint(point_x,point_y);
}
};
addMouseListener(mouse_listener);
// マウスドラッグイベント
MouseMotionListener mouse_motion_listener = new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
int point_x = e.getX();
int point_y = e.getY();
PaintPoint(point_x,point_y);
}
};
addMouseMotionListener(mouse_motion_listener);
}
// 描画メソッド
public void update(Graphics g)
{
paint(g);
}
// 描画メソッド
public void paint(Graphics g)
{
for(int i=0;i<polygon.npoints;i++)
{
int x = polygon.xpoints[i];
int y = polygon.ypoints[i];
g.drawLine(x,y,x,y);
}
}
// 指定された1dotを描画する
protected void PaintPoint(int x,int y)
{
if(x > width || y > height)
{
return;
}
polygon.addPoint(x,y);
repaint();
}
// ドットの集合を返す
public Polygon getImage()
{
return(polygon);
}
// 指定されたPolygonオブジェクトから、イメージを描画する
public void setImage(Polygon points)
{
Graphics gra = getGraphics();
gra.clearRect(0,0,width,height);
this.polygon = points;
repaint();
}
}
Source is here. (ZIP Format,1551Byte,Shift-JIS)