博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

applet main共存 五角星和五面形

Posted on 2012-08-08 16:54  紫冰龙  阅读(209)  评论(0编辑  收藏  举报
import javax.swing.*;

import java.applet.Applet;
import java.awt.*;
import java.awt.geom.Point2D;
import java.util.Vector;

public class DrawStar extends Applet {
    Vector<Point2D.Double> points = new Vector<Point2D.Double>();
    Polygon poa,pob;
    double r = 100;
    double n = r+50;
    public DrawStar(){
        computerPoints(5);
    }
    public void init(){
        computerPoints(5);
        
    }
    private void computerPoints(int n){
        
        double pi = 3.14159265;
        points.add(new Point2D.Double(0,r));
        for(int i=1;i<n;i++) {
            double x = r*Math.sin(2*pi*i/n);
            double y = r*Math.cos(2*pi*i/n);
            points.add(new Point2D.Double(x,y));
        
        }
        
    }
    public void paint(Graphics g) {
        fillPolygonA();
        fillPolygonB();
        g.drawPolygon(poa);
        g.drawPolygon(pob);
    }
    private void fillPolygonA() {
        poa = new Polygon();
        for(int i=0;i<points.size();i++) {
            int x = (int)(points.get(i).getX()+n);
            int y = (int)(points.get(i).getY()+n);
            poa.addPoint(x, y);
            
        }
    }
    private void fillPolygonB() {
        int s = points.size();
        pob = new Polygon();
        int i=0;
        for(;;) {
            int x = (int)(points.get(i).getX()+n);
            int y = (int)(points.get(i).getY()+n+200);
            pob.addPoint(x, y);
            i+=2;
            if(i%s==0)  break;
            else i%=s;
            
        }
    }
    public  static void main(String[] args ) {
        JFrame f = new JFrame("Test");
        f.setSize(300,600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(new DrawStar());
        f.setVisible(true);
    }
    
}