如何使用g.fillRect方法在Java中创建Rectangle对象 [英] How to create a Rectangle object in Java using g.fillRect method
本文介绍了如何使用g.fillRect方法在Java中创建Rectangle对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个矩形对象,然后使用paint()将其绘制到applet中。我试过了:
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
然后尝试使用
将它绘制到小程序
g.draw(R);
没有用。有没有办法在Java中做到这一点?我已经搜索了谷歌在其生命的一英寸内寻找答案,但我一直无法找到答案。请帮助!
解决方案
试试这个:
$ b
public void paint(Graphics g){
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(),r.getY(),r.getWidth(),r.getHeight());
}
[b]
< pre $ //通过显式转换
public void paint(Graphics g){
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(
(int)r.getX(),
(int)r.getY(),
(int)r.getWidth(),
(int)r.getHeight()
);
}
I need to create a rectangle object and then paint it to the applet using paint(). I tried
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
Then tried to paint it to the applet using
g.draw(r);
It didn't work. Is there a way to do this in java? I have scoured google to within an inch of its life for an answer, but I haven't been able to find an answer. Please help!
解决方案
Try this:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
[edit]
// With explicit casting
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos, yPos, width, height);
g.fillRect(
(int)r.getX(),
(int)r.getY(),
(int)r.getWidth(),
(int)r.getHeight()
);
}
这篇关于如何使用g.fillRect方法在Java中创建Rectangle对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文