Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem using fillRoundRectangle and setClipping
Problem using fillRoundRectangle and setClipping [message #455562] Tue, 17 May 2005 11:20 Go to next message
Eclipse UserFriend
Hi,

I use fillRoundRectangle and setClipping methods to draw a specific
"widget" with rounded corners and a colored top part header. (see code
example).

I've tried many times to change the parameters (width, height, arc...) but
I do not manage to fill correctly the top header area with the blue color
: near the rectangle corners, the filling is not well done.

Moreover, I'm asked to add a shadow around the rounded rectangle, how can
I do ?

Thanks for your help,
Helene

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MainRounded {

private static Display display;

public static void main(String[] args) {

display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout());

Composite mainComposite = new Composite(shell, SWT.NONE);
GridData gridData = new GridData();
gridData.widthHint = 600;
gridData.heightHint = 70;
mainComposite.setLayoutData(gridData);
mainComposite.setLayout(new GridLayout());

Composite roundedComposite = new CommandRoundedComposite(mainComposite);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.verticalAlignment = SWT.FILL;
roundedComposite.setLayoutData(gridData);

shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

public final class CommandRoundedComposite extends Composite {

public CommandRoundedComposite(Composite composite) {
super(composite, SWT.NO_REDRAW_RESIZE);
drawRoundedComposite();
}

public void drawRoundedComposite() {

this.setLayout(getNoMarginsGridLayout());

this.addPaintListener(new PaintListener() {

public void paintControl(PaintEvent e) {
GC gc = e.gc;
int arcWidth = 35;
int arcHeight = 35;
final Rectangle rect = getClientArea();

gc.setLineStyle(SWT.LINE_SOLID);
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK ));
gc.drawRoundRectangle(rect.x+2, rect.y+2, rect.width-5, rect.height-4,
arcWidth, arcHeight);
gc.setClipping(rect.x+3, rect.y+3, rect.width-5, arcHeight-7);
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE) );
gc.fillRoundRectangle(rect.x+3, rect.y+3, rect.width-6, rect.height-5,
arcWidth+5, arcHeight+5);
}
});
}

public GridLayout getNoMarginsGridLayout() {
GridLayout gridLayout = new GridLayout();
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
return gridLayout;
}
}
Re: Problem using fillRoundRectangle and setClipping [message #455563 is a reply to message #455562] Tue, 17 May 2005 13:23 Go to previous messageGo to next message
Eclipse UserFriend
Playing with some values using your own code, this worked fine for me;

gc.setClipping(rect.x + 2, rect.y + 2, rect.width - 5,
arcHeight - 4);
gc.setLineStyle(SWT.LINE_SOLID);

gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE) );
gc.fillRoundRectangle(rect.x + 2, rect.y + 2, rect.width -
4, rect.height - 3, arcWidth, arcHeight);

gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK ));
gc.drawRoundRectangle(rect.x + 2, rect.y + 2, rect.width -
5, rect.height - 4, arcWidth, arcHeight);

As for doing dropshadows, the easiest way I've found is to just draw your
shadow first, by drawing filled rectangles in various gray colors that live
slightly outside the normal area, like down and to the right. Then do
another filled rectangle in a different color that is 1 pixel left and 1
pixel north of the previous one, and so on. That way you get a gradient
effect. Then in the end just draw your actual widget on top of it all, and
the dropshadow will be the only visible thing sticking out.

Hope that made sense.

Emil

"hortiz" <hortiz@xxxxx.com> wrote in message
news:c0a3408a94a1abaa49d13c093f44b16c$1@www.eclipse.org...
> Hi,
>
> I use fillRoundRectangle and setClipping methods to draw a specific
> "widget" with rounded corners and a colored top part header. (see code
> example).
>
> I've tried many times to change the parameters (width, height, arc...) but
> I do not manage to fill correctly the top header area with the blue color
> : near the rectangle corners, the filling is not well done.
>
> Moreover, I'm asked to add a shadow around the rounded rectangle, how can
> I do ?
>
> Thanks for your help,
> Helene
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class MainRounded {
>
> private static Display display;
> public static void main(String[] args) {
>
> display = new Display ();
> Shell shell = new Shell (display);
> shell.setLayout(new GridLayout());
>
> Composite mainComposite = new Composite(shell, SWT.NONE);
> GridData gridData = new GridData();
> gridData.widthHint = 600;
> gridData.heightHint = 70;
> mainComposite.setLayoutData(gridData);
> mainComposite.setLayout(new GridLayout());
>
> Composite roundedComposite = new CommandRoundedComposite(mainComposite);
> gridData = new GridData(GridData.FILL_HORIZONTAL);
> gridData.verticalAlignment = SWT.FILL;
> roundedComposite.setLayoutData(gridData);
>
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
> }
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.events.PaintListener;
> import org.eclipse.swt.graphics.GC;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
>
> public final class CommandRoundedComposite extends Composite {
>
> public CommandRoundedComposite(Composite composite) {
> super(composite, SWT.NO_REDRAW_RESIZE);
> drawRoundedComposite();
> }
> public void drawRoundedComposite() {
> this.setLayout(getNoMarginsGridLayout());
>
> this.addPaintListener(new PaintListener() {
>
> public void paintControl(PaintEvent e) {
> GC gc = e.gc;
> int arcWidth = 35;
> int arcHeight = 35;
> final Rectangle rect = getClientArea();
>
> gc.setLineStyle(SWT.LINE_SOLID);
> gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK ));
> gc.drawRoundRectangle(rect.x+2, rect.y+2, rect.width-5, rect.height-4,
> arcWidth, arcHeight);
> gc.setClipping(rect.x+3, rect.y+3, rect.width-5, arcHeight-7);
> gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE) );
> gc.fillRoundRectangle(rect.x+3, rect.y+3, rect.width-6, rect.height-5,
> arcWidth+5, arcHeight+5);
> }
> });
> }
>
> public GridLayout getNoMarginsGridLayout() {
> GridLayout gridLayout = new GridLayout();
> gridLayout.horizontalSpacing = 0;
> gridLayout.verticalSpacing = 0;
> gridLayout.marginHeight = 0;
> gridLayout.marginWidth = 0;
> return gridLayout;
> }
> }
>
>
>
>
>
Re: Problem using fillRoundRectangle and setClipping [message #455599 is a reply to message #455563] Wed, 18 May 2005 03:31 Go to previous message
Eclipse UserFriend
Thanks for your answer, it works also for me.
Previous Topic:Smoother Animation
Next Topic:NewWindow2:how retrive sFeatures from new window opened
Goto Forum:
  


Current Time: Fri Jul 04 20:15:24 EDT 2025

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

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

Back to the top