2013-01-10 2 views
3

SwingUtilities, 구현을 사용하여하고 용기 (JPanel, JLabel)에서 청취하지 않고, (API의 모든 신고자를 구현되지 않음) Icon/ImageIcon에서 MouseEvents을 수신 또는 이벤트를 변환하여 가능하세요 code (@pietblok) 등과 같은 일반 바닐라에 Icon/ImageIconMouseEvent는 및 아이콘/이미지 아이콘

편집

뭔가를 XxxListener을 추가하지만, 그래픽 개체를 만드는 경우 내 질문에 아마 대답은, 잘 모르겠어요, BufferedImage의 및 paintIcon 지난 오입니다 F 속성은

, 당신은 인 JPanel 또는 JLabel의 같은 ​​기존 JComponent의를 사용하거나 임시을 구현해야합니다 그것을 할 수없는, 아니

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.util.Map; 
import java.util.WeakHashMap; 

import javax.swing.Icon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class TestMouseAwareIcon { 

    public static class MouseAwareIcon extends MouseAdapter implements Icon { 

     private static final long serialVersionUID = 1L; 
     private int size = 80, halfSize = 40; 
     private final BufferedImage image; 
     private Map components = new WeakHashMap(); 

     public MouseAwareIcon() { 
      super(); 
      image = createImage(); 
     } 

     @Override 
     public int getIconHeight() { 
      return image.getHeight(); 
     } 

     @Override 
     public int getIconWidth() { 
      return image.getWidth(); 
     } 

     @Override 
     public void mouseClicked(MouseEvent event) { 
      Object source = event.getSource(); 
      if (source instanceof Component) { 
       Component component = (Component) source; 
       Point paintPoint = (Point) components.get(component); 
       if (paintPoint == null) { 
        System.out.println("unknown component"); 
       } else { 
        Point mousePoint = event.getPoint(); 
        int imageX = mousePoint.x - paintPoint.x; 
        int imageY = mousePoint.y - paintPoint.y; 
        if (imageX >= 0 && imageX < this.getIconWidth() && imageY >= 0 
          && imageY < this.getIconHeight()) { 
         int argb = image.getRGB(imageX, imageY); 
         int alpha = (argb << 0) >>> 24; 
         int red = (argb << 8) >>> 24; 
         int green = (argb << 16) >>> 24; 
         int blue = (argb << 24) >>> 24; 
         System.out.println("Color clicked on " 
           + component.getName() + ": " + alpha + "," 
           + red + "," + green + "," + blue); 
         int fillX = halfSize * (imageX/halfSize); 
         int fillY = halfSize * (imageY/halfSize); 
         Graphics2D g2 = image.createGraphics(); 
         g2.setColor(new Color(255 - red, 255 - green, 
           255 - blue, alpha)); 
         g2.fill3DRect(fillX, fillY, halfSize, halfSize, true); 
         g2.dispose(); 
         component.repaint(); 
        } else { 
         System.out.println("Clicked outside image area"); 
        } 
       } 
      } 
     } 

     @Override 
     public void paintIcon(Component component, Graphics g, int x, int y) { 
      ((Graphics2D) g).drawImage(image, null, x, y); 
      if (!components.containsKey(component)) { 
       component.addMouseListener(this); 
      } 
      components.put(component, new Point(x, y)); 
     } 

     private BufferedImage createImage() { 
      BufferedImage image1 = new BufferedImage(size, size, 
        BufferedImage.TYPE_INT_ARGB); 
      Graphics2D g2 = image1.createGraphics(); 
      Color[] colors = new Color[]{Color.BLACK, Color.RED, Color.GREEN, 
       Color.BLUE}; 
      int colorIndex = 0; 
      for (int x = 0; x < size; x += halfSize) { 
       for (int y = 0; y < size; y += halfSize) { 
        g2.setColor(colors[colorIndex]); 
        g2.fill3DRect(x, y, halfSize, halfSize, true); 
        colorIndex++; 
       } 
      } 
      g2.dispose(); 
      return image1; 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       final JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       JLabel label = new JLabel(new MouseAwareIcon()); 
       label.setName("label"); 
       frame.getContentPane().add(label); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     });  
    } 
} 
+2

Icon/ImageIcon을 렌더링하는 유일한 두 가지 방법은 아이콘 렌더링을 지원하는 구성 요소 (예 :'JLabel' 및/또는'JButton')에 연결하거나 자신의 그림을 그리는 것입니다. – MadProgrammer

+2

장식되지 않은 버튼을 사용하는 이유는 무엇입니까? –

답변

1

(I 몇 similair 코드,이 SSCCE의 형태를 보았다) JComponent는 당신이 원하는 것을 할 수 있습니다.

관련 문제