2013-08-23 156 views
-1

我之前也發佈過這個問題,但是這次我只添加了必需和必要的代碼,儘管代碼有點冗長。 我想在Jlabel中加載圖像,然後在用戶單擊下一個按鈕時更改圖像之後。當用戶想要移動或縮放圖像時,只需選擇圖像邊緣即可輕鬆完成,但不起作用。 除了縮放和移動圖像,所有問題都解決了。如何通過拖動邊緣來移動和縮放圖像?

我的代碼:

public class CopyOfPictureEditor extends JFrame 
{ 
    private static final long serialVersionUID = 6676383931562999417L; 
    String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"}; 
    Stack<File> pictures ; 
    JLabel label = new JLabel(); 
    BufferedImage a = null; 
    JPanel panel = new JPanel(); 

    public CopyOfPictureEditor() 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());    
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     }    
     JMenuBar menubar = new JMenuBar(); 
     JMenu toolsmenu = new JMenu(" File ");   
     final File dir = new File(""); 
     final JFileChooser file; 
     file = new JFileChooser(); 
     file.setCurrentDirectory(dir); 
     file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
     file.showOpenDialog(panel); 
     String path = file.getSelectedFile().getAbsolutePath(); 
     System.out.println(path); 
     pictures= getFilesInFolder(path.toString());   
     JButton NEXT = new JButton("");   
     NEXT.setToolTipText("Next Image");   
     Image imgn = null;   
     try 
     { 
      imgn = ImageIO.read(getClass().getResource("/images/next12.png")); 
     }catch (IOException e) {  
      e.printStackTrace(); 
     }       
     NEXT.setIcon(new ImageIcon(imgn));    
     JPanel buttonPane = new JPanel();   
     buttonPane.add(Box.createRigidArea(new Dimension(250,0)));    
     buttonPane.add(Box.createRigidArea(new Dimension(250,0)));   
     NEXT.addActionListener(new ActionListener() {   
      @Override 
      public void actionPerformed(ActionEvent arg0) {   
       nextImage(); 
      } 
     });   
     buttonPane.add(NEXT); 
     getContentPane().add(buttonPane, BorderLayout.SOUTH);   
     setJMenuBar(menubar); 
     menubar.add(toolsmenu);  
     panel.add(label,BorderLayout.CENTER);  
     add(panel);   
     setTitle("Aero Picture Editor"); 
     setVisible(true);  
     setPreferredSize(getPreferredSize()); 
     setLocation(0,0); 
      label.addMouseListenet(new MouseHandler());  
      label.addMouseMotionListenet(new MouseHandler()); 
    } 
    public Stack<File> getFilesInFolder(String startPath){ 
     File startFolder = new File(startPath); 
     Stack<File> picturestack = new Stack<File>();  
     String extension; 
     int dotindex; 
     for (File file : startFolder.listFiles()) { 
      extension = ""; 
      dotindex = file.getName().lastIndexOf('.'); 

      if (dotindex > 0) { 
       extension = file.getName().substring(dotindex + 1);      
       for (String filetype : validpicturetypes){ 
        if (extension.equals(filetype)) { 
         picturestack.add(file); 
        } 
       } 
      } 
     } 
    return picturestack; 
    } 
     public void nextImage() { 
     String p; 
     File f; 
     try{ 
      f= pictures.pop().getAbsoluteFile(); 
      a=ImageIO.read(f); 
      p = f.getPath(); 
      System.out.println(p);     
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     ImageIcon image = new ImageIcon(a);   
     label.setIcon(image);   
     repaint(); 
    }  
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponents(g); 
     Graphics2D g2d = (Graphics2D) g.create();   
     int x = (getWidth() - a.getWidth())/2; 
     int y = (getHeight() - a.getHeight())/2;   
     AffineTransform at = new AffineTransform(); 
     at.translate(x, y);   
     g2d.setTransform(at);    
     g2d.drawImage(a, 0, 0, this); 
     g2d.dispose(); 
    } 
    public enum MouseAction { 
     Move(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)), 
     ResizeSouth(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)), 
     ResizeNorth(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)), 
     ResizeEast(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)), 
     ResizeWest(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)), 
     ResizeNorthEast(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)), 
     ResizeNorthWest(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)), 
     ResizeSouthEast(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)), 
     ResizeSouthWest(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR)); 
     private Cursor cursor; 
     private MouseAction(Cursor cursor) { 
      this.cursor = cursor; 
     } 
     public Cursor getCursor() { 
      return cursor; 
     } 
    } 
    public class MouseHandler extends MouseAdapter 
    { 
     private MouseAction action; 
     private Point clickPoint; 
     private boolean ignoreMoves; 
     protected void updateAction(MouseEvent e) { 
      int x = e.getX(); 
      int y = e.getY(); 
      int width = getWidth(); 
      int height = getHeight(); 
      if (x < 10 && y < 10)  { 
       action = MouseAction.ResizeNorthWest; 
      }   else if (x > width - 10 && y < 10)   { 
       action = MouseAction.ResizeNorthWest; 
      }    else if (y < 10)    { 
       action = MouseAction.ResizeNorth; 
      }    else if (x < 10 && y > height - 10)   { 
       action = MouseAction.ResizeSouthWest; 
      }    else if (x > width - 10 && y > height - 10)   { 
       action = MouseAction.ResizeSouthEast; 
      }    else if (y > height - 10)    { 
       action = MouseAction.ResizeSouth; 
      }    else if (x < 10)   { 
       action = MouseAction.ResizeWest; 
      }   else if (x > width - 10)    { 
       action = MouseAction.ResizeEast; 
      }    else    { 
       action = MouseAction.Move; 
      }   setCursor(action.getCursor()); 
     } 
     @Override 
     public void mouseMoved(MouseEvent e) { 
      if (!ignoreMoves) 
      { 
       updateAction(e); 
      } 
     } 
     @Override 
     public void mousePressed(MouseEvent e) { 
      updateAction(e); 
      ignoreMoves = true; 
      clickPoint = e.getPoint(); 
      repaint();   
      System.out.println(e.getX()); 
      System.out.println(e.getY());*/ 
     } 
     @Override 
     public void mouseReleased(MouseEvent e) { 
      clickPoint = null; 
      ignoreMoves = false;   
     } 
     @Override 
     public void mouseDragged(MouseEvent e) {   
      switch (action) { 
       case Move: { 
        Point p = e.getPoint(); 
        p.x -= clickPoint.x;     
        p=SwingUtilities.convertPoint(label, p, null);      
        setLocation(p); 
       } 
       break; 
       case ResizeWest: { 
        Point p = e.getPoint(); 
        int xDelta = p.x - clickPoint.x; 
        int width = getWidth() - xDelta; 
        int x = getX() + xDelta; 
        setSize(width, getHeight()); 
        setLocation(x, getY()); 
        revalidate(); 
       } 
       break; 
       case ResizeEast: { 
        Point p = e.getPoint(); 
        int xDelta = p.x - clickPoint.x; 
        int width = getWidth() + xDelta;     
        setSize(width, getHeight()); 
        revalidate(); 
        clickPoint = p; 
       } 
       break; 
       case ResizeNorth: { 
        Point p = e.getPoint(); 
        int yDelta = p.y - clickPoint.y; 
        int height = getHeight() - yDelta; 
        int y = getY() + yDelta;      
        setSize(getWidth(), height); 
        setLocation(getX(), y); 
        revalidate(); 
       } 
       break; 
       case ResizeSouth: { 
        Point p = e.getPoint(); 
        int yDelta = p.y - clickPoint.y; 
        int height = getHeight() + yDelta;     
        setSize(getWidth(), height); 
        revalidate(); 
        clickPoint = p; 
       } 
       break; 
      } 
     } 
     @Override 
     public void mouseExited(MouseEvent e) 
     {   
     } 
    } 
} 
+0

你有沒有可以編譯的代碼示例?你看到什麼錯誤? – Robert

+0

@Robert - 沒有先生,沒有錯誤,當我實現這個代碼時,整個窗口被拖拽,我無法通過選擇邊緣來拖動或縮放圖像。 –

+3

@ user2659972停止發佈有關同一主題的問題,並做其他人已經多次詢問的問題,提供[SSCCE](http://sscce.org)。 –

回答

2

發現這是:

label.addMouseListener(new MouseHandler());  
label.addMouseMotionListener(new MouseHandler()); 

既然你設置你的mousePressed和clickPoint想擁有它的mouseDragged,應該是同一個對象。你應該在mouseDragged中獲得一些空指針?

+0

所以現在我需要做些什麼來解決它呢? –

+0

MouseHandler mouseHandler = new mouseHandler(); label.addMouseListenet(mouseHandler); label.addMouseMotionListenet(MouseHandler);所以你會得到一些NullpointerExceptions? – JohnnyAW

+0

不,先生,它不能正常工作。是拖動工程,但拖動整個窗口。不,我沒有得到空指針異常 –

-1

很難看到沒有完整的代碼,但是對於一個,你在你的MouseDragged方法調用窗口本身setSizesetLocation。如果你想獲得用戶點擊你的對象需要從e.getSource()得到它,如:

public void mouseDragged(MouseEvent e){ 
    JLabel l = (JLabel)e.getSource(); 
    switch(action){ 
     case ResizeWest: 
       Point p = e.getPoint(); 
       int xDelta = p.x - clickPoint.x; 
       int width = getWidth() - xDelta; 
       int x = getX() + xDelta; 
       l.setSize(width, getHeight()); // call setSize on JLabel l 
       l.setLocation(x, getY()); 
       l.revalidate(); 
       break; 
    } 

順便說一句,你爲什麼要使用一個JLabel這個?我將使用一個Java對象,它使用JFrame的圖形上下文將其自身繪製到JFrame上。

+0

先生,我必須創建新的'JLabel'實例或我的全局實例'label'將在'l.setSize(width,getHeight()); l.setLocation(x,getY()); l.revalidate();' –

+0

你會有多少個標籤?一?然後你可以調用它的方法,但是如果你想擴展到多個,最好從'MouseEvent'獲取源對象。 – Robert

+0

我在整個應用程序中僅使用了單個標籤,即在全局部分中聲明。在每個地方我正在重新粉刷並更換該標籤。 –

相關問題