My try as follows,which doesn't come up with anything:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display,
"D:/topic.png");
GC gc = new GC(image);
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
gc.drawText("I've been drawn on",0,0,true);
gc.dispose();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
// TODO Auto-generated method stub
}
See the SWT-Snippets for examples. This one uses an image label
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.BORDER);
label.setImage (image);
You are missing one thing in your code. Event Handler for paint. Normally when you create a component it generates a paint event. All the drawing related stuff should go in it.
Also you need not to create the GC explicitly.. It comes with the event object :)
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ImageX
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
shell.setLayout(new FillLayout ());
final Image image = new Image(display, "C:\\temp\\flyimage1.png");
shell.addListener (SWT.Paint, new Listener ()
{
public void handleEvent (Event e) {
GC gc = e.gc;
int x = 10, y = 10;
gc.drawImage (image, x, y);
gc.dispose();
}
});
shell.setSize (600, 400);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
if(image != null && !image.isDisposed())
image.dispose();
display.dispose ();
}
}
Related
I try to make scroll working and I also want to auto resize window and content that is in the window. My interface is going to have couple of composite blocks that are going to parse some information, and fields inside block are static and they going to be always same fields in same block `
public void open() {
Display display = Display.getDefault();
createContents();
shell.addListener (SWT.Resize, new Listener () {
public void handleEvent (Event e) {
Rectangle rect = shell.getClientArea ();
System.out.println(rect);
}
});
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell(SWT.SHELL_TRIM |SWT.V_SCROLL | SWT.H_SCROLL);
shell.addMouseWheelListener(new MouseWheelListener() {
public void mouseScrolled(MouseEvent e) {
}
});
shell.setSize(1546, 878);
shell.setBackground(SWTResourceManager.getColor(255, 255, 255));
shell.setMaximized(true);
shell.setMinimumSize(1500, 600);
shell.setText("Test App for nothing");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
I am trying to write a simple app that create a transparent overlay over the full screen and draw a line at my current mouse position. This line should then follow my mouse movements until mouse press when the app is to exit.
I currently have an issue with redraw in the app that will not work and I guess that I have missplaced and/or missunderstood how the redraw should be used.
How to redraw an application like this correctly and resource efficient?
Sample code:
public class TAGuideLine {
static Display display = new Display();
static Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
public static void main(String[] args) {
shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
shell.setMaximized(true);
shell.setFullScreen(true);
shell.setLayoutData(0);
shell.layout(true, true);
shell.setAlpha(50);
shell.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
System.exit(0);
}
});
shell.addListener(SWT.MouseMove, new Listener() {
public void handleEvent(Event e) {
drawMyLine(MouseInfo.getPointerInfo().getLocation().x,
MouseInfo.getPointerInfo().getLocation().y);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
shell.redraw();
shell.layout();
display.dispose();
}
public static void drawMyLine(int x, int y) {
final GC gc = new GC(shell);
gc.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
gc.setLineWidth(8);
gc.drawLine(x - 250, y - 250, x + 250, y + 250);
gc.dispose();
shell.open();
}
}
To draw on a Shell you usually add a paint listener that does the actual drawing. The mouse event listener would just store the coordinates to draw and then trigger a redraw on the shell.
Below is a sketch of what the code could look like:
List<Point> points = new ArrayList<>();
shell.addListener( SWT.Paint, new Listener() {
public void handleEvent( Event event ) {
event.gc.setForeground( display.getSystemColor( SWT.COLOR_GREEN ) );
event.gc.setLineWidth( 8 );
for( Point point : points ) {
event.gc.drawLine( point.x - 250, point.y - 250, point.x + 250, point.y + 250 );
}
} );
shell.addListener( SWT.MouseMove, new Listener() {
public void handleEvent( Event event ) {
points.add( MouseInfo.getPointerInfo().getLocation() );
shell.redraw();
}
} );
Class GC also has a drawPolyLine() method that might be more suitable for this use case.
Unrelated to your question but still: a more graceful way to exit the application is to dispose of the Shell with shell.dispose()instead of calling System.exit().
I'm trying to hide a SWT shell when the Display is minimized. I'm missing something and would be most thankful for any help.
Additional Info: This shell is actually a popup that gets drawn when the user clicks on a composite. In the end, my goal is to hide this popup-shell when the composite is not visible (user minimized the window or switched between windows, say with Alt+Tab for example).
Here's my code:
static Shell middleClickNodeInfoShell ;
static Label nodeIdLabel ;
void init(){
...
/** Focused node on middle click*/
middleClickNodeInfoShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.MODELESS);
middleClickNodeInfoShell.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
middleClickNodeInfoShell.setLayout(createNoMarginLayout(1, false));
nodeIdLabel = new Label(middleClickNodeInfoShell, SWT.NONE);
Display.getDefault().addListener(SWT.Iconify,new Listener() {
#Override
public void handleEvent(Event arg0) {
// TODO Auto-generated method stub
middleClickNodeInfoShell.setVisible(false);
}
});
}
#Override
public boolean onMouseClicked(Button button, ScreenPosition screenPos,
final GeoPosition arg2) {
...
nodeIdLabel.setText("Node Id: "+node.getId());
middleClickNodeInfoShell.setLocation(pos.getX()+displayX,pos.getY()+displayY+30);
middleClickNodeInfoShell.setVisible(true);
middleClickNodeInfoShell.pack();
}
Here is sample code that will help you do figure out what you are looking for
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setText("Shell Example");
shell.setLayout(new RowLayout());
final Button button = new Button(shell, SWT.PUSH);
button.setText("Click Me");
final Shell tip = new Shell(shell,SWT.MODELESS);
tip.setLayout(new FillLayout());
Label lbl = new Label(tip, SWT.NONE);
lbl.setText("***tooltip***");
tip.pack();
shell.addControlListener(new ControlListener() {
#Override
public void controlResized(ControlEvent e) {
changeTipLocation(display, button, tip);
}
#Override
public void controlMoved(ControlEvent e) {
changeTipLocation(display, button, tip);
}
});
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
changeTipLocation(display, button, tip);
tip.open();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void changeTipLocation(final Display display, final Button button, final Shell tip) {
Rectangle bounds = button.getBounds();
Point loc = button.getLocation();
tip.setLocation(display.map(button, null, new Point(loc.x+bounds.width, loc.y+bounds.height)));
}
I am working with SWT and I would like to be able to resize a composite by dragging the corner of it, the same way that you can resize a shell. I'm sure someone out there has implemented a good solution. Thanks.
I think what you are looking for can be implemented with org.eclipse.swt.widgets.Tracker
here is sample working code:
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.open();
final Composite b = new Composite(shell, SWT.NONE);
b.setBounds(20, 20, 80, 80);
b.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
b.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
tracker.setStippled(true);
Rectangle rect = b.getBounds();
tracker.setRectangles(new Rectangle[] { rect });
if (tracker.open()) {
Rectangle after = tracker.getRectangles()[0];
b.setBounds(after);
}
tracker.dispose();
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
I am trying to find out how to show text outline by using of swt graphics.
More precisely I need to write code which shows text in following way:
http://java.sun.com/developer/onlineTraining/Media/2DText/Art/StarryShape.gif
I found following code and I'd like to translate it from awt to swt.
FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Times",Font.BOLD,w/10);
String s = new String("The Starry Night");
TextLayout tl = new TextLayout(s, f, frc);
float sw = (float) tl.getBounds().getWidth();
AffineTransform transform = new AffineTransform();
transform.setToTranslation(w/2-sw/2, h/4);
Shape shape = tl.getOutline(transform);
Rectangle r = shape.getBounds();
g2.setColor(Color.blue);
g2.draw(shape);
(code from java.sun.com/developer/onlineTraining/Media/2DText/style.html )
But I can't figure out how to get Outline of the TextLayout in swt.
Is there such possibility?
Well there is a possibility of doing this using Path class in SWT. For example:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class ShapeText
{
public static void main(String[] args)
{
final Display display = new Display();
Font font = new Font(display, "Times", 50, SWT.BOLD);
final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
final Path path;
try {
path = new Path(display);
path.addString("The Starry Night", 0, 0, font);
} catch (SWTException e) {
System.out.println(e.getMessage());
display.dispose();
return;
}
Shell shell = new Shell(display);
shell.addListener(SWT.Paint, new Listener()
{
public void handleEvent(Event e)
{
GC gc = e.gc;
//Transform a = new Transform(display);
//a.shear(0.7f, 0f);
//gc.setTransform(a);
gc.setForeground(blue);
gc.fillPath(path);
gc.drawPath(path);
}
});
shell.setSize(530,120);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
path.dispose();
font.dispose();
display.dispose();
}
}
The above code is not an exact translation of the Swing snippet that you have posted but the intent is same.
Also check this link : http://www.eclipse.org/swt/snippets/
Specially the Path and Pattern section.
Hope this will help.