-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) 
        {           
        }
    }
}
4

2 回答 2

2

找到了这个:

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

由于您将 clickPoint 设置为 mousePressed 并希望将其设置为 mouseDragged,因此它应该是同一个对象。您实际上应该在 mouseDragged 中获得一些空指针吗?

于 2013-08-23T12:02:05.670 回答
-1

setSize没有完整的代码很难看到,但一方面,您在方法中调用setLocationWindow 本身MouseDragged。如果要获取用户单击的对象,则需要从中获取它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 上。

于 2013-08-23T11:52:16.737 回答