开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。数据库采用:MySQL。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。无数据库,纯窗体模式,直接运行Main文件即可以

主要功能

本次分享的为Java编写的画图板,功能类似window自带的画板,主要的功能有以下一些

1 画笔功能:画直线、画曲线

2橡皮擦功能:类似window的橡皮擦,擦擦指定的区域

3 画矩形:拖动画一个矩形

4 画椭圆:拖动画一个椭圆

5 文字:在指定的区域编写文字,包括文字

6 设置:设置字体类型,字体大小、字体颜色,

7 打开图片,修改图片,保存图片

8 撤销和重做功能。可以对指定的操作重做,也可以指定的操作进行撤销

9 设置画图板的前景颜色和背景颜色

这个项目涵盖了java 窗体编程的各种知识,包括UI界面设计、时间处理、文件、事件处理操作等。通过这个项目能快速提升java 窗体编程,是非常好的一个项目。代码可以直接运行,没有任何bug。有详细的操作手册。

实现效果




graphics java 画图锯齿抖动 java画图工具_开发语言


graphics java 画图锯齿抖动 java画图工具_java_02


graphics java 画图锯齿抖动 java画图工具_java_03


graphics java 画图锯齿抖动 java画图工具_Powered by 金山文档_04


主要代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.font.*;

public class Painter extends JFrame implements ActionListener {
    private    Container c = getContentPane();
    private    String menuBar[]={"文件(F)","编辑(E)","视图(V)","说明(H)"};
    private    String menuItem[][]={
        {"新建(N)|78","打开(O)|79","保存(S)|83","另存为(A)","退出(X)|88"},
        {"撤消(U)|90","重复(R)|89","剪切(T)|87","复制(C)|68","粘贴(P)|85"},
        {"工具箱(T)|84","色块(C)|76","状态栏(S)","属性栏(M)"},
        {"关于画板(A)"}
    };
    private    JMenuItem jMenuItem[][]=new JMenuItem[4][5];
    private    JMenu jMenu[];
    private    JCheckBoxMenuItem jCheckBoxMenuItem[] = new JCheckBoxMenuItem[4];
    private    String ButtonName[]={"直线","矩开","椭圆","圆角矩形","贝氏曲线","扇型","多边形","铅笔","橡皮擦","文字","选取"};
    private JToggleButton jToggleButton[];
    private ButtonGroup buttonGroup;
    private    JPanel jPanel[]=new JPanel[5];//1绘图区,2工具箱,3色块,4属性栏
    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"};
    private    Icon tool[]=new ImageIcon[11];
    private    int i,j,show_x,show_y,drawMethod=7,draw_panel_width=700,draw_panel_height=500;
    private Paint color_border,color_inside;
    private SetPanel setPanel;
    private DrawPanel drawPanel;
    private UnderDrawPanel underDrawPanel;
    private ColorPanel colorPanel;
    private Stroke stroke;
    private Shape shape;
    private String isFilled;
    
    
    public Painter(){
        //设定JMenuBar,并产生MenuItem、并设置快捷键
        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 );
        c.setLayout( new BorderLayout() );
        for(i=0;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]);
            jToggleButton[i] = new JToggleButton(tool[i]);
            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]);
        jToggleButton[7].setSelected(true);
        jToolBar.setLayout( new GridLayout( 6, 2, 2, 2 ) );
        jPanel[2].add(jToolBar);
        
        jToolBar.setFloatable(false);//无法移动
        
        colorPanel=new ColorPanel();
        jPanel[3].setLayout(new FlowLayout(FlowLayout.LEFT));
        jPanel[3].add(colorPanel);
        
        drawPanel=new DrawPanel();
        underDrawPanel=new UnderDrawPanel();
        underDrawPanel.setLayout(null);
        underDrawPanel.add(drawPanel);
        drawPanel.setBounds(new Rectangle(2, 2, draw_panel_width, draw_panel_height));
        
        setPanel=new SetPanel();
        jPanel[4].add(setPanel);
        
        jPanel[0].setLayout( new BorderLayout() );
        jPanel[0].add(underDrawPanel,BorderLayout.CENTER);
        jPanel[0].add(jPanel[2],BorderLayout.WEST);
        jPanel[0].add(jPanel[3],BorderLayout.SOUTH);
        jPanel[0].add(jPanel[4],BorderLayout.EAST);
        
        jLabel[0].setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        underDrawPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        underDrawPanel.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);
        
        setSize(draw_panel_width,draw_panel_height);
        setTitle("七喜猫猫画板");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }




    @SuppressWarnings("deprecation")
    public void save(){
        FileDialog fileDialog = new FileDialog( new Frame() , "请指定一个文件名", FileDialog.SAVE );
        fileDialog.show();
        if(fileDialog.getFile()==null) return;
        drawPanel.filename = fileDialog.getDirectory()+fileDialog.getFile();
    }
    
    @SuppressWarnings("deprecation")
    public void actionPerformed( ActionEvent e ){
        for(i=0;i<ButtonName.length;i++){
            if(e.getSource()==jToggleButton[i]){
                drawMethod=i;
                if(drawMethod==5)
                    setPanel.pie_add_ctrl();
                else
                    setPanel.pie_remove_ctrl();
                if(drawMethod==7 || drawMethod==8)
                    setPanel.pencil_add_ctrl();
                else
                    setPanel.pencil_remove_ctrl();
                drawPanel.clear();
                drawPanel.repaint();
                   jMenuItem[1][2].setEnabled(false);
                   jMenuItem[1][3].setEnabled(false);
            }
        }
        
        if(e.getSource()==jMenuItem[1][0]){
            drawPanel.undo();
        }
        else if(e.getSource()==jMenuItem[1][1]){
            drawPanel.redo();
        }
        else if(e.getSource()==jMenuItem[1][2]){
            drawPanel.cut();
        }
        else if(e.getSource()==jMenuItem[1][3]){
            drawPanel.copy();
        }
        else if(e.getSource()==jMenuItem[1][4]){
            drawPanel.paste();
        }
        else if(e.getSource()==jMenuItem[0][0]){//开新文档
            underDrawPanel.remove(drawPanel);
            drawPanel=null;
            drawPanel=new DrawPanel();
            underDrawPanel.add(drawPanel);
            drawPanel.setBounds(new Rectangle(2, 2, draw_panel_width, draw_panel_height));
            underDrawPanel.ctrl_area.setLocation(draw_panel_width+3,draw_panel_height+3);
            underDrawPanel.ctrl_area2.setLocation(draw_panel_width+3,draw_panel_height/2+3);
            underDrawPanel.ctrl_area3.setLocation(draw_panel_width/2+3,draw_panel_height+3);
            repaint();
        }
        else if(e.getSource()==jMenuItem[0][1]){//开启旧文档
            FileDialog fileDialog = new FileDialog( new Frame() , "选择一个文档", FileDialog.LOAD );
            fileDialog.show();
            if(fileDialog.getFile()==null) return;
            
            underDrawPanel.removeAll();
            drawPanel=null;
            drawPanel=new DrawPanel();
            underDrawPanel.add(drawPanel);
            drawPanel.setBounds(new Rectangle(2, 2, draw_panel_width, draw_panel_height));
            
            drawPanel.openfile(fileDialog.getDirectory()+fileDialog.getFile());
        }
        else if(e.getSource()==jMenuItem[0][2]){//存储档案
            if(drawPanel.filename==null){
                save();
            }
            else{
                try{
                    int dotpos = drawPanel.filename.lastIndexOf('.');
                    ImageIO.write(drawPanel.bufImg, drawPanel.filename.substring(dotpos + 1), new File(drawPanel.filename));
                }
                catch(IOException even) {
                    JOptionPane.showMessageDialog(null, even.toString(),"无法存储文档", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        else if(e.getSource()==jMenuItem[0][3]){//另存新档
            save();
            try{
                int dotpos = drawPanel.filename.lastIndexOf('.');
                ImageIO.write(drawPanel.bufImg, drawPanel.filename.substring(dotpos + 1), new File(drawPanel.filename));
            }
            catch(IOException even) {
                JOptionPane.showMessageDialog(null, even.toString(),"无法保存", JOptionPane.ERROR_MESSAGE);
            }
        }
        else if(e.getSource()==jMenuItem[0][4]){//离开
            System.exit(0);
        }
        else if(e.getSource()==jMenuItem[3][0]){//关于
            JOptionPane.showMessageDialog(null, "七喜猫猫稳定版 ", "七喜笨猫", 1, new ImageIcon("img/paint.gif"));
        }
        for(i=0;i<2;i++){
            if(jCheckBoxMenuItem[i].isSelected())
                jPanel[i+2].setVisible( true );
               else
                   jPanel[i+2].setVisible( false );
           }
           if(jCheckBoxMenuItem[3].isSelected()){
               setPanel.setVisible( true );
               jPanel[4].setVisible( true );
           }
           else{
               setPanel.setVisible( false );
               jPanel[4].setVisible( false );
           }
        if(jCheckBoxMenuItem[2].isSelected())
            jLabel[0].setVisible( true );
           else
               jLabel[0].setVisible( false );
    }
    
        public void actionPerformed( ActionEvent e ){
            jDialog.dispose();
        }
        public void itemStateChanged( ItemEvent e ){
            if ( e.getSource() == bold )
                if ( e.getStateChange() == ItemEvent.SELECTED )
                    valBold = Font.BOLD;
                else
                    valBold = Font.PLAIN;
            if ( e.getSource() == italic )
                if ( e.getStateChange() == ItemEvent.SELECTED )
                    valItalic = Font.ITALIC;
                else
                    valItalic = Font.PLAIN;
        }
        
        public Dimension getPreferredSize(){
            return new Dimension( draw_panel_width, draw_panel_height );
        }
        
        public void openfile(String filename){//开启旧档
            Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
            ImageIcon icon = new ImageIcon(filename);
            g2d_bufImg.drawImage(icon.getImage(),0,0,this);
            
            count++;
            bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
            g2d_bufImg_data.drawImage(bufImg,0,0,this);
            
            repaint();
        }
        
        public void undo(){//复原
               count--;
            
               draw_panel_width=bufImg_data[count].getWidth();
               draw_panel_height=bufImg_data[count].getHeight();
               drawPanel.setSize(draw_panel_width,draw_panel_height);

            bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
            jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用来绘图
            this.removeAll();
            this.add(jlbImg);
            jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
            
            Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
            g2d_bufImg.setPaint(Color.white);
            g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
            g2d_bufImg.drawImage(bufImg_data[count],0,0,this);

            underDrawPanel.ctrl_area.setLocation(draw_panel_width+3,draw_panel_height+3);
            underDrawPanel.ctrl_area2.setLocation(draw_panel_width+3,draw_panel_height/2+3);
            underDrawPanel.ctrl_area3.setLocation(draw_panel_width/2+3,draw_panel_height+3);
            
            underDrawPanel.x=draw_panel_width;
            underDrawPanel.y=draw_panel_height;
            
               if(count<=0)
                   jMenuItem[1][0].setEnabled(false);
            jMenuItem[1][1].setEnabled(true);
            cut=3;
               repaint();
           }

        public void redo(){//重做
            count++;
            
               draw_panel_width=bufImg_data[count].getWidth();
               draw_panel_height=bufImg_data[count].getHeight();
               drawPanel.setSize(draw_panel_width,draw_panel_height);

            bufImg = new BufferedImage(draw_panel_width, draw_panel_height,BufferedImage.TYPE_3BYTE_BGR);
            jlbImg = new JLabel(new ImageIcon(bufImg));//在JLabel上放置bufImg,用来绘图
            this.removeAll();
            this.add(jlbImg);
            jlbImg.setBounds(new Rectangle(0, 0, draw_panel_width, draw_panel_height));
            
            Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
            g2d_bufImg.setPaint(Color.white);
            g2d_bufImg.fill(new Rectangle2D.Double(0,0,draw_panel_width,draw_panel_height));
            g2d_bufImg.drawImage(bufImg_data[count],0,0,this);

            underDrawPanel.ctrl_area.setLocation(draw_panel_width+3,draw_panel_height+3);
            underDrawPanel.ctrl_area2.setLocation(draw_panel_width+3,draw_panel_height/2+3);
            underDrawPanel.ctrl_area3.setLocation(draw_panel_width/2+3,draw_panel_height+3);
            
            underDrawPanel.x=draw_panel_width;
            underDrawPanel.y=draw_panel_height;
            
            if(redo_lim<count)
                jMenuItem[1][1].setEnabled(false);
            jMenuItem[1][0].setEnabled(true);
            cut=3;
               repaint();
           }
        
        public void cut(){
            bufImg_cut = new BufferedImage((int)rectangle2D_select.getWidth(), (int)rectangle2D_select.getHeight(),  
BufferedImage.TYPE_3BYTE_BGR);
            BufferedImage copy = bufImg.getSubimage((int)rectangle2D_select.getX(),(int)rectangle2D_select.getY(),(int) 
rectangle2D_select.getWidth(),(int)rectangle2D_select.getHeight());
            Graphics2D g2d_bufImg_cut = (Graphics2D) bufImg_cut.createGraphics();
            g2d_bufImg_cut.drawImage(copy,0,0,this);
            
            Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
            g2d_bufImg.setPaint(Color.WHITE);
            g2d_bufImg.fill(new Rectangle2D.Double((int)rectangle2D_select.getX(),(int)rectangle2D_select.getY(),(int) 
rectangle2D_select.getWidth(),(int)rectangle2D_select.getHeight()));
            
            redo_lim=count++;
            jMenuItem[1][1].setEnabled(false);
            
               //新增一张BufferedImage型态至bufImg_data[count],并将bufImg绘制至bufImg_data[count]//
               bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
               Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
               g2d_bufImg_data.drawImage(bufImg,0,0,this);

            //判断坐标为新起点//
            press=0;
            
            //让复原MenuItem可以点选//
               if(count>0)
                   jMenuItem[1][0].setEnabled(true);
               jMenuItem[1][2].setEnabled(false);
               jMenuItem[1][3].setEnabled(false);
            jMenuItem[1][4].setEnabled(true);
            cut=3;
            repaint();
        }
        public void copy(){
            bufImg_cut = new BufferedImage((int)rectangle2D_select.getWidth(), (int)rectangle2D_select.getHeight(),  
BufferedImage.TYPE_3BYTE_BGR);
            BufferedImage copy = bufImg.getSubimage((int)rectangle2D_select.getX(),(int)rectangle2D_select.getY(),(int) 
rectangle2D_select.getWidth(),(int)rectangle2D_select.getHeight());
            Graphics2D g2d_bufImg_cut = (Graphics2D) bufImg_cut.createGraphics();
            g2d_bufImg_cut.drawImage(copy,0,0,this);
            jMenuItem[1][4].setEnabled(true);
            cut=1;
            repaint();
        }
        public void paste(){
            cut=2;
            repaint();
        }
        public void mousePressed(MouseEvent e) {
            x1=e.getX();
            y1=e.getY();
            if(first==0){
                polygon = new Polygon();
                polygon.addPoint(x1, y1);
                first=1;
            }
            //判断坐标为新起点//
            press=1;
            chk=0;
            if(cut!=2) cut=0;
        }

        public void mouseReleased(MouseEvent e) {
            x2=e.getX();
            y2=e.getY();
            
            if(step_chk==0)//控制贝氏曲线用
                step=1;
            else if(step_chk==1)
                step=2;
            
            if(step_chk_arc==0)//控制扇型用
                chk=step_arc=1;
            else if(step_chk_arc==1)
                chk=step_arc=2;
            
            if(drawMethod==6 && click!=1){
                polygon.addPoint(x2, y2);
                repaint();
            }
            if(drawMethod==10){
                if(cut!=2) cut=1;
                select_x=(int)rectangle2D_select.getX();
                select_y=(int)rectangle2D_select.getY();
                select_w=(int)rectangle2D_select.getWidth();
                select_h=(int)rectangle2D_select.getHeight();
                jMenuItem[1][2].setEnabled(true);
                jMenuItem[1][3].setEnabled(true);
            }

            if((step_chk==2 && step==2) || (step_chk_arc==2 && step_arc==2) || drawMethod==0 || drawMethod==1 || drawMethod==2 || drawMethod==3 ||  
drawMethod==7 || drawMethod==8 || drawMethod==9 || cut==2){//当不是画贝氏曲线或是已经完成贝氏曲线时画
                toDraw();
            }
        }
        public void clear(){
            cut=select_x=select_y=select_w=select_h=step_chk_arc=step_arc=first=step_chk=step=0;
            x1=x2=y1=y2=-1;
        }
        
        public void toDraw(){
            if(x1<0 || y1<0) return;//防止误按
            chk=3;
            draw(x1,y1,x2,y2);
            
            //画出图形至bufImg//
            Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
            if(cut!=2){
                if(color_inside!=null && drawMethod!=8){
                    g2d_bufImg.setPaint(color_inside);
                    g2d_bufImg.fill(shape);
                }
                if(color_border!=null && drawMethod!=8){
                    g2d_bufImg.setPaint(color_border);
                    g2d_bufImg.setStroke(stroke);
                    g2d_bufImg.draw(shape);
                }
            }
            else{
                   g2d_bufImg.drawImage(bufImg_cut,x2,y2,this);
            }
            repaint();
            clear();
            //记录可重做最大次数,并让重做不可按//
            redo_lim=count++;
            jMenuItem[1][1].setEnabled(false);
            
               //新增一张BufferedImage型态至bufImg_data[count],并将bufImg绘制至bufImg_data[count]//
               bufImg_data[count] = new BufferedImage(draw_panel_width, draw_panel_height, BufferedImage.TYPE_3BYTE_BGR);
               Graphics2D g2d_bufImg_data = (Graphics2D) bufImg_data[count].getGraphics();
               g2d_bufImg_data.drawImage(bufImg,0,0,this);
               
            //判断坐标为新起点//
            press=0;
            
            //让复原MenuItem可以点选//
               if(count>0)
                   jMenuItem[1][0].setEnabled(true);
        }
        
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mouseClicked(MouseEvent e){
            if(click==1){//连点两下时
                toDraw();
            }
            click=1;
        }
        
        public void mouseDragged(MouseEvent e){
            x2=e.getX();
            y2=e.getY();
            if(drawMethod==7 || drawMethod==8){
                draw(x1,y1,x2,y2);
                x1=e.getX();
                y1=e.getY();
            }
            if(drawMethod!=9)
                repaint();
        }

        public void mouseMoved(MouseEvent e) {
            show_x=x2=e.getX();
            show_y=y2=e.getY();
            
            jLabel[0].setText(show_x+","+show_y);
            click=0;
            if(drawMethod==7 || drawMethod==8 || cut==2)
                repaint();
        }
        
        public void draw(int input_x1,int input_y1,int input_x2,int input_y2){
            if(drawMethod==0){//直线时,让shape为Line2D
                shape=line2D;
                line2D.setLine(input_x1,input_y1,input_x2,input_y2);
            }
            else if(drawMethod==1){//矩型时,让shape为Rectangle2D
                shape=rectangle2D;
                rectangle2D.setRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1- 
input_y2));
            }
            else if(drawMethod==2){//椭圆时
                shape=ellipse2D;
                ellipse2D.setFrame(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1- 
input_y2));
            }
            else if(drawMethod==3){//圆角矩型
                shape=roundRectangle2D;
                roundRectangle2D.setRoundRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs 
(input_y1-input_y2),10.0f,10.0f);
            }
            else if(drawMethod==4){//贝氏曲线
                shape=cubicCurve2D;
                if(step==0){
                    cubicCurve2D.setCurve(input_x1,input_y1,input_x1,input_y1,input_x2,input_y2,input_x2,input_y2);
                    temp_x1=input_x1;
                    temp_y1=input_y1;
                    temp_x2=input_x2;
                    temp_y2=input_y2;
                    step_chk=0;
                }
                else if(step==1){
                    cubicCurve2D.setCurve(temp_x1,temp_y1,input_x2,input_y2,input_x2,input_y2,temp_x2,temp_y2);
                    temp_x3=input_x2;
                    temp_y3=input_y2;
                    step_chk=1;
                }
                else if(step==2){
                    cubicCurve2D.setCurve(temp_x1,temp_y1,temp_x3,temp_y3,input_x2,input_y2,temp_x2,temp_y2);
                    step_chk=2;
                }
            }
            else if(drawMethod==5){//扇型,chk用来防止意外的repaint//
                if(step_arc==0 || chk==1){//步骤控制
                    shape=ellipse2D;
                    ellipse2D.setFrame(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs 
(input_y1-input_y2));
                    temp_x1=input_x1;
                    temp_y1=input_y1;
                    temp_x2=input_x2;
                    temp_y2=input_y2;
                    step_chk_arc=0;
                }
                else if(step_arc==1 || chk==2){//步骤控制
                    shape=arc2D;

                    center_point_x = Math.min(temp_x1,temp_x2)+Math.abs(temp_x1-temp_x2)/2;
                    center_point_y = Math.min(temp_y1,temp_y2)+Math.abs(temp_y1-temp_y2)/2;
                    
                    double a = Math.pow(Math.pow(input_x2-center_point_x,2)+Math.pow(input_y2-center_point_y,2),0.5);
                    double b = input_x2-center_point_x;
                    if(input_y2>center_point_y)
                        start=360+Math.acos(b/a)/Math.PI*-180;
                    else
                        start=Math.acos(b/a)/Math.PI*180;
                    
                    arc2D.setArc(Math.min(temp_x1,temp_x2),Math.min(temp_y1,temp_y2),Math.abs(temp_x1-temp_x2),Math.abs(temp_y1- 
temp_y2),start,0,pie_shape);
                    step_chk_arc=1;
                }
                else if(step_arc==2 || chk==3){//步骤控制
                    shape=arc2D;
                    
                    double a = Math.pow(Math.pow(input_x2-center_point_x,2)+Math.pow(input_y2-center_point_y,2),0.5);
                    double b = input_x2-center_point_x;
                    if(input_y2>center_point_y)
                        end=360+Math.acos(b/a)/Math.PI*-180-start;
                    else
                        end=Math.acos(b/a)/Math.PI*180-start;
                    if(end<0){end=360-Math.abs(end);}
                    
                    arc2D.setArc(Math.min(temp_x1,temp_x2),Math.min(temp_y1,temp_y2),Math.abs(temp_x1-temp_x2),Math.abs(temp_y1- 
temp_y2),start,end,pie_shape);
                    step_chk_arc=2;
                }
            }
            else if(drawMethod==6){//多边型
                shape=polygon;
            }
            else if(drawMethod==7 || drawMethod==8){//任意线&橡皮擦
                Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
                
                shape=line2D;
                line2D.setLine(input_x1,input_y1,input_x2,input_y2);
                if(drawMethod==7)
                    g2d_bufImg.setPaint(color_border);
                else
                    g2d_bufImg.setPaint(Color.white);
                g2d_bufImg.setStroke(stroke);
                g2d_bufImg.draw(shape);
            }
            
            else if(drawMethod==9){//文字
                Graphics2D g2d_bufImg = (Graphics2D) bufImg.getGraphics();
                FontRenderContext frc = g2d_bufImg.getFontRenderContext();
                jDialog.show();
                
                Font f = new Font(textField_font.getText(),valBold + valItalic,size);
                TextLayout tl = new TextLayout(textField_word.getText(), f, frc);
                double sw = tl.getBounds().getWidth();
                double sh = tl.getBounds().getHeight();

                AffineTransform Tx = AffineTransform.getScaleInstance(1, 1);
                Tx.translate(input_x2,input_y2+sh);
                shape = tl.getOutline(Tx);
            }
            else if(drawMethod==10){//选取工具
                shape=rectangle2D;
                rectangle2D.setRect(Math.min(input_x1,input_x2),Math.min(input_y1,input_y2),Math.abs(input_x1-input_x2),Math.abs(input_y1- 
input_y2));
            }
            if(color_border instanceof GradientPaint){//使用渐层填色读取拖拉坐标
                color_border = new GradientPaint( input_x1,input_y1, (Color)((GradientPaint)color_border).getColor1(), input_x2,input_y2,  
(Color)((GradientPaint)color_border).getColor2(), true );
            }
            if(color_inside instanceof GradientPaint){
                color_inside = new GradientPaint( input_x1,input_y1, (Color)((GradientPaint)color_inside).getColor1(), input_x2,input_y2,  
(Color)((GradientPaint)color_inside).getColor2(), true );
            }
        }
        
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            super.paint(g2d);//重绘底层JPanel以及上面所有组件

            if(press==1 && drawMethod!=10 && !(x1<0 || y1<0)) {//绘图在最上面的JLabel上,并判断是不是起点才画
                draw(x1,y1,x2,y2);
                if(drawMethod==8) return;
                if(color_inside!=null){
                    g2d.setPaint(color_inside);
                    g2d.fill(shape);
                }
                if(color_border!=null){
                    g2d.setPaint(color_border);
                    g2d.setStroke(stroke);
                    g2d.draw(shape);
                }
            }

            if(drawMethod==10 && cut==0){//选取控制、判断是否选取、剪下、或贴上
                g2d.setPaint(Color.black);
                g2d.setStroke(basicStroke_select);
                rectangle2D_select.setRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
                g2d.draw(rectangle2D_select);
            }
            if(cut==1){
                g2d.setPaint(Color.black);
                g2d.setStroke(basicStroke_select);
                rectangle2D_select.setRect(select_x,select_y,select_w,select_h);
                g2d.draw(rectangle2D_select);
            }
            if(cut==2){
                   g2d.drawImage(bufImg_cut,x2,y2,this);
               }

            //跟随游标的圆形//
            if(drawMethod==7 || drawMethod==8){
                g2d.setPaint(Color.black);
                g2d.setStroke(basicStroke_pen);
                ellipse2D_pan.setFrame(x2-setPanel.number/2,y2-setPanel.number/2,setPanel.number,setPanel.number);
                g2d.draw(ellipse2D_pan);
            }
        }
    }

    public static void main( String args[] ){
        try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch(Exception e){e.printStackTrace();}
        
        Painter app = new Painter();
        app.setVisible(true);
        app.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
}