2013-03-22 252 views
0

的accourate点我Panel,鼠标拖动功能跳过一些问题,例如:鼠标拖动未返回鼠标

如果鼠标按下点是:847,我迅速地拖着鼠标它返回:853,861,....

我希望将值精确到一个点。这怎么能实现?

这是我的代码。

package Main; 

import java.awt.AWTException; 
import java.awt.AlphaComposite; 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Cursor; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Insets; 
import java.awt.KeyboardFocusManager; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.RenderingHints; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionAdapter; 
import java.awt.event.MouseMotionListener; 
import java.awt.geom.Area; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.imageio.IIOImage; 
import javax.imageio.ImageIO; 
import javax.imageio.ImageWriteParam; 
import javax.imageio.ImageWriter; 
import javax.imageio.stream.ImageOutputStream; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.filechooser.FileNameExtensionFilter; 


public class ScreenCapture{ 
    static ImageArea ia = new ImageArea(); 
    static JFrame mainFrame = new JFrame(); 
    static Rectangle rectScreenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 

    static Robot robot; 
    private static File movieFolder=null; 

    static String cl="null"; 
    JScrollPane jsp; 
    static Date date = new Date(); 
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_h_mm_ss_a"); 
    static String formattedDate = sdf.format(date); 

    public ScreenCapture() throws AWTException { } 

public static void save() { 
    if (ia.getImage() == null) { 
    System.out.println("No captured image."); 
    return; 
    } 
    ImageWriter writer = null; 
    ImageOutputStream ios = null; 

    try { 
    Iterator iter = ImageIO.getImageWritersByFormatName("jpeg"); 

    if (!iter.hasNext()) { 
     System.out.println("Unable to save image to jpeg file type."); 
     return; 
    } 
    //writer = (ImageWriter) iter.next(); 
    //ios = ImageIO.createImageOutputStream(new File("c:\\Users\\Aadi\\Desktop   \\a.jpg")); 
    //writer.setOutput(ios); 
    //ImageWriteParam iwp = writer.getDefaultWriteParam(); 
    //iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
    //iwp.setCompressionQuality(0.95f); 
    BufferedImage im = (BufferedImage) ia.getImage(); 
    BufferedImage im1 = im.getSubimage(ia.selectedRectangle.x+1, ia.selectedRectangle.y+1, ia.selectedRectangle.width-1, ia.selectedRectangle.height-1); 
      //robot.createScreenCapture(new Rectangle(ia.selectedRectangle.x+1,  ia.selectedRectangle.y+1, ia.selectedRectangle.width-1, ia.selectedRectangle.height-1)); 
    //writer.write(null, 
    // new IIOImage(im.getSubimage(ia.selectedRectangle.x, ia.selectedRectangle.y, ia.selectedRectangle.width, ia.selectedRectangle.height), null, null), iwp); 
    //Graphics g2d = im1.createGraphics(); 
    //ia.paint(g2d); 
    if(movieFolder == null){ 
            movieFolder = null; 
         //MainIconPage d1 = new MainIconPage(); 
        if(Main.MainIconPage.jfc == null) 
         Main.MainIconPage.jfc = new JFileChooser(); 
    // set selection to folders only 
    Main.MainIconPage.filter = new FileNameExtensionFilter("Portable Network Graphics", "png", "png"); 

    Main.MainIconPage.jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
    Main.MainIconPage.jfc.setSelectedFile(new File("C:/"+"screenShot"+formattedDate+".png")); 
    Main.MainIconPage.jfc.setFileFilter(Main.MainIconPage.filter); 
    // show chooser dialog 
    int r = Main.MainIconPage.jfc.showSaveDialog(Main.MainIconPage.jfc); 
    // on selection 
    if (r == JFileChooser.APPROVE_OPTION) { 
     // set the save folder 
     movieFolder = Main.MainIconPage.jfc.getSelectedFile(); 
    //ImageIO.write(screenShot, "PNG", new File(movieFolder,"")); 
    ImageIO.write(im1, "PNG", new File(movieFolder,"")); 
    cl = movieFolder.toURL().toString().substring(6); 
    }else if(r == JFileChooser.CANCEL_OPTION){ 
    Main.MainIconPage.jfc.hide(); 
    Main.MainIconPage.jfc = null; 
    }} 

    } catch (Exception e2) { 
    e2.printStackTrace(); 
    } 

    } 
    public static void main(String[] args) throws Exception { 
    try{ 
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    }catch(Exception ex){} 

    final ScreenCapture sc = new ScreenCapture(); 
    sc.robot = new Robot(); 
    sc.mainFrame.setUndecorated(true); 
    sc.mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    sc.mainFrame.setVisible(false); 

     BufferedImage biScreen = robot.createScreenCapture(rectScreenSize); 
     sc.mainFrame.setVisible(true); 

     ia.setImage(biScreen); 
    sc.mainFrame.getContentPane().add(ia); 
    sc.mainFrame.setVisible(true); 
    sc.mainFrame.addKeyListener(new KeyListener() { 

     @Override 
     public void keyTyped(KeyEvent e) { 
      //throw new UnsupportedOperationException("Not supported yet."); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      char c = e.getKeyChar(); 
      if(c==KeyEvent.VK_ESCAPE){ 
      sc.mainFrame.dispose(); 
      } 
     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      //throw new UnsupportedOperationException("Not supported yet."); 
     } 
    }); 
    } 
} 
    class ImageArea extends JPanel 
    implements MouseListener, MouseMotionListener{ 
    private Image image; 
    final static float dash1[] = { 2.0f }; 
    final static BasicStroke dashed = new BasicStroke(1.0f, 
    BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); 
    Point startPoint = new Point(), endPoint = new Point(); 
    buttonActions btAction = new buttonActions(); 
    Rectangle selectedRectangle=null; 
    boolean dragging=false; 
    ImageArea component; 
    private int width1; 
    private int height1; 
    private final int PROX_DIST=3; 
    private int mousePressedX; 
    private int mousePressedY; 
    private int x1; 
    private int y1; 
    private int x2; 
    private int y2; 
    private int x3; 
    private int y3; 
    private int width,height; 
    static JButton btSave,btClose,btBrowse,btEdit,btSize; 
    ImageIcon save = new ImageIcon(ClassLoader.getSystemResource("images/save.png")); 
    ImageIcon close = new ImageIcon(ClassLoader.getSystemResource("images/close.png")); 
    ImageIcon tool = new ImageIcon(ClassLoader.getSystemResource("images/tool.png")); 
    ImageIcon browse = new ImageIcon(ClassLoader.getSystemResource("images/browse.png")); 
    private boolean move=false; 
    private int moveX; 
    private int moveY; 
    boolean yes=true; 
    private Point mouseDraggedPoint; 
    private Point mousePressedPoint; 
    private boolean not; 
    private boolean reverse=false; 
    private int resetHeight; 
    private int resetWidth; 
    private boolean resetRectangle=false; 
    SwingUtil s = new SwingUtil(); 
    private Rectangle areaRect; 
    public ImageArea() { 
    component = this; 
    btSave =new JButton(save); 
    btSave.setPreferredSize(new Dimension(40,40)); 
    btSave.setToolTipText("Save"); 
    btSave.setActionCommand("btSave"); 
    btClose = new JButton(close); 
    btClose.setPreferredSize(new Dimension(40,40)); 
    btClose.setToolTipText("Close"); 
    btClose.setActionCommand("btClose"); 
    btBrowse = new JButton(browse); 
    btBrowse.setPreferredSize(new Dimension(40,40)); 
    btBrowse.setToolTipText("Browse"); 
    btBrowse.setActionCommand("btBrowse"); 
    btEdit = new JButton(tool); 
    btEdit.setPreferredSize(new Dimension(40,40)); 
    btEdit.setToolTipText("Edit your image!"); 
    btEdit.setActionCommand("btEdit"); 
    btSize = new JButton(); 
    btSize.setEnabled(false); 
    btSave.addMouseListener(new MouseStatic(this,1)); 
    btEdit.addMouseListener(new MouseStatic(this,2)); 
    btBrowse.addMouseListener(new MouseStatic(this,3)); 
    btClose.addMouseListener(new MouseStatic(this,4)); 
    this.add(btSave); 
    this.add(btClose); 
    this.add(btBrowse); 
    this.add(btEdit); 
    this.add(btSize); 
    this.requestFocus(true); 
    this.requestFocusInWindow(true); 
    this.addKeyListener(new KeyListener() { 

     @Override 
     public void keyTyped(KeyEvent e) { 
      //throw new UnsupportedOperationException("Not supported yet."); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      char c = e.getKeyChar(); 
      if(c==KeyEvent.VK_ESCAPE){ 
      ScreenCapture.mainFrame.dispose(); 
      } 
     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      //throw new UnsupportedOperationException("Not supported yet."); 
     } 
    }); 
    this.setLayout(null); 
    this.setFocusable(true); 
    this.requestFocus(true); 
    this.requestFocusInWindow(true); 
    addMouseListener(this);  // listens for own mouse and 
    addMouseMotionListener(this); 
    } 



    public Image getImage() { 
    return image; 
    } 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if(image != null) 
    g.drawImage(image, 0, 0, this); 

    if (startPoint.x != endPoint.x || startPoint.y != endPoint.y) { 
    x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x; 
    y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y; 
    x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x; 
    y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y; 
    if (Main.MainIconPage.isFirstTime) { 
    areaRect = new Rectangle(Main.MainIconPage.d); 
    Main.MainIconPage.isFirstTime = false; 
    } 
    Graphics2D g2 = (Graphics2D) g.create(); 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 
    g2.setStroke(dashed); 

    if(x1 ==0 && y1 == 0){ 
    g2.dispose(); 
    } 
    else{ 
     g2.setColor(Color.BLACK); 
     g2.draw(selectedRectangle); 
     g2.setColor(Color.black); 
     Area area = new Area(); 

    area.add(new Area(new Rectangle2D.Float(0, 0, getWidth(), getHeight()))); 

     g2.setColor(Color.BLACK.brighter()); 

     int width = getWidth() - 1; 
     int height = getHeight() - 1; 

     int openWidth = 200; 
    int openHeight = 200; 

    int x = (width - openWidth)/2; 
    int y = (height - openHeight)/2; 


    area.subtract(new Area(selectedRectangle)); 



    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
    g2.fill(area); 
    g2.setColor(Color.WHITE); 
    g2.setStroke(new BasicStroke(1.5f)); 

    g2.setColor(Color.BLACK.darker()); 
    g2.fillRect(selectedRectangle.x-4, selectedRectangle.y-4, 4, 4); 
    g2.fillRect(selectedRectangle.x-4,  (selectedRectangle.y+selectedRectangle.height/2)-4, 4, 4); 
    g2.fillRect(selectedRectangle.x-4, selectedRectangle.y+selectedRectangle.height, 4, 4); 
    g2.fillRect((selectedRectangle.x+selectedRectangle.width/2)-4, selectedRectangle.y-4, 4, 4); 
    g2.fillRect(selectedRectangle.x+selectedRectangle.width, selectedRectangle.y-4, 4, 4); 
    g2.fillRect((selectedRectangle.x+selectedRectangle.width/2)-4, selectedRectangle.y+selectedRectangle.height, 4, 4); 
    g2.fillRect(selectedRectangle.x+selectedRectangle.width, selectedRectangle.y+selectedRectangle.height, 4, 4); 
    g2.fillRect(selectedRectangle.x+selectedRectangle.width, (selectedRectangle.y+selectedRectangle.height/2)-4, 4, 4); 
    g2.dispose(); 
    }} 
    } 

    public void setImage(Image image) { 
    this.image = image; 
    setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this))); 
    revalidate(); 
    startPoint.x = endPoint.x; 
    startPoint.y = endPoint.y; 
    repaint(); 
    } 


    @Override 
    public void mouseClicked(MouseEvent e) { 
    if(!selectedRectangle.contains(e.getPoint())){ 
    ScreenCapture.mainFrame.dispose(); 
    } 
    if(selectedRectangle.x == selectedRectangle.y){ 
    ScreenCapture.mainFrame.dispose(); 
    Main.ScreenCapture.mainFrame=null; 
    } 
    } 

    @Override 
    public void mousePressed(MouseEvent e) { 

    this.requestFocus(true); 
    this.requestFocusInWindow(true); 
    if(!dragging && !move){if (image == null) { 
      return; 
     } 
    if(selectedRectangle==null) 
     selectedRectangle = new Rectangle(); 
     startPoint = endPoint = e.getPoint(); 
     mousePressedX = e.getX(); 
     mousePressedY = e.getY(); 
     btSize.setVisible(true); 
     repaint(); 
    } 
    else if(move&&!dragging){ 
        moveX =selectedRectangle.x- e.getX(); 
        moveY = selectedRectangle.y-e.getY(); 
        mousePressedPoint = e.getPoint(); 
        btSave.setVisible(false); 
        btEdit.setVisible(false); 
        btBrowse.setVisible(false); 
        btClose.setVisible(false); 
        not=true; 

    } 
    else if(this.getCursor() != Cursor.getDefaultCursor() || component.getCursor()!= Cursor.getDefaultCursor()) { 

     dragging = true; 

    } 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 

    if(selectedRectangle.x != selectedRectangle.y){ this.requestFocus(true); 
    this.requestFocusInWindow(true); 
    dragging = true; 
    move=false; 
    resetHeight = selectedRectangle.height; 
    resetWidth = selectedRectangle.width; 
    btSave.setBounds(selectedRectangle.x+selectedRectangle.width+2, selectedRectangle.y+selectedRectangle.height-174, 42, 42); 
    btEdit.setBounds(selectedRectangle.x+selectedRectangle.width+2, selectedRectangle.y+selectedRectangle.height-130, 42, 42); 
    btBrowse.setBounds(selectedRectangle.x+selectedRectangle.width+2, selectedRectangle.y+selectedRectangle.height-86, 42, 42); 
    btClose.setBounds(selectedRectangle.x+selectedRectangle.width+2, selectedRectangle.y+selectedRectangle.height-42, 42, 42); 
    btSave.setVisible(true); 
    btEdit.setVisible(true); 
    btBrowse.setVisible(true); 
    btClose.setVisible(true); 
    btSize.setVisible(true); 
    if(selectedRectangle.y<20){ 
    btSize.setBounds(moveX+e.getX(),moveY+e.getY(), 80, 20); 

    }else{ 
    btSize.setBounds(selectedRectangle.x,selectedRectangle.y-20, 80, 20); 
    } 
    if(yes){ 
     btSave.addActionListener(btAction); 
    btClose.addActionListener(btAction); 
    btBrowse.addActionListener(btAction); 
    btEdit.addActionListener(btAction); 
    yes=false;}} 
    } 



    @Override 
    public void mouseDragged(MouseEvent e) { 
    endPoint = e.getPoint(); 
    if(!dragging&&!move){ 

     if (image == null) 
     return; 
    int mouseDraggedX = e.getX(); 
    int mouseDraggedY = e.getY(); 
    width1 = Math.abs(mouseDraggedX - mousePressedX); 
    height1 = Math.abs(mouseDraggedY - mousePressedY); 

    x3 = Math.min(mouseDraggedX, mousePressedX); 
    y3 = Math.min(mouseDraggedY, mousePressedY); 



     x1 = (startPoint.x < endPoint.x) ? startPoint.x : endPoint.x; 
     y1 = (startPoint.y < endPoint.y) ? startPoint.y : endPoint.y; 
     x2 = (startPoint.x > endPoint.x) ? startPoint.x : endPoint.x; 
     y2 = (startPoint.y > endPoint.y) ? startPoint.y : endPoint.y; 
         width = (x2-2 - x1) + 1; 
         height = (y2-2 - y1) + 1; 
         if(x1<x2 &&y1<y2){ 
       btSize.setBounds(x3, y3-20, 80, 20); 
         } 
       selectedRectangle.reshape(x3, y3, width1, height1);  
       btSize.setText(width+"x"+height); 
       repaint(); 
    repaint();} 
    else if(move && component.getCursor().equals(Cursor.getDefaultCursor())){ 
    mouseDraggedPoint = e.getPoint(); 


    int resetY = Math.abs(mousePressedPoint.y-selectedRectangle.y); 
    int resetX = Math.abs(mousePressedPoint.x-selectedRectangle.x); 
    int y = mousePressedPoint.y-e.getPoint().y; 
    int x = mousePressedPoint.x-e.getPoint().x; 

    if(!checkRect() ||selectedRectangle.y<1) 
    { 
     if(not){ 
     mousePressedPoint=e.getPoint(); 
     selectedRectangle.y=selectedRectangle.y+1; 

     selectedRectangle.height=resetHeight-y+1; 
     not=false; 
     } 


     resetY = Math.abs(mousePressedPoint.y-selectedRectangle.y); 
     if(resetY>e.getPoint().y){ 
    selectedRectangle.x=moveX+e.getX(); 
    selectedRectangle.y=selectedRectangle.y; 
    selectedRectangle.width=selectedRectangle.width; 
    selectedRectangle.height=resetHeight-y; 
     }else{ 
     selectedRectangle.setLocation(moveX+e.getX(),moveY+e.getY()); 
     } 

    }else{ 
    selectedRectangle.setLocation(moveX+e.getX(),moveY+e.getY()); 
    } 


    if(selectedRectangle.y<20){ 
    btSize.setBounds(moveX+e.getX(),moveY+e.getY(), 80, 20); 
    btSize.setBounds(selectedRectangle.x, 0, 80, 20); 
    } 
    else{ 
    btSize.setBounds(moveX+e.getX(),moveY+e.getY()-20, 80, 20); 
    btSize.setBounds(selectedRectangle.x, selectedRectangle.y-20, 80, 20); 
    } 
    btSize.setText(selectedRectangle.width+"x"+selectedRectangle.height); 
    repaint(); 
    }else { 
    endPoint = e.getPoint(); 
     Point p1 = e.getPoint(); 
     Rectangle r = component.selectedRectangle; 
     int type = component.getCursor().getType(); 
     int dx = p1.x - r.x; 
     int dy = p1.y - r.y; 
     switch(type) { 
      case Cursor.N_RESIZE_CURSOR: 
       int height=height1 = Math.abs(r.height - dy); 
       selectedRectangle.reshape(r.x, r.y+dy, r.width, height); 
       repaint(); 
       break; 
      case Cursor.NW_RESIZE_CURSOR: 
       int width =width1 = Math.abs(r.width - dx); 
       height=height1 = Math.abs(r.height - dy); 
       selectedRectangle.reshape(r.x+dx, r.y+dy, width, height); 
       repaint(); 
       break; 
      case Cursor.W_RESIZE_CURSOR: 
       width =width1 = Math.abs(r.width - dx); 
       selectedRectangle.reshape(r.x+dx, r.y, width, r.height); 
       repaint(); 
       break; 
      case Cursor.SW_RESIZE_CURSOR: 
       width =width1 = Math.abs(r.width - dx); 
       height=height1 = Math.abs(dy); 
       selectedRectangle.reshape(r.x+dx, r.y, width, height); 
       repaint(); 
       break; 
      case Cursor.S_RESIZE_CURSOR: 
       height=height1 = Math.abs(dy); 
       selectedRectangle.reshape(r.x, r.y, r.width, height); 
       repaint(); 
       break; 
      case Cursor.SE_RESIZE_CURSOR: 
       width =width1 = Math.abs(dx); 
       height=height1 = Math.abs(dy); 
       selectedRectangle.reshape(r.x, r.y, width, height); 
       repaint(); 
       break; 
      case Cursor.E_RESIZE_CURSOR: 
       width =width1 = Math.abs(dx); 
       selectedRectangle.reshape(r.x, r.y, width, r.height); 
       repaint(); 
       break; 
      case Cursor.NE_RESIZE_CURSOR: 
       width =width1 = Math.abs(dx); 
       height=height1 = Math.abs(r.height - dy); 
       selectedRectangle.reshape(r.x, r.y+dy, width, height); 
       repaint(); 
       break; 
      default: 
       System.out.println("unexpected type: " + type); 
     } 
     btSave.setVisible(false); 
     btEdit.setVisible(false); 
     btBrowse.setVisible(false); 
     btClose.setVisible(false); 
     btSize.setText(width1+"x"+height1); 
     btSize.setBounds(selectedRectangle.x, selectedRectangle.y-20, 80, 20); 
    } 
    } 

    @Override 
    public void mouseMoved(MouseEvent e) { 
    Point p = e.getPoint(); 
    if(!isOverRect(p)) { 
     if(component.getCursor() != Cursor.getDefaultCursor()) { 

      component.setCursor(Cursor.getDefaultCursor()); 
      dragging = true; 
      move = false; 

     } 
     return; 
    }else{ 

    dragging = false; 
    move = true; 


    } 
    int outcode = getOutcode(p); 
    Rectangle r = component.selectedRectangle; 
    switch(outcode) { 
     case Rectangle.OUT_TOP: 
      if(Math.abs(p.y - r.y) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.N_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_TOP + Rectangle.OUT_LEFT: 
      if(Math.abs(p.y - r.y) < PROX_DIST && 
       Math.abs(p.x - r.x) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.NW_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_LEFT: 
      if(Math.abs(p.x - r.x) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.W_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_LEFT + Rectangle.OUT_BOTTOM: 
      if(Math.abs(p.x - r.x) < PROX_DIST && 
       Math.abs(p.y - (r.y+r.height)) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.SW_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_BOTTOM: 
      if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.S_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_BOTTOM + Rectangle.OUT_RIGHT: 
      if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST && 
       Math.abs(p.y - (r.y+r.height)) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.SE_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_RIGHT: 
      if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.E_RESIZE_CURSOR)); 
      } 
      break; 
     case Rectangle.OUT_RIGHT + Rectangle.OUT_TOP: 
      if(Math.abs(p.x - (r.x+r.width)) < PROX_DIST && 
       Math.abs(p.y - r.y) < PROX_DIST) { 
       component.setCursor(Cursor.getPredefinedCursor(
            Cursor.NE_RESIZE_CURSOR)); 
      } 
      break; 
     default: // center 
      component.setCursor(Cursor.getDefaultCursor()); 
    } 


    } 
    private int getOutcode(Point p) { 
    Rectangle r = (Rectangle)component.selectedRectangle.clone(); 
    r.grow(-PROX_DIST, -PROX_DIST); 
    return r.outcode(p.x, p.y);   
    } 

    private boolean isOverRect(Point p) { 
    Rectangle r = (Rectangle)component.selectedRectangle.clone(); 
    r.grow(PROX_DIST, PROX_DIST); 
    return r.contains(p); 
    } 

    private String mouseDirection(Point mousePressedPoint, Point mouseDraggedPoint) { 



    return ""; 
    } 

    private static class buttonActions implements ActionListener { 

    public buttonActions() { 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     switch(e.getActionCommand()){ 
      case "btSave": 
       Main.ScreenCapture.save(); 
       break; 
      case "btBrowse": 
       Main.ScreenCapture.browse(); 
       break; 
      case "btEdit": 
       Main.ScreenCapture.edit(); 
       break; 
      case "btClose": 
       Main.ScreenCapture.close(); 

       break; 
     } 
    } 
    } 
    boolean checkRect() { 
    if (this.areaRect == null) { 
    return false; 
    } 

    if (this.areaRect.contains(this.selectedRectangle.x-10, this.selectedRectangle.y-10, this.selectedRectangle.width, this.selectedRectangle.height)) { 
    return true; 
    } 
    int new_x = this.selectedRectangle.x; 
    int new_y = this.selectedRectangle.y; 

    if ((this.selectedRectangle.x + this.selectedRectangle.width) > this.areaRect.getWidth()) { 
    new_x = (int) this.areaRect.getWidth() - (this.selectedRectangle.width-1); 
    } 
    if (this.selectedRectangle.x < 1) { 
    new_x = -1; 
    } 
    if ((this.selectedRectangle.y + this.selectedRectangle.height) > this.areaRect.getHeight()) { 
    new_y = (int) this.areaRect.getHeight() - (this.selectedRectangle.height-1); 
    } 
    if (this.selectedRectangle.y < 1) { 
    new_y = -1; 
    } 
    this.selectedRectangle.setLocation(new_x, new_y); 
    return false; 
    } 
    } 
+1

太多的代码阅读(至少10倍)。请构建一个最小测试用例。 – Dukeling 2013-03-22 09:16:12

回答

0

我认为这是绝对正常的情况。鼠标驱动程序(在OS内)以特定频率工作,它以恒定速度执行一系列测量。所以,如果你的鼠标移动速度比被监控的速度快,你可能会发现你提到的行为。

解决方法:在OS的设置鼠标指针的

  • 增加灵敏度;
  • 代码执行插值(在已知点上构建函数,然后为未知点重新计算)。