如何用Java编写一个绘制图形的小程序?

如何用Java编写一个绘制图形的小程序?,第1张

import java.awt.*

import java.awt.event.*

import java.awt.geom.*

import javax.swing.*

//不规则图形的绘制

public class IrregularShapeDemo extends JFrame {

GeneralPath gPath= new GeneralPath()//GeneralPath对象实例

Point aPoint

//构造函数

public IrregularShapeDemo() {

super("不规则图形的绘制")//调用父类构造函数

enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)//允许事件

setSize(300, 200)//设置窗口尺寸

setVisible(true)//设置窗口可视

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//关闭窗口时退出程序

}

public void paint(Graphics g) { //重载窗口组件的paint()方法

Graphics2D g2D = (Graphics2D)g//获取图形环境

g2D.draw(gPath)//绘制路径

}

public static void main(String[] args) {

new IrregularShapeDemo()

}

protected void processMouseEvent(MouseEvent e) { //鼠标事件处理

if(e.getID() == MouseEvent.MOUSE_PRESSED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath = new GeneralPath()//重新实例化GeneralPath对象

gPath.moveTo(aPoint.x,aPoint.y)//设置路径点

}

}

protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理

if(e.getID() == MouseEvent.MOUSE_DRAGGED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath.lineTo(aPoint.x, aPoint.y)//设置路径

gPath.moveTo(aPoint.x, aPoint.y)

repaint()//重绘组件

}

}

}

1. 抽象形状类

package com

//抽象的形状类

public abstract class Shape{

}

2. 显示接口

package com

//接口

public interface IDisplay{

void display() //显示图形的基本信息

double getArea() //计算面积

double getGirth() //计算周长

}

3. 三角形

package com.tri

import com.*

//三角形类

public class Triangle extends Shape implements IDisplay{

protected double a

protected double b

protected double c

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

this.a = a

this.b = b

this.c = c

}

@Override

public double getArea() {

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

return Math.sqrt(s*(s-a)*(s-b)*(s-c))

}

@Override

public double getGirth() {

return this.a + this.b + this.c

}

@Override

public void display() {

System.out.println("三角形")

System.out.println("边长:" + a + ", " + b + ", " + c)

}

}

4. 矩形类

package com.rec

import com.*

//矩形类

public class Rectangle extends Shape implements IDisplay {

protected double width

protected double height

public Rectangle(double width, double height){

this.width = width

this.height = height

}

@Override

public double getArea() {

return this.width * this.height

}

@Override

public double getGirth() {

return 2 * ( this.width + this.height)

}

@Override

public void display() {

System.out.println("矩形")

System.out.println("宽:" + this.width + ", 高:" + this.height)

}

}

5. 圆类

package com.cir

import com.*

//圆类

public class Circle extends Shape implements IDisplay {

protected double radius

public Circle(double radius){

this.radius = radius

}

@Override

public double getArea() {

return Math.PI * this.radius * this.radius

}

@Override

public double getGirth() {

return 2 * Math.PI * this.radius

}

@Override

public void display() {

System.out.println("圆")

System.out.println("半径:" + this.radius)

}

}

6. 正多边形

package com.mul

import com.*

//正多边形类

public class MulSide extends Shape implements IDisplay {

protected double side //边长

protected int n //边数

public MulSide(double side, int n){

this.side = side

this.n = n

}

@Override

public double getArea() {

return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4

}

@Override

public double getGirth() {

return this.side * this.n

}

@Override

public void display() {

System.out.println("正多边形")

System.out.println("连长:" + this.side + ", 边数:" + this.n)

}

}

7. 主类(测试类)

package com

import java.util.Scanner

import com.cir.Circle

import com.mul.MulSide

import com.rec.Rectangle

import com.tri.Triangle

public class Test22 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in)

double a, b, c

double width, height

double radius

double side

int n

IDisplay s

System.out.println("请输入三角形的基本信息")

System.out.print("边长1:")

a = scan.nextDouble()

System.out.print("边长2:")

b = scan.nextDouble()

System.out.print("边长3:")

c = scan.nextDouble()

s = new Triangle(a, b, c)

s.display()

System.out.println("三角形的面积:" + s.getArea())

System.out.println("三角形的周长:" + s.getGirth())

System.out.println("请输入矩形的基本信息")

System.out.print("宽:")

width = scan.nextDouble()

System.out.print("高:")

height = scan.nextDouble()

s = new Rectangle(width, height)

s.display()

System.out.println("矩形的面积:" + s.getArea())

System.out.println("矩的周长:" + s.getGirth())

System.out.println("请输入圆的基本信息")

System.out.print("半径:")

radius = scan.nextDouble()

s = new Circle(radius)

s.display()

System.out.println("圆的面积:" + s.getArea())

System.out.println("圆的周长:" + s.getGirth())

System.out.println("请输入正多边形的基本信息")

System.out.print("边长:")

side = scan.nextDouble()

System.out.print("边数:")

n = scan.nextInt()

s = new MulSide(side, n)

s.display()

System.out.println("正多边形的面积:" + s.getArea())

System.out.println("正多边形的周长:" + s.getGirth())

}

}

运行测试:

请输入三角形的基本信息

边长1:3

边长2:4

边长3:5

三角形

边长:3.0, 4.0, 5.0

三角形的面积:6.0

三角形的周长:12.0

请输入矩形的基本信息

宽:3

高:4

矩形

宽:3.0, 高:4.0

矩形的面积:12.0

矩的周长:14.0

请输入圆的基本信息

半径:2

半径:2.0

圆的面积:12.566370614359172

圆的周长:12.566370614359172

请输入正多边形的基本信息

边长:2

边数:6

正多边形

连长:2.0, 边数:6

正多边形的面积:10.39230484541326

正多边形的周长:12.0


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11881082.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存