Octagon.java
资源名称:Octagon.rar [点击查看]
上传用户:topwell
上传日期:2022-05-30
资源大小:1k
文件大小:1k
源码类别:
网络编程
开发平台:
DOS
- import java.awt.*;
- import javax.swing.*;
- public class Octagon extends JFrame{
- public Octagon(){
- add(new OctagonPanel());
- }
- public static void main(String[] args){
- Octagon frame = new Octagon();
- frame.setTitle("Octagon");
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(300, 300);
- frame.setVisible(true);
- }
- }
- class OctagonPanel extends JPanel{
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- //Get the center point and radius
- int xCenter = getSize().width / 2;
- int yCenter = getSize().height / 2;
- int radius = (Math.min(xCenter, yCenter) - 20);
- System.out.println("xCenter = " + xCenter + ", yCenter = " + yCenter +
- ", radius = " + radius);
- Polygon octagon = new Polygon();
- for(int i = 0; i < 8; i++){
- double angle = Math.PI * i * 45.0 / 180;
- int x = (int)(xCenter - radius * Math.cos(angle));
- int y = (int)(yCenter - radius * Math.sin(angle));
- octagon.addPoint(x, y);
- System.out.println("x = " + x + ", y = " + y);
- }
- // Draw a octagon
- g.drawPolygon(octagon);
- }
- }
English
