java画板总结

  学习java这段时间一类,对java刚开始的陌生与好奇渐渐的转变为心中有数,遇到的问题总是能够解决的,关键是用什么方法。
 对于我来说,老师上课讲的那一套是他们自己的东西,刚开始遇到一些所谓的“专业术语”还感到有点措手不及,但我们学习java更重要的是通过它来
总结出一些自己的学习方法,对我们适用的才是我们需要的。在学习中,我更喜欢将老师讲的一些框框套套和生活中的实际例子结合起来,显得更形象
更简单明了,在运用时显得更有我个人的一些风格,当技术个人化的时候,我们在运用它的时候才会更得心应手。还有就是不懂就要问,然后再不断的
调试,朝自己想要的方式去摸索,这方面很重要,所以现在感觉还是很欠缺。

闲话少说,来小结一下我们的仿XP画图板。
 完成画图板,我们首先需要实现它的外观,也就是需要在窗体上添加一些面板、按钮、画布。在初步实现了这些外表的功能之后,我们再去实现画图板
 “作画”的功能。
 首先,一个画图板是在一个窗体上显示的,然后画板主要分为三个大块儿,分别是画板(DrawPanel)、图形选择面板(ToolPanel)、颜色选择面
 板(ColorPanel)。我们需要把窗体设置为边框布局 【BorderLayout()】,届时才能更好的把个面板放在对应的位置。
 
 
 ToolPanel
 图形选择面板大小设置好,布局选用流式布局,不过要调整好按钮的大小,上部分添加的是按钮,然后在按钮上加上图片(自己截的),下面是一个附
 加选项面板。先在ToolPanel上添加图形按钮和附加面板,创建图形按钮对象时就对其添加动作监听器(ActionListener)并且设置动作命令,每
 当我们点击图形按钮时,就得到动作命令,然后根据不同的动作命令进行判断,再在附加面板上添加附加选择按钮,当然在创建附加选择按钮的同时也
 要对其添加另外一个监听器并且设置动作命令。当我们点击附件按钮式,就会得到动作命令,有人会问:"得到动作命令干什么?"先卖个关子,后面详解。
 


public class ToolPanel extends JToolBar  {

    //初始化一个ToolPanel面板
 public ToolPanel() {
  showtool();
 }

 private String type = "line";
 private String item_type = "number_1";

 public String gettype() {
  return type;
 }

 public String get_itemtype() {
  return item_type;
 }

 private void showtool() {

 this.setBackground(new Color(235,235,235));
 this.setPreferredSize(new Dimension(70, 400));
 this.setOrientation(javax.swing.JToolBar.VERTICAL);
 this.setLayout(new FlowLayout(FlowLayout.LEFT));
 LineBorder lineborder = new LineBorder(Color.gray,1);
 this.setBorder(lineborder);

 String[] array = { "images/brush.jpg", "images/spray.jpg",
   "images/pencil.jpg", "images/fillRect.jpg", "images/easer.jpg",
   "images/rect.jpg", "images/oval.jpg", "images/line.jpg",
   "images/polygon.jpg", "images/roundrect.jpg" };

 final JPanel item_panel = new JPanel();
 item_panel.setBorder(new BevelBorder(0,Color.GRAY,new Color(192,192,192)));
 item_panel.setPreferredSize(new Dimension(60, 70));
 item_panel.setLayout(new FlowLayout(FlowLayout.CENTER));
  
    //创建一个附加按钮动作监听器
 final ActionListener item_listener = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   item_type = e.getActionCommand();
   //System.out.println(item_type);
  }
 };  
  
//***********在附加面板上添加选项按钮********************//
 ActionListener listener = new ActionListener() {

 public void actionPerformed(ActionEvent e) {
  type = e.getActionCommand();
 
 if (type.equals("line")) {
  item_panel.removeAll();      //在显示按钮之前先移除原来添加上去的所有组件
     
  ImageIcon image_1 = new ImageIcon("images/lineSize_1.gif");   //创建一个ImageIcon对象
  JButton button_1 = new JButton(image_1);   //将图片添加到按钮上
  button_1.setBackground(new Color(235,235,235));
  button_1.setActionCommand("line_type_1");    //设置动作命令
  button_1.addActionListener(item_listener);    //为附加按钮添加动作监听器
  button_1.setPreferredSize(new Dimension(50, 15));    //设置按钮面积大小
     
  ImageIcon image_2 = new ImageIcon("images/lineSize_2.gif");   //同上
  JButton button_2 = new JButton(image_2); 
  button_2.setBackground(new Color(235,235,235)); 
  button_2.setActionCommand("line_type_2");   
  button_2.addActionListener(item_listener);    
  button_2.setPreferredSize(new Dimension(50, 15));
     
  ImageIcon image_3 = new ImageIcon("images/lineSize_3.gif");
  JButton button_3 = new JButton(image_3);
  button_3.setBackground(new Color(235,235,235));
  button_3.setActionCommand("line_type_3");
  button_3.addActionListener(item_listener);
  button_3.setPreferredSize(new Dimension(50, 15));

  item_panel.add(button_1);
  item_panel.add(button_2);
  item_panel.add(button_3);
     
  item_panel.updateUI();     //强制刷新
 }
    
 //下面的操作同上
 else if (type.equals("oval")) {     
 ..............
 }
    

 else if (type.equals("rect")) {
 ..............
 }
    
 else if (type.equals("roundrect")) {
 ................
 }
    
 else if (type.equals("spray")) {
 ................
 }
    
 else if (type.equals("brush")) {
 ................
 }
    
 else if (type.equals("easer")) {
 ................ 
 }
 else if (type.equals("pencil")){
 ................ 
 }
    
 else if (type.equals("polygon")){
 ................ 
 }
    
 }
};
 
 //当按钮比较多的时候就要考虑用for 循环来创建对象统一操作

 for (int i = 0; i < array.length; i++) {
  ImageIcon image = new ImageIcon(array[i]);

  JButton button = new JButton(image);

  // 获取图片的名字
  String image_name = array[i].substring(array[i].indexOf("/") + 1,
     array[i].lastIndexOf(".jpg"));
  // 为每一个工具按钮添加动作命令
  button.setActionCommand(image_name);
  button.setBackground(Color.LIGHT_GRAY);
  // 为工具按钮添加监听器
  button.addActionListener(listener);

  button.setPreferredSize(new Dimension(27, 27));

  Insets insets = new Insets(0,0,0,0);
  button.setMargin(insets);
  if (image_name.equals("pencil")){
   button.setSelected(true);
  }   
  this.add(button);
 } 
 this.add(item_panel);
}
}
 
 
 //***************************************//
 //**************************************//
 
 CorlorPanel
 
 将颜色选择面板设置为流式布局,上面有添加了两个面板panel_left、panel_right。  panel_right设置为网格布局,上面主要是颜色选择按钮,
 panel_left是前景色按钮和背景色按钮,对panel_right上面的按钮添加MouseListener(因为涉及左右键点击问题),每当左键点击按钮时,就
 把当前按钮的颜色赋给背景色按钮,右键则相反。我们最这个类里面有一个获取前景色按钮和背景色按钮比的颜色的方法,“这又用来干嘛呢?”,继续卖关
 子。

 //初始化一个面板
 public ColorPanel() {
  showcolorpanel();
 }

 private Color FColor = Color.WHITE;
 private Color BColor = Color.BLACK;
 
 public Color getFColor(){
     return FColor;
 }
 
 public Color getBColor(){
     return BColor;
 }
 
 private void showcolorpanel() {

  // 设置颜色面板的基本属性
  this.setBackground(new Color(235,235,235));
  // 设置面板的面积大小
  this.setPreferredSize(new Dimension(500, 60));
  this.setLayout(new FlowLayout(FlowLayout.LEFT));

  // **********************************设置左边的面板*********************************************//
  JPanel panel_left = new JPanel();
  panel_left.setBorder(new BevelBorder(0,Color.GRAY,new Color(192,192,192)));
  panel_left.setBackground(new Color(235,235,235));
  panel_left.setPreferredSize(new Dimension(40, 40));
  // 绝对布局 自定义坐标(x,y)和大小
  panel_left.setLayout(null);

  EtchedBorder etchedborder = new EtchedBorder();
  
  final JButton Fbutton = new JButton();
  //将按钮设置成不可点击
  Fbutton.setEnabled(false);
  Fbutton.setFocusable(false);
  Fbutton.setBorder(etchedborder);
  Fbutton.setBackground(Color.WHITE);
  //绝对布局时设置坐标和面积大小
  Fbutton.setBounds(15, 15, 18, 18);

  final JButton Bbutton = new JButton();
  Bbutton.setBorder(etchedborder);
  //将按钮设置成不可点击
  Bbutton.setEnabled(false);
  Bbutton.setFocusable(false);
  Bbutton.setBackground(Color.BLACK);
  //绝对布局时设置坐标和面积大小
  Bbutton.setBounds(5, 5, 18, 18);
 
  panel_left.add(Bbutton);
  panel_left.add(Fbutton); 

  this.add(panel_left);

  panel_left.updateUI();
  //****************************************设置右面板*******************************************//
  JPanel panel_right = new JPanel();
  panel_right.setBackground(new Color(235,235,235));
  panel_right.setLayout(new GridLayout(2, 10, 1, 1));
  Color [] array = { new Color(0,0,0),        new Color(128,128,128),
              new Color(128, 0, 64),   new Color(255,0,0),
              new Color(255, 128, 64), new Color(255,255,0),
              new Color(0, 128, 0),    new Color(0, 128, 255),
              new Color(0, 0, 160),    new Color(128, 0, 128),
                    new Color(255, 255, 255),new Color(192, 192, 192),
                    new Color(128, 64, 0),   new Color(255, 128, 192),
                    new Color(239, 166, 56), new Color(239, 228, 176),
                    new Color(181, 230, 29), new Color(153, 217, 234),
                    new Color(112, 146, 190),new Color(200, 191, 231),   
                 };

  //因为涉及“左右键”选择颜色,所以用MouseAdapter(一个抽象类,实现了MouseListenre、MouseWheelListener、MouseMotionListener)
  //匿名内部类的定义方法
  MouseAdapter mouselistener = new MouseAdapter(){
   public void mousePressed(MouseEvent e) {
    JButton button = new JButton();
    button = (JButton) (e.getSource());
    
    if ( e.getButton() == 1){
     BColor = button.getBackground();
     Bbutton.setBackground(BColor);     
    }    
    else if (e.getButton() == 3){
     FColor = button.getBackground();
     Fbutton.setBackground(FColor);     
    }     
   }   
  };
  
  for (int i = 0; i < array.length; i++) {
   JButton button = new JButton();
   button.setBackground(array[i]);
   button.setBorder(new BevelBorder(0,Color.GRAY,new Color(192,192,192)));
   button.addMouseListener(mouselistener);
   //设置为不可用   
   button.setEnabled(false);
   button.setPreferredSize(new Dimension(18, 18));
   panel_right.add(button);
  }

  this.add(panel_right);

 }

}
 //*****************************************//
 //*****************************************//
 
 DrawPanel
 
 最终我们是需要在画板面板上作画,需要画布(从面板上获取)、颜色、图形。后两者就是我们为什么要从ToolPanel的到按钮的动作命令、
 ColorPanel上得到前景色和背景色按钮的颜色!作图完之后我们需要对其做一个重绘。重写一个继承了JPanel的MyPanel类中paint(Graohics g)
 方法。重绘有三种方法(存图、存点、存动作)。
 
 画的同时就把画布上的像素点保存起来
      //截屏存储操作
    Object object = e.getSource();
    JPanel drawpanel = (JPanel)(object);
    //获取到drawpanel的左上角相对于屏幕的坐标点
    Point point = drawpanel.getLocationOnScreen();
    //获取到drawpanel的面积大小
    Dimension dimension = drawpanel.getPreferredSize();
    //创建一个要截取的区域,相当于是一个屏幕裁剪模具
    Rectangle rect = new Rectangle(point,dimension);
    //画完一个图形之后就把图像截取下来
    BufferedImage cut_image = robot.createScreenCapture(rect);
       
    data = new int[cut_image.getHeight()][cut_image.getWidth()];
       
    for (int i=0;i<data.length;i++)
     for (int j=0;j<data[i].length;j++){
      int RGB = cut_image.getRGB(j, i);
      data[i][j] = RGB;
      //System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
  }
 
 //*******************************************//
 
  //写一个类继承JPanel,重写JPanel的paint方法
 class MyPanel extends JPanel{
   
 // 重写绘制组件的方法
 //当然这面板主要是取得画布绘画,没有添加其他组件
 //因为JPanel里面的paint方法只会重绘组件,所以我们在“画图”的同时存储画布图像,"它"重绘组件时我们就顺便把图形再画出来
 public void paint(Graphics g) {
  // 调用父类的绘制方法来正确的绘制组件本身
  super.paint(g);
  // 当需要重新绘制的时候就将二维数组中的数据绘制一次\
  //System.out.println("listener="+listener);
  if (listener != null) {
  // 获取监听器中的二维数组
  int[][] data = listener.data;
  //System.out.println("data="+data);
  if (data != null) {// 如果已经保存了数据,才需要绘制
   for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data[i].length; j++) {
     int cNum = data[i][j];
     // 创建颜色对象
     Color color = new Color(cNum);
     g.setColor(color);
     // 绘制点
        g.drawLine(j, i, j, i);        
       }
      }
     }
    }
   }
  }
  
至于在画图方面还有一些小技巧,待以后续写~~

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值