题目
定义4个类,MyShape、MyLine、MyRectangle和MyOval,其中MyShape是其他三个类的父类。MyShape为抽象类,包括图形位置的四个坐标;一个无参的构造方法,将所有的坐标设置为0;一个带参的构造函数,将所有的坐标设置为相应值;每个坐标的设置和读取方法;abstract void draw(Graphics g)方法。MyLine类负责画直线,实现父类的draw方法;MyRectangle负责画矩形,实现父类的draw方法;MyOval负责画椭圆,实现父类的draw方法。编写一个应用程序,使用上面定义的类,随机选取位置和形状,绘制20个图形。
代码
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class shapeDrawing {
//固定套路
public static void main(String[] args)
{
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//JFrame创建窗体
class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("Shapes");
setSize(400,400);
//窗体中加入面板
DrawPanel panel = new DrawPanel();
add(panel);
}
}
//JPanel在面板中显示信息
class DrawPanel extends JPanel
{
//重写paintComponent方法
public void paintComponent(Graphics g)
{
super.paintComponent(g);
MyShape s;
double c1,c2,c3,c4;
for(int i=0;i<5;i++)
{
//生成四个随机数
c1=Math.random()*200;
c2=Math.random()*200;
c3=Math.random()*200;
c4=Math.random()*200;
//图形绘制
s=new MyLine(c1,c2,c3,c4);
s.draw(g);
s=new MyRectangle(c2,c1,c3,c4);
s.draw(g);
s=new MyOval(c4,c3,c2,c1);
s.draw(g);
}
}
}
//抽象类MyShape,为其他三个类的父类
abstract class MyShape
{
//四个坐标
private double coordinate1;
private double coordinate2;
private double coordinate3;
private double coordinate4;
//无参构造
public MyShape()
{
coordinate1=0;
coordinate2=0;
coordinate3=0;
coordinate4=0;
}
//含参构造
public MyShape(double a,double b,double c,double d)
{
coordinate1=a;
coordinate2=b;
coordinate3=c;
coordinate4=d;
}
//抽象方法
public abstract void draw(Graphics g);
//设置和获取四个坐标
public double getCoordinate1() {
return coordinate1;
}
public void setCoordinate1(double coordinate1) {
this.coordinate1 = coordinate1;
}
public double getCoordinate2() {
return coordinate2;
}
public void setCoordinate2(double coordinate2) {
this.coordinate2 = coordinate2;
}
public double getCoordinate3() {
return coordinate3;
}
public void setCoordinate3(double coordinate3) {
this.coordinate3 = coordinate3;
}
public double getCoordinate4() {
return coordinate4;
}
public void setCoordinate4(double coordinate4) {
this.coordinate4 = coordinate4;
}
}
//画直线
class MyLine extends MyShape
{
//构造
public MyLine(double a,double b,double c,double d)
{
super(a,b,c,d);
}
//画
public void draw(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
//四个坐标为:startx,starty,endx,endy
Line2D line=new Line2D.Double(getCoordinate1(),getCoordinate2(),getCoordinate3(),getCoordinate4());
g2.draw(line);
}
}
//画矩形
class MyRectangle extends MyShape
{
//构造
public MyRectangle(double a,double b,double c,double d)
{
super(a,b,c,d);
}
//画
public void draw(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
//四个坐标为:startx,starty,width,height
Rectangle2D rect=new Rectangle2D.Double(getCoordinate1(),getCoordinate2(),getCoordinate3(),getCoordinate4());
g2.draw(rect);
}
}
//画椭圆
class MyOval extends MyShape
{
//构造
public MyOval(double a,double b,double c,double d)
{
super(a,b,c,d);
}
public void draw(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
//四个坐标为:startx,starty,width,height
Ellipse2D e=new Ellipse2D.Double(getCoordinate1(),getCoordinate2(),getCoordinate3(),getCoordinate4());
g2.draw(e);
}
}