package Painter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Painter extends JFrame implements ActionListener{
private boolean hasDraw=false;
private Container c = getContentPane();
private String menuBar[]={"文件(F)","编辑(E)","视图(V)","说明(H)"};
private String menuItem[][]={
{"新建(N)|78","打开(O)|79","保存(S)|83","另存为(A)","退出(X)|69"},
{"撤消(U)|90","重复(R)|89","剪切(T)|88","复制(C)|67","粘贴(P)|86"},
{"工具栏(T)|84","色块(C)|75","状态栏(S)"},
{"关于(A)"}
};
private JMenuItem jMenuItem[][]=new JMenuItem[4][5];
private JMenu jMenu[];
private JCheckBoxMenuItem jCheckBoxMenuItem[] = new JCheckBoxMenuItem[3];
private String ButtonName[]={"直线","矩形","椭圆","圆角矩形","贝氏曲线","扇型","多边形","铅笔","橡皮擦","文字","选取","喷绘"};
private JToggleButton jToggleButton[];
private ButtonGroup buttonGroup;
private JPanel jPanel[]=new JPanel[5];
private JLabel jLabel[]=new JLabel[1];//状态列
private String toolname[]={"img/tool1.gif","img/tool2.gif","img/tool3.gif","img/tool4.gif","img/tool5.gif","img/tool8.gif","img/tool9.gif","img/tool7.gif","img/tool6.gif","img/tool10.gif","img/tool11.gif","img/tool12.gif"};
private Icon tool[]=new ImageIcon[12];
private int i,j,show_x,show_y,drawMethod=7;
public int draw_panel_width=700;
public int draw_panel_height=500;
public Paint color_border;
public Paint color_inside;
private Setter setPanel;
public PaintFiled drawPanel;
private ResizeBlock resizeBlock;
private ColorBox colorPanel;
public Stroke stroke;
private Shape shape;
private String isFilled;
private BufferedImage backGround;
public Painter(){
//设定JMenuBar,并产生JMenuItem、设置快捷键
JMenuBar bar = new JMenuBar();
jMenu=new JMenu[menuBar.length];
for(i=0;i<menuBar.length;i++){
jMenu[i] = new JMenu(menuBar[i]);
jMenu[i].setMnemonic(menuBar[i].split("\\(")[1].charAt(0));
bar.add(jMenu[i]);
}
for(i=0;i<menuItem.length;i++){
for(j=0;j<menuItem[i].length;j++){
if(i==0 && j==4 || i==1 && j==2) jMenu[i].addSeparator();
if(i!=2){
jMenuItem[i][j] = new JMenuItem(menuItem[i][j].split("\\|")[0]);
if(menuItem[i][j].split("\\|").length!=1)
jMenuItem[i][j].setAccelerator(KeyStroke.getKeyStroke(Integer.parseInt(menuItem[i][j].split("\\|")[1]), ActionEvent.CTRL_MASK) );
jMenuItem[i][j].addActionListener(this);
jMenuItem[i][j].setMnemonic(menuItem[i][j].split("\\(")[1].charAt(0));
jMenu[i].add(jMenuItem[i][j]);
}
else{
jCheckBoxMenuItem[j] = new JCheckBoxMenuItem(menuItem[i][j].split("\\|")[0]);
if(menuItem[i][j].split("\\|").length!=1)
jCheckBoxMenuItem[j].setAccelerator(KeyStroke.getKeyStroke(Integer.parseInt(menuItem[i][j].split("\\|")[1]), ActionEvent.CTRL_MASK) );
jCheckBoxMenuItem[j].addActionListener(this);
jCheckBoxMenuItem[j].setMnemonic(menuItem[i][j].split("\\(")[1].charAt(0));
jCheckBoxMenuItem[j].setSelected( true );
jMenu[i].add(jCheckBoxMenuItem[j]);
}
}
}
this.setJMenuBar(bar);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
confirm();
}
});
try {
backGround=ImageIO.read(this.getClass().getResource("img/board.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
c.setLayout( new BorderLayout() );
jPanel[0]=new JPanel(){
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
Paint p=new TexturePaint(backGround,new Rectangle2D.Double(0,0,100,100));
g2d.setPaint(p);
g2d.fill(this.getBounds());
//
super.paint(g);
}
};
for(i=1;i<5;i++)
jPanel[i]=new JPanel();
jLabel[0]=new JLabel(" 状态栏");
buttonGroup = new ButtonGroup();
JToolBar jToolBar=new JToolBar("工具箱",JToolBar.VERTICAL);
jToggleButton=new JToggleButton[ButtonName.length];
for(i=0;i<ButtonName.length;i++){
//tool[i] = new ImageIcon(toolname[i]);
java.net.URL url=this.getClass().getResource(toolname[i]);
if(url==null)System.out.print(i);
tool[i]=new ImageIcon(url);
jToggleButton[i] = new JToggleButton(tool[i]);
if(i==6)
jToggleButton[i].setToolTipText("按Ctrl并单击完成最后一个操作");
jToggleButton[i].addActionListener( this );
jToggleButton[i].setFocusable( false );
buttonGroup.add(jToggleButton[i]);
}
jToolBar.add(jToggleButton[7]);
jToolBar.add(jToggleButton[8]);
jToolBar.add(jToggleButton[0]);
jToolBar.add(jToggleButton[4]);
jToolBar.add(jToggleButton[1]);
jToolBar.add(jToggleButton[3]);
jToolBar.add(jToggleButton[2]);
jToolBar.add(jToggleButton[5]);
jToolBar.add(jToggleButton[6]);
jToolBar.add(jToggleButton[9]);
jToolBar.add(jToggleButton[10]);
jToolBar.add(jToggleButton[11]);
jToggleButton[7].setSelected(true);
jToolBar.setLayout( new GridLayout( 6,2, 2, 2 ) );
jPanel[2].add(jToolBar);
jToolBar.setFloatable(false);//不可以动
colorPanel=new ColorBox();
colorPanel.painter=this;
color_border=new Color(0,0,0);
color_inside=null;
jPanel[3].setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel[3].add(colorPanel);
drawPanel=new PaintFiled();
resizeBlock=new ResizeBlock();
//resizeBlock.setLayout(null);
resizeBlock.add(drawPanel);
drawPanel.setBounds(new Rectangle(2, 2, draw_panel_width, draw_panel_height));
final JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent arg0) {
// TODO Auto-generated method stub
if(tabbedPane.getSelectedIndex()==1)
{
jPanel[2].setVisible(false);
}
else
setPanel.setVisible(false);
}});
setPanel=new Setter();
setPanel.painter=this;
tabbedPane.addTab("设置",null, setPanel);
tabbedPane.addTab("工具箱",null, jPanel[2]);
jPanel[4].add(tabbedPane);
//jPanel[4].add(setPane);
jPanel[2].setPreferredSize(new Dimension(100,100));
jPanel[0].setLayout( new BorderLayout() );
jPanel[0].add(resizeBlock,BorderLayout.CENTER);
//jPanel[0].add(jPanel[2],BorderLayout.WEST);
jPanel[0].add(jPanel[3],BorderLayout.WEST);
jPanel[0].add(jPanel[4],BorderLayout.EAST);
jLabel[0].setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
resizeBlock.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
resizeBlock.setBackground(new Color(128,128,128));
jPanel[3].setBorder(BorderFactory.createMatteBorder(1,0,0,0,new Color(172,168,153)));
c.add(jPanel[0],BorderLayout.CENTER);
c.add(jLabel[0],BorderLayout.SOUTH);
stroke=new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
setSize(draw_panel_width,draw_panel_height);
setTitle("画图板");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
}
public void save(){
FileDialog fileDialog = new F