JFC の おえかき。機能追加2AdvertisementSwing + Java2D
グリッド線表示
メニュー作りクラス jpg png 書き出し 描画をiteratorにした あとなんだっけ。
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(0);
}
MyFrame mf = new MyFrame();
mf.setTitle("JFC ペイント");
mf.setSize(600, 500);
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("開く", "open", this));
menu.add(Create.createMenuItem("保存", "save", this));
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("画面消去", "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 & 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("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("clear")){
panel.setVector(new Vector());
panel.repaint();
}
//設定メニュー
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{
Point start;
Point end;
private int figureFlag; // 図形のフラグ
private boolean antialiasFlag; // アンチエイリアス
private boolean gridFlag; // グリッド表示
private Color drawColor; // 描画色
private Color subColor; // 補助色
private float size; // 線の太さ
private Vector figureBox; // 図形コレクション
public MyPanel(){
drawColor = Color.black;
subColor = Color.cyan;
antialiasFlag = false;
gridFlag = false;
figureFlag = 1;
size = 1.0f;
figureBox = new Vector();
start = new Point();
end = new Point();
setBackground(Color.white);
addMouseListener(this);
addMouseMotionListener(this);
}
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){}
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())));
}
}
final class MyJFileChooser{
private static JFileChooser chooser = new JFileChooser();
private MyJFileChooser(){};
public static JFileChooser getInstance(){
return chooser;
}
}
final class MyJColorChooser{
private static JColorChooser chooser = new JColorChooser();
private MyJColorChooser(){};
public static JColorChooser getInstance(){
return chooser;
}
}
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 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;
}
}
実行結果サンプル
Advertisement |
ショートカット・634トップページ・このカテゴリのトップページに戻る ・634ラボ UIコレクションギャラリー サイト検索Y!ログール |