2012-08-07 2 views
2

반투명 한 프레임이 있고 사용자가 원하는 곳에 선을 그려주는 간단한 앱을 만들려고합니다. 마우스 이벤트를 잡기 위해 리스너를 추가 했으므로 그에 따라 적절히 표시됩니다. 잘 작동하지만 문제는 다음과 같습니다. 1) 창이 투명하지 않습니다. 2) 완전히 검은 색이며 선이 흰색으로 표시됩니다. 당신이 JFrame (this.addMouseListener(this);)스윙 코드로 예기치 않은 출력 받기

  • 이 코드 라인 setContentPane(jp); 넣어 MouseListener를 추가하기 때문에,

    import java.awt.Color; 
    import java.awt.GradientPaint; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.Paint; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.SwingUtilities; 
    
    public class MouseListen2 extends JFrame implements MouseListener { 
    
        String str = "Nothing"; 
        int x[][] = new int[100][2]; 
        int count = 0; 
        int flag = 1; 
        boolean draw = false; 
    
        MouseListen2() { 
         super("Line Draw App"); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         setSize(400, 300); 
         this.addMouseListener(this); 
         setBackground(new Color(0, 0, 0, 0)); 
         JPanel jp = new JPanel() { 
    
          public void paintComponent(Graphics g) { 
           super.paintComponent(g); 
           Graphics2D g2 = (Graphics2D) g; 
           Paint gp = new GradientPaint(300, 700, new Color(20, 20, 210, 0), 100, 00, new Color(10, 20, 40, 255)); 
           g2.setPaint(gp); 
           g2.fillRect(0, 0, getWidth(), getHeight()); 
          } 
         }; 
         setContentPane(jp); 
         setVisible(true); 
         //c.setOpaque(true); 
        } 
    
        public void paint(Graphics g) { 
         //Graphics g=this.getGraphics(); 
         //super.paint(g); 
         Graphics2D g2 = (Graphics2D) g; 
         g2.clearRect(0, 0, getWidth(), getHeight()); 
         g2.drawString(str, 50, 50); 
         //initially count=0 hence i<-1 so loop will not automatically run in the beginning 
         for (int i = 0; i < count - 1; i = i + 2) { 
          g2.drawLine(x[i][0], x[i][1], x[i + 1][0], x[i + 1][1]); 
         } 
         //repaint(); using this here creates an infinite loop as after mouse event paint is called and at the end 
         //this method is again called using the repaint() and so on the loop continues. 
        } 
    
        public static void main(String[] args) { 
         JFrame.setDefaultLookAndFeelDecorated(true); 
         SwingUtilities.invokeLater(new Runnable() { 
    
          public void run() { 
           new MouseListen2(); 
          } 
         }); 
        } 
    
        @Override 
        public void mouseClicked(MouseEvent e) { 
         str = "clicked"; 
         repaint(); 
    
        } 
    
        @Override 
        public void mousePressed(MouseEvent e) { 
         str = "pressed"; 
         repaint(); 
         x[count][0] = e.getX(); 
         x[count][1] = e.getY(); 
         count++; 
        } 
    
        @Override 
        public void mouseReleased(MouseEvent e) { 
         str = "released"; 
         draw = true; 
         x[count][0] = e.getX(); 
         x[count][1] = e.getY(); 
         count++; 
         //draw(); 
         repaint(); 
        } 
    
        @Override 
        public void mouseEntered(MouseEvent e) { 
         str = "entered"; 
         repaint(); 
        } 
    
        @Override 
        public void mouseExited(MouseEvent e) { 
         str = "exited"; 
         repaint(); 
        } 
    } 
    
  • 답변

    3
    1. JPanelMouseListener를 추가 할 수 있습니다

      사람 여기 는 코드 수 JPanel에서 BorderLayout.CENTER까지의 주소는 JFrame으로,이 경우 (그렇지 않습니다. 또 다른 JComponentJFrame에 추가 된이) mouse event에 대한 다음 JFrame

    2. 로 전체/사용 가능한 모든 공간이 채워 것은/단지 JPanel, 마우스가 JFrameRootPane 또는 ContentPane

    3. 제거에 액세스 할 수 없습니다 accesible입니다 How to Create Translucent and Shaped Windows를 참조 JPanel

    +0

    감사합니다 mKorbel, 문제가 해결되었지만 한 가지 문제가 있습니다. 마우스로 그린 선은 반투명이됩니다. –

    +0

    라인에 색이 맞지 않습니다 :-) – mKorbel

    2

    paintComponent-public void paint(Graphics g) 및 rellated 코드 블록의 이동을 주석 처리합니다.

    관련 문제