15.8

import java.awt.*;
import java.util.Scanner;

import javax.swing.*;

public class Test extends JFrame{
    
    public Test(){
        add(new PolygonPanel());
    }
    
    public static void  main(String[] args){    
        Test frame = new Test();
        frame.setSize(400, 400);
        frame.setTitle("Exercise15_8");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setVisible(true);        
    }
}

class PolygonPanel extends JPanel{   
    
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        
        int xCenter = getWidth()/2;
        int yCenter = getHeight()/2;
        int radius =(int)(Math.min(getWidth(), getHeight())*0.4);
        
        Polygon polygon = new Polygon();
        
        polygon.addPoint(xCenter, yCenter);
       g.drawPolygon(x, y, 4);
    }   
}
View Code
原文地址:https://www.cnblogs.com/wanjiang/p/5625235.html