新手问题:我想请教一下如果我已生成n个随机点,需要插入什么SWT语句可以在这n个点的位置生成n个标号为1...n的实心圆?麻烦哪位高人帮忙指点一下!

解决方案 »

  1.   

    package com.lifesting.bist.face;import java.util.Random;import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;public class DrawCycle {
    public static void main(String[] args) {
    Display display = new Display() ;
    Shell shell  = new Shell(display);
    shell.setSize(500, 500);
    shell.setText("Drawing");
    shell.addListener(SWT.Paint, new Listener(){ public void handleEvent(Event event) {
    Rectangle rect = ((Shell)event.widget).getClientArea();
    GC gc = event.gc;

    Random r = new Random();
    //How may cycles do you wanna draw?
    int cycle_number = 20;

    for (int i = 0; i < cycle_number; i++)
    {
    int o_x = r.nextInt(400);
    int o_y = r.nextInt(400);
    int rl = r.nextInt(20)+5;
    int[] data = drawCycle(new Point(o_x,o_y), rl);
    gc.drawPolygon(data);
    }

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

    public static int[] drawCycle(Point o,int r_length)
    {
    //more the number be set, more the cycle be precise.  
    int point_number = 1000;
    int[] points = new int[point_number*2]; //X,Y
    for (int i = 0; i < point_number; i++) {
    double r = Math.PI*2*i / point_number;
    points[2*i] = (int) (o.x + Math.cos(r)*r_length);
    points[2*i+1] = (int) (o.y+Math.sin(r)*r_length);
    }
    return points;
    }
    }
      

  2.   

    //Improved by adding the cycle's number orderpackage com.lifesting.bist.face;import java.util.Random;import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;public class DrawCycle {
    public static void main(String[] args) {
    Display display = new Display() ;
    Shell shell  = new Shell(display);
    shell.setSize(500, 500);
    shell.setText("Drawing");
    shell.addListener(SWT.Paint, new Listener(){ public void handleEvent(Event event) {
    GC gc = event.gc;
    Random r = new Random();
    //How may cycles do you wanna draw?
    int cycle_number = 20;

    for (int i = 0; i < cycle_number; i++)
    {
    int o_x = r.nextInt(400);
    int o_y = r.nextInt(400);
    int rl = r.nextInt(20)+5;
    int[] data = drawCycle(new Point(o_x,o_y), rl);
    //Draw Title
    String label = String.valueOf(i);
    Point label_xy = gc.stringExtent(label);
    gc.drawText(String.valueOf(i), o_x-label_xy.x/2, o_y-label_xy.y/2);

    gc.drawPolygon(data);
    }

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

    public static int[] drawCycle(Point o,int r_length)
    {
    //more the number be set, more the cycle be precise.  
    int point_number = 1000;
    int[] points = new int[point_number*2]; //X,Y
    for (int i = 0; i < point_number; i++) {
    double r = Math.PI*2*i / point_number;
    points[2*i] = (int) (o.x + Math.cos(r)*r_length);
    points[2*i+1] = (int) (o.y+Math.sin(r)*r_length);
    }
    return points;
    }
    }
      

  3.   

    //if you need '实心圆',try the code below:package com.lifesting.bist.face;import java.util.Random;import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;public class DrawCycle {
    public static void main(String[] args) {
    Display display = new Display() ;
    Shell shell  = new Shell(display);
    shell.setSize(500, 500);
    shell.setText("Drawing");
    shell.addListener(SWT.Paint, new Listener(){ public void handleEvent(Event event) {
    GC gc = event.gc;
    Random r = new Random();
    //How may cycles do you wanna draw?
    int cycle_number = 20;
    gc.setForeground(event.display.getSystemColor(SWT.COLOR_WHITE));
    for (int i = 0; i < cycle_number; i++)
    {
    int o_x = r.nextInt(400);
    int o_y = r.nextInt(400);
    int rl = r.nextInt(20)+10;
    int[] data = drawCycle(new Point(o_x,o_y), rl);

    Color old_bg = gc.getBackground();
    gc.setBackground(event.display.getSystemColor(SWT.COLOR_BLACK));
    gc.fillPolygon(data);
    gc.setBackground(old_bg);

    String label = String.valueOf(i);
    Point label_xy = gc.stringExtent(label);
    gc.drawText(String.valueOf(i), o_x-label_xy.x/2, o_y-label_xy.y/2,true);
    }

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

    public static int[] drawCycle(Point o,int r_length)
    {
    //more the number be set, more the cycle be precise.  
    int point_number = 1000;
    int[] points = new int[point_number*2]; //X,Y
    for (int i = 0; i < point_number; i++) {
    double r = Math.PI*2*i / point_number;
    points[2*i] = (int) (o.x + Math.cos(r)*r_length);
    points[2*i+1] = (int) (o.y+Math.sin(r)*r_length);
    }
    return points;
    }
    }