2010-04-26 19 views
1

我必須爲我的白板應用程序添加清晰的屏幕選項,通常的過程是繪製一個填充矩形的圖像大小。但在我的應用程序中,我將透明面板添加到另一個上面,即作爲圖層,如果按照常規步驟從底層面板繪製圖形將不會顯示。請告訴我任何邏輯來做到這一點。如何將清除選項添加到此白板?

public void createFrame() 

{ 
    JFrame frame = new JFrame(); 
    JLayeredPane layerpane=frame.getLayeredPane(); 
    board= new Whiteboard(client); //board is a transparent panel 
    // tranparent image: 
    board.image = new BufferedImage(590,690, BufferedImage.TYPE_INT_ARGB); 
    board.setBounds(74,23,590,690); 
    board.setImage(image);  
    virtualboard.setImage(image); //virtualboardboard is a transparent panel 
    virtualboard.setBounds(74,23,590,690); 
    JPanel background=new JPanel(); 
    background.setBackground(Color.white); 
    background.setBounds(74,25,590,685); 
    layerpane.add(board,new Integer(5)); 
    layerpane.add(virtualboard,new Integer(4));//Panel where remote user draws 
    layerpane.add(background,new Integer(3)); 
    layerpane.add(board.colourButtons(),new Integer(2)); 
    layerpane.add(board.shapeButtons(),new Integer(1)); 
    layerpane.add(board.createEmptyPanel(),new Integer(0)); 
} 

回答

1

爲什麼你不「清楚」(我的意思是不要在你的板上畫任何東西),並調用JLayeredPane上的重繪。這將強制重新繪製完整的面板堆棧,不是嗎?

編輯:從羅曼蓋伊與StackLayout添加代碼示例,請參閱ClearShape &的paintComponent方法

EDIT2:調用重繪也可以用良好的裁剪區域渲染更快完成,但我讓你修改代碼爲...

package swing; 

import java.awt.AlphaComposite; 
import java.awt.BasicStroke; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Composite; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.Shape; 
import java.awt.event.ActionEvent; 
import java.awt.geom.Ellipse2D; 
import java.awt.image.BufferedImage; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ClearLayeredPanelsGUI extends JFrame { 
    Action clearRect; 
    Action drawRect; 
    Action clearRound; 
    Action drawRound; 

    TransparentBoard rectBoard; 
    TransparentBoard roundBoard; 
    JPanel whiteBackground; 

    JPanel stack; 
    JPanel buttonBar; 

    public ClearLayeredPanelsGUI() { 
     initPanels(); 
    } 

    private void initPanels() { 
     initStackPanel(); 
     initButtonPanel(); 

     this.getContentPane().setLayout(new BorderLayout()); 
     this.getContentPane().add(stack, BorderLayout.CENTER); 
     this.getContentPane().add(buttonBar, BorderLayout.NORTH); 
    } 

    private void initStackPanel() { 
     stack = new JPanel(new StackLayout()); 
     stack.setPreferredSize(new Dimension(300, 300)); 

     whiteBackground = new JPanel(); 
     whiteBackground.setOpaque(true); 
     whiteBackground.setBackground(Color.WHITE); 

     rectBoard = new TransparentBoard(Color.BLUE, new Rectangle(0, 0, 100, 200)); 
     roundBoard = new TransparentBoard(Color.RED, new Ellipse2D.Float(0.0f, 0.0f, 150.0f, 100.0f)); 


     stack.add(whiteBackground, StackLayout.TOP); 
     stack.add(roundBoard, StackLayout.TOP); 
     stack.add(rectBoard, StackLayout.TOP); 

     stack.setOpaque(false); 
    } 

    private void initButtonPanel() { 
     buttonBar = new JPanel(new FlowLayout()); 

     buttonBar.add(new JButton(new ClearAction("Clear Rect", rectBoard))); 
     buttonBar.add(new JButton(new DrawAction("Draw Rect", rectBoard))); 
     buttonBar.add(new JButton(new ClearAction("Clear Ellipse", roundBoard))); 
     buttonBar.add(new JButton(new DrawAction("Draw Ellipse", roundBoard))); 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     ClearLayeredPanelsGUI f = new ClearLayeredPanelsGUI(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setTitle("Stack of transparent panes with Clear"); 
//  f.pack(); 
     f.setSize(new Dimension(500,350)); 
     f.setVisible(true); 
    } 

    private class DrawAction extends AbstractAction { 
     private TransparentBoard board; 
     public DrawAction(String txt, TransparentBoard b) { 
      super(txt); 
      this.board = b; 
     } 
     public void actionPerformed(ActionEvent e) { 
      board.drawShape(); 
      stack.repaint(); 
     } 
    } 

    private class ClearAction extends AbstractAction { 
     private TransparentBoard board; 
     public ClearAction(String txt, TransparentBoard b) { 
      super(txt); 
      this.board = b; 
     } 
     public void actionPerformed(ActionEvent e) { 
      board.clearShape(); 
      stack.repaint(); 
     } 
    } 

    private class TransparentBoard extends JComponent { 
     BufferedImage board = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB); 
     Dimension d = new Dimension(300,300); 

     private Color paintColor; 
     private Shape shape; 

     public TransparentBoard(Color color, Shape s) { 
      setOpaque(false); 
      Graphics2D g2d = (Graphics2D) board.getGraphics(); 
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); 
      g2d.fillRect(0,0,board.getWidth(),board.getHeight()); 

      this.paintColor = color; 
      this.shape = s; 

      clearShape(); 
      drawShape(); 
     } 

     public void drawShape() { 
      Graphics2D g2d = (Graphics2D) board.getGraphics(); 
      g2d.translate(50, 50); 
      g2d.setColor(Color.BLACK); 
      g2d.setStroke(new BasicStroke(2.0f)); 
      g2d.draw(shape); 
      g2d.setColor(paintColor); 
      g2d.fill(shape); 

      repaint(); 
     } 

     public void clearShape() { 
      Graphics2D g2d = (Graphics2D) board.getGraphics(); 
      Composite oldComposite = g2d.getComposite(); 
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); 
      g2d.fillRect(0,0,board.getWidth(),board.getHeight()); 
      //reset alpha composite 
      g2d.setComposite(oldComposite); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      if (board != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 

       try { 
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
        g2d.drawImage(board, 0, 0, board.getWidth(), board.getHeight(), null); 
       } finally { 
        g2d.dispose(); 
       } 
      } 
     } 
    } 
} 
+0

只有在用戶完成繪圖後才能清除完整的屏幕,因此已經有圖像被繪製。 如果這不是您指定的方法,那麼您可以發佈任何代碼片段嗎? – swift 2010-04-26 11:42:51

+0

我希望這樣做,但我無法使用JLayeredPane繪製透明組件的任何工作示例。我可以在一個單獨的窗格中進行此操作(所有圖層都具有透明度),但是您是如何使用JLayeredPane執行此操作的?你可以創建一個小的運行示例嗎? – 2010-04-26 18:09:22

+0

Thanx Matthieu,Clearshape()方法可以正常工作,但即使在圖像上繪製了一個fillrect後,也可以從底層面板看到圖形,這是如何實現的? 不會填充完整的圖像,使面板不透明? PLZ你可以簡單介紹一下嗎? plz – swift 2010-04-27 04:46:58

0

刪除整個白板組件並再次添加它。 或嘗試向我們展示更多代碼,白板可能是您自定義類無法找到的。

相關問題