189 8069 5689

java五边形代码,规则的五边形

java画图形

import java.awt.BorderLayout;

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、网站建设、乐都网络推广、微信小程序、乐都网络营销、乐都企业策划、乐都品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供乐都建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JApplet;

import javax.swing.JComboBox;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class GraphTest extends JApplet {

private static final long serialVersionUID = -3038851324293731224L;

private PaintingPane paintingPane;

private Painter currentPainter = Painter.CIRCLE;

private final static int DEFAULT_RADIUS = 50;

private int radius = DEFAULT_RADIUS;

private Color color = getColor();

public void init() {

this.setLayout(new BorderLayout());

paintingPane = new PaintingPane();

paintingPane.setPreferredSize(new Dimension(250, 250));

this.add(paintingPane, BorderLayout.CENTER);

JPanel controlPane = new JPanel();

final SpinnerNumberModel sm = new SpinnerNumberModel(DEFAULT_RADIUS, 10, 100, 1);

JSpinner spinner = new JSpinner(sm);

spinner.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

radius = sm.getNumber().intValue();

color = getColor();

repaint();

}

});

final ComboBoxModel cbm = new DefaultComboBoxModel(Painter.values());

JComboBox combo = new JComboBox(cbm);

combo.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

if (e.getStateChange() == ItemEvent.SELECTED) {

currentPainter = (Painter) cbm.getSelectedItem();

color = getColor();

repaint();

}

}

});

JLabel spinnerLabel = new JLabel("Radius: ");

JLabel comboLabel = new JLabel("Figure: ");

spinnerLabel.setLabelFor(spinner);

comboLabel.setLabelFor(combo);

controlPane.setPreferredSize(new Dimension(150, 250));

controlPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

controlPane.add(spinnerLabel);

controlPane.add(spinner);

controlPane.add(comboLabel);

controlPane.add(combo);

this.add(controlPane, BorderLayout.EAST);

this.setSize(400, 200);

this.setVisible(true);

}

private enum Painter {

CIRCLE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

g.setColor(color);

g.fillOval(d.width / 2 - parameter, d.height / 2 - parameter,

parameter * 2, parameter * 2);

}

public String toString() { return "Circle"; }

},

LINE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xOffset = d.width / 2, yOffset = d.height / 2;

g.setColor(color);

g.drawLine(xOffset - parameter, yOffset, xOffset + parameter,

yOffset);

}

public String toString() { return "Line"; }

},

DIAMOND {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offset = (int) (parameter / SQUARE_ROOT_2);

int[] xCoors = {

xCenter - offset, xCenter, xCenter + offset, xCenter,

};

int[] yCoors = {

yCenter, yCenter + offset, yCenter, yCenter - offset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 4);

}

public String toString() { return "Diamond"; }

},

TRIANGLE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offsetPos = (int) (parameter / SQUARE_ROOT_3);

int offsetNeg = offsetPos / 2;

int offsetX = parameter / 2;

int[] xCoors = {

xCenter - offsetX, xCenter + offsetX, xCenter,

};

int[] yCoors = {

yCenter + offsetNeg, yCenter + offsetNeg,

yCenter - offsetPos,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 3);

}

public String toString() { return "Triangle"; }

},

SQUARE {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int offset = parameter / 2;

int[] xCoors = {

xCenter - offset, xCenter + offset, xCenter + offset,

xCenter - offset,

};

int[] yCoors = {

yCenter + offset, yCenter + offset,

yCenter - offset, yCenter - offset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 4);

}

public String toString() { return "Square"; }

},

PENTAGON {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int xOffset2 = parameter / 2;

int yOffset3 = (int) (xOffset2 / TANGENT_36);

int yOffset1 = (int) (xOffset2 / SINE_36);

int xOffset1 = (int) (yOffset1 * COSINE_18);

int yOffset2 = (int) (yOffset1 * SINE_18);

int[] xCoors = {

xCenter - xOffset1, xCenter - xOffset2, xCenter + xOffset2,

xCenter + xOffset1, xCenter,

};

int[] yCoors = {

yCenter - yOffset2, yCenter + yOffset3, yCenter + yOffset3,

yCenter - yOffset2, yCenter - yOffset1,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 5);

}

public String toString() { return "Pentagon"; }

},

HEXAGON {

void paint(Graphics g, JPanel panel, int parameter,

Color color) {

Dimension d = panel.getSize();

int xCenter = d.width / 2, yCenter = d.height / 2;

int xOffset = parameter / 2;

int yOffset = (int) (xOffset * SQUARE_ROOT_3);

int[] xCoors = {

xCenter - parameter, xCenter - xOffset, xCenter + xOffset,

xCenter + parameter, xCenter + xOffset, xCenter - xOffset,

};

int[] yCoors = {

yCenter, yCenter + yOffset, yCenter + yOffset,

yCenter, yCenter - yOffset, yCenter - yOffset,

};

g.setColor(color);

g.fillPolygon(xCoors, yCoors, 6);

}

public String toString() { return "Hexagon"; }

};

abstract void paint(Graphics g, JPanel panel, int parameter,

Color color);

static final double SQUARE_ROOT_2 = Math.pow(2.0, 0.5);

static final double SQUARE_ROOT_3 = Math.pow(3.0, 0.5);

static final double SINE_36 = Math.sin(Math.PI / 5);

static final double TANGENT_36 = Math.tan(Math.PI / 5);

static final double COSINE_18 = Math.cos(Math.PI / 10);

static final double SINE_18 = Math.sin(Math.PI / 10);

}

private class PaintingPane extends JPanel {

private static final long serialVersionUID = 8856506825301606303L;

public void paint(Graphics g) {

super.paint(g);

currentPainter.paint(g, this, radius, color);

}

}

private static Color getColor() {

return possibleColors[(int) (Math.random() * 7)];

}

private static final Color[] possibleColors = {

Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,

Color.CYAN, Color.BLUE, Color.MAGENTA,

};

}

java怎么画出一个五角星里边有一个五边形

画一条水平线,通过此线上的任意点做一个圆。 2.将圆规的一腿放在圆与直线的其一交点上,通过上述圆的圆心画半圆,并与之交两点。连接这两点做垂直线,与先前的水平线相交与(a)点. 3.张开圆规,以水平线与第一个圆的两个交点为圆心以相同半径在水平线上下第一个圆外分别做两个交点,这样可以得到一条通过第一个圆圆心的正交线,与第一个圆相交的位于水平线上方的点称之为(b).这是正五边形的第一个角。 4.将圆规的一脚放在(a)点上,(a)(b)间距为半径做另一个圆,交水平线于点(c)。 5.将圆规的一脚放在(b)点上,(b)(c)间距为半径做圆,交第一个圆于两点,这是正五边形的第二、三两点。 6.将圆规的一脚分别放在二、三两点上,同样是(b)(c)间距为半径交第一个圆于另外两点,这两点就是正五边形的最后两点。 连接相邻两点就构成了正五边形。 7.如果不是连接相邻两点(即对角线连接),就会得到一个五芒星。

如何使用java 程序实现将一个不规则的五边形分成三个三角形,然后用海伦公式求三角形面积,最后得到

public class Triangle

{

private double a;

private double b;

private double c;

public void Triangle(double a, double b, double c)

{

this.a = a;

this.b =b;

this.c = c;

}

public double square()

{

double s = (a + b + c) / 2;

double res = Math.sqrt(s * (s -a)(s - b)(s - c));

}

public static void main(String[] args)

{

double sabe = Triganle(52.1, 68.2, 33.4).square();

double sbce = Triganle(57.2, 71.9, 68.2).square();

double secd = Triganle(71.9, 43.5, 51.9).square();

double res = sabe + sbce + secd;

System.out.println("the area of the graph is: " + res);

}

这个Java作业怎么写?

public class Test {

public static void main(String[] args) {

// 创建一个边长分别为3,4,5的三角形

Shape triangle = new Triangle(3, 4, 5);

triangle.perimeter();

triangle.area();

// 创建一个边长5的正方形

Shape square = new Square(5);

square.perimeter();

square.area();

// 创建一个半径5的圆

Shape circle = new Circle(5);

circle.perimeter();

circle.area();

// 创建一个边长5的正五边形

Shape pentagon = new Pentagon(5);

pentagon.perimeter();

pentagon.area();

}

}

/**

* 图形接口

*/

interface Shape {

/**

* 面积计算

*/

double area();

/**

* 周长计算

*/

double perimeter();

}

/**

* 三角形

*/

class Triangle implements Shape {

private double a;

private double b;

private double c;

@Override

public double area() {

// 用海伦公式, 已知三角形3条边长算面积

double p = (a + b + c) / 2;

double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));

System.out.println("\t三角形面积: " + area);

return area;

}

@Override

public double perimeter() {

double perimeter = a + b + c;

System.out.println("\t三角形周长: " + perimeter);

return perimeter;

}

public Triangle(double a, double b, double c) {

this.a = a;

this.b = b;

this.c = c;

System.out.println("三角形边长分别为: " + a + " , " + b + " ," + c);

}

}

/**

* 正方形

*/

class Square implements Shape {

private double length;

@Override

public double area() {

double area = Math.pow(length, 2);

System.out.println("\t正方形面积: " + area);

return area;

}

@Override

public double perimeter() {

double perimeter = 4 * length;

System.out.println("\t正方形周长: " + perimeter);

return perimeter;

}

public Square(double length) {

this.length = length;

System.out.println("正方形边长为: " + length);

}

}

/**

* 圆形

*/

class Circle implements Shape {

private double radius;

@Override

public double area() {

double area = Math.PI * Math.pow(radius, 2);

System.out.println("\t圆面积: " + area);

return area;

}

@Override

public double perimeter() {

double perimeter = Math.PI * 2 * radius;

System.out.println("\t圆周长: " + perimeter);

return perimeter;

}

public Circle(double radius) {

this.radius = radius;

System.out.println("圆半径为: " + radius);

}

}

/**

* 正五边形

*/

class Pentagon implements Shape {

private double length;

@Override

public double area() {

double area = 5 * Math.pow(length, 2) * Math.tan(Math.toRadians(54)) / 4;

System.out.println("\t五边形面积: " + area);

return 0;

}

@Override

public double perimeter() {

double perimeter = 5 * length;

System.out.println("\t五边形周长: " + perimeter);

return perimeter;

}

public Pentagon(double length) {

this.length = length;

System.out.println("正五边形边长为: " + length);

}

}

java 程序 构造正五边形

请lz把以下代码存为:Graph5.java,然后编译运行即可:

面积与周长请lz自行计算(边长从代码里可以看到,提示:g.drawLine(m_point[i].m_fx, m_point[i].m_fy, m_point[0].m_fx,m_point[0].m_fy,分别为起点和重点的XY坐标)。

-------------

import java.awt.*;

import java.applet.*;

public class Graph5 extends Applet {

int width;

int height;

public void init() {

width = getSize().width;

height = getSize().height;

} // end init()

public void paint(Graphics g) {

int fR = 0;

if (width height)

fR = height;

else

fR = width;

Point[] m_point = new Point[5];

double fcos18 = Math.cos((double) 18 / 180 * Math.PI);

double fsin18 = Math.sin((double) 18 / 180 * Math.PI);

double fcos54 = Math.cos((double) 54 / 180 * Math.PI);

double fsin54 = Math.sin((double) 54 / 180 * Math.PI);

double fcos90 = Math.cos((double) 90 / 180 * Math.PI);

double fsin90 = Math.sin((double) 90 / 180 * Math.PI);

fR/=2;

int r1 = fR - 1;

int r2 = fR-1;

int iStar = 5;

for (int i = 0; i iStar; ++i) {

m_point[i] = new Point();

}

m_point[0].m_fx = (int) (fR * fcos18) + r1;

m_point[0].m_fy = (int) (fR * (-fsin18)) + r2;

m_point[1].m_fx = (int) (fR * fcos90) + r1;

m_point[1].m_fy = (int) (fR * (-fsin90)) + r2;

m_point[2].m_fx = (int) (fR * (-fcos18)) + r1;

m_point[2].m_fy = (int) (fR * (-fsin18)) + r2;

m_point[3].m_fx = (int) (fR * (-fcos54)) + r1;

m_point[3].m_fy = (int) (fR * (fsin54)) + r2;

m_point[4].m_fx = (int) (fR * (fcos54)) + r1;

m_point[4].m_fy = (int) (fR * (fsin54)) + r2;

for (int i = 0; i iStar; ++i) {

if (i == (iStar - 1)) {

g.drawLine(m_point[i].m_fx, m_point[i].m_fy, m_point[0].m_fx,

m_point[0].m_fy);

return;

} else

g.drawLine(m_point[i].m_fx, m_point[i].m_fy,

m_point[i + 1].m_fx, m_point[i + 1].m_fy);

}

}

}

class Point {

public int m_fx;

public int m_fy;

}


名称栏目:java五边形代码,规则的五边形
链接地址:http://cdxtjz.com/article/hdpgcs.html

其他资讯