编写一个程序画出矩形,如何补全主函数?
主函数是:
public class recttest { public static void main(String[] args) {
Rect r1 = new Rect(1,1,4,4);
Rect r2 = new Rect(2,3,5,6);
Rect u = r1.union(r2);
Rect i = r2.intersection(r1);
r1.move(r1.x1 , r1.y1);
if (u.isInside(r1.x1, r2.y1))
System.out.println("(" + r2.x1 + "," + r2.y1 +
") is inside the union");
System.out.println(r1 + "union" + r2 + "=" + u);
System.out.println(r1 + "intersect" + r2 + "=" + i);
System.out.println(r1);
//DrawableRect ra = new DrawableRect(1,1,4,4);
//Graphics g = new Rect();
}}父类是:
public class Rect { public int x1, y1, x2, y2;
public Rect (int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public Rect (int width, int height){
this(0 , 0, width, height);
}
public Rect(){
this (0, 0, 0, 0);
}
public void move (int deltax, int deltay){
x1 += deltax;
x2 += deltax;
y1 += deltax;
y2 += deltax;
}
public boolean isInside (int x, int y){
return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2));
}
public Rect union(Rect r){
return new Rect ((this.x1 < r.x1) ? this.x1 : r.x1,
(this.y1 < r.y1) ? this.y1 : r.y1,
(this.x2 > r.x2) ? this.x2 : r.x2,
(this.y2 > r.y2) ? this.y2 : r.y2);
}
public Rect intersection (Rect r){
Rect result = new Rect ((this.x1 > r.x1 ) ? this.x1 : r.x1,
(this.y1 > r.y1 ) ? this.y1 : r.y1,
(this.x2 < r.x2 ) ? this.x2 : r.x2,
(this.y2 < r.y2 ) ? this.y2 : r.y2);
if (result.x1 > result.x2 ){result.x1 = result.x2 = 0;}
if (result.y1 > result.y2 ){result.y1 = result.y2 = 0;}
return result;
}
public String toString(){
return "[" + x1 + "," + y1 + ";" + x2 + "," + y2 +"]";
}}
子类是:
import java.awt.Graphics;public class DrawableRect extends Rect { public DrawableRect(int x1, int y1, int x2, int y2) {
super(x1, y1, x2, y2);}
public void draw (Graphics g){
g.drawRect(x1, y1, (x2-x1), (y2-y1));
}}
主函数是:
public class recttest { public static void main(String[] args) {
Rect r1 = new Rect(1,1,4,4);
Rect r2 = new Rect(2,3,5,6);
Rect u = r1.union(r2);
Rect i = r2.intersection(r1);
r1.move(r1.x1 , r1.y1);
if (u.isInside(r1.x1, r2.y1))
System.out.println("(" + r2.x1 + "," + r2.y1 +
") is inside the union");
System.out.println(r1 + "union" + r2 + "=" + u);
System.out.println(r1 + "intersect" + r2 + "=" + i);
System.out.println(r1);
//DrawableRect ra = new DrawableRect(1,1,4,4);
//Graphics g = new Rect();
}}父类是:
public class Rect { public int x1, y1, x2, y2;
public Rect (int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public Rect (int width, int height){
this(0 , 0, width, height);
}
public Rect(){
this (0, 0, 0, 0);
}
public void move (int deltax, int deltay){
x1 += deltax;
x2 += deltax;
y1 += deltax;
y2 += deltax;
}
public boolean isInside (int x, int y){
return ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2));
}
public Rect union(Rect r){
return new Rect ((this.x1 < r.x1) ? this.x1 : r.x1,
(this.y1 < r.y1) ? this.y1 : r.y1,
(this.x2 > r.x2) ? this.x2 : r.x2,
(this.y2 > r.y2) ? this.y2 : r.y2);
}
public Rect intersection (Rect r){
Rect result = new Rect ((this.x1 > r.x1 ) ? this.x1 : r.x1,
(this.y1 > r.y1 ) ? this.y1 : r.y1,
(this.x2 < r.x2 ) ? this.x2 : r.x2,
(this.y2 < r.y2 ) ? this.y2 : r.y2);
if (result.x1 > result.x2 ){result.x1 = result.x2 = 0;}
if (result.y1 > result.y2 ){result.y1 = result.y2 = 0;}
return result;
}
public String toString(){
return "[" + x1 + "," + y1 + ";" + x2 + "," + y2 +"]";
}}
子类是:
import java.awt.Graphics;public class DrawableRect extends Rect { public DrawableRect(int x1, int y1, int x2, int y2) {
super(x1, y1, x2, y2);}
public void draw (Graphics g){
g.drawRect(x1, y1, (x2-x1), (y2-y1));
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货