JFC の おえかき。機能追加 4AdvertisementSwing + Java2D
ショートカットキー
新規作成 追加。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
/**
* メインクラス
*/
public class JfcPaint{
public static void main(String[] args){
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
try{
javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(
new javax.swing.plaf.metal.DefaultMetalTheme());
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}catch(Exception e){
System.err.println(e.getMessage());
System.exit(1);
}
MyFrame mf = new MyFrame();
mf.setTitle("JFC ペイント");
mf.setSize(400, 400);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}
/**
* アプリケーションのフレームを管理するクラス
*/
class MyFrame extends JFrame implements ActionListener{
private JMenuBar menuBar;
private JToolBar toolBar;
private JColorChooser colorChooser;
private JFileChooser fileChooser;
private MyPanel panel;
public MyFrame(){
panel = new MyPanel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, "Center");
getContentPane().add(createToolBar(), "South");
setJMenuBar(createMenuBar());
}
private JToolBar createToolBar(){
toolBar = new JToolBar();
toolBar.add(Create.createButton("/", "figureLine", this));
toolBar.add(Create.createButton("□", "figureRectangle", this));
toolBar.add(Create.createButton("○", "figureEllipse", this));
toolBar.add(Create.createButton("■", "figureFillRectangle", this));
toolBar.add(Create.createButton("●", "figureFillEllipse", this));
return toolBar;
}
private JMenuBar createMenuBar(){
menuBar = new JMenuBar();
JMenu menu;
JMenu w_menu;
menu = Create.createMenu("ファイル", "file", this);
menu.add(Create.createMenuItem("新規作成", "new",
this, KeyEvent.VK_N, InputEvent.CTRL_MASK));
menu.add(new JSeparator());
menu.add(Create.createMenuItem("開く", "open", this,
KeyEvent.VK_O, InputEvent.CTRL_MASK));
menu.add(Create.createMenuItem("保存", "save", this,
KeyEvent.VK_S, InputEvent.CTRL_MASK));
menu.add(new JSeparator());
menu.add(Create.createMenuItem("jpgとして保存", "jpg", this));
menu.add(Create.createMenuItem("pngとして保存", "png", this));
menu.add(new JSeparator());
menu.add(Create.createMenuItem("終了", "quit", this));
menuBar.add(menu);
menu = Create.createMenu("編集", "edit", this);
menu.add(Create.createMenuItem("元に戻す", "undo", this,
KeyEvent.VK_Z, InputEvent.CTRL_MASK));
menu.add(Create.createMenuItem("やり直し", "redo", this,
KeyEvent.VK_Y, InputEvent.CTRL_MASK));
menu.add(new JSeparator());
menu.add(Create.createMenuItem("画面消去", "clear", this));
menuBar.add(menu);
menu = Create.createMenu("設定", "conf", this);
w_menu = Create.createMenu("図形", "figure", this);
ButtonGroup figureGroup = new ButtonGroup();
w_menu.add(Create.createMenuItem("直線", "figureLine", this));
w_menu.add(Create.createMenuItem("矩形", "figureRectangle", this));
w_menu.add(Create.createMenuItem("楕円", "figureEllipse", this));
w_menu.add(Create.createMenuItem("矩形(塗りつぶし)",
"figureFillRectangle", this));
w_menu.add(Create.createMenuItem("楕円(塗りつぶし)",
"figureFillEllipse", this));
menu.add(w_menu);
w_menu = Create.createMenu("線の太さ", "lineBold", this);
ButtonGroup lineGroup = new ButtonGroup();
w_menu.add(Create.createRadioButton(" 1px","lineBold1",
true, lineGroup, this));
w_menu.add(Create.createRadioButton(" 2px","lineBold2",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 3px","lineBold3",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 4px","lineBold4",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 5px","lineBold5",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 6px","lineBold6",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 7px","lineBold7",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 8px","lineBold8",
false, lineGroup, this));
w_menu.add(Create.createRadioButton(" 9px","lineBold9",
false, lineGroup, this));
w_menu.add(Create.createRadioButton("10px","lineBold10",
false, lineGroup, this));
menu.add(w_menu);
menu.add(new JSeparator());
menu.add(Create.createMenuItem("描画色選択","drawColor", this));
menu.add(Create.createMenuItem("補助色選択","subColor", this));
menu.add(Create.createMenuItem("背景色選択", "backColor", this));
menu.add(new JSeparator());
menu.add(Create.createCheckBoxMenuItem("ツールバーの表示",
"toolbar", true, this));
menu.add(Create.createCheckBoxMenuItem("グリッドの表示",
"grid", false, this));
menu.add(Create.createCheckBoxMenuItem("アンチエイリアス",
"antialias", false, this));
menuBar.add(menu);
menu = Create.createMenu("ヘルプ", "help", this);
w_menu = Create.createMenu("Look and Feel", "landf", this);
ButtonGroup lfGroup = new ButtonGroup();
w_menu.add(Create.createRadioButton("metal", "metal",
true , lfGroup, this));
w_menu.add(Create.createRadioButton("motif", "motif",
false, lfGroup, this));
w_menu.add(Create.createRadioButton("windows", "windows",
false, lfGroup, this));
menu.add(w_menu);
menu.add(Create.createMenuItem("バージョン情報", "version", this));
menuBar.add(menu);
return menuBar;
}
private void savePicture(){
fileChooser = MyJFileChooser.getInstance();
try{
int returnVal = fileChooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(panel.getVector());
oos.close();
fos.close();
}
}catch(IOException e){
System.err.println(e.getMessage());
}
}
private void openPicture(){
fileChooser = MyJFileChooser.getInstance();
try{
int returnVal = fileChooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
panel.setVector((Vector)ois.readObject());
ois.close();
fis.close();
panel.repaint();
}
}catch(IOException e){
System.err.println(e.getMessage());
}catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
private void chooseColor(){
colorChooser = MyJColorChooser.getInstance();
panel.setColor(colorChooser.showDialog(
this, "描画色選択", panel.getColor()));
}
private void chooseSubColor(){
colorChooser = MyJColorChooser.getInstance();
panel.setSubColor(colorChooser.showDialog(
this, "補助色選択", panel.getSubColor()));
}
private void chooseBackColor(){
colorChooser = MyJColorChooser.getInstance();
panel.setBackground(colorChooser.showDialog(
this, "背景色選択", panel.getBackground()));
}
public void actionPerformed(ActionEvent e){
//ファイルメニュー
if(e.getActionCommand().equals("new")){
if (panel.isUndo() || panel.isRedo()){
int result = JOptionPane.showConfirmDialog(getContentPane(),
"保存確認", "現在のデータを保存しますか?",
JOptionPane.YES_NO_CANCEL_OPTION);
switch(result){
case JOptionPane.YES_OPTION:
openPicture();
panel.initPanel();
break;
case JOptionPane.NO_OPTION:
panel.initPanel();
break;
case JOptionPane.CANCEL_OPTION:
}
}
}else if(e.getActionCommand().equals("open")){
openPicture();
}else if(e.getActionCommand().equals("save")){
savePicture();
}else if(e.getActionCommand().equals("jpg")){
fileChooser = MyJFileChooser.getInstance();
try{
int returnVal = fileChooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
BufferedImage img = new BufferedImage(
panel.getWidth(), panel.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(panel.getBackground());
g2.fill(new Rectangle2D.Double(
0, 0, img.getWidth(), img.getHeight()));
Iterator iterator = panel.iterator();
while(iterator.hasNext()){
((Figure)iterator.next()).draw(g2);
}
ImageIO.write(img, "jpg", fileChooser.getSelectedFile());
}
}catch(IOException ie){
System.err.println(ie.getMessage());
}
}else if(e.getActionCommand().equals("png")){
fileChooser = MyJFileChooser.getInstance();
try{
int returnVal = fileChooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
BufferedImage img = new BufferedImage(
panel.getWidth(), panel.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(panel.getBackground());
g2.fill(new Rectangle2D.Double(
0, 0, img.getWidth(), img.getHeight()));
Iterator iterator = panel.iterator();
while(iterator.hasNext()){
((Figure)iterator.next()).draw(g2);
}
ImageIO.write(img, "png", fileChooser.getSelectedFile());
}
}catch(IOException ie){
System.err.println(ie.getMessage());
}
}else if(e.getActionCommand().equals("quit")){
System.exit(0);
}
//編集メニュー
if(e.getActionCommand().equals("undo")){
panel.undo();
}else if(e.getActionCommand().equals("redo")){
panel.redo();
}else if(e.getActionCommand().equals("clear")){
panel.clear();
}
//設定メニュー
if(e.getActionCommand().equals("figureLine")){
panel.setFigureFlag(1);
}else if(e.getActionCommand().equals("figureRectangle")){
panel.setFigureFlag(2);
}else if(e.getActionCommand().equals("figureEllipse")){
panel.setFigureFlag(3);
}else if(e.getActionCommand().equals("figureFillRectangle")){
panel.setFigureFlag(4);
}else if(e.getActionCommand().equals("figureFillEllipse")){
panel.setFigureFlag(5);
}
if(e.getActionCommand().equals("lineBold1")){
panel.setLineSize(1);
}else if(e.getActionCommand().equals("lineBold2")){
panel.setLineSize(2);
}else if(e.getActionCommand().equals("lineBold3")){
panel.setLineSize(3);
}else if(e.getActionCommand().equals("lineBold4")){
panel.setLineSize(4);
}else if(e.getActionCommand().equals("lineBold5")){
panel.setLineSize(5);
}else if(e.getActionCommand().equals("lineBold6")){
panel.setLineSize(6);
}else if(e.getActionCommand().equals("lineBold7")){
panel.setLineSize(7);
}else if(e.getActionCommand().equals("lineBold8")){
panel.setLineSize(8);
}else if(e.getActionCommand().equals("lineBold9")){
panel.setLineSize(9);
}else if(e.getActionCommand().equals("lineBold10")){
panel.setLineSize(10);
}
if(e.getActionCommand().equals("drawColor")){
chooseColor();
}else if(e.getActionCommand().equals("subColor")){
chooseSubColor();
}else if(e.getActionCommand().equals("backColor")){
chooseBackColor();
}
if(e.getActionCommand().equals("toolbar")){
toolBar.setVisible(!toolBar.isVisible());
}else if(e.getActionCommand().equals("antialias")){
panel.setAntialias(!panel.getAntialias());
panel.repaint();
}else if(e.getActionCommand().equals("grid")){
panel.setGrid(!panel.getGrid());
panel.repaint();
}
// ヘルプメニュー
if(e.getActionCommand().equals("version")){
JOptionPane.showMessageDialog(getContentPane(),
"おえかき\nバージョン 1.0");
}else if(e.getActionCommand().equals("metal")){
try{
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}else if(e.getActionCommand().equals("motif")){
try{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}else if(e.getActionCommand().equals("windows")){
try{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}
}
}
/**
* 描画領域を管理するクラス
*/
class MyPanel extends JPanel implements MouseMotionListener, MouseListener{
private Point start;
private Point end;
private int figureFlag; // 図形のフラグ
private boolean antialiasFlag; // アンチエイリアス
private boolean gridFlag; // グリッド表示
private Color drawColor; // 描画色
private Color subColor; // 補助色
private float size; // 線の太さ
private Vector figureBox; // 図形コレクション
private MyHistory history; // 履歴
public MyPanel(){
drawColor = Color.black;
subColor = Color.cyan;
antialiasFlag = false;
gridFlag = false;
figureFlag = 1;
size = 1.0f;
figureBox = new Vector();
history = new MyHistory();
history.add(figureBox.clone());
start = new Point();
end = new Point();
setBackground(Color.white);
addMouseListener(this);
addMouseMotionListener(this);
}
public void initPanel(){
this.clear();
history.clearHistory();
history.add(figureBox.clone());
}
public boolean isUndo(){
return history.isUndo();
}
public void undo(){
if(history.isUndo()){
figureBox = (Vector)history.undo();
repaint();
}else{
System.out.println("undo false");
}
}
public boolean isRedo(){
return history.isRedo();
}
public void redo(){
if(history.isRedo()){
figureBox = (Vector)history.redo();
repaint();
}else{
System.out.println("redo false");
}
}
public void clear(){
figureBox = new Vector();
repaint();
}
public void setLineSize(float f){
size = f;
}
public float getLineSize(){
return size;
}
public void setAntialias(boolean b){
antialiasFlag = b;
}
public boolean getAntialias(){
return antialiasFlag;
}
public void setGrid(boolean b){
gridFlag = b;
}
public boolean getGrid(){
return gridFlag;
}
public void setFigureFlag(int n){
figureFlag = n;
}
public int getFigureFlag(){
return figureFlag;
}
public void setColor(Color c){
drawColor = c;
}
public Color getColor(){
return drawColor;
}
public void setSubColor(Color c){
subColor = c;
}
public Color getSubColor(){
return subColor;
}
public void setVector(Vector v){
figureBox = v;
}
public Vector getVector(){
return figureBox;
}
public Iterator iterator(){
return figureBox.iterator();
}
public void paint(Graphics g){
super.paint(g);
//情報
g.setColor(subColor);
g.fillRect(0, 0, 10, 10);
g.setColor(Color.black);
g.drawRect(0, 0, 10, 10);
g.setColor(drawColor);
g.fillRect(5, 5, 10, 10);
g.setColor(Color.black);
g.drawRect(5, 5, 10, 10);
Graphics2D g2 = (Graphics2D)g;
if(gridFlag){ //グリッド描画
drawGrid(g2);
}
if(antialiasFlag){ //アンチエイリアス
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
// 図形描画
Iterator iterator = figureBox.iterator();
while(iterator.hasNext()){
((Figure)iterator.next()).draw(g2);
}
}
public void drawGrid(Graphics2D g2){
g2.setColor(Color.lightGray);
for(int i = 0; i < this.getHeight(); i = i + 15){
g2.draw(new Line2D.Double(0, i, this.getWidth(), i));
}
for(int i = 0; i < this.getWidth(); i = i + 15){
g2.draw(new Line2D.Double(i, 0, i, this.getHeight()));
}
}
public void mouseDragged(MouseEvent e){
end.setLocation(e.getX(), e.getY());
((Figure)figureBox.lastElement()).setEnd(end);
repaint();
}
public void mousePressed(MouseEvent e){
start.setLocation(e.getX(), e.getY());
end.setLocation(e.getX(), e.getY());
switch(figureFlag){
case 1:
figureBox.add(new LineData(start, end, drawColor, size));
break;
case 2:
figureBox.add(new RectangleData(start, end, drawColor, size));
break;
case 3:
figureBox.add(new EllipseData(start, end, drawColor, size));
break;
case 4:
figureBox.add(new FillRectangleData(start, end, drawColor, size));
break;
case 5:
figureBox.add(new FillEllipseData(start, end, drawColor, size));
break;
}
}
public void mouseReleased(MouseEvent e){
// 履歴記録
history.add(figureBox.clone());
}
public void mouseClicked(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
/**
* すべての図形のスーパークラス(抽象クラス)
*/
abstract class Figure implements Serializable{
protected Point start;
protected Point end;
protected Color col;
protected float size;
public Figure(){};
public Figure(Point sp, Point ep, Color c, float s){
start = (Point)sp.clone();
end = (Point)ep.clone();
col = c;
size = s;
}
public void setColor(Color c){
col = c;
}
public void setStart(Point s){
start = (Point)s.clone();
}
public void setEnd(Point e){
end = (Point)e.clone();
}
abstract void draw(Graphics2D g2);
}
/**
* 直線を描画するクラス
*/
class LineData extends Figure{
public LineData(Point start, Point end, Color c, float s){
super(start, end, c, s);
}
public void draw(Graphics2D g2){
g2.setColor(col);
g2.setStroke(new BasicStroke(size));
g2.draw(new Line2D.Double(start, end));
}
}
/**
* 矩形を管理するクラス
*/
class RectangleData extends Figure{
public RectangleData(Point sp, Point ep, Color c, float s){
super(sp, ep, c, s);
}
public void draw(Graphics2D g2){
g2.setColor(col);
g2.setStroke(new BasicStroke(size));
g2.draw(new Rectangle2D.Double(
start.getX()<end.getX()?start.getX():end.getX(),
start.getY()<end.getY()?start.getY():end.getY(),
Math.abs(start.getX()-end.getX()),
Math.abs(start.getY()-end.getY())));
}
}
/**
* 楕円を描画するクラス
*/
class EllipseData extends Figure{
public EllipseData(Point sp, Point ep, Color c, float s){
super(sp, ep, c, s);
}
public void draw(Graphics2D g2){
g2.setColor(col);
g2.setStroke(new BasicStroke(size));
g2.draw(new Ellipse2D.Double(
start.getX()<end.getX()?start.getX():end.getX(),
start.getY()<end.getY()?start.getY():end.getY(),
Math.abs(start.getX()-end.getX()),
Math.abs(start.getY()-end.getY())));
}
}
/**
* 塗りつぶされた矩形を描画するクラス
*/
class FillRectangleData extends Figure{
public FillRectangleData(Point sp, Point ep, Color c, float s){
super(sp, ep, c, s);
}
public void draw(Graphics2D g2){
g2.setColor(col);
g2.setStroke(new BasicStroke(size));
g2.fill(new Rectangle2D.Double(
start.getX()<end.getX()?start.getX():end.getX(),
start.getY()<end.getY()?start.getY():end.getY(),
Math.abs(start.getX()-end.getX()),
Math.abs(start.getY()-end.getY())));
}
}
/**
* 塗りつぶされた描画を管理するクラス
*/
class FillEllipseData extends Figure{
public FillEllipseData(Point sp, Point ep, Color c, float s){
super(sp, ep, c, s);
}
public void draw(Graphics2D g2){
g2.setColor(col);
g2.setStroke(new BasicStroke(size));
g2.fill(new Ellipse2D.Double(
start.getX()<end.getX()?start.getX():end.getX(),
start.getY()<end.getY()?start.getY():end.getY(),
Math.abs(start.getX()-end.getX()),
Math.abs(start.getY()-end.getY())));
}
}
/**
* JFileChooser の数をひとつに制限するクラス(Singletonパターン)
*/
final class MyJFileChooser{
private static JFileChooser chooser = new JFileChooser();
private MyJFileChooser(){};
public static JFileChooser getInstance(){
return chooser;
}
}
/**
* JColorChooser の数をひとつに制限するクラス(Singletonパターン)
*/
final class MyJColorChooser{
private static JColorChooser chooser = new JColorChooser();
private MyJColorChooser(){};
public static JColorChooser getInstance(){
return chooser;
}
}
/**
* 与えられたパラメータを使用して 各Componentを作成するクラス
*/
final class Create{
private static JButton button;
private Create(){}
public static JButton createButton(String title, String cmd,
ActionListener act){
button = new JButton(title);
button.setActionCommand(cmd);
button.addActionListener(act);
return button;
}
public static JMenu createMenu(String title, String cmd, ActionListener act){
JMenu menu = new JMenu(title);
menu.setActionCommand(cmd);
menu.addActionListener(act);
return menu;
}
public static JMenuItem createMenuItem(String title, String cmd,
ActionListener act){
JMenuItem item = new JMenuItem(title);
item.setActionCommand(cmd);
item.addActionListener(act);
return item;
}
public static JMenuItem createMenuItem(String title, String cmd,
ActionListener act, int keyCode, int modifiers){
JMenuItem item = new JMenuItem(title);
item.setActionCommand(cmd);
item.addActionListener(act);
item.setAccelerator(KeyStroke.getKeyStroke(keyCode, modifiers));
return item;
}
public static JCheckBoxMenuItem createCheckBoxMenuItem(String title, String cmd,
ActionListener act){
JCheckBoxMenuItem citem = new JCheckBoxMenuItem(title);
citem.setActionCommand(cmd);
citem.addActionListener(act);
return citem;
}
public static JCheckBoxMenuItem createCheckBoxMenuItem(String title, String cmd,
boolean checked, ActionListener act){
JCheckBoxMenuItem citem = new JCheckBoxMenuItem(title, checked);
citem.setActionCommand(cmd);
citem.addActionListener(act);
return citem;
}
public static JRadioButton createRadioButton(String title, String cmd,
ActionListener act){
JRadioButton rbutton = new JRadioButton(title);
rbutton.setActionCommand(cmd);
rbutton.addActionListener(act);
return rbutton;
}
public static JRadioButton createRadioButton(String title, String cmd,
boolean selected, ActionListener act){
JRadioButton rbutton = new JRadioButton(title, selected);
rbutton.setActionCommand(cmd);
rbutton.addActionListener(act);
return rbutton;
}
public static JRadioButton createRadioButton(String title, String cmd,
boolean selected, ButtonGroup group, ActionListener act){
JRadioButton rbutton = new JRadioButton(title, selected);
rbutton.setActionCommand(cmd);
rbutton.addActionListener(act);
group.add(rbutton);
return rbutton;
}
}
/**
* 履歴を管理するクラス
* 内部に状態を保持し、Undo/Redo呼び出しに対して
* 保持している状態オブジェクトのコピーを返却する。
*/
class MyHistory implements Cloneable{
private Vector history;
private int index;
MyHistory(){
history = new Vector();
index = -1;
}
/**
* 履歴を初期化する。
*/
public void clearHistory(){
history = new Vector();
index = -1;
}
/**
* 元に戻す処理でさらに要素があるなら true を返す
*/
public boolean isUndo(){
if(index > 0){
return true;
}else{
return false;
}
}
/**
* やり直し処理でさらに要素があるなら true を返す
*/
public boolean isRedo(){
if(index < history.size() - 1){
return true;
}else{
return false;
}
}
/**
* 履歴に要素を追加
*/
public void add(Object obj){
for(int i = history.size() - 1; i > index; i--){
history.removeElementAt(i);
}
history.add(obj);
index = index + 1;
}
/**
* Undo処理の実行
* index のひとつ前の要素を返す
*/
public Object undo(){
if(index > 0){
index = index - 1;
Vector vec = (Vector)history.elementAt(index);
return vec.clone();
}else{
return null;
}
}
/**
* Redo処理の実行
* index のひとつ後の要素を返す
*/
public Object redo(){
if(index < history.size() - 1){
index = index + 1;
Vector vec = (Vector)history.elementAt(index);
return vec.clone();
}else{
return null;
}
}
}
実行結果サンプル![]()
Advertisement |
ショートカット・634・634ブログ ・このカテゴリのトップページに戻る ・Incubator(Pukiwiki) ・634ラボ UIコレクションギャラリー ZO-3ジェネレーター サイト検索Y!ログール |