Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Polygon Figure with gradient pattern ,does not paint properly at different zooms.
Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #634540] Fri, 22 October 2010 07:23 Go to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
I have created a snippet to recreate my problem
package test;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.PolygonShape;
import org.eclipse.draw2d.ScalableFreeformLayeredPane;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * <dl>
 * <dd> <code>Draw2dPatternTest.java</code> TODO: Class Description goes here.</dd>
 * </dl>
 * 
 * @author vijay
 * @version 1.0.0
 */

public class Draw2dPatternTest
{
    public static class CustomPolygonShape extends PolygonShape
    {
        /**
         * @see org.eclipse.draw2d.PolygonShape#shapeContainsPoint(int, int)
         */
        @Override
        protected boolean shapeContainsPoint(int x, int y)
        {
            return getPoints().polygonContainsPoint(x, y);
        }

        /**
         * @see org.eclipse.draw2d.PolygonShape#fillShape(org.eclipse.draw2d.Graphics)
         */
        @Override
        protected void fillShape(Graphics graphics)
        {
            Pattern pattern = new Pattern(Display.getCurrent(), getBounds().getTopLeft().x,
                    getBounds().getTopLeft().y, getBounds().getBottomRight().x, getBounds()
                            .getBottomRight().y, ColorConstants.white, getBackgroundColor());
            graphics.setBackgroundPattern(pattern);
            graphics.fillPolygon(getPoints());
            pattern.dispose();
        }

        /**
         * @see org.eclipse.draw2d.PolygonShape#outlineShape(org.eclipse.draw2d.Graphics)
         */
        @Override
        protected void outlineShape(Graphics graphics)
        {
            graphics.drawPolygon(getPoints());
        }
    }

    public static class DiamondFigure extends CustomPolygonShape
    {
        /** points list for diamond figure. */
        private PointList pl;

        /**
         * @see org.eclipse.draw2d.Figure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
         */
        @Override
        public void setBounds(Rectangle rect)
        {
            super.setBounds(rect);
            pl = new PointList();
            pl.addPoint(bounds.x + bounds.width / 2, bounds.y);
            pl.addPoint(bounds.x, bounds.y + bounds.height / 2);
            pl.addPoint(bounds.x + bounds.width / 2, bounds.y + bounds.height);
            pl.addPoint(bounds.x + bounds.width, bounds.y + bounds.height / 2);
            setPoints(pl);
        }

    }

    private static String string;

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3, true));
        final Button zoomOut = new Button(shell, SWT.PUSH);
        zoomOut.setText("Zoom Out");
        final Label l = new Label(shell, SWT.None);
        Button zoomIn = new Button(shell, SWT.PUSH);
        zoomIn.setText("Zoom In");

        string = "Zoom Level=";
        l.setText(string + "1");
        FigureCanvas c = new FigureCanvas(shell);
        GridData layoutData = new GridData(GridData.FILL_BOTH);
        layoutData.horizontalSpan = 3;
        c.setLayoutData(layoutData);
        c.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        final ScalableFreeformLayeredPane figure = new ScalableFreeformLayeredPane();
        figure.setBackgroundColor(ColorConstants.white);
        figure.setForegroundColor(ColorConstants.white);
        CustomPolygonShape s = new DiamondFigure();
        s.setBounds(new Rectangle(0, 0, 100, 100));
        s.setBackgroundColor(ColorConstants.green);
        figure.add(s);
        c.setContents(figure);
        SelectionListener listener = new SelectionListener()
        {
            double i = 1;

            @Override
            public void widgetSelected(SelectionEvent e)
            {
                if (e.getSource() == zoomOut)
                {
                    i += .20d;
                    figure.setScale(i);
                }
                else
                {
                    i -= .20d;
                    figure.setScale(i);
                }
                l.setText(string + i);
                shell.layout();
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e)
            {
                // TODO Auto-generated method stub

            }
        };
        zoomOut.addSelectionListener(listener);
        zoomIn.addSelectionListener(listener);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch()) display.sleep();
        }

        display.dispose();
    }
}


http://lh3.ggpht.com/_3SpBG8kdCvs/TME6E4BWisI/AAAAAAAADc8/IIS08s1RsNE/s800/pattern.JPG

as you can see from the snippet application at higher zoom levels the diamont shape patternt painting gets distorted.

My questions are
1.Is the problem OS level.
2.Is it a bug in draw2d.
3.is there any workaround


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #634984 is a reply to message #634540] Mon, 25 October 2010 10:39 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Ok i found the issue...

The issue is in setting the pattern,it should also consider the zoom factor if Graphics is an instance of ScaledGraphic

or

use setBackgroundPattern(Device device, int x1, int y1, int x2, int y2, Color color1, Color color2) instead of setBackgroundPattern(Pattern pattern)...

Now if i use setBackgroundPattern(Device device, int x1, int y1, int x2, int y2, Color color1, Color color2) it accordingly creates the Pattern instance but i did not find any code to dispose the same...
Hence using swt sleak i found that the System resource Pattern is being created but not disposed....

I think this is a bug(or is it)???


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #634992 is a reply to message #634540] Mon, 25 October 2010 11:01 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
No Message Body

---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay

[Updated on: Wed, 27 October 2010 08:10]

Report message to a moderator

Re: Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #800921 is a reply to message #634540] Fri, 17 February 2012 17:42 Go to previous messageGo to next message
Xihui Chen is currently offline Xihui ChenFriend
Messages: 35
Registered: June 2011
Member

I have the same problem. Is there any conclusion for this problem?
It seems there is a bug report for this problem, but has no reply.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=241100
Re: Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #894610 is a reply to message #634540] Mon, 09 July 2012 19:51 Go to previous messageGo to next message
Xihui Chen is currently offline Xihui ChenFriend
Messages: 35
Registered: June 2011
Member
OK, I have a workaround. You can use the below method to create your pattern:

public static Pattern createScaledPattern(Graphics graphics, Device device,
float x1, float y1, float x2, float y2, Color color1, int alpha1,
Color color2, int alpha2) {
double scale = graphics.getAbsoluteScale();
return new Pattern(device, (int) (x1 * scale), (int) (y1 * scale),
(int) (x2 * scale), (int) (y2 * scale), color1, alpha1, color2,
alpha2);
}

public static Pattern createScaledPattern(Graphics graphics, Device device,
float x1, float y1, float x2, float y2, Color color1, Color color2)
{
double scale = graphics.getAbsoluteScale();
return new Pattern(device, (int) (x1 * scale), (int) (y1 * scale),
(int) (x2 * scale), (int) (y2 * scale), color1, color2);
}
Re: Polygon Figure with gradient pattern ,does not paint properly at different zooms. [message #952731 is a reply to message #894610] Sun, 21 October 2012 18:40 Go to previous message
Jörg Fiedler is currently offline Jörg FiedlerFriend
Messages: 1
Registered: October 2012
Junior Member
Very nice. Thanks a lot. Works for me.
Previous Topic:GEF with Eclipse RCP 4x
Next Topic:Canvas Context menu during mouse Hover
Goto Forum:
  


Current Time: Fri Apr 26 03:47:34 GMT 2024

Powered by FUDForum. Page generated in 0.03563 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top