[英]Drawing Rectangles in Java awt
I am interested in drawing a Rectangle object using AWT methods (I know, it's old). 我对使用AWT方法绘制Rectangle对象感兴趣(我知道,它很旧)。 I have looked at other code on the forum that has answers, but they are not working for me.
我在论坛上查看了其他具有答案的代码,但它们对我不起作用。 Here is what I am looking to do...
这就是我想要做的...
paint(Graphics g) {
Rectangle r = new Rectangle(5,5,20,20);
g.drawRect(r.getX(),r.getY(),r.getWidth(),r.getHeight());
}
But what I have to do is: 但是我要做的是:
g.drawRect((int)r.getX().........);
and cast every value to an int. 并将每个值都转换为一个int Am I doing something wrong?
难道我做错了什么? The code examples that I found have the solution without the casting.
我发现的代码示例具有无需强制转换的解决方案。 If I don't cast, I get an error.
如果我不进行转换,则会出现错误。 Surely, there should be something more simple.
当然,应该有一些更简单的方法。
Thanks in advance. 提前致谢。
You can cast Graphics to Graphics2D to use its draw() method to draw 'java.awt.shap' objects like this: 您可以将Graphics转换为Graphics2D,以使用其draw()方法绘制“ java.awt.shap”对象,如下所示:
paint(Graphics g) {
Grahpics2D g2 = (Graphics2D) g;
Shape s = new Rectangle(5,5,20,20);
g2.draw(s);
}
This is an easy way to do it. 这是一种简单的方法。 I figured it out:
我想到了:
paint(Graphics g) {
Rectangle r = new Rectangle(5,5,30,30);
g.drawRect(r.x,r.y,r.width,r.height);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.