⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minidrawpad.java~28~

📁 自制小画板!为JBUILDER版本。相当具有参考价值
💻 JAVA~28~
📖 第 1 页 / 共 4 页
字号:
        jToolBar.add(Stroke);
        jToolBar.add(Word);
        jToolBar.add(blod);
        jToolBar.add(italic);
        jToolBar.add(style);
        jToolBar.add(size);
        contentPane.add(jToolBar, java.awt.BorderLayout.NORTH);
        jMenuFile.add(jMenuFileNew);
        jMenuFile.add(jMenuFileOpen);
        jMenuFile.add(jMenuFileSave);
        jMenuFile.addSeparator();
        jMenuFile.add(jMenuFileExit);
        jMenuColor.add(jMenuColorChoose);
        jMenuStroke.add(jMenuStrokeChoose);
        jMenuHelp.add(jMenuAbout);
        contentPane.add(drawingArea, java.awt.BorderLayout.CENTER);
        contentPane.add(statusBar, java.awt.BorderLayout.SOUTH);
        jPopupMenu1.add(jMenuItem1);
        jPopupMenu1.add(jMenuItem2);
        jPopupMenu1.add(jMenuItem3);
        createNewItem();
        //show();
    }
    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
        MiniDrawPad_AboutBox dlg = new MiniDrawPad_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.pack();
        dlg.setVisible(true);
    }

    /**鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作**/
            class mouseA extends MouseAdapter{				//鼠标事件mouseA类,继承了MouseAdapter
                    public void mousePressed(MouseEvent e){		//定义鼠标按下事件
                            statusBar.setText("当前坐标在:["+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                                                                                                            //设置状态提示
                            itemList[index].x1 = itemList[index].x2 = e.getX();//设定坐标X轴
                            itemList[index].y1 = itemList[index].y2 = e.getY();//设定坐标Y轴

    /**如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作**/
                            if(currentChoice == 3 || currentChoice == 13){
                                    itemList[index].x1 = itemList[index].x2 = e.getX();//设定坐标X轴
                                    itemList[index].y1 = itemList[index].y2 = e.getY();//设定坐标Y轴
                                    index++;							//指针加1
                                    createNewItem();					//创建新图
                            }

    /**如果当前选择的图形式文字输入,则进行下面操作**/
                            if(currentChoice==18){
                                    itemList[index].x1 = e.getX();		//设定坐标X轴
                                    itemList[index].y1 = e.getY();		//设定坐标Y轴

                                    String input;						//输入字符串
                                    input = JOptionPane.showInputDialog(
                                            "请键入文字!");				//显示输入对话框内容
                                    itemList[index].s1 = input;
                                    itemList[index].x2 = f1;			//设定坐标X轴
                                    itemList[index].y2 = f2;			//设定坐标Y轴
                                    itemList[index].s2 = style1;		//设定字体风格

                                    index++;							//指针加1
                                    currentChoice = 18;
                                    createNewItem();					//创建新图
                                    drawingArea.repaint();				//图形式文字输入
                            }
                    }
    /**鼠标释放**/
                    public void mouseReleased(MouseEvent e){	//定义鼠标释放
                            statusBar.setText("当前坐标在:["+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                                                                                                            //设置状态提示
                            if(currentChoice == 3 || currentChoice == 13){
                                    itemList[index].x1 = e.getX();		//设定坐标X轴
                                    itemList[index].y1 = e.getY();		//设定坐标Y轴
                            }
                            itemList[index].x2 = e.getX();			//设定坐标X轴
                            itemList[index].y2 = e.getY();			//设定坐标Y轴
                            repaint();
                            index++;								//指针加1
                            createNewItem();						//创建新图
                    }
    /**鼠标进入**/
                    public void mouseEntered(MouseEvent e){		//定义鼠标进入
                            statusBar.setText("当前坐标在:"+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                    }											//设置状态提示
    /**鼠标退出**/
                    public void mouseExited(MouseEvent e){		////定义鼠标退出
                            statusBar.setText("当前坐标在:"+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                    }											//设置状态提示
    /**鼠标右键**/
                    public void mouseClicked(MouseEvent e){		//定义鼠标右键
                            statusBar.setText("当前坐标在:"+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                            int mods=e.getModifiers();				//设置状态提示
                            if((mods&InputEvent.BUTTON3_MASK)!=0){	//右键判断
                                    jPopupMenu1.show(drawingArea,e.getX(),e.getY());
                                                                                                            //显示快捷菜单
                            }
                    }
            }//mouseA完成

    /**鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作**/
            class mouseB extends MouseMotionAdapter{
    /**鼠标拖移**/
                    public void mouseDragged(MouseEvent e){
                            statusBar.setText("当前坐标在:["+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                                                                                                            //设置状态提示
                            if(currentChoice == 3 || currentChoice == 13){
                                    itemList[index-1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX();//设定坐标X轴
                                    itemList[index-1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY();//设定坐标Y轴
                                    index++;							//指针加1
                                    createNewItem();					//创建新图
                            }
                            else{
                                    itemList[index].x2 = e.getX();		//设定坐标X轴
                                    itemList[index].y2 = e.getY();		//设定坐标Y轴
                            }
                            repaint();								//重画
                    }
    /**鼠标移动**/
                    public void mouseMoved(MouseEvent e){
                            statusBar.setText("当前坐标在:["+e.getX()+","+e.getY()+"]"+"已经画了"+index+"个图...");
                    }											//设置状态提示
            }

    /**选择字体风格时候用到的时间侦听器类,加入到字体风格的选择框中**/
                                                                                                            //checkBoxHandler类继承ItemListener
            private class checkBoxHandler implements ItemListener{
                    public void itemStateChanged(ItemEvent e){	//定义字体改变事件
                            if(e.getSource() == blod)
                                    if(e.getStateChange() == ItemEvent.SELECTED)
                                            f1 = Font.BOLD;
                                    else
                                            f1 = Font.PLAIN;
                            if(e.getSource() == italic)
                                    if(e.getStateChange() == ItemEvent.SELECTED)
                                            f2 = Font.ITALIC;
                                    else
                                            f2 = Font.PLAIN;
                    }
            }

    /**画图板面板类,用来画图**/
            class DrawPanel extends JPanel{					//画图面板类继承JPanel
    /**定义画图面板**/
                    public DrawPanel(){							//定义画图面板
                            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                                                                                                            //设置光标
                            setBackground(Color.white);				//设置背景色为白色
                            addMouseListener(new mouseA());			//添加鼠标mouseA类侦听
                            addMouseMotionListener(new mouseB());	//添加鼠标mouseb类侦听
                    }
                    public void paintComponent(Graphics g){		//调用该UI委托的paint方法
                            super.paintComponent(g);				//定义绘画组件
                            Graphics2D g2d = (Graphics2D)g;			//定义画笔

                            int j = 0;
                            while(j <= index){
                                    draw(g2d,itemList[j]);
                                    j++;
                            }
                    }
                    void draw(Graphics2D g2d,drawings i){
                            i.draw(g2d);							//将画笔传入到各个子类中,用来完成各自的绘图
                    }
            }

    /**新建一个画图基本单元对象的程序段**/
            void createNewItem(){
                    if(currentChoice == 18)						//进行相应的游标设置
                            drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
                    else
                            drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

                    switch(currentChoice){
                            case 3:
                                    itemList[index] = new Pencil();		//随笔化
                            break;
                            case 4:
                                    itemList[index] = new Line();		//直线
                            break;
                            case 5:
                                    itemList[index] = new Rect();		//矩形
                            break;
                            case 6:
                                    itemList[index] = new fillRect();	//填充矩形
                            break;
                            case 7:
                                    itemList[index] = new Oval();		//椭圆
                            break;
                            case 8:
                                    itemList[index] = new fillOval();	//填充椭圆
                            break;
                            case 9:
                                    itemList[index] = new Circle();		//圆形
                            break;
                            case 10:
                                    itemList[index] = new fillCircle();	//填充圆形
                            break;
                            case 11:
                                    itemList[index] = new RoundRect();	//圆角矩形
                            break;
                            case 12:
                                    itemList[index] = new fillRoundRect();//填充圆角矩形
                            break;
                            case 13:
                                    itemList[index] = new Rubber();		//橡皮擦
                            break;
                            case 14:
                                    itemList[index] = new Polygon1();	//三角形
                            break;
                            case 15:
                                    itemList[index] = new fillPolygon1();//填充三角形
                            break;
                            case 16:								//撤销
                                    if(x==0){
                                            index = index - 2;
                                            repaint();
                                            x=x+2;
                                    }
                                    else{
                                            index = index -1;
                                            repaint();
                                            x = x + 1;
                                    }
                            break;
                            case 17:								//重画
                                    index = index + 1;
                                    repaint();
                                    x = x - 1;
                            break;
                            case 18:
                                    itemList[index] = new Word();		//文字
                            break;
                    }
                    itemList[index].type = currentChoice;		//当前颜色
                    itemList[index].R = R;
                    itemList[index].G = G;
                    itemList[index].B = B;
                    itemList[index].stroke = stroke;			//当前粗细
            }

    /**选择当前颜色程序段**/
            public void chooseColor(){
                    color = JColorChooser.showDialog(MiniDrawPad.this,"请选择颜色",color);
                    R = color.getRed();
                    G = color.getGreen();
                    B = color.getBlue();						//得到颜色
                    itemList[index].R = R;
                    itemList[index].G = G;
                    itemList[index].B = B;						//保存为当前颜色

            }


    /**选择当前线条粗细程序段**/
            public void setStroke(){
                    String input;								//从对话框得到画笔粗细值
                    input = JOptionPane.showInputDialog("请键入画笔粗细值(大于0且小于100)");
                    stroke = Float.parseFloat(input);
                    itemList[index].stroke = stroke;			//保存为当前画笔粗细值
            }

    /**保存图形文件程序段**/
            public void saveFile(){
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    int result = fileChooser.showSaveDialog(this);
                    if(result == JFileChooser.CANCEL_OPTION)
                            return;
                    File fileName = fileChooser.getSelectedFile();
                    fileName.canWrite();

                    if(fileName == null || fileName.getName().equals(""))
                            JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
                                                    "Invalid File Name",JOptionPane.ERROR_MESSAGE);
                    else{
                                    try{
                                                    fileName.delete();
                                                    FileOutputStream fos = new FileOutputStream(fileName);

                                                    output = new ObjectOutputStream(fos);
                                                    drawings record;

                                                    output.writeInt(index);

                                                    for(int i=0;i<index;i++){
                                                            drawings p = itemList[i];
                                                            output.writeObject(p);
                                                            output.flush();			//将所有图形信息强制转换成父类线性化存储到文件中
                                                    }
                                                    output.close();
                                                    fos.close();
                            }
                                    catch(IOException ioe){
                                            ioe.printStackTrace();
                                    }
                    }
            }

    /**打开一个图形文件程序段**/
            public void loadFile(){
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    int result = fileChooser.showOpenDialog(this);
                    if(result == JFileChooser.CANCEL_OPTION)
                            return;
                    File fileName = fileChooser.getSelectedFile();
                    fileName.canRead();

                    if(fileName == null || fileName.getName().equals(""))
                            JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
                                                    "Invalid File Name",JOptionPane.ERROR_MESSAGE);
                    else{
                                    try{

                                                    FileInputStream fis = new FileInputStream(fileName);

                                                    input = new ObjectInputStream(fis);
                                                    drawings inputRecord;

                                                    int countNumber = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -