tags:

views:

63

answers:

2

i have to add clear screen option to my whiteboard application, usual procedure is to draw a fill rect to the sizeof the image. But in my app i have transparent panels added one above the other i.e as layers, if i follow the usual procedure the drawing from the underlying panel wont be visible. please tell me any logic to do this.

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

Remove whole Whiteboard component and add it again. Or try showing us more code , Whiteboard is probably you custom class can't find it.

Xorty
+1  A: 

Why don't you "clear" (I mean do not draw anything on your boards) and call a repaint on the JLayeredPane. This will force to redraw the full stack of Panels, no?

EDIT: add a code sample with StackLayout from Romain GUY, see the ClearShape & paintComponent methods

EDIT2: calling the repaint can also be done using the good Clipping region to render faster, but I let you modify the code for this...

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();
                }
            }
        }
    }
}
Matthieu BROUILLARD
The complete screen should be cleared only after users finish drawing, so there will be images drawn to it already. if this is not the method you specified, then can you post any code snippet?
swift
I would be please to do so, but I could not have any working example with JLayeredPane drawing transparent components. I can do this in a single Pane (painting with transparency all layers) but how do the hell did you do this with your JLayeredPane? Can you create a small running example?
Matthieu BROUILLARD
Thanx Matthieu, Clearshape() method works fine but even after drawing a fillrect over the image it is possible to see the drawings from the underlying panels, how is this possible?Wont the fillrect fills the complete image which makes the panel opaque?plz can u brief it? plz
swift
Please provide some code example as a SSSCE (http://sscce.org/), it is difficult for us to know what your real problem is. An sometimes by creating a simple example that demonstrates the bad behaviour you can find a solution by yourself. During my tests with JLayeredPane I didn't succeed in having transparentlayers, so please give some code example to get more help.
Matthieu BROUILLARD