2013-04-12 45 views
0

通常當我想拍的動畫,我通過繪製矩形重置所有圖形畫面:如何在transperant JFrame上使用paintComponent重置圖形?

@Override 
public void paintComponent(Graphics g) { 
    g.setColor(Color.white); 
    g.fillRect(0,0,1000,1000); 

    // now im drawing the animation on empty screen 
} 

後,我drawed的矩形我可以畫空白屏幕上的動畫,因此動畫會移動,而不是傳播。現在我想在Transparent JFrame上繪製動畫。如何從Precedings圖紙中清空組件,並仍然保持JFrame傳輸?

回答

2
  1. 確保你的元件都是非不透明
  2. 對於透明度的工作,只要你願意,你不需要做的事情paintComponent()(尤其是不填充白色矩形)
  3. 重置所有內容,設置標誌或條件並調用repaint()。在paintComponent()中,檢查該標誌或條件,如果爲true,則不要繪製任何東西。

這裏是一個半透明框架的小演示,它在鼠標移動的任何地方繪製一條線。無論你何時點擊框架,它都會刪除所有內容。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class TestTransparentFrame { 

    private class PaintPanel extends JPanel { 
     private List<Point> points = new ArrayList<Point>(); 

     public PaintPanel() { 
      setOpaque(false); 
      MouseAdapter adapter = new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        points.clear(); 
        repaint(); 
       } 

       @Override 
       public void mouseMoved(MouseEvent e) { 
        points.add(e.getPoint()); 
        repaint(); 
       } 
      }; 
      addMouseListener(adapter); 
      addMouseMotionListener(adapter); 
      setBorder(BorderFactory.createLineBorder(Color.GREEN)); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (points.size() > 1) { 
       g.setColor(Color.RED); 
       Point p1 = points.get(0); 

       for (int i = 1; i < points.size(); i++) { 
        Point p2 = points.get(i); 
        g.drawLine(p1.x, p1.y, p2.x, p2.y); 
        p1 = p2; 
       } 
      } 
     } 

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

    protected void createAndShowGUI() throws MalformedURLException, IOException { 
     JFrame frame = new JFrame("Test transparent painting"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setUndecorated(true); 
     frame.setBackground(new Color(0, 0, 0, 50)); 
     frame.add(new PaintPanel()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new TestTransparentFrame().createAndShowGUI(); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 

} 
相關問題