一、题目要求
定义4个类,MyShape、MyLine、MyRectangle和MyOval,其中MyShape是其他三个类的父类。MyShape为抽象类,包括图形位置的四个坐标;一个无参的构造方法,将所有的坐标设置为0;一个带参的构造函数,将所有的坐标设置为相应值;每个坐标的设置和读取方法;abstract void draw(Graphics g)方法。MyLine类负责画直线,实现父类的draw方法;MyRectangle负责画矩形,实现父类的draw方法;MyOval负责画椭圆,实现父类的draw方法。编写一个应用程序,使用上面定义的类,随机选取位置和形状,绘制20个图形。示例输出如图所示。
二、代码示例
package JFrame_learning;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
abstract class MyShape{
double zs;double zx;double ys;double yx;
public MyShape(){
zs=0;zx=0;ys=0;yx=0;
}
public MyShape(double a,double b,double c,double d) {
this.zs=a;this.zx=b;this.ys=c;this.yx=d;
}
public double getZs() {
return zs;
}
public void setZs(int zs) {
this.zs = zs;
}
public double getZx() {
return zx;
}
public void setZx(int zx) {
this.zx = zx;
}
public double getYs() {
return ys;
}
public void setYs(int ys) {
this.ys = ys;
}
public double getYx() {
return yx;
}
public void setYx(int yx) {
this.yx = yx;
}
abstract void draw(Graphics g);
}
class MyLine extends MyShape{
public MyLine(double a,double b,double c,double d) {
super(a,b,c,d);
}
void draw(Graphics g) {
Graphics2D g2=(Graphics2D )g;
Line2D line=new Line2D.Double(this.zs,this.zx,this.ys,this.yx);
g2.draw(line);
}
}
class MyRectangle extends MyShape{
public MyRectangle(double a,double b,double c,double d) {
super(a,b,c,d);
}
void draw(Graphics g) {
Graphics2D g2=(Graphics2D )g;
Rectangle2D rectangle=new Rectangle2D.Double(this.zs,this.zx,this.ys,this.yx);
g2.draw(rectangle);
}
}
class MyOval extends MyShape{
public MyOval(double a,double b,double c,double d) {
super(a,b,c,d);
}
void draw(Graphics g) {
Graphics2D g2=(Graphics2D )g;
Ellipse2D ellipse=new Ellipse2D.Double(this.zs,this.zx,this.ys,this.yx);
g2.draw(ellipse);
}
}
public class Second_term {
public static void main(String []args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame Frame = new JFrame();
Frame.add(new DrawComponent());
Frame.pack();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
}
});
}
}
class DrawComponent extends JComponent
{
private static final int DEFAULT_WIDTH=600;
private static final int DEFAULT_HEIGHT=600;
public void paintComponent(Graphics g)
{
for(int i=0;i<20;i++) {
double a = Math.random()*300;
double b = Math.random()*300;
double c = Math.random()*300;
double d = Math.random()*300;
if(i<6) {
MyOval aMyOval = new MyOval(a, b, c, d);
aMyOval.draw(g);
}
else if(i<12) {
MyRectangle aMyRectangle = new MyRectangle(a, b, c, d);
aMyRectangle.draw(g);
}
else {
MyLine aLine = new MyLine(a, b, c, d);
aLine.draw(g);
}
}
}
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/103140.html