我需要创建一个rectangle对象,然后使用paint()将其绘制到applet。我试过了
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);然后尝试使用以下命令将其绘制到applet
g.draw(r);它没有起作用。在java中有什么方法可以做到这一点吗?我已经搜遍了谷歌,几乎找不到答案,但我一直没能找到答案。请帮帮我!
发布于 2012-08-01 03:10:15
试试这个:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}编辑
// 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()
);
}发布于 2012-08-01 01:30:46
您可以这样尝试:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
g.drawRect (x, y, width, height); //can use either of the two//
g.fillRect (x, y, width, height);
g.setColor(color);
}
}其中x是x坐标y是y坐标要使用的color=the颜色,例如Color.blue
如果你想使用rectangle对象,你可以这样做:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
g.setColor(color);
}
} 发布于 2018-10-26 09:25:27
Note:drawRect和fillRect是不同的。
绘制指定矩形的轮廓:
public void drawRect(int x,
int y,
int width,
int height)填充指定的矩形。使用图形上下文的当前颜色填充矩形:
public abstract void fillRect(int x,
int y,
int width,
int height)https://stackoverflow.com/questions/11745595
复制相似问题