2012-10-11 4 views
1

나는 그것을 paintComponent (호가있는 다른 형식으로 그리기)로 덮어 쓰기하여 패널에 배치하여 레이블을 만들었습니다. 모든 것이 정상적으로 작동했지만 같은 위치에있는 라벨 (.setlocation)이 문제를 일으키고 있습니다.JLabel 겹치기

다른 모양의 동일한 위치에 3 개의 레이블이 있다고 가정 해 보겠습니다. A-B-C A가 먼저 작성된 후 B가 작성되고 마지막으로 c가 작성됩니다. 내가 클릭하면 내 기능은

"youve를이를 클릭"하지만 B 또는 C를 클릭하면 내가 A.

(I가 추가 해요 라벨을 클릭 한 것처럼,이 역할을하는 및 표시 페인트 스크롤 창 적층 패널)에서 다음

내가 만드는 코드는 라벨

for(int i=0;i<rowcount;i++){ 
     arcstart = Rawazimuth(azimuth[i]); 
        //custom JLabel to paint it like an arc 
     MyLabel FLabel = new MyLabel(100, 50, (int)arcstart,cellid[i],lon[i],lat[i]); 
     FLabel.setOpaque(true); 
     Dimension LabelSize = new Dimension(100,50); 
     FLabel.setSize(LabelSize); 
        //locate the JLabel to labels location. it might be same or not 
     FLabel.setLocation(lon[i], lat[i]); 

     MyPanel.add(FLabel);  
} 

이 내 사용자 지정 JLabel의 클래스입니다

public MyLabel(int W, int H, int start,int outcellid,int lonn, int latt) { 
    addMouseListener(this); 
    arcsizeW = W; 
    arcsizeH = H; 
    arcstart = start; 
    cellid = outcellid; 
    clicked = false; 
    lon = lonn; 
    lat = latt; 
} 

@Override 
public void paintComponent(Graphics g){ 
    // true if button is clicked, so paint acordingly 
    if(clicked ==true){ 
     g.setColor(dummyg.getColor()); 
    }else{ 
     g.setColor(Color.blue); 
    } 
    // draw the arc 
    g.fillArc(0, 0,arcsizeW , arcsizeH, arcstart, 60); 
} 

//if cell is clicked, change its color to red and print its cellid 
@Override 
public void mouseClicked(MouseEvent e) { 

    System.out.println(cellid); 

    dummyg = this.getGraphics(); 
    dummyg.setColor(Color.red); 
    this.paintComponent(dummyg); 
    clicked = true; 
}// other listener stuff} 

그래서 어떻게 방지합니까? 내가 jlayerpane을 사용할 수 있다고 생각하지만 최소한 4 개의 레이어가 필요합니다.

+3

자바 명명 규칙을 배우고 준수하십시오. – kleopatra

+0

은 JPanel에서 어떤 레이아웃을 사용하고 있는지 알려주고, 기본값은 FlowLayout이며 이제는 겹칠 수 있습니다. – Gianmarco

+0

절대 레이아웃을 사용하고 있습니다. – Ozan

답변

0

나는 내 문제를 해결했습니다.

내가 발견 한 실제 문제는 자바가 x, y 위치와 너비가 각각 & 인 각 구성 요소를 해석하는 것입니다. 따라서 레이블이 겹치지 않더라도 (그래픽으로) 동일한 구성 요소로 해석 될 수 있습니다.

내 문제를 해결하려면 마우스 클릭 지점과 구성 요소 센터 사이의 각도를 찾았습니다. 가장 가까운 구성 요소를 계산하고 도달했습니다.

이러한 상황에서는 레이어가 작동하지 않습니다. 다른 방법으로는 실제 모양의 contains 메서드를 재정의 할 수 있습니다.

3

모든 것이 정상적으로 작동하지만 동일한 위치 (.setlocation)에있는 JLabel은 문제가 발생합니다.

다른 모양으로 같은 위치에 3 개의 Jlabels이 있다고 가정 해 보겠습니다. A-B-C A가 처음 만들어진 다음 B가 만들어지고 마지막으로 c가 만들어집니다.

사용하는 나무 방법이있다

5

마 문의하시기 바랍니다 super.paintComponent. 구성 요소가 그림을 수정하는 것을 확인하는 것이 중요합니다.

마우스 이벤트는 비와 같습니다. 마우스 이벤트를 수신하고 멈추도록 등록 된 첫 번째 구성 요소에 충돌합니다 (비가 우산을 치는 것처럼).

중복되는 구성 요소가있는 경우 겹치는 부분은 마우스 이벤트가 처리 된 첫 번째 구성 요소 (이 경우 A)로 이동합니다.

여기에 내가 무슨 뜻인지 보여 빠른 예입니다 ... 당신은 각각의 원 위에 마우스로

enter image description here enter image description here

, 그것은 강조 중간 유혹하려고합니다 ...

public class OverlappingMouseTest { 

    public static void main(String[] args) { 
     new OverlappingMouseTest(); 
    } 

    public OverlappingMouseTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new BackgroundPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      }    
     }); 
    } 

    protected class BackgroundPane extends JLayeredPane { 

     private CirclePane redPane; 
     private CirclePane greenPane; 
     private CirclePane bluePane; 

     public BackgroundPane() {    
      redPane = new CirclePane(Color.RED); 
      greenPane = new CirclePane(Color.GREEN); 
      bluePane = new CirclePane(Color.BLUE); 

      add(redPane); 
      add(greenPane); 
      add(bluePane);    
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     public void doLayout() {    
      Dimension size = new Dimension(100, 100); 
      int width = getWidth() - 1; 
      int height = getHeight() - 1; 

      Point p = new Point(); 
      p.x = (width/2) - 50; 
      p.y = (height/2) - 75;    
      redPane.setBounds(new Rectangle(p, size)); 

      p.x = (width/2) - 75; 
      p.y = (height/2) - 25; 
      bluePane.setBounds(new Rectangle(p, size)); 

      p.x = (width/2) - 25; 
      p.y = (height/2) - 25; 
      greenPane.setBounds(new Rectangle(p, size));    
     }   
    } 

    protected class CirclePane extends JPanel { 

     private boolean mouseHover = false; 

     public CirclePane(Color background) { 
      setOpaque(false); 
      setBackground(background); 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseExited(MouseEvent me) { 
        mouseHover = false; 
        repaint(); 
       } 

       @Override 
       public void mouseEntered(MouseEvent me) { 
        mouseHover = true; 
        repaint(); 
       }     
      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(100, 100); 
     } 

     @Override 
     protected void paintComponent(Graphics grphcs) {    
      super.paintComponent(grphcs); 

      Graphics2D g2d = (Graphics2D) grphcs.create(); 
      if (!mouseHover) { 
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
      } 
      g2d.setColor(getBackground()); 
      g2d.fill(new Ellipse2D.Float(0, 0, getWidth() - 1, getHeight() - 1)); 
      g2d.dispose();    
     }   
    }  
} 
+0

+1 멋진 그림 :-) – kleopatra

+0

@kleopatra는 너희들을 행복하게 해줘야한다.) – MadProgrammer

+0

+1 나는 그들의 손에 너무 많은 시간을 가진 사람들을 감지한다. – Robin