拖拽的图片 : 图像 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 图像屏幕截图 
拖拽的图片
拖拽的图片
  
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
/*
 * SelectionDemo.java is a 1.4 application that requires one other file:
 * images/starfield.gif
 */

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;

/*
 * This displays an image. When the user drags within the image, this program
 * displays a rectangle and a string indicating the bounds of the rectangle.
 */
public class SelectionDemo {
  JLabel label;

  static String starFile = "starfield.gif";

  private void buildUI(Container container, ImageIcon image) {
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

    SelectionArea area = new SelectionArea(image, this);
    container.add(area);

    label = new JLabel("Drag within the image.");
    label.setLabelFor(area);
    container.add(label);

    //Align the left edges of the components.
    area.setAlignmentX(Component.LEFT_ALIGNMENT);
    label.setAlignmentX(Component.LEFT_ALIGNMENT)//redundant
  }

  public void updateLabel(Rectangle rect) {
    int width = rect.width;
    int height = rect.height;

    //Make the coordinates look OK if a dimension is 0.
    if (width == 0) {
      width = 1;
    }
    if (height == 0) {
      height = 1;
    }

    label.setText("Rectangle goes from (" + rect.x + ", " + rect.y
        ") to (" (rect.x + width - 1", "
        (rect.y + height - 1").");
  }

  /** Returns an ImageIcon, or null if the path was invalid. */
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = SelectionDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SelectionDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    SelectionDemo controller = new SelectionDemo();
    controller.buildUI(frame.getContentPane(), createImageIcon(starFile));

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  private class SelectionArea extends JLabel {
    Rectangle currentRect = null;

    Rectangle rectToDraw = null;

    Rectangle previousRectDrawn = new Rectangle();

    SelectionDemo controller;

    public SelectionArea(ImageIcon image, SelectionDemo controller) {
      super(image)//This component displays an image.
      this.controller = controller;
      setOpaque(true);
      setMinimumSize(new Dimension(1010))//don't hog space

      MyListener myListener = new MyListener();
      addMouseListener(myListener);
      addMouseMotionListener(myListener);
    }

    private class MyListener extends MouseInputAdapter {
      public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect = new Rectangle(x, y, 00);
        updateDrawableRect(getWidth(), getHeight());
        repaint();
      }

      public void mouseDragged(MouseEvent e) {
        updateSize(e);
      }

      public void mouseReleased(MouseEvent e) {
        updateSize(e);
      }

      /*
       * Update the size of the current rectangle and call repaint.
       * Because currentRect always has the same origin, translate it if
       * the width or height is negative.
       
       * For efficiency (though that isn't an issue for this program),
       * specify the painting region using arguments to the repaint()
       * call.
       *  
       */
      void updateSize(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect.setSize(x - currentRect.x, y - currentRect.y);
        updateDrawableRect(getWidth(), getHeight());
        Rectangle totalRepaint = rectToDraw.union(previousRectDrawn);
        repaint(totalRepaint.x, totalRepaint.y, totalRepaint.width,
            totalRepaint.height);
      }
    }

    protected void paintComponent(Graphics g) {
      super.paintComponent(g)//paints the background and image

      //If currentRect exists, paint a box on top.
      if (currentRect != null) {
        //Draw a rectangle on top of the image.
        g.setXORMode(Color.white)//Color of line varies
        //depending on image colors
        g.drawRect(rectToDraw.x, rectToDraw.y, rectToDraw.width - 1,
            rectToDraw.height - 1);

        controller.updateLabel(rectToDraw);
      }
    }

    private void updateDrawableRect(int compWidth, int compHeight) {
      int x = currentRect.x;
      int y = currentRect.y;
      int width = currentRect.width;
      int height = currentRect.height;

      //Make the width and height positive, if necessary.
      if (width < 0) {
        width = - width;
        x = x - width + 1;
        if (x < 0) {
          width += x;
          x = 0;
        }
      }
      if (height < 0) {
        height = - height;
        y = y - height + 1;
        if (y < 0) {
          height += y;
          y = 0;
        }
      }

      //The rectangle shouldn't extend past the drawing area.
      if ((x + width> compWidth) {
        width = compWidth - x;
      }
      if ((y + height> compHeight) {
        height = compHeight - y;
      }

      //Update rectToDraw after saving old value.
      if (rectToDraw != null) {
        previousRectDrawn.setBounds(rectToDraw.x, rectToDraw.y,
            rectToDraw.width, rectToDraw.height);
        rectToDraw.setBounds(x, y, width, height);
      else {
        rectToDraw = new Rectangle(x, y, width, height);
      }
    }
  }
}

           
         
    
  
Related examples in the same category
1. Image size Image size
2. 图片演示图片演示
3. 获取图像颜色模型
4. 图像RGB值滤波
5. 创建过滤器,可以修改任何值RGB像素的图像。
6. 此过滤器删除所有红色的值
7. 加载和绘制图像
8. 绘制图标绘制图标
9. 图像处理:亮度和对比度图像处理:亮度和对比度
10. 用鼠标拖动图像移动事件用鼠标拖动图像移动事件
11. 图像动画及螺纹图像动画及螺纹
12. 灰度图像颜色的影响灰度图像颜色的影响
13. 图像缓冲图像缓冲
14. 图像效果:联合图像效果:联合
15. AffineTransform演示AffineTransform演示
16. 图像效果:旋转图像DataBuffer图像效果:旋转图像DataBuffer
17. 图像效果:锐化,模糊图像效果:锐化,模糊
18. 图象尺度
19. 图像剪裁
20. 绘画的图像与Convolve运行
21. 演示使用的图像I / O库
22. 添加图像拖拽行为
23. 通过剪贴板传送图像物体通过剪贴板传送图像物体
24. 反锯齿反锯齿
25. 图像操作图像操作
26. 图像浏览器图像浏览器
27. 得到图像尺寸;非负值
28. 独立的图像浏览器-适用于任何AWT支持的格式
29. 应用Toolkit.getImage ( )
30. Image Processing Test Image Processing Test
31. 图像传输试验图像传输试验
32. 双缓冲图像
33. Graband淡出:显示图像和褪色的黑
34. Graband淡出与Rasters
35. 图像旋转45度
36. 转换java.awt.image.BufferedImage以java.awt.Image
37. 过滤图像红,绿,蓝颜色
38. TYPE_INT_RGB和TYPE_INT_ARGB通常被用来
39. 可以从缓冲图像修改像素
40. 计算平均值的图像栅格
41. 使用PixelGrabber获取像素数据从一个Image对象
42. 翻转图像
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.