关于java.awt.Graphics g问题
我定义了一个父类packageRect;publicclassRectClass{publicintx1,x2,y1,y2;publicRectClass(intx1,i...
我定义了一个父类
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<rec.x1? this.x1 : rec.x1,
this.x2<rec.x2? this.x2 : rec.x2,
this.x1<rec.y1? this.y1 : rec.y1,
this.x1<rec.y2? this.y2 : rec.y2);
}
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 展开
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<rec.x1? this.x1 : rec.x1,
this.x2<rec.x2? this.x2 : rec.x2,
this.x1<rec.y1? this.y1 : rec.y1,
this.x1<rec.y2? this.y2 : rec.y2);
}
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 展开
2个回答
展开全部
因为 Graphics 是一个抽象类,所以应用程序不能直接调用此构造方法。Graphics可以从其他图形上下文获取,或者通过在swing组件上调用 getGraphics 来创建。
public Graphics getGraphics()为组件创建一个图形上下文。如果组件当前是不可显示的,则此方法返回 null。
返回:组件的图形上下文,如果其没有,则返回 null
从以下版本开始:JDK1.0
另请参见:paint(java.awt.Graphics)
例如:
JFrame jf=new JFrame();
jf.setVisible(true);
Graphics g=jf.getGraphics();
然后再使用g
rec_draw.draw(g);
rec_color.draw(g);
如果画不上的话,尝试把jf.setVisible(true);放在rec_color.draw(g);后面,不敢保证一定能画上,因为在swing组件上画图的一般方法是重写paint()方法,例如:
class DrawPanel extends JPanel {
public DrawPanel() {
super();
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(0,0,100,100);
}
public static void main(String args[]){
JFrame jf=new JFrame();
jf.setSize(600,600);
jf.setLocation(200,200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(new DrawPanel(),"Center");
jf.setVisible(true);
}
}
你要找实例的话,建议找一本书学学吧,那样更系统一点
public Graphics getGraphics()为组件创建一个图形上下文。如果组件当前是不可显示的,则此方法返回 null。
返回:组件的图形上下文,如果其没有,则返回 null
从以下版本开始:JDK1.0
另请参见:paint(java.awt.Graphics)
例如:
JFrame jf=new JFrame();
jf.setVisible(true);
Graphics g=jf.getGraphics();
然后再使用g
rec_draw.draw(g);
rec_color.draw(g);
如果画不上的话,尝试把jf.setVisible(true);放在rec_color.draw(g);后面,不敢保证一定能画上,因为在swing组件上画图的一般方法是重写paint()方法,例如:
class DrawPanel extends JPanel {
public DrawPanel() {
super();
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(0,0,100,100);
}
public static void main(String args[]){
JFrame jf=new JFrame();
jf.setSize(600,600);
jf.setLocation(200,200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(new DrawPanel(),"Center");
jf.setVisible(true);
}
}
你要找实例的话,建议找一本书学学吧,那样更系统一点
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询