JFC の おえかき。機能追加 6AdvertisementSwing + Java2D
Jpg, Png 出力のダイアログにフィルタリング設定。
JInternalFrame に情報出力(中途半端) import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import java.io.*; import java.util.*; import java.util.prefs.*; import javax.imageio.*; import javax.swing.*; /** * メインクラス */ public class JfcPaint{ public static void main(String[] args){ JFrame.setDefaultLookAndFeelDecorated(true); MyFrame mf = new MyFrame("JFC ペイント"); mf.show(); } } /** * アプリケーションのフレームを管理するクラス */ class MyFrame extends JFrame implements ActionListener, WindowListener{ public static final int CONST_FIGURE_LINE = 1; public static final int CONST_FIGURE_RECT = 2; public static final int CONST_FIGURE_ELLIPSE = 3; public static final int CONST_FIGURE_FILLRECT = 4; public static final int CONST_FIGURE_FILLELLIPSE = 5; private JMenuBar menuBar; private JToolBar toolBar; private JColorChooser colorChooser; private JFileChooser fileChooser; private MyPanel panel; private MyPreferences pref; // 設定保存用 private final static String CONFIG_PASS = "./frameConfig.xml"; public MyFrame(String caption){ // 設定ファイル読み込み super(caption); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pref = new MyPreferences(); pref.loadPreferences(CONFIG_PASS); panel = new MyPanel(); getContentPane().setLayout(new BorderLayout()); getContentPane().add(panel, "Center"); getContentPane().add(createToolBar(), "South"); setJMenuBar(createMenuBar()); addWindowListener(this); this.setSize(pref.getWindowSize()); panel.setAntialias(pref.getAntialias()); panel.setGrid(pref.getGrid()); toolBar.setVisible(pref.getToolbar()); } 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", pref.getToolbar(), this)); menu.add(Create.createCheckBoxMenuItem("グリッドの表示", "grid", pref.getGrid(), this)); menu.add(Create.createCheckBoxMenuItem("アンチエイリアス", "antialias", pref.getAntialias(), this)); menu.add(Create.createCheckBoxMenuItem("情報ウィンドウの表示", "infowindow", pref.getInfoWindow(), this)); menuBar.add(menu); menu = Create.createMenu("ヘルプ", "help", this); 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(){ panel.setColor(JColorChooser.showDialog( this, "描画色選択", panel.getColor())); } private void chooseSubColor(){ panel.setSubColor(JColorChooser.showDialog( this, "補助色選択", panel.getSubColor())); } private void chooseBackColor(){ panel.setBackground(JColorChooser.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: break; } } }else if(e.getActionCommand().equals("open")){ openPicture(); }else if(e.getActionCommand().equals("save")){ savePicture(); }else if(e.getActionCommand().equals("jpg")){ fileChooser = new JFileChooser(); fileChooser.addChoosableFileFilter(new JpegFileFilter()); 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 = new JFileChooser(); fileChooser.addChoosableFileFilter(new PngFileFilter()); 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(CONST_FIGURE_LINE); }else if(e.getActionCommand().equals("figureRectangle")){ panel.setFigureFlag(CONST_FIGURE_RECT); }else if(e.getActionCommand().equals("figureEllipse")){ panel.setFigureFlag(CONST_FIGURE_ELLIPSE); }else if(e.getActionCommand().equals("figureFillRectangle")){ panel.setFigureFlag(CONST_FIGURE_FILLRECT); }else if(e.getActionCommand().equals("figureFillEllipse")){ panel.setFigureFlag(CONST_FIGURE_FILLELLIPSE); } 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()); pref.setToolbar(toolBar.isVisible()); }else if(e.getActionCommand().equals("antialias")){ panel.setAntialias(!panel.getAntialias()); pref.setAntialias(panel.getAntialias()); panel.repaint(); }else if(e.getActionCommand().equals("grid")){ panel.setGrid(!panel.getGrid()); pref.setGrid(panel.getGrid()); panel.repaint(); }else if(e.getActionCommand().equals("infowindow")){ panel.setInfoWindow(!panel.getInfoWindow()); pref.setInfoWindow(panel.getInfoWindow()); panel.repaint(); } // ヘルプメニュー if(e.getActionCommand().equals("version")){ JOptionPane.showMessageDialog(getContentPane(), "おえかき\nバージョン 1.0"); } } public void windowClosing(WindowEvent e){ // 終了直前に現在の設定を保存。 pref.setWindowSize(new Dimension(this.getWidth(), this.getHeight())); pref.savePreferences(CONFIG_PASS); } public void windowActivated(WindowEvent e){}; public void windowClosed(WindowEvent e){}; public void windowDeactivated(WindowEvent e){}; public void windowDeiconified(WindowEvent e){}; public void windowIconified(WindowEvent e){}; public void windowOpened(WindowEvent e){} } /** * 描画領域を管理するクラス */ class MyPanel extends JPanel implements MouseMotionListener, MouseListener{ public static final int CONST_FIGURE_LINE = MyFrame.CONST_FIGURE_LINE; public static final int CONST_FIGURE_RECT = MyFrame.CONST_FIGURE_RECT; public static final int CONST_FIGURE_ELLIPSE = MyFrame.CONST_FIGURE_ELLIPSE; public static final int CONST_FIGURE_FILLRECT = MyFrame.CONST_FIGURE_FILLRECT; public static final int CONST_FIGURE_FILLELLIPSE = MyFrame.CONST_FIGURE_FILLELLIPSE; private Point start; private Point end; private int figureFlag; // 図形のフラグ private boolean antialiasFlag; // アンチエイリアス private boolean gridFlag; // グリッド表示 private Color drawColor; // 描画色 private Color subColor; // 補助色 private Color backColor; // 背景色 private float size; // 線の太さ private Vector figureBox; // 図形コレクション private MyHistory history; // 履歴 private JInternalFrame jif; // インナーフレーム private InfoPanel infoPanel; // 情報パネル public MyPanel(){ drawColor = Color.BLACK; subColor = Color.CYAN; backColor = Color.WHITE; figureFlag = CONST_FIGURE_LINE; size = 1.0f; figureBox = new Vector(); history = new MyHistory(); history.add(figureBox.clone()); start = new Point(); end = new Point(); this.setBackground(backColor); this.addMouseListener(this); this.addMouseMotionListener(this); // 引数:タイトル、サイズ変更、クローズ、最大化、アイコン化 jif = new JInternalFrame("Info", false , true, false, true); infoPanel = new InfoPanel(); jif.setContentPane(infoPanel); this.add(jif); jif.show(); } 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){ infoPanel.setLineSize(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 boolean getInfoWindow(){ return jif.isVisible(); } public void setInfoWindow(boolean b){ if(b && jif.isClosed()){ jif = new JInternalFrame("Info", false , true, false, true); jif.setContentPane(infoPanel); this.add(jif); jif.show(); }else{ jif.setVisible(b); } } public void setFigureFlag(int figure){ infoPanel.setFigure(figure); figureFlag = figure; } 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); Graphics2D g2 = (Graphics2D)g; if(gridFlag){ //グリッド描画 drawGrid(g2); } if(antialiasFlag){ //アンチエイリアス g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } infoPanel.setColor(drawColor, subColor, this.getBackground()); infoPanel.setFigure(figureFlag); jif.setSize(200, 100); jif.repaint(); // 図形描画 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 CONST_FIGURE_LINE: figureBox.add(new LineData(start, end, drawColor, size)); break; case CONST_FIGURE_RECT: figureBox.add(new RectangleData(start, end, drawColor, size)); break; case CONST_FIGURE_ELLIPSE: figureBox.add(new EllipseData(start, end, drawColor, size)); break; case CONST_FIGURE_FILLRECT: figureBox.add(new FillRectangleData(start, end, drawColor, size)); break; case CONST_FIGURE_FILLELLIPSE: 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; } } /** * 与えられたパラメータを使用して 各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 vector = (Vector)history.elementAt(index); return vector.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; } } } /** * 各種設定を保持。 */ class MyPreferences{ private Preferences pref; public MyPreferences(){ pref = Preferences.userNodeForPackage(MyPreferences.class); } /** * ウィンドウのサイズを記録 */ public void setWindowSize(Dimension dim){ pref.putInt("width", (int)dim.getWidth()); pref.putInt("height", (int)dim.getHeight()); } /** * ウィンドウのサイズを返す */ public Dimension getWindowSize(){ int width = pref.getInt("width", 500); int height = pref.getInt("height", 500); return new Dimension(width, height); } /** * アンチエイリアスの設定 */ public void setAntialias(boolean bool){ pref.putBoolean("antialias", bool); } /** * アンチエイリアスの状態を返す */ public boolean getAntialias(){ return pref.getBoolean("antialias", false); } /** * ツールバーの状態を設定 */ public void setToolbar(boolean bool){ pref.putBoolean("toolbar", bool); } /** * ツールバーの状態を返す */ public boolean getToolbar(){ return pref.getBoolean("toolbar", false); } /** * 状態ウィンドウ表示の設定 */ public void setInfoWindow(boolean bool){ pref.putBoolean("infowindow", bool); } /** * 状態ウィンドウ表示の状態を返す */ public boolean getInfoWindow(){ return pref.getBoolean("infowindow", false); } /** * グリッド表示の設定 */ public void setGrid(boolean bool){ pref.putBoolean("grid", bool); } /** * グリッド表示の状態を返す */ public boolean getGrid(){ return pref.getBoolean("grid", false); } /** * 現在の設定を出力 */ public void savePreferences(String str){ try{ File file = new File(str); FileOutputStream fos = new FileOutputStream(file); pref.exportNode(fos); //XML書き出し fos.close(); }catch(IOException e){ System.err.println(e.getMessage()); System.exit(1); }catch(BackingStoreException e){ System.err.println(e.getMessage()); System.exit(1); } } /** * 設定ファイルの読み込み */ public void loadPreferences(String str){ File conf = new File(str); if(conf.exists()){ try{ FileInputStream fis = new FileInputStream(conf); Preferences.importPreferences(fis); fis.close(); }catch(FileNotFoundException e){ System.err.println(e.getMessage()); System.exit(1); }catch(IOException e){ System.err.println(e.getMessage()); System.exit(1); }catch(InvalidPreferencesFormatException e){ System.err.println(e.getMessage()); System.exit(1); }catch(Exception e){ System.err.println(e.getMessage()); System.exit(1); } } } } /** * 情報表示専用パネル */ class InfoPanel extends JPanel{ private final int CONST_FIGURE_LINE = MyPanel.CONST_FIGURE_LINE; private final int CONST_FIGURE_RECT = MyPanel.CONST_FIGURE_RECT; private final int CONST_FIGURE_ELLIPSE = MyPanel.CONST_FIGURE_ELLIPSE; private final int CONST_FIGURE_FILLRECT = MyPanel.CONST_FIGURE_FILLRECT; private final int CONST_FIGURE_FILLELLIPSE = MyPanel.CONST_FIGURE_FILLELLIPSE; private Color drawColor; private Color subColor; private Color backColor; private int figure; private float size; public InfoPanel(){ size = 1.0f; } public void setFigure(int figure){ this.figure = figure; repaint(); } public void setLineSize(float size){ this.size = size; repaint(); } public void setColor(Color main, Color sub, Color background){ drawColor = main; subColor = sub; backColor = background; repaint(); // 再描画 } public void paint(Graphics g){ super.paint(g); this.setBackground(backColor); Graphics2D g2 = (Graphics2D)g; // 図形の表示 g2.setColor(drawColor); g2.setStroke(new BasicStroke(size)); switch(figure){ case CONST_FIGURE_LINE: g2.draw(new Line2D.Double(10, 10, 40, 40)); break; case CONST_FIGURE_RECT: g2.draw(new Rectangle2D.Double(10, 10, 40, 40)); break; case CONST_FIGURE_ELLIPSE: g2.draw(new Ellipse2D.Double(10, 10, 40, 40)); break; case CONST_FIGURE_FILLRECT: g2.fill(new Rectangle2D.Double(10, 10, 40, 40)); break; case CONST_FIGURE_FILLELLIPSE: g2.fill(new Ellipse2D.Double(10, 10, 40, 40)); break; } } } JpegFileFilter.java
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class JpegFileFilter extends FileFilter{
public boolean accept(File file){
if(file.isDirectory()){
return true;
}
String fileName = file.getName().toLowerCase();
if(fileName.endsWith(".jpg")){
return true;
}else{
return false;
}
}
public String getDescription() {
return "jpegファイル(*.jpg)";
}
}
PngFileFilter.java
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class PngFileFilter extends FileFilter{
public boolean accept(File file){
if(file.isDirectory()){
return true;
}
String fileName = file.getName().toLowerCase();
if(fileName.endsWith(".png")){
return true;
}else{
return false;
}
}
public String getDescription(){
return "pngファイル(*.png)";
}
}
実行結果
Advertisement |
ショートカット・634・このカテゴリのトップページに戻る ・634labs UIコレクションギャラリー サイト検索Y!ログール |