java awt graphics_关于java.awt.Graphics g问题

我定义了一个父类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

展开

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值