如何制作一个矩形';使用Java中的GUI标记的s面(仅限SWT)?
给定以下代码:如何制作一个矩形';使用Java中的GUI标记的s面(仅限SWT)?,java,user-interface,geometry,swt,Java,User Interface,Geometry,Swt,给定以下代码: 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 cl
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);
}
}
在上面的代码中,我创建了3个矩形,我想用不同的颜色标记矩形的一条边,或者换句话说,能够拾取矩形的每一条边,并对其进行标记。可能吗
注意,Ron在填充矩形后,只需在其一侧画一条线。 例如:
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.
注意:如果你想画所有的边,最好只画:
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);
在
填充
矩形后,只需在其一侧绘制一条线。
例如:
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.
注意:如果你想画所有的边,最好只画:
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);
谢谢您的帮助,但是行“e.gc.setColor(Color.BLUE);”有问题,我得到了一个编译错误,因为没有“BLUE”。知道怎么回事吗?我受够了,我的朋友,谢谢。解决方案是:“e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));”再次感谢!对不起,我把AWT和SWT弄混了!编辑答案。感谢您的帮助,但是“e.gc.setColor(Color.BLUE);”行有问题,我得到了一个编译错误,因为没有“BLUE”。知道怎么回事吗?我受够了,我的朋友,谢谢。解决方案是:“e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_BLUE));”再次感谢!对不起,我把AWT和SWT弄混了!编辑答案。