/**

  This small app illustrates a problem I'm having with painting tranparent
  child components.

  What I want to do is to have:
  1. a parent panel that displays a buffered image.
  2. some text drawn in the upper right-hand corner of the parent panel.
  3. transparent child panels positioned within the parent panel that have
     their own paint routines so that the buffered image can be seen
     under the child panels.

  Here is my problem. When the application comes up initially, everything is
  fine. However, when I trigger the repainting of a child panel separately,
  (by passing the mouse over it) the parent's paintComponent() gets called
  to paint the background for the child. Here the WRONG PART OF THE PARENT
  PANEL IS PAINTED UNDER THE CHILD.

  To see this
  1. Run WidgetTest.java, notice that the child panels are outlined in red
     and are empty.
  2. Move the mouse over one of the child panels. Note that the text from
     the parent's upper right hand corner now appears as the child's
background.
  3. Minimize the window and bring it back up, notice that now the
children's
     backgrounds are again clear.
  4. Grab the lower righ-hand corner of the window to resize (make it
smaller)
     such that the child panels appear INSIDE THE IMAGE. Again all should
look
     fine at this point.
  5. Again, trigger a child repaint by mousing into one of the children and
     notice the change in background.

 My question is: how do I get the CORRECT PORTION OF THE PARENT PANEL TO
 REPAINT UNDER EACH CHILD?

     Source code follows

 */
package widgettest;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Canvas;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;

public class WidgetTest extends JPanel
{

    private BufferedImage bufferedImage = null;
    final private int     imageWidth    = 256;
    final private int     imageHeight   = 256;

    // initial distance between edges of image an outer edges of panel
    final private int     imageMargin   = 24;

    final private int     panelWidth  = imageWidth  + (imageMargin * 2);
    final private int     panelHeight = imageHeight + (imageMargin * 2);


    public WidgetTest( )
    {
        super( );

        // set panel to be a little larger than the image
        setPreferredSize(new Dimension(panelWidth, panelHeight));

        bufferedImage = makeBufferedImage( );

        addWidgets();
    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        System.out.println("WidgetTest.paintComponent");

        Graphics2D g2 = (Graphics2D) g;

        AffineTransform oldAtx = g2.getTransform();

        int imageWidth  = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();
        int panelWidth  = getWidth();
        int panelHeight = getHeight();

        // set transform to center image
        g2.setTransform(AffineTransform.getTranslateInstance((panelWidth -
imageWidth) / 2,












                                                 (panelHeight -imageHeight)/
2));

        g2.drawImage(bufferedImage, 0, 0, this);

        // set transform for text
        g2.setTransform(new AffineTransform());
        g2.drawString("Test", 2, 12);

        // put back old transform
        g2.setTransform(oldAtx);
    }

    /**
     *  Put a transparent child panel in each of the four corners of the
     *  parent panel.
     */
    private void addWidgets()
    {
        setLayout(new GridBagLayout());

        GridBagConstraints gbConst = new GridBagConstraints();

        // dummy component in upper left hand corner
        gbConst.gridx = 0;      // col
        gbConst.gridy = 0;      // row
        gbConst.weightx = 0.0;  // col
        gbConst.weighty = 0.0;  // row
        gbConst.anchor = GridBagConstraints.NORTHWEST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        gbConst.insets = new Insets(0, 0, 0, 0);
        gbConst.fill = GridBagConstraints.NONE;
        add(new Canvas() , gbConst);

        gbConst.gridx = 2;      // col
        gbConst.gridy = 0;      // row
        gbConst.weightx = 0.0;  // col
        gbConst.weighty = 0.0;  // row
        gbConst.anchor = GridBagConstraints.NORTHEAST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        gbConst.insets = new Insets(0, 0, 0, 0);  // cf Manning Swing book
        gbConst.fill = GridBagConstraints.NONE;
        add(new ChildPanel() , gbConst);

        // dummy component to expand middle row
        gbConst.gridx = 0;      // col
        gbConst.gridy = 1;      // row
        gbConst.weightx = 0.0;  // col
        gbConst.weighty = 1.0;  // row; middle row is expandable
        gbConst.anchor = GridBagConstraints.WEST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        add(new Canvas(), gbConst);

        // dummy component to expand middle col
        gbConst.gridx = 1;      // col
        gbConst.gridy = 1;      // row
        gbConst.weightx = 1.0;  // col; middle col is expandable
        gbConst.weighty = 0.0;  // row
        gbConst.anchor = GridBagConstraints.WEST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        add(new Canvas(), gbConst);


        gbConst.gridx = 0;      // col
        gbConst.gridy = 2;      // row
        gbConst.weightx = 0.0;  // col
        gbConst.weighty = 0.0;  // row
        gbConst.anchor = GridBagConstraints.SOUTHWEST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        gbConst.insets = new Insets(0, 0, 0, 0);
        gbConst.fill = GridBagConstraints.NONE;
        add(new ChildPanel() , gbConst);

        gbConst.gridx = 2;      // col
        gbConst.gridy = 2;      // row
        gbConst.weightx = 0.0;  // col
        gbConst.weighty = 0.0;  // row
        gbConst.anchor = GridBagConstraints.SOUTHEAST;
        gbConst.gridwidth  = 1;
        gbConst.gridheight = 1;
        gbConst.insets = new Insets(0, 0, 0, 0);  // cf Manning Swing book
        gbConst.fill = GridBagConstraints.NONE;
        add(new ChildPanel() , gbConst);
    }

    private BufferedImage makeBufferedImage( )
    {
        BufferedImage bi = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_ARGB);
        Graphics2D    g2 = bi.createGraphics( );

        g2.setColor(Color.black);
        g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));

        // draw a big rect with a big X in it
        g2.drawRect(2, 2,               imageWidth - 4, imageHeight - 4);
        g2.drawLine(2, 2,               imageWidth - 4, imageHeight - 4);
        g2.drawLine(2, imageHeight - 4, imageWidth - 4, 2);

        // draw four smaller + marks, each in a different color
        Point2D topCenterPt    = new Point2D.Double(imageWidth * 0.5,
imageHeight * 0.25);
        Point2D leftCenterPt   = new Point2D.Double(imageWidth * 0.25,
imageHeight * 0.5);
        Point2D rightCenterPt  = new Point2D.Double(imageWidth * 0.75,
imageHeight * 0.5);
        Point2D bottomCenterPt = new Point2D.Double(imageWidth * 0.5,
imageHeight * 0.75);

        drawPlusSign(g2, topCenterPt,    Color.red);
        drawPlusSign(g2, leftCenterPt,   Color.green.brighter());
        drawPlusSign(g2, rightCenterPt,  Color.blue.brighter());
        drawPlusSign(g2, bottomCenterPt, Color.orange.brighter());

        return bi;
    }

    /**
     * Draw a plus sign centered on pt in the specified color
     */
    private void drawPlusSign(Graphics2D g2, Point2D pt, Color color)
    {
        double delta = (imageWidth / 4) - 4;

        double x = pt.getX();
        double y = pt.getY();

        g2.setColor(color);
        g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));

        g2.drawLine((int)(x - delta), (int) y , (int)(x + delta), (int) y);
        g2.drawLine((int)x, (int)(y - delta) , (int)x, (int)(y + delta));
    }

    //////////////////      INNER CLASSES

    class ChildPanel extends JPanel
    {
        public ChildPanel()
        {
            Dimension dim = new Dimension(20,20);

            setPreferredSize(dim);
            setMinimumSize(dim);

            setOpaque(false);

            setBorder(BorderFactory.createLineBorder(Color.red));
            addMouseListener(new ChildMouseListener(this));
        }

        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            System.out.println("ChildPanel.paintComponent");
        }
    }

    class ChildMouseListener extends MouseAdapter
    {
        private JPanel panel;
        public ChildMouseListener(JPanel p)
        {
            panel = p;
        }

        /**
         * Invoked when the mouse enters a component.
         */
        public void mouseEntered(MouseEvent e)
        {
            super.mouseEntered(e);
            panel.repaint();
        }

        /**
         * Invoked when the mouse exits a component.
         */
        public void mouseExited(MouseEvent e)
        {
            super.mouseExited(e);
            panel.repaint();
        }
    }


    public static void main(String[ ] args)
    {
        JFrame frame = new JFrame("My Frame Title");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane( ).add(new WidgetTest( ), BorderLayout.CENTER);
        frame.setSize(400, 200);
        frame.pack( );
        frame.setVisible(true);
    }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to