我使用此方法创建一个正方形:
public void displayCircle() {
JFrame frame = new JFrame();
frame.setSize(400, 300);
int x1 = (int) A.getX();
int x2 = (int) B.getX();
int x3 = (int) C.getX();
int x4 = (int) D.getX();
// 25, 156, 175, 80
int y1 = (int) A.getY();
int y2 = (int) B.getY();
int y3 = (int) C.getY();
int y4 = (int) D.getY();
applet = new CircleApplet(new int[] { x1, x2, x3, x4 }, new int[] { y1, y2, y3, y4 });
int R = (int) ((Math.random()) * 256);
int G = (int) ((Math.random()) * 256);
int Bb = (int) ((Math.random()) * 256);
applet.setForeground(new Color(R, G, Bb, 200));
frame.getContentPane().add(applet);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
applet.stop();
applet.destroy();
System.exit(0);
}
});
frame.setVisible(true);
applet.init();
applet.start();
}
使用此圈子小程序:
import java.applet.*;
import java.awt.*;
@SuppressWarnings("deprecation")
public class CircleApplet extends Applet {
int[] pointsX,pointsY;
int numPoints=0;
/**
*
*/
private static final long serialVersionUID = 1L;
public CircleApplet() {}
public CircleApplet(int[] pointsX, int[] pointsY) {
this.pointsX=pointsX;
this.pointsY=pointsY;
this.numPoints=Math.min(pointsX.length, pointsY.length);
}
public void paint(Graphics g) {
//System.out.println(this.numPoints);
if (this.numPoints>0)
g.fillPolygon(pointsX,pointsY,numPoints);
for(int x = 0; x < numPoints; x++){
g.setColor(new Color(0, 0, 0));
char alpha = 'X';
switch (x){
case 0:
alpha = 'A';
break;
case 1:
alpha = 'B';
break;
case 2:
alpha = 'C';
break;
case 3:
alpha = 'D';
break;
default:
break;
}
g.drawString(alpha +" (" + pointsX[x] + " , " + pointsY[x] + ")", pointsX[x], pointsY[x]);
}
}
}
我想创建上面提到的圈子,如何创建圈子?我是否使用弧线或者我必须重写它,问题本身是直接的但我仍然不理解小程序。