我定义了一个父类packageRect;publicclassRectClass{publicintx1,x2,y1,y2;publicRectClass(intx1,inty1,intx2,inty2){this.x1=x1;this.y1=y1;this.x2=x2;this.y2=y2;}publicRectClas...
我定义了一个父类
package Rect;
public class RectClass{
public int x1,x2,y1,y2;
public RectClass(int x1,int y1,int x2,int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public RectClass(int width,int height)
{this(0, 0, width, height);}
public RectClass() {this(0, 0, 0, 0);}
public void move(int moveX, int moveY)
{
x1 += moveX;
x2 += moveX;
y1 += moveY;
y2 += moveY;
}
public boolean isInside(int x, int y)
{
return ((x>=x1) && (x<=x2) && (y>=y1) && (y<=y2));
}
public RectClass union(RectClass rec)
{
return new RectClass(this.x1
this.x2
this.x1
this.x1
}
public String toString() {
return ("["+x1+ "," +y1+ ";" +x2+ "," +y2+ "]");
}
}
2个子类
package Rect;
public class DrawRect extends RectClass
{
public DrawRect(int x1, int y1, int x2, int y2)
{ super(x1, y1, x2, y2);}
public void draw(java.awt.Graphics g)
{
g.drawRect(x1, y1, (x2 - x1), (y2 - y1));
}
}
package Rect;
import java.awt.*;
public class ColoredRect extends DrawRect {
protected Color border,fill;
public ColoredRect(int x1, int y1, int x2, int y2, Color border,Color fill)
{
super(x1,y1,x2,y2);
this.border = border;
this.fill = fill;
}
public void draw(Graphics g) {
g.setColor(fill);
g.fillRect(x1,y1,(x2-x1),(y2-y1));
g.setColor(border);
g.drawRect(x1,y1,(x2-x1),(y2-y1));
}
}
在下面用到子类,但是子类的draw方法中含一个抽象类的参数Graphics ,我不知道怎么用,请高手门指教,给个示例!!
package Rect;
import java.awt.*;
public class RectInstance{
public static void main(String args[]) {
DrawRect rec_draw = new DrawRect(1,1,4,4);
ColoredRect rec_color = new ColordedRect(2,3,5,6,new Color(0,125,79), new Color(0,77,79));
rec_draw.draw(new Graphics());
rec_color.draw(new Graphics());
}
}
大虾,我跪求实例啊~~~~
help me
展开