如何为此白板添加清除选项?

时间:2010-04-26 10:37:21

标签: java swing

我必须在我的白板应用程序中添加清除屏幕选项,通常的步骤是将填充矩形绘制到图像的大小。但是在我的应用程序中,我将透明面板添加到另一个上面,即作为图层,如果我按照通常的程序,从底层面板的绘图将不可见。请告诉我这样做的任何逻辑。

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));
}

2 个答案:

答案 0 :(得分:1)

你为什么不“清楚”(我的意思是不要在你的电路板上画任何东西)并在JLayeredPane上重新粉刷。这将强制重绘整个面板堆栈,没有?

编辑:使用来自Romain GUY的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();
                }
            }
        }
    }
}

答案 1 :(得分:0)

删除整个白板组件并重新添加。 或者尝试向我们展示更多代码,Whiteboard可能是你自定义类无法找到的。