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

Python011

如何用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()//重绘组件

}

}

}

没有输入全称,或者你编译的文件名和源代码中公开类的类名不一致。

用java编写简单图形,用JAVA编写一个图形应用程序,可以是一个简单的文本编辑器、计算器。不要用鼠标事件。要求在窗口中出现几个按钮,分别是圆形,矩形,多边形,圆弧,椭圆,直线,三角形等。

点击其中一个按钮就会出现相应的图形,图形的大小颜色不做具体要求。

我来写一下吧:abstract class Shape{private double cprivate double spublic abstract void girth()public abstract void area()public void setGirth(double c){this.c = c}public void setArea(double s){this.s = s}public double getGirth(){return c}public double getArea(){return s}public void outInfo(){}}class Circle extends Shape{private static final double PI = 3.1415926private double r//定义一个构造函数public Circle(double r){this.r = r}//重写抽象方法public void girth() {double a =2*PI*rsuper.setGirth(a)}public void area() {double b =PI*r*rsuper.setArea(b)}public void outInfo() {this.girth()this.area()System.out.println("所求圆形周长为:"+super.getGirth())System.out.println("所求圆形面积为:"+super.getArea())}}class Rectangle extends Shape{private double heightprivate double width//定义一个构造函数public Rectangle(double height,double width){this.height = heightthis.width = width}//重写抽象方法public void girth() {double a =2*(height+width)super.setGirth(a)}public void area() {double b =(height*width)super.setArea(b)}public void outInfo() {this.girth()this.area()System.out.println("所求矩形周长为:"+super.getGirth())System.out.println("所求矩形面积为:"+super.getArea())}}class Triangle extends Shape{private double lengthAprivate double lengthBprivate double lengthC//定义一个构造函数public Triangle(double lengthA,double lengthB,double lengthC){this.lengthA = lengthAthis.lengthB = lengthBthis.lengthC = lengthC}//重写抽象方法public void girth() {double a =(lengthA+lengthB+lengthC)super.setGirth(a)}public void area() {if((lengthA+lengthB <lengthC) || (lengthA + lengthC <lengthB) || (lengthB+lengthC <lengthA)) {System.out.println("两边之和必须大于第三个边")System.exit(0)}double p = (lengthA+lengthB+lengthC)/2double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC))super.setArea(b)}public void outInfo() {this.girth()this.area()System.out.println("所求三角形周长为:"+super.getGirth())System.out.println("所求三角形面积为:"+super.getArea())}}public class ShapeTest {public static void main (String [] args){Shape circle = new Circle(3.0)Shape rectangle = new Rectangle(8.0,7.0)Shape triangle = new Triangle(3.0,4.0,5.0)circle.outInfo()rectangle.outInfo()triangle.outInfo()}}