简体   繁体   中英

How to make one of a rectangle's sides marked using GUI in Java (SWT only)?

Given the following code :

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class BasicShapes {

    private Shell shell;

    public BasicShapes(Display display) {    
        shell = new Shell(display);
        shell.addPaintListener(new ExmaplePaintListener());
        shell.setText("Basic shapes");
        shell.setSize(430, 300);
        shell.setLocation(300, 300);
        shell.open();

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

    private class ExmaplePaintListener implements PaintListener {

        public void paintControl(PaintEvent e) {    
            drawRectangles(e);
            e.gc.dispose();
        }
    }

    private void drawRectangles(PaintEvent e) {    
        e.gc.setAntialias(SWT.ON);
        e.gc.setBackground(new Color(e.display, 150, 150, 150));    
        e.gc.fillRectangle(20, 20, 120, 80);
        e.gc.fillRectangle(180, 20, 80, 80);
        e.gc.fillRectangle(280,20,100,79);
    }    
}

In the above code I create 3 rectangles , and I want to make one of the rectangle's sides to be marked / color it with a different color , or in other words , to have the ability to pick each one of a rectangle's sides , and mark it . Is it possible ?

Regards,Ron

After fill ing the rectangle, just draw a line on one of its sides. For example:

e.gc.fillRectangle(280, 20, 100, 79);
e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE)); //Set the edge color
e.gc.drawLine(280,20, 280+100,20); //Modify this to print
                                   //another side of the rectangle.

Note: if you want to draw all of the sides, it's obviously better to just:

e.gc.fillRectangle(280, 20, 100, 79);
e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE)); //Set the edge color
e.gc.drawRectangle(280, 20, 100, 79);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM