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

📄 huituban.java

📁 java 做的图形编辑器 java 做的图形编辑器 java 做的图形编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package huituban;

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;


public class huituban extends JFrame     //主类,扩展了JFrame类,用来生成主界面
 {
  private ObjectInputStream  input;
  private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件

  private JButton Anniu[];         //按钮数组,存放以下名称的功能按钮

  private String names[]={
          "xinjian","dakai","baocun", 
          "Pencil","Line","Rect","fRect","Oval","fOval","RoundRect","frRect", "qinchu","Rubber","wo","Color","Stroke","Word"	
          };



  private Icon items[];
  Icon zhao=new ImageIcon("jun.gif") ;
  private String tipText[]={   //鼠标停留提示说明
                "新建",
                "打开",
                "保存",
                "铅笔画",
                "绘制直线",
                "绘制空心矩形",
                "绘制以指定颜色填充的实心矩形",
                "绘制空心椭圆",
                "绘制以指定颜色填充的实心椭圆",               
                "绘制空心圆角矩形",
                "绘制以指定颜色填充的实心圆角矩形",
                "鼠标选定区域的清除",
                "橡皮擦",
                "about me",
                "选择颜色",
                "选择大小",
                "文字输入"
              };

  JToolBar buttonPanel ;		       //定义按钮面板
  private JLabel statusBar;            //显示鼠标状态的提示条

  private DrawPanel drawingArea;       //画图区域
  private int width=550,height=400;    //定义画图区域初始大小

  drawings[] itemList=new drawings[5000]; //用来存放基本图形的数组
  private int currentChoice=3;            //设置默认画图状态为随笔画
  int index=0;                         //当前已经绘制的图形数目
  private Color color=Color.black;     //当前画笔颜色
  int R,G,B;                           //用来存放当前色彩值

  int f1,f2;                  //用来存放当前字体风格
  String style1;              //用来存放当前字体
  private float stroke=1.0f;  //设置画笔粗细,默认值为1.0f

  public huituban()        //构造函数
  {
   super("欢迎使用赵文君的画图板小程序");
   JMenuBar bar=new JMenuBar();		//定义菜单条
   JMenu fileMenu=new JMenu("文件");
   fileMenu.setMnemonic('F');

//新建文件菜单条
   JMenuItem newItem=new JMenuItem("新建");
   newItem.setMnemonic('N');
   newItem.addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   newFile();		//如果被触发,则调用新建文件函数段
                  }
          }
   );
   fileMenu.add(newItem);

//保存文件菜单项
   JMenuItem saveItem=new JMenuItem("保存");
   saveItem.setMnemonic('S');
   saveItem.addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   saveFile();		//如果被触发,则调用保存文件函数段
                  }
          }
   );
   fileMenu.add(saveItem);

//打开文件菜单项
   JMenuItem loadItem=new JMenuItem("打开");
   loadItem.setMnemonic('L');
   loadItem.addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   loadFile();		//如果被触发,则调用打开文件函数段
                  }
          }
   );
   fileMenu.add(loadItem);

   fileMenu.addSeparator();

//退出菜单项
   JMenuItem exitItem=new JMenuItem("退出");
   exitItem.setMnemonic('X');
   exitItem.addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   System.exit(0);	//如果被触发,则退出画图板程序
                  }
          }
   );
   fileMenu.add(exitItem);
   bar.add(fileMenu);

//设置颜色菜单条
   JMenu colorMenu=new JMenu("颜色");
   colorMenu.setMnemonic('C');

//选择颜色菜单项
   JMenuItem colorItem=new JMenuItem("选择颜色");
   colorItem.setMnemonic('O');
   colorItem.addActionListener(
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                   {
                    chooseColor();	//如果被触发,则调用选择颜色函数段
                   }
       }
      );
   colorMenu.add(colorItem);
   bar.add(colorMenu);

//设置线条粗细菜单条
    JMenu strokeMenu=new JMenu("粗细");
    strokeMenu.setMnemonic('S');

//设置线条粗细菜单项
    JMenuItem strokeItem=new JMenuItem("增加粗度");
    strokeItem.setMnemonic('K');
    strokeItem.addActionListener(
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                    {
                     setStroke();
                     }
                   }
              );
           strokeMenu.add(strokeItem);
           bar.add(strokeMenu);

//设置提示菜单条
    JMenu helpMenu=new JMenu("帮助");
    helpMenu.setMnemonic('H');

//设置提示菜单项
    JMenuItem aboutItem=new JMenuItem("关于!");
    aboutItem.setMnemonic('A');
    aboutItem.addActionListener(
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                    {
                     JOptionPane.showMessageDialog(null,
                        "谢谢使用!\n多媒体四班  赵文君\n(20052110010406) ",
                        " 画图板程序说明 ",
                         JOptionPane.INFORMATION_MESSAGE ,zhao);
                     }
                   }
              );
    helpMenu.add(aboutItem);
    bar.add(helpMenu);

    items=new ImageIcon[names.length];

//创建各种基本图形的按钮
    drawingArea=new DrawPanel();
    Anniu=new JButton[names.length];
    buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
    buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
    ButtonHandler handler=new ButtonHandler();
    ButtonHandler1 handler1=new ButtonHandler1();

//导入按钮图标
    for(int i=0;i<Anniu.length;i++)
    {
     items[i]=new ImageIcon(names[i] + ".gif");
     Anniu[i]=new JButton("",items[i]);
     Anniu[i].setToolTipText(tipText[i]);
     buttonPanel.add(Anniu[i]);
    }

//将动作监听器加入按钮里面
    for(int i=3;i<Anniu.length-3;i++)
    {
     Anniu[i].addActionListener(handler);
    }

    Anniu[0].addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   newFile();
                  }
          }
     );

    Anniu[1].addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   loadFile();
                  }
          }
     );

    Anniu[2].addActionListener(
          new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                   saveFile();
                  }
          }
     );
    Anniu[Anniu.length-4].addActionListener(handler1);
    Anniu[Anniu.length-3].addActionListener(handler1);
    Anniu[Anniu.length-2].addActionListener(handler1);
    Anniu[Anniu.length-1].addActionListener(handler1);

//界面布局
    Container c=getContentPane();
    super.setJMenuBar( bar );
    c.add(buttonPanel,BorderLayout.NORTH);
    c.add(drawingArea,BorderLayout.CENTER);

    statusBar=new JLabel();
    c.add(statusBar,BorderLayout.SOUTH);
    statusBar.setText("     欢迎使用画图板小程序!!!  :)");

    createNewItem();
    setSize(width,height);
    setVisible(true) ;  
  }


//按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作
public class ButtonHandler implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   for(int j=3;j<Anniu.length-4;j++)
   {
      if(e.getSource()==Anniu[j])
         {currentChoice=j;
          createNewItem();
          repaint();}
        }
    }
 }

//按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作
public class ButtonHandler1 implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==Anniu[Anniu.length-3])
         {chooseColor();}
    if(e.getSource()==Anniu[Anniu.length-2])
         {setStroke();}
    if(e.getSource()==Anniu[Anniu.length-1])
         {JOptionPane.showMessageDialog(null,
             "请用鼠标点击的想输入的位置",
             "提示",JOptionPane.INFORMATION_MESSAGE );
          currentChoice=13;
          createNewItem();
          repaint();
          }
     if(e.getSource()==Anniu[Anniu.length-4])
         {JOptionPane.showMessageDialog(null,
                        "谢谢使用!\n多媒体四班  赵文君\n(20052110010406) ",
                        " 画图板程序说明 ",
                         JOptionPane.INFORMATION_MESSAGE ,zhao);
          }     
    }
 }


//鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作
 class mouseA extends MouseAdapter
 {
   public void mousePressed(MouseEvent e)
    {statusBar.setText("     Mouse Pressed @:[" + e.getX() +
                              ", " + e.getY() + "]");//设置状态提示

     itemList[index].x1=itemList[index].x2=e.getX();
     itemList[index].y1=itemList[index].y2=e.getY();

    //如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作
    if(currentChoice==3||currentChoice==12)
    {
     itemList[index].x1=itemList[index].x2=e.getX();
     itemList[index].y1=itemList[index].y2=e.getY();
     index++;
     createNewItem();
     }

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

      String input;
      input=JOptionPane.showInputDialog(
          "Please input the text you want!");
      itemList[index].s1=input;
      itemList[index].x2=f1;
      itemList[index].y2=f2;
      itemList[index].s2=style1;

      index++;
      currentChoice=13;
      createNewItem();
      drawingArea.repaint();
      }
      //
      
      
      //
    }

   public void mouseReleased(MouseEvent e)
    {statusBar.setText("     Mouse Released @:[" + e.getX() +
                              ", " + e.getY() + "]");

    if(currentChoice==3||currentChoice==12)
    {
     itemList[index].x1=e.getX();
     itemList[index].y1=e.getY();
    }
     itemList[index].x2=e.getX();
     itemList[index].y2=e.getY();
     repaint();
     index++;
     createNewItem();
    }

   public void mouseEntered(MouseEvent e)
   {
           statusBar.setText("     Mouse Entered @:[" + e.getX() +
                              ", " + e.getY() + "]");
           }

   public void mouseExited(MouseEvent e)
   {
           statusBar.setText("     Mouse Exited @:[" + e.getX() +
                              ", " + e.getY() + "]");
           }
  }


//鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作
 class mouseB extends MouseMotionAdapter
 {
  public void mouseDragged(MouseEvent e)
  {statusBar.setText("     Mouse Dragged @:[" + e.getX() +
                              ", " + e.getY() + "]");

   if(currentChoice==3||currentChoice==12)
   {

⌨️ 快捷键说明

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