java画板粗实现

         画板是此次暑假课程第一个项目,让我学到了很多。当然此次画板程序还是有些缺陷。如:喷枪,橡皮之类的撤销键尚不十分完善。

1,  首先创建一个窗体,为其添加基本属性。采用边框布局,分为画板区,工具区,颜料区。并设置可见

public void init() {

       setTitle("我的画板");

       // 通过相对路径创建一个ImageIcon的对象

       ImageIcon ii = new ImageIcon("img/icon.png");

       // imageIcon对象中获取image,设置上去

       setIconImage(ii.getImage());

       // 设置大小

       setSize(600, 600);

       // 设置位置

       setLocationRelativeTo(null);

       // 设置默认关闭

       setDefaultCloseOperation(EXIT_ON_CLOSE);

  }

颜料区

private void addColorPanel() {

       colorPanel = new ColorPanel(this);

       add(colorPanel, BorderLayout.SOUTH);

    }

工具区

    private void addToolsPanel() {

       toolsPanel = new ToolsPanel(this);

       add(toolsPanel, BorderLayout.WEST);

    }

画板区

    private void addDrawPanel() {

       // 创建绘制面板

       drawPanel = new DrawPanel(this);

       add(drawPanel, BorderLayout.CENTER);

  }

2,  给工具区添加选择按钮,颜料区添加颜料选择,画板分别添加添加监听。

工具区

public class ToolsPanel extends JPanel {

   

    String[] allCommand = {"wj","xz","xp","pt","xg","fdj","qb","sz","pq","wz","xx","qx","jx","dbx","ty","yjjx"};

   

    MainFrame mainFrame;

    public ToolsPanel(MainFramemainFrame)

    {

       this.mainFrame =mainFrame;

       setPreferredSize(new Dimension(60, 0));

       setBackground(new Color(240,240,240));

       setLayout(new FlowLayout(FlowLayout.CENTER,0,0));

       //创建一个往上凸起的边框

       BevelBorder border = new BevelBorder(BevelBorder.RAISED,new Color(240,240,240),newColor(160,160,160));

       setBorder(border);

       //创建一个按钮组,使得所有按钮能够互斥

       ButtonGroup gg = new ButtonGroup();

       for (inti = 1; i <=allCommand.length;i++) {

           ImageIcon i1 = new ImageIcon(getClass().getResource("/img/"+i+"-1.png"));

           ImageIcon i2 = new ImageIcon(getClass().getResource("/img/"+i+"-2.png"));

           ImageIcon i3 = new ImageIcon(getClass().getResource("/img/"+i+"-3.png"));

           ImageIcon i4 = new ImageIcon(getClass().getResource("/img/"+i+"-4.png"));

           //创建单选按钮

           JRadioButton jrb = new JRadioButton();

           //设置了一个ActionCommand

           jrb.setActionCommand(allCommand[i-1]);

      

           if(allCommand[i-1].equals("qb"))

           {

              jrb.setSelected(true);

           }

          

           //设置轻量级组建的大小

           jrb.setPreferredSize(new Dimension(25, 25));

           //设置按钮四种状态下的图标

           jrb.setIcon(i1);

           jrb.setRolloverIcon(i2);

           jrb.setPressedIcon(i3);

           jrb.setSelectedIcon(i4);

          

           jrb.setMargin(new Insets(0, -2, 0, 0));

          

          

           //给按钮增加监听

           jrb.addActionListener(new ToolsActionListener(mainFrame));

          

           add(jrb);

           gg.add(jrb);

       }

      

    }

颜料区

public class ColorPanel extends JPanel {

    添加颜料

    Color[] colors = { new Color(0, 0, 0), new Color(128, 128, 128),

           new Color(128, 0, 0),new Color(128, 128, 0), new Color(0, 128, 0),

           new Color(0, 128, 128),new Color(0, 0, 128),

           new Color(128, 0, 128),new Color(128, 128, 64),

           new Color(0, 64, 64),new Color(0, 128, 255),

           new Color(0, 64, 128),new Color(128, 0, 255),

           new Color(128, 64, 0),new Color(255, 255, 255),

           new Color(192, 192, 192),new Color(255, 0, 0),

           new Color(255, 255, 0),new Color(0, 255, 0),

           new Color(0, 255, 255),new Color(0, 0, 255),

           new Color(255, 0, 255),new Color(255, 255, 128),

           new Color(0, 255, 128),new Color(128, 255, 255),

           new Color(128, 128, 255),new Color(255, 0, 128),

           new Color(255, 128, 64) };

   

   

    MainFrame mainFrame;

 

    private JButtonjbt1;

    private JButtonjbt2;

   

    public ColorPanel(MainFramemainFrame)

    {

       this.mainFrame =mainFrame;

       setPreferredSize(new Dimension(0, 50));

       setBorder(new LineBorder(Color.GRAY));

       setLayout(new FlowLayout(FlowLayout.LEFT,0,0));

      

      

       //创建底下的面板

       JPanel back = createBack();

       back.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));

       //创建左边

       JPanel selectedColor = createSelected();

      

       //创建右边

       JPanel allColor = createAll();

 

       back.add(selectedColor);

       back.add(allColor);

      

       add(back);

      

    }

   

    private JPanel createSelected() {

       JPanel jp = new JPanel();

       jp.setPreferredSize(new Dimension(30, 30));

       jp.setBackground(Color.WHITE);

      

       //设置布局为绝对布局,每一个加入到容器的组建必须setBonds()

       jp.setLayout(null);

       //设置边框

       BevelBorder border = new BevelBorder(1,Color.WHITE,Color.GRAY);

       jp.setBorder(border);

       //增加按钮

       BevelBorder border1 = new BevelBorder(0,Color.WHITE,Color.GRAY);

       jbt1 = new JButton();

       jbt1.setBackground(Color.BLACK);

       jbt1.setBounds(5,5,15,15);

       jbt1.setEnabled(false);

       jbt1.setBorder(border1);

      

       jbt2 = new JButton();

       jbt2.setBackground(Color.WHITE);

       jbt2.setBounds(10,10,15,15);

       jbt2.setEnabled(false);

       jbt2.setBorder(border1);

      

       jp.add(jbt1);

       jp.add(jbt2);

      

       returnjp;

    }

 

    private JPanel createAll() {

       JPanel jp = new JPanel();

      

       jp.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));

       jp.setPreferredSize(new Dimension(210, 30));

       jp.setBackground(Color.GREEN);

      

      

       BevelBorder border1 = new BevelBorder(1,Color.WHITE,Color.GRAY);

      

       for (inti = 0; i < colors.length; i++) {

           JButton jbt = new JButton();

           jbt.setPreferredSize(new Dimension(15, 15));

           jbt.setBackground(colors[i]);

           jbt.setBorder(border1);

          

          

           jbt.addMouseListener(new ColorActionListener(mainFrame));

          

           jp.add(jbt);

          

       }

      

      

       returnjp;

    }

 

    public JPanel createBack()

    {

       //创建一个面板

       JPanel back  = new JPanel();

       //设置大小

       back.setPreferredSize(new Dimension(250, 50));

       //设置背景颜色

//     back.setBackground(Color.RED);

       //设置一个空的边框

       back.setBorder(new EmptyBorder(10, 0, 10, 10));

       returnback;

    }

   

    public void changeSelectedBKColor(Color color)

    {

       jbt1.setBackground(color);

    }

    public void changeSelectedTCColor(Color color)

    {

       jbt2.setBackground(color);

    }

}

 

监听

颜料监听

public void mouseClicked(MouseEvent e) {

       // TODO Auto-generatedmethod stub

       //获取事件源==》按钮

       //获取鼠标按钮1左键,3右键

       int button = e.getButton();

       System.out.println(button);

      

       Object source = e.getSource();

       //强制转换为JButton

       if(sourceinstanceof JButton)

       {

           JButton jbt = (JButton)source;

           System.out.println(jbt.getBackground());

//         mainFrame.setCurrentSelectedColor(jbt.getBackground());

      

           if(button == 1)

           {

              mainFrame.setBkColor(jbt.getBackground());

              mainFrame.colorPanel.changeSelectedBKColor(jbt.getBackground());

           }

           else if(button == 3)

           {

              mainFrame.setTcColor(jbt.getBackground());

              mainFrame.colorPanel.changeSelectedTCColor(jbt.getBackground());

           }

       工具监听

public void actionPerformed(ActionEvent e) {

       System.out.println(e.getActionCommand());

       String actionCommand = e.getActionCommand();

       mainFrame.setsTool(actionCommand);

    }  

3, 为工具栏添加功能算法

比如直线

    x2 = e.getX();

           y2 = e.getY();

       graphics.drawLine(x1, y1, x2, y2);

      

 

4, 进行封装,完善重绘功能

直线

x2 = e.getX();

           y2 = e.getY();

 

                  //创建一个直线对象,调用直线对象的绘制方法,将直线画出来

           DrawPanel.ls.add(new LineShape(x1,y1, x2, y2,bkColor));

        mainFrame.repaint();

5, 添加撤销和恢复功能

撤销

public class UndoActionListener implements ActionListener{

  static ShapeContainersc1=new ShapeContainer();

  MainFrame mf;

  public UndoActionListener(MainFramemf)

  {

      this.mf=mf;

  }

  public void actionPerformed(ActionEvent e){

      ShapeContainer sc=DrawPanel.ls;

      if(sc.getSize()==0){

         return;

      }

      Shape shape=sc.get(sc.getSize()-1);

      sc1.add(shape);

      sc.remove(sc.getSize()-1);

      mf.repaint();

  }

}

恢复

public class RedoActionListener implements ActionListener{

MainFrame mf;

public RedoActionListener(MainFramemf)

{

    this.mf=mf;

}

@Override

public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    ShapeContainer sc1=DrawPanel.ls;

    ShapeContainer sc2=UndoActionListener.sc1;

    Shape shape=sc2.get(sc2.getSize()-1);

    sc2.remove(sc2.getSize()-1);

    sc1.add(shape);

    mf.repaint();

}


6,  调整布局,排除bug,完善代码。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 画板UI设计(总体布局) (1)创建窗体并设置相关属性; (2)给整个窗体添加一个中间容器用来覆盖整个窗体; (3)设置边框布局,因为整个画板大致为左边部分,中间部分,菜单栏三个部分,用边框布局比较合适; (4)给窗体添加左面板,中间面板; (5)给左面板添加按钮,并设置按钮图片和效果; (6)给左面板添加左子面板,用来存放颜色按钮; 2.画板功能设计 (1)给左画板中的按钮组中的每个按钮添加鼠标监听器; (2)点击不同按钮,绘制不同的图形; (3)给左子面板中的每个颜色按钮添加鼠标监听器; (4)根据下面板中选中的颜色按钮,来获取按钮的背景颜色,并将该颜色设置成画笔的颜色; (5)铅笔功能 1、铅笔是鼠标拖动时画的线,所需要实现鼠标移动监听器,我们采用一个类来实现多个接口; 2、添加新的鼠标监听器类; 3、在鼠标移动时间中实现画笔的逻辑(其实就是绘制直线,鼠标每移动一个像素,就会触发移动事件,通过移动事件获取鼠标的坐标,与上一次移动的坐标连线就可以了; (6)刷子功能 刷子其实就是加的画笔,画出来的直线更,这里需要用到Graphic2D画笔来设置画笔的细。 (7)橡皮擦功能 橡皮擦就是把画笔颜色设置成相同的背景颜色就可以了,拖动鼠标时进行擦除,所以在鼠标拖动事件中编写。 (8)喷桶功能 1、定位方法:鼠标拖动事件实现; 2、随机数生成:Random; 3、实现原理,在鼠标拖动附近绘制很多的原点; 3. 画板的保存和重绘的设计 (1)给我们的画板添加菜单条、菜单以及菜单项 (2)给每个菜单项添加监听器 (3)点击不同的菜单项实现相应的功能 (4)图形保存功能:利用对象输出流,将容器对象写入文件; (5)打开图形功能:利用对象输入流,将容器对象读入,然后将容器里面的图形对象在画板上绘制出来 (6)新建文件功能:新建文件,将画板上绘制的内容清空(清空之前可以确认是否需要进行保存)清屏(重绘)功能依次方法; (7)文件保存格式为BMP格式; (8)文字功能:读取文本框中的文本并打印到屏幕鼠标的相应的响应位置,传入输入的文字大小的参数,以此来改变文字的大小; 4.弹泡泡功能的设计 根据Java多线程来实现弹泡泡功能;泡泡的位置颜色随机出现,并且做到碰到边框会变色;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值